gitweb: check given hash before trying to create snapshot
PATCH v3 1/1 gitweb: check given hash before trying to create snapshot
Quoted 1 line
[This mail was very strangely wrapped; I fixed this for readability]
Yeah, sorry, format=flowed
Quoted 10 lines
In first test you check that _contents_ contain specific error message, but you do not check if HTTP status code matches it (it should, because of how
die_errorworks). In second test you check HTTP status. If thet/t9501-gitweb-standalone-http-status.shis to be about status, I guess that you should check HTTP status, and not contents of the page (which is more likely to change, e.g. due to some prettifying).In t9501 tests you need, I think, only the HTTP headers part, unless you want also to check that the contents matches. There was some sed script shown to extract only HTTP headers.
Yes, t9501 also checks for the specific error message, this was originally how it had to be done in order to show what trap was being executed for that first block of checks in the snapshot routine. The header does not contain the specific error string that gitweb outputs though, only the standard message (i.e. 404 Not Found), so I check the script output for negative test cases.
Quoted 7 lines
Quoted 6 lines
Also, right now gitweb will not accept tags for hashes. This seems to be because it passes the --verify option to rev-parse, but the output from using and not using the verify option seems to be the same (other than also accepting all tree-ishes). Could you let me know if there is a good reason not to take off the --verify option? Otherwise, I would like to take it off in the next version of this patch.
Quoted 8 lines
Errr, what?
$ 5096:[gitweb/web@git]# git rev-parse --verify v1.5.0 6db027ffe03210324939b3dd655c4223ca023b45 $ git rev-parse --verify refs/tags/v1.5.0 6db027ffe03210324939b3dd655c4223ca023b45So it works as intended. The problem must be in some other place.
Let's just say the problem is not in the computer. :(
--
Mark Rada (ferrous26)
[email protected]
--->8---
Makes things nicer in cases when you hand craft the snapshot URL but make a typo in defining the hash variable (e.g. netx instead of next); you will now get an error message instead of a broken tarball.
To maintain backwards compatibility, git_get_head_hash is now a wrapper for git_get_full_hash, as suggested by Jakub Narebski.
Tests for t9501 are included to demonstrate changed functionality.
--- gitweb/gitweb.perl | 13 ++++++++++--- t/t9501-gitweb-standalone-http-status.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-)
gitweb/gitweb.perl +10 -3
@@ -1983,11 +1983,17 @@ sub quote_command { # get HEAD ref of given project as hash sub git_get_head_hash { + return git_get_full_hash(shift, 'HEAD'); +} + +# given a project and tree-ish, returns full hash +sub git_get_full_hash { my $project = shift; + my $hash = shift; my $o_git_dir = $git_dir; my $retval = undef; $git_dir = "$projectroot/$project"; - if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") { + if (open my $fd, '-|', git_cmd(), 'rev-parse', '--verify', $hash) { my $head = <$fd>; close $fd; if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
@@ -5196,8 +5202,9 @@ sub git_snapshot { die_error(403, "Unsupported snapshot format"); } - if (!defined $hash) { - $hash = git_get_head_hash($project); + my $snapshot = git_get_full_hash($project, $hash); + if (!$snapshot) { + die_error(404, 'Hash id was not valid'); } my $name = $project;
t/t9501-gitweb-standalone-http-status.sh +29 -0
@@ -75,4 +75,33 @@ test_expect_success \ test_debug 'cat gitweb.output' +# ---------------------------------------------------------------------- +# snapshot hash ids + +test_expect_success \ + 'snapshots: good treeish id' \ + 'gitweb_run "p=.git;a=snapshot;h=master;sf=tgz" && + grep "Status: 200 OK" gitweb.output' +test_debug 'cat gitweb.output' + +test_expect_success \ + 'snapshots: bad treeish id' \ + 'gitweb_run "p=.git;a=snapshot;h=frizzumFrazzum;sf=tgz" && + grep "404 - Hash id was not valid" gitweb.output' +test_debug 'cat gitweb.output' + +test_expect_success \ + 'snapshots: good object id' \ + 'ID=`git rev-parse --verify HEAD` && + gitweb_run "p=.git;a=snapshot;h=$ID;sf=tgz" && + grep "Status: 200 OK" gitweb.output' +test_debug 'cat gitweb.output' + +test_expect_success \ + 'snapshots: bad object id' \ + 'gitweb_run "p=.git;a=snapshot;h=abcdef01234;sf=tgz" && + grep "404 - Hash id was not valid" gitweb.output' +test_debug 'cat gitweb.output' + + test_done
Signature · 1 line
1.6.4.2