@matheusbsbs / matheus-bagliano / commits / 24d0693a68
Fork 0

gitweb: Split validate_input into validate_pathname and validate_refname

Split validate_input subroutine into validate_pathname which is used for $project, $file_name and $file_parent parameters, and validate_refname which is used for $hash, $hash_base, $hash_parent and $hash_parent_base parameters. Reintroduce validation of $file_name and $file_parent parameters, removed in a2f3db2f validate_pathname in addition to what validate_input did checks also for doubled slashes and NUL character. It does not check if input is textual hash, and does not check if all characters are from the following set: [a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]. validate_refname first check if the input is textual hash, then checks if it is valid pathname, then checks for invalid characters (according to git-check-ref-format manpage). It does not check if all charactes are from the [a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%] set. We do not have to validate pathnames we got from git. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Sep 26, 2006 at 01:57 UTC 24d0693a68008baacd827b1345c957e871488596
1 file changed +47 -16
gitweb/gitweb.perl
+47 -16
@@ -200,9 +200,10 @@ if (defined $action) {
200 }
201 }
202
203 +# parameters which are pathnames
204 our $project = $cgi->param('p');
205 if (defined $project) {
205 - if (!validate_input($project) ||
206 + if (!validate_pathname($project) ||
207 !(-d "$projectroot/$project") ||
208 !(-e "$projectroot/$project/HEAD") ||
209 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
@@ -212,38 +213,50 @@ if (defined $project) {
213 }
214 }
215
215 -# We have to handle those containing any characters:
216 our $file_name = $cgi->param('f');
217 +if (defined $file_name) {
218 + if (!validate_pathname($file_name)) {
219 + die_error(undef, "Invalid file parameter");
220 + }
221 +}
222 +
223 our $file_parent = $cgi->param('fp');
224 +if (defined $file_parent) {
225 + if (!validate_pathname($file_parent)) {
226 + die_error(undef, "Invalid file parent parameter");
227 + }
228 +}
229
230 +# parameters which are refnames
231 our $hash = $cgi->param('h');
232 if (defined $hash) {
221 - if (!validate_input($hash)) {
233 + if (!validate_refname($hash)) {
234 die_error(undef, "Invalid hash parameter");
235 }
236 }
237
238 our $hash_parent = $cgi->param('hp');
239 if (defined $hash_parent) {
228 - if (!validate_input($hash_parent)) {
240 + if (!validate_refname($hash_parent)) {
241 die_error(undef, "Invalid hash parent parameter");
242 }
243 }
244
245 our $hash_base = $cgi->param('hb');
246 if (defined $hash_base) {
235 - if (!validate_input($hash_base)) {
247 + if (!validate_refname($hash_base)) {
248 die_error(undef, "Invalid hash base parameter");
249 }
250 }
251
252 our $hash_parent_base = $cgi->param('hpb');
253 if (defined $hash_parent_base) {
242 - if (!validate_input($hash_parent_base)) {
254 + if (!validate_refname($hash_parent_base)) {
255 die_error(undef, "Invalid hash parent base parameter");
256 }
257 }
258
259 +# other parameters
260 our $page = $cgi->param('pg');
261 if (defined $page) {
262 if ($page =~ m/[^0-9]/) {
@@ -273,7 +286,7 @@ sub evaluate_path_info {
286 $project =~ s,/*[^/]*$,,;
287 }
288 # validate project
276 - $project = validate_input($project);
289 + $project = validate_pathname($project);
290 if (!$project ||
291 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
292 ($strict_export && !project_in_list($project))) {
@@ -294,12 +307,12 @@ sub evaluate_path_info {
307 } else {
308 $action ||= "blob_plain";
309 }
297 - $hash_base ||= validate_input($refname);
298 - $file_name ||= $pathname;
310 + $hash_base ||= validate_refname($refname);
311 + $file_name ||= validate_pathname($pathname);
312 } elsif (defined $refname) {
313 # we got "project.git/branch"
314 $action ||= "shortlog";
302 - $hash ||= validate_input($refname);
315 + $hash ||= validate_refname($refname);
316 }
317 }
318 evaluate_path_info();
@@ -387,16 +400,34 @@ sub href(%) {
400 ## ======================================================================
401 ## validation, quoting/unquoting and escaping
402
390 -sub validate_input {
391 - my $input = shift;
403 +sub validate_pathname {
404 + my $input = shift || return undef;
405
393 - if ($input =~ m/^[0-9a-fA-F]{40}$/) {
394 - return $input;
406 + # no '.' or '..' as elements of path, i.e. no '.' nor '..'
407 + # at the beginning, at the end, and between slashes.
408 + # also this catches doubled slashes
409 + if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
410 + return undef;
411 }
396 - if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
412 + # no null characters
413 + if ($input =~ m!\0!) {
414 return undef;
415 }
399 - if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
416 + return $input;
417 +}
418 +
419 +sub validate_refname {
420 + my $input = shift || return undef;
421 +
422 + # textual hashes are O.K.
423 + if ($input =~ m/^[0-9a-fA-F]{40}$/) {
424 + return $input;
425 + }
426 + # it must be correct pathname
427 + $input = validate_pathname($input)
428 + or return undef;
429 + # restrictions on ref name according to git-check-ref-format
430 + if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
431 return undef;
432 }
433 return $input;