Skip to content

Commit cb886ee

Browse files
committed
Add captcha in account creation
1 parent 92d380b commit cb886ee

7 files changed

Lines changed: 66 additions & 2 deletions

File tree

Bugzilla/Install/Filesystem.pm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ sub FILESYSTEM {
214214
dirs => DIR_CGI_WRITE },
215215
$assetsdir => { files => WS_SERVE,
216216
dirs => DIR_CGI_OVERWRITE | DIR_ALSO_WS_SERVE },
217+
"$datadir/captcha" => { files => CGI_WRITE,
218+
dirs => DIR_CGI_WRITE },
217219

218220
# Readable directories
219221
"$datadir/mining" => { files => CGI_READ,
@@ -282,6 +284,7 @@ sub FILESYSTEM {
282284
$extensionsdir => DIR_CGI_READ,
283285
# Directories that cgi scripts can write to.
284286
"$datadir/db" => DIR_CGI_WRITE,
287+
"$datadir/captcha" => DIR_CGI_WRITE,
285288
$attachdir => DIR_CGI_WRITE,
286289
$graphsdir => DIR_CGI_WRITE | DIR_ALSO_WS_SERVE,
287290
$webdotdir => DIR_CGI_WRITE | DIR_ALSO_WS_SERVE,
@@ -430,7 +433,7 @@ EOT
430433

431434
"$assetsdir/.htaccess" => { perms => WS_SERVE, contents => <<EOT
432435
# Allow access to .css files
433-
<FilesMatch \\.(css|js)\$>
436+
<FilesMatch \\.(css|js|png)\$>
434437
<IfModule mod_version.c>
435438
<IfVersion < 2.4>
436439
Allow from all

Bugzilla/Install/Requirements.pm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ sub REQUIRED_MODULES {
166166
# 2.0 is the first version that will work with JSON::RPC.
167167
version => '2.01',
168168
},
169+
{
170+
package => 'Authen-Captcha',
171+
module => 'Authen::Captcha',
172+
version => '1.024',
173+
},
169174
);
170175

171176
if (ON_WINDOWS) {

Bugzilla/Template.pm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,18 @@ sub _concatenate_js {
591591
return [ $file ];
592592
}
593593

594+
sub _captcha_path {
595+
my $token = shift;
596+
return "" unless $token;
597+
598+
my $cgi_path = bz_locations()->{cgi_path};
599+
my $assets_path = bz_locations()->{assetsdir};
600+
my $captcha_path = "$assets_path" . "/$token.png";
601+
602+
$captcha_path =~ s/^\Q$cgi_path\E\///o;
603+
return $captcha_path;
604+
}
605+
594606
# YUI dependency resolution
595607
sub yui_resolve_deps {
596608
my ($yui, $yui_deps) = @_;
@@ -1112,6 +1124,7 @@ sub create {
11121124
'css_files' => \&css_files,
11131125
yui_resolve_deps => \&yui_resolve_deps,
11141126
concatenate_js => \&_concatenate_js,
1127+
captcha_path => \&_captcha_path,
11151128

11161129
# All classifications (sorted by sortkey, name)
11171130
'all_classifications' => sub {

createaccount.cgi

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use Bugzilla::Constants;
1717
use Bugzilla::Error;
1818
use Bugzilla::Token;
1919

20+
use Authen::Captcha;
21+
2022
# Just in case someone already has an account, let them get the correct footer
2123
# on an error message. The user is logged out just after the account is
2224
# actually created.
@@ -33,13 +35,32 @@ my $login = $cgi->param('login');
3335
my $uid = $cgi->param('uid');
3436

3537
# Modified for Mer to send uid to account creation and to put uid into
36-
# the $vars for the templates
38+
# the $vars for the templates. And added captcha for the robots
39+
40+
my $captcha_data = bz_locations()->{'datadir'} . "/captcha";
41+
my $captcha_output = bz_locations()->{'assetsdir'};
42+
my $captcha = Authen::Captcha->new(
43+
data_folder => $captcha_data,
44+
output_folder => $captcha_output,
45+
);
46+
3747
if (defined($login)) {
3848
# Check the hash token to make sure this user actually submitted
3949
# the create account form.
4050
my $token = $cgi->param('token');
4151
check_hash_token($token, ['create_account']);
4252

53+
my $captcha_token = $cgi->param('captcha_token');
54+
my $captcha_code = $cgi->param('captcha_code');
55+
my $result = $captcha->check_code($captcha_code, $captcha_token);
56+
if ($result == 0) {
57+
ThrowCodeError('captcha_check_error');
58+
} elsif ($result == -1) {
59+
ThrowUserError('captcha_expired');
60+
} elsif ($result < -1 ) {
61+
ThrowUserError('captcha_invalid');
62+
}
63+
4364
$user->check_and_send_account_creation_confirmation($login, $uid);
4465
$vars->{'login'} = $login;
4566
$vars->{'uid'} = $uid;
@@ -49,6 +70,8 @@ if (defined($login)) {
4970
exit;
5071
}
5172

73+
$vars->{'captcha_token'} = $captcha->generate_code(10);
74+
5275
# Show the standard "would you like to create an account?" form.
5376
$template->process("account/create.html.tmpl", $vars)
5477
|| ThrowTemplateError($template->error());

template/en/default/account/create.html.tmpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@
8686
Username:
8787
</span>
8888
<input size="35" id="uid" name="uid" required>
89+
90+
<br><img src="[% captcha_path(captcha_token) FILTER html %]"><br>
91+
<span class="label">
92+
Enter the above code:
93+
</span>
94+
<input id="captcha_code" name="captcha_code" required>
95+
<input type="hidden" name="captcha_token" value="[% captcha_token FILTER html %]">
96+
8997
<input type="hidden" id="token" name="token" value="[% issue_hash_token(['create_account']) FILTER html %]">
9098
<input type="submit" id="send" value="Send">
9199
</form>

template/en/default/global/code-error.html.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@
389389
Bugzilla does not support the search type
390390
"[% operator.truncate(30, "...") FILTER html %]".
391391

392+
[% ELSIF error == "captcha_check_error" %]
393+
[% title = "Captcha check failed" %]
394+
Checking the captcha code failed.
395+
392396
[% ELSE %]
393397
[%# Try to find hooked error messages %]
394398
[% error_message = Hook.process("errors") %]

template/en/default/global/user-error.html.tmpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,14 @@
19471947
[% END %]
19481948
with the External Login ID "[% extern_id FILTER html %]".
19491949

1950+
[% ELSIF error == "captcha_expired" %]
1951+
[% title = "Invalid captcha" %]
1952+
The used captcha has already expired.
1953+
1954+
[% ELSIF error == "captcha_invalid" %]
1955+
[% title = "Invalid captcha" %]
1956+
The given captcha code is not correct.
1957+
19501958
[% ELSE %]
19511959

19521960
[%# Try to find hooked error messages %]

0 commit comments

Comments
 (0)