]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/todo/require_CAPTCHA_to_edit.mdwn
ad38d92358d991aa1614fab96e35eb7e0be337d2
[git.ikiwiki.info.git] / doc / todo / require_CAPTCHA_to_edit.mdwn
1 I don't necessarily trust all OpenID providers to stop bots.  I note that ikiwiki allows [[banned_users]], and that there are other todos such as [[todo/openid_user_filtering]] that would extend this.  However, it might be nice to have a CAPTCHA system.
3 I imagine a plugin that modifies the login screen to use <http://recaptcha.net/>.  You would then be required to fill in the captcha as well as log in in the normal way.
5 -- [[users/Will]]
7 > I hate CAPTCHAs with a passion. Someone else is welcome to write such a
8 > plugin.
9 >
10 > If spam via openid (which I have never ever seen yet) becomes
11 > a problem, a provider whitelist/blacklist seems like a much nicer
12 > solution than a CAPTCHA. --[[Joey]]
14 >> Apparently there has been openid spam (you can google for it).  But as for
15 >> white/black lists, were you thinking of listing the openids, or the content?
16 >> Something like the moinmoin global <http://master.moinmo.in/BadContent>
17 >> list?
19 >>> OpenID can be thought of as pushing the problem of determining if
20 >>> someone is a human or a spambot back from the openid consumer to the
21 >>> openid provider. So, providers that make it possible for spambots to
22 >>> use their openids, or that are even set up explicitly for use in
23 >>> spamming, would be the ones to block. Or, providers that are known to
24 >>> use very good screening for humans would be the ones to allow.
25 >>> (Openid delegation makes it a bit harder than just looking at the
26 >>> openid url though.) --[[Joey]]
28 >>>> Well, OpenID only addresses authentication issues, not authorisation issues.
29 >>>> Given that it is trivial to set up your own OpenID provider (a full provider, not
30 >>>> just a forward to another provider), I can't see a
31 >>>> blacklist working in the long term (it would be like blacklisting email).
32 >>>> A whitelist might work (it would not be quite as bad as whitelisting email).  In any case,
33 >>>> there is now a captcha plugin for those that want it.  It is accessible
34 >>>> (there is an audio option) and serves a social purpose along with
35 >>>> keeping bots out (the captcha is used to help digitise hard to read
36 >>>> words in books for [Carnegie Mellon University](http://www.cs.cmu.edu/) and
37 >>>> [The Internet Archive](http://www.archive.org/) ).  Finally, because the actual captcha is outsourced
38 >>>> it means that someone else is taking care of keeping it ahead of
39 >>>> the bot authors.
41 >> As [[spam_fighting]] shows, OpenID spam is now real. Yahoo, at least, would need to be blocked, according to the above, which seems like a bold move. --[[anarcat]]
43 Okie - I have a first pass of this.  There are still some issues.
45 Currently the code verifies the CAPTCHA.  If you get it right then you're fine.
46 If you get the CAPTCHA wrong then the current code tells formbuilder that
47 one of the fields is invalid.  This stops the login from going through.
48 Unfortunately, formbuilder is caching this validity somewhere, and I haven't
49 found a way around that yet.  This means that if you get the CAPTCHA
50 wrong, it will continue to fail.  You need to load the login page again so
51 it doesn't have the error message on the screen, then it'll work again.
53 > fixed this - updated code is attached.
55 A second issue is that the OpenID login system resets the 'required' flags
56 of all the other fields, so using OpenID will cause the CAPTCHA to be
57 ignored.
59 > This is still not fixed.  I would have thought the following patch would
60 > have fixed this second issue, but it doesn't.
62 (code snipped as a working [[patch]] is below)
64 >> What seems to be happing here is that the openid plugin defines a
65 >> validate hook for openid_url that calls validate(). validate() in turn
66 >> redirects the user to the openid server for validation, and exits. If
67 >> the openid plugins' validate hook is called before your recaptcha
68 >> validator, your code never gets a chance to run. I don't know how to
69 >> control the other that FormBuilder validates fields, but the only fix I
70 >> can see is to somehow influence that order. 
71 >>
72 >> Hmm, maybe you need to move your own validation code out of the validate
73 >> hook. Instead, just validate the captcha in the formbuilder_setup hook.
74 >> The problem with this approach is that if validation fails, you can't
75 >> just flag it as invalid and let formbuilder handle that. Instead, you'd
76 >> have to hack something in to redisplay the captcha by hand. --[[Joey]]
78 >>> Fixed this.  I just modified the OpenID plugin to check if the captcha
79 >>> succeeded or failed.  Seeing as the OpenID plugin is the one that is
80 >>> abusing the normal validate method, I figured it was best to keep
81 >>> the fix in the same place.  I also added a config switch so you can set if
82 >>> the captcha is needed for OpenID logins. OpenID defaults to ignoring
83 >>> the captcha.
84 >>> Patch is inline below.
85 >>> I think this whole thing is working now.
87 >>>> Ok, glad it's working. Not thrilled that it needs to modify the
88 >>>> openid plugin, especially as I'm not sure if i I will integrate the
89 >>>> captcha plugin into mainline. Also because it's not very clean to have
90 >>>> the oprnid plugin aware of another plugin like that. I'd like to
91 >>>> prusue my idea of not doing the captcha validation in the validate
92 >>>> hook.
94 --- a/IkiWiki/Plugin/openid.pm
95 +++ b/IkiWiki/Plugin/openid.pm
96 @@ -18,6 +18,7 @@ sub getopt () {
97         error($@) if $@;
98         Getopt::Long::Configure('pass_through');
99         GetOptions("openidsignup=s" => \$config{openidsignup});
100 +       GetOptions("openidneedscaptcha=s" => \$config{openidneedscaptcha});
101  }
102  
103  sub formbuilder_setup (@) {
104 @@ -61,6 +62,7 @@ sub formbuilder_setup (@) {
105                         # Skip all other required fields in this case.
106                         foreach my $field ($form->field) {
107                                 next if $field eq "openid_url";
108 +                               next if $config{openidneedscaptcha} && $field eq "recaptcha";
109                                 $form->field(name => $field, required => 0,
110                                         validate => '/.*/');
111                         }
112 @@ -96,6 +98,18 @@ sub validate ($$$;$) {
113                 }
114         }
115  
116 +       if ($config{openidneedscaptcha} && defined $form->field("recaptcha")) {
117 +               foreach my $field ($form->field) {
118 +                       next unless ($field eq "recaptcha");
119 +                       if (! $field->validate) {
120 +                               # if they didn't get the captcha right,
121 +                               # then just claim we validated ok so the
122 +                               # captcha can cause a fail
123 +                               return 1;
124 +                       }
125 +               }
126 +       }
128         my $check_url = $claimed_identity->check_url(
129                 return_to => IkiWiki::cgiurl(do => "postsignin"),
130                 trust_root => $config{cgiurl},
133 Instructions
134 =====
136 You need to go to <http://recaptcha.net/api/getkey> and get a key set.
137 The keys are added as options.
139         reCaptchaPubKey => "LONGPUBLICKEYSTRING",
140         reCaptchaPrivKey => "LONGPRIVATEKEYSTRING",
142 You can also use "signInSSL" if you're using ssl for your login screen.
145 The following code is just inline.  It will probably not display correctly, and you should just grab it from the page source.
147 ----------
149 #!/usr/bin/perl
150 # Ikiwiki password authentication.
151 package IkiWiki::Plugin::recaptcha;
153 use warnings;
154 use strict;
155 use IkiWiki 2.00;
157 sub import {
158         hook(type => "formbuilder_setup", id => "recaptcha", call => \&formbuilder_setup);
161 sub getopt () {
162         eval q{use Getopt::Long};
163         error($@) if $@;
164         Getopt::Long::Configure('pass_through');
165         GetOptions("reCaptchaPubKey=s" => \$config{reCaptchaPubKey});
166         GetOptions("reCaptchaPrivKey=s" => \$config{reCaptchaPrivKey});
169 sub formbuilder_setup (@) {
170         my %params=@_;
172         my $form=$params{form};
173         my $session=$params{session};
174         my $cgi=$params{cgi};
175         my $pubkey=$config{reCaptchaPubKey};
176         my $privkey=$config{reCaptchaPrivKey};
177         debug("Unknown Public Key.  To use reCAPTCHA you must get an API key from http://recaptcha.net/api/getkey")
178                 unless defined $config{reCaptchaPubKey};
179         debug("Unknown Private Key.  To use reCAPTCHA you must get an API key from http://recaptcha.net/api/getkey")
180                 unless defined $config{reCaptchaPrivKey};
181         my $tagtextPlain=<<EOTAG;
182                 <script type="text/javascript"
183                         src="http://api.recaptcha.net/challenge?k=$pubkey">
184                 </script>
186                 <noscript>
187                         <iframe src="http://api.recaptcha.net/noscript?k=$pubkey"
188                                 height="300" width="500" frameborder="0"></iframe><br>
189                         <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
190                         <input type="hidden" name="recaptcha_response_field" 
191                                 value="manual_challenge">
192                 </noscript>
193 EOTAG
195         my $tagtextSSL=<<EOTAGS;
196                 <script type="text/javascript"
197                         src="https://api-secure.recaptcha.net/challenge?k=$pubkey">
198                 </script>
200                 <noscript>
201                         <iframe src="https://api-secure.recaptcha.net/noscript?k=$pubkey"
202                                 height="300" width="500" frameborder="0"></iframe><br>
203                         <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
204                         <input type="hidden" name="recaptcha_response_field" 
205                                 value="manual_challenge">
206                 </noscript>
207 EOTAGS
209         my $tagtext;
211         if ($config{signInSSL}) {
212                 $tagtext = $tagtextSSL;
213         } else {
214                 $tagtext = $tagtextPlain;
215         }
216         
217         if ($form->title eq "signin") {
218                 # Give up if module is unavailable to avoid
219                 # needing to depend on it.
220                 eval q{use LWP::UserAgent};
221                 if ($@) {
222                         debug("unable to load LWP::UserAgent, not enabling reCaptcha");
223                         return;
224                 }
226                 die("To use reCAPTCHA you must get an API key from http://recaptcha.net/api/getkey")
227                         unless $pubkey;
228                 die("To use reCAPTCHA you must get an API key from http://recaptcha.net/api/getkey")
229                         unless $privkey;
230                 die("To use reCAPTCHA you must know the remote IP address")
231                         unless $session->remote_addr();
233                 $form->field(
234                         name => "recaptcha",
235                         label => "",
236                         type => 'static',
237                         comment => $tagtext,
238                         required => 1,
239                         message => "CAPTCHA verification failed",
240                 );
242                 # validate the captcha.
243                 if ($form->submitted && $form->submitted eq "Login" &&
244                                 defined $form->cgi_param("recaptcha_challenge_field") && 
245                                 length $form->cgi_param("recaptcha_challenge_field") &&
246                                 defined $form->cgi_param("recaptcha_response_field") && 
247                                 length $form->cgi_param("recaptcha_response_field")) {
249                         my $challenge = "invalid";
250                         my $response = "invalid";
251                         my $result = { is_valid => 0, error => 'recaptcha-not-tested' };
253                         $form->field(name => "recaptcha",
254                                 message => "CAPTCHA verification failed",
255                                 required => 1,
256                                 validate => sub {
257                                         if ($challenge ne $form->cgi_param("recaptcha_challenge_field") or
258                                                         $response ne $form->cgi_param("recaptcha_response_field")) {
259                                                 $challenge = $form->cgi_param("recaptcha_challenge_field");
260                                                 $response = $form->cgi_param("recaptcha_response_field");
261                                                 debug("Validating: ".$challenge." ".$response);
262                                                 $result = check_answer($privkey,
263                                                                 $session->remote_addr(),
264                                                                 $challenge, $response);
265                                         } else {
266                                                 debug("re-Validating");
267                                         }
269                                         if ($result->{is_valid}) {
270                                                 debug("valid");
271                                                 return 1;
272                                         } else {
273                                                 debug("invalid");
274                                                 return 0;
275                                         }
276                                 });
277                 }
278         }
281 # The following function is borrowed from
282 # Captcha::reCAPTCHA by Andy Armstrong and are under the PERL Artistic License
284 sub check_answer {
285     my ( $privkey, $remoteip, $challenge, $response ) = @_;
287     die
288       "To use reCAPTCHA you must get an API key from http://recaptcha.net/api/getkey"
289       unless $privkey;
291     die "For security reasons, you must pass the remote ip to reCAPTCHA"
292       unless $remoteip;
294         if (! ($challenge && $response)) {
295                 debug("Challenge or response not set!");
296                 return { is_valid => 0, error => 'incorrect-captcha-sol' };
297         }
299         my $ua = LWP::UserAgent->new();
301     my $resp = $ua->post(
302         'http://api-verify.recaptcha.net/verify',
303         {
304             privatekey => $privkey,
305             remoteip   => $remoteip,
306             challenge  => $challenge,
307             response   => $response
308         }
309     );
311     if ( $resp->is_success ) {
312         my ( $answer, $message ) = split( /\n/, $resp->content, 2 );
313         if ( $answer =~ /true/ ) {
314             debug("CAPTCHA valid");
315             return { is_valid => 1 };
316         }
317         else {
318             chomp $message;
319             debug("CAPTCHA failed: ".$message);
320             return { is_valid => 0, error => $message };
321         }
322     }
323     else {
324         debug("Unable to contact reCaptcha verification host!");
325         return { is_valid => 0, error => 'recaptcha-not-reachable' };
326     }
329 1;