Skip to contentSkip to conversation

gitweb: check given hash before trying to create snapshot

PATCH In review Mark Rada 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore

Mark Rada

author
#0 lore raw

PATCH v5 1/2 gitweb: check given hash before trying to create snapshot

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.

Tests for t9501 are included to demonstrate added functionality.

---

Changes since v4:
- used Jakub's suggestion for checking hash validity
- moved git_get_full_hash to the second commit
- changed test cases format, suggested by Junio
- added another test case for tagged objects due to the bug Junio pointed out

Sorry it's been a while since the v4, school started and I got buried under a whole lot of other things I had to take care of first. I've got time now, so further fix ups will happen in a more reasonable time frame (but hopefully aren't needed!).

 gitweb/gitweb.perl                       |    7 ++++-
 t/t9501-gitweb-standalone-http-status.sh |   39 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 2 deletions(-)
gitweb/gitweb.perl +5 -2
index 24b2193..8d4a2ae 100755
@@ -5196,8 +5196,11 @@ sub git_snapshot {
 		die_error(403, "Unsupported snapshot format");
 	}
 
-	if (!defined $hash) {
-		$hash = git_get_head_hash($project);
+	my $type = git_get_type("$hash^{}");
+	if (!$type) {
+		die_error(404, 'Object does not exist');
+	}  elsif ($type eq 'blob') {
+		die_error(400, 'Object is not a tree-ish');
 	}
 
 	my $name = $project;
t/t9501-gitweb-standalone-http-status.sh +39 -0
index d0ff21d..0688a57 100644
@@ -75,4 +75,43 @@ test_expect_success \
 test_debug 'cat gitweb.output'
 
 
+# ----------------------------------------------------------------------
+# snapshot hash ids
+
+test_expect_success 'snapshots: good tree-ish 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 tree-ish id' '
+	gitweb_run "p=.git;a=snapshot;h=frizzumFrazzum;sf=tgz" &&
+	grep "404 - Object does not exist" gitweb.output
+'
+test_debug 'cat gitweb.output'
+
+test_expect_success 'snapshots: bad tree-ish id (tagged object)' '
+	echo object > tag-object &&
+	git add tag-object &&
+	git commit -m "Object to be tagged" &&
+	git tag tagged-object `git hash-object tag-object` &&
+	gitweb_run "p=.git;a=snapshot;h=tagged-object;sf=tgz" &&
+	grep "400 - Object is not a tree-ish" 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 - Object does not exist" gitweb.output
+'
+test_debug 'cat gitweb.output'
+
+
 test_done
Signature · 1 line
1.6.4.GIT
Acked-by on v5 1/2
#1 lore raw
Quoted 8 lines · Mark Rada

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.

Tests for t9501 are included to demonstrate added functionality.

---

Very nice, and I think robust solution.

[...]

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

Quoted 4 lines
t/t9501-gitweb-standalone-http-status.sh +0 -0
index d0ff21d..0688a57 100644

BTW. the rest of test scripts are executable, but not this one? Why? (But correcting this should be done, if needed, in separate commit).

t/t9501-gitweb-standalone-http-status.sh test_expect_success \ · line 86

Quoted 13 lines
t/t9501-gitweb-standalone-http-status.sh +9 -0
@@ -75,4 +75,43 @@ test_expect_success \
 test_debug 'cat gitweb.output'
 
 
+# ----------------------------------------------------------------------
+# snapshot hash ids
+
+test_expect_success 'snapshots: good tree-ish id' '
+	gitweb_run "p=.git;a=snapshot;h=master;sf=tgz" &&
+	grep "Status: 200 OK" gitweb.output
+'
+test_debug 'cat gitweb.output'
+

What *could* be improved (but don't *need to*) is to check also HTTP status and not only formatted error message:

t/t9501-gitweb-standalone-http-status.sh test_expect_success \ · line 88

Quoted 2 lines
t/t9501-gitweb-standalone-http-status.sh +2 -0
+test_expect_success 'snapshots: bad tree-ish id' '
+	gitweb_run "p=.git;a=snapshot;h=frizzumFrazzum;sf=tgz" &&

+ grep "Status: 404 Not Found" gitweb.output &&

t/t9501-gitweb-standalone-http-status.sh test_expect_success \ · line 91

Quoted 3 lines
t/t9501-gitweb-standalone-http-status.sh +3 -0
+	grep "404 - Object does not exist" gitweb.output
+'
+test_debug 'cat gitweb.output'

And similarly in other cases. But it is not something required. I think what it is now is good enough.

Signature · 2 lines
Jakub Narebski
Poland