Skip to content

Commit 36612e4

Browse files
jnarebgitster
authored andcommitted
gitweb: Handle invalid regexp in regexp search
When using regexp search ('sr' parameter / $search_use_regexp variable is true), check first that regexp is valid. Without this patch we would get an error from Perl during search (if searching is performed by gitweb), or highlighting matches substring (if applicable), if user provided invalid regexp... which means broken HTML, with error page (including HTTP headers) generated after gitweb already produced some output. Add test that illustrates such error: for example for regexp "*\.git" we would get the following error: Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE \.git/ at /var/www/cgi-bin/gitweb.cgi line 3084. Reported-by: Ramsay Jones <[email protected]> Signed-off-by: Jakub Narebski <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f174a25 commit 36612e4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

gitweb/gitweb.perl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,16 @@ sub evaluate_and_validate_params {
10541054
if (length($searchtext) < 2) {
10551055
die_error(403, "At least two characters are required for search parameter");
10561056
}
1057-
$search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1057+
if ($search_use_regexp) {
1058+
$search_regexp = $searchtext;
1059+
if (!eval { qr/$search_regexp/; 1; }) {
1060+
(my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1061+
die_error(400, "Invalid search regexp '$search_regexp'",
1062+
esc_html($error));
1063+
}
1064+
} else {
1065+
$search_regexp = quotemeta $searchtext;
1066+
}
10581067
}
10591068
}
10601069

t/t9501-gitweb-standalone-http-status.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,14 @@ our $maxload = undef;
134134
EOF
135135

136136

137+
# ----------------------------------------------------------------------
138+
# invalid arguments
139+
140+
test_expect_success 'invalid arguments: invalid regexp (in project search)' '
141+
gitweb_run "a=project_list;s=*\.git;sr=1" &&
142+
grep "Status: 400" gitweb.headers &&
143+
grep "400 - Invalid.*regexp" gitweb.body
144+
'
145+
test_debug 'cat gitweb.headers'
146+
137147
test_done

0 commit comments

Comments
 (0)