gitweb: Return 1 on validation success instead of passed input
Users of validate_* passing "0" might get failures on correct name because of coercion of "0" to false in code like: die_error(500, "invalid ref") unless (check_ref_format ("0")); Also, the validate_foo subs are renamed to is_valid_foo. Signed-off-by: Krzesimir Nowak <krzesimir@endocode.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Krzesimir Nowak committed
Dec 11, 2013 at 12:54 UTC
23faf546ae59d945848389070507bc418de733b6
1 file changed
+32
-29
gitweb/gitweb.perl
+32
-29
@@ -994,7 +994,7 @@ our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_bas
994
sub evaluate_and_validate_params {
995
our $action = $input_params{'action'};
996
if (defined $action) {
997
- if (!validate_action($action)) {
997
+ if (!is_valid_action($action)) {
998
die_error(400, "Invalid action parameter");
999
}
1000
}
@@ -1002,7 +1002,7 @@ sub evaluate_and_validate_params {
1002
# parameters which are pathnames
1003
our $project = $input_params{'project'};
1004
if (defined $project) {
1005
- if (!validate_project($project)) {
1005
+ if (!is_valid_project($project)) {
1006
undef $project;
1007
die_error(404, "No such project");
1008
}
@@ -1010,21 +1010,21 @@ sub evaluate_and_validate_params {
1010
1011
our $project_filter = $input_params{'project_filter'};
1012
if (defined $project_filter) {
1013
- if (!validate_pathname($project_filter)) {
1013
+ if (!is_valid_pathname($project_filter)) {
1014
die_error(404, "Invalid project_filter parameter");
1015
}
1016
}
1017
1018
our $file_name = $input_params{'file_name'};
1019
if (defined $file_name) {
1020
- if (!validate_pathname($file_name)) {
1020
+ if (!is_valid_pathname($file_name)) {
1021
die_error(400, "Invalid file parameter");
1022
}
1023
}
1024
1025
our $file_parent = $input_params{'file_parent'};
1026
if (defined $file_parent) {
1027
- if (!validate_pathname($file_parent)) {
1027
+ if (!is_valid_pathname($file_parent)) {
1028
die_error(400, "Invalid file parent parameter");
1029
}
1030
}
@@ -1032,21 +1032,21 @@ sub evaluate_and_validate_params {
1032
# parameters which are refnames
1033
our $hash = $input_params{'hash'};
1034
if (defined $hash) {
1035
- if (!validate_refname($hash)) {
1035
+ if (!is_valid_refname($hash)) {
1036
die_error(400, "Invalid hash parameter");
1037
}
1038
}
1039
1040
our $hash_parent = $input_params{'hash_parent'};
1041
if (defined $hash_parent) {
1042
- if (!validate_refname($hash_parent)) {
1042
+ if (!is_valid_refname($hash_parent)) {
1043
die_error(400, "Invalid hash parent parameter");
1044
}
1045
}
1046
1047
our $hash_base = $input_params{'hash_base'};
1048
if (defined $hash_base) {
1049
- if (!validate_refname($hash_base)) {
1049
+ if (!is_valid_refname($hash_base)) {
1050
die_error(400, "Invalid hash base parameter");
1051
}
1052
}
@@ -1066,7 +1066,7 @@ sub evaluate_and_validate_params {
1066
1067
our $hash_parent_base = $input_params{'hash_parent_base'};
1068
if (defined $hash_parent_base) {
1069
- if (!validate_refname($hash_parent_base)) {
1069
+ if (!is_valid_refname($hash_parent_base)) {
1070
die_error(400, "Invalid hash parent base parameter");
1071
}
1072
}
@@ -1418,27 +1418,30 @@ sub href {
1418
## ======================================================================
1419
## validation, quoting/unquoting and escaping
1420
1421
-sub validate_action {
1422
- my $input = shift || return undef;
1421
+sub is_valid_action {
1422
+ my $input = shift;
1423
return undef unless exists $actions{$input};
1424
- return $input;
1424
+ return 1;
1425
}
1426
1427
-sub validate_project {
1428
- my $input = shift || return undef;
1429
- if (!validate_pathname($input) ||
1427
+sub is_valid_project {
1428
+ my $input = shift;
1429
+
1430
+ return unless defined $input;
1431
+ if (!is_valid_pathname($input) ||
1432
!(-d "$projectroot/$input") ||
1433
!check_export_ok("$projectroot/$input") ||
1434
($strict_export && !project_in_list($input))) {
1435
return undef;
1436
} else {
1435
- return $input;
1437
+ return 1;
1438
}
1439
}
1440
1439
-sub validate_pathname {
1440
- my $input = shift || return undef;
1441
+sub is_valid_pathname {
1442
+ my $input = shift;
1443
1444
+ return undef unless defined $input;
1445
# no '.' or '..' as elements of path, i.e. no '.' nor '..'
1446
# at the beginning, at the end, and between slashes.
1447
# also this catches doubled slashes
@@ -1449,33 +1452,33 @@ sub validate_pathname {
1452
if ($input =~ m!\0!) {
1453
return undef;
1454
}
1452
- return $input;
1455
+ return 1;
1456
}
1457
1458
sub is_valid_ref_format {
1456
- my $input = shift || return undef;
1459
+ my $input = shift;
1460
1461
+ return undef unless defined $input;
1462
# restrictions on ref name according to git-check-ref-format
1463
if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1464
return undef;
1465
}
1462
- return $input;
1466
+ return 1;
1467
}
1468
1465
-sub validate_refname {
1466
- my $input = shift || return undef;
1469
+sub is_valid_refname {
1470
+ my $input = shift;
1471
1472
+ return undef unless defined $input;
1473
# textual hashes are O.K.
1474
if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1470
- return $input;
1475
+ return 1;
1476
}
1477
# it must be correct pathname
1473
- $input = validate_pathname($input)
1474
- or return undef;
1478
+ is_valid_pathname($input) or return undef;
1479
# check git-check-ref-format restrictions
1476
- is_valid_ref_format($input)
1477
- or return undef;
1478
- return $input;
1480
+ is_valid_ref_format($input) or return undef;
1481
+ return 1;
1482
}
1483
1484
# decode sequences of octets in utf8 into Perl's internal form,