Skip to contentSkip to conversation

gitweb: export-ok option

PATCH In review Matthias Lederhofer 3 participants 15 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gitweb: export-ok option

Similar to git-daemon checking for git-daemon-export-ok this introduces an optional check before showing a repository in gitweb.

I also changed the 'No such directory' error message to 'No such project' because the user visiting my gitweb should not know what directories I have in the project root directory.

undef $project; is used to prevent displaying the description.
---
Perhaps there should be another option which allows only those repositories to be shown which are in $projects_list.

---
 Makefile           |    2 ++
 gitweb/gitweb.perl |   18 ++++++++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)
Makefile +2 -0
index 7b3114f..63df24c 100644
@@ -132,6 +132,7 @@ GITWEB_HOMETEXT = indextext.html
 GITWEB_CSS = gitweb.css
 GITWEB_LOGO = git-logo.png
 GITWEB_FAVICON = git-favicon.png
+GITWEB_EXPORT_OK =
 
 export prefix bindir gitexecdir template_dir GIT_PYTHON_DIR
 
@@ -637,6 +638,7 @@ gitweb/gitweb.cgi: gitweb/gitweb.perl
 	    -e 's|++GITWEB_CSS++|$(GITWEB_CSS)|g' \
 	    -e 's|++GITWEB_LOGO++|$(GITWEB_LOGO)|g' \
 	    -e 's|++GITWEB_FAVICON++|$(GITWEB_FAVICON)|g' \
+	    -e 's|++GITWEB_EXPORT_OK++|$(GITWEB_EXPORT_OK)|g' \
 	    $< >$@+
 	chmod +x $@+
 	mv $@+ $@
gitweb/gitweb.perl +12 -6
index d89f709..3944d13 100755
@@ -54,6 +54,9 @@ our $favicon = "++GITWEB_FAVICON++";
 # source of projects list
 our $projects_list = "++GITWEB_LIST++";
 
+# show repository only if this file exists
+our $export_ok = "++GITWEB_EXPORT_OK++";
+
 # list of git base URLs used for URL to where fetch project from,
 # i.e. full URL is "$git_base_url/$project"
 our @git_base_url_list = ("++GITWEB_BASE_URL++");
@@ -181,12 +184,13 @@ if (defined $project) {
 }
 if (defined $project) {
 	if (!validate_input($project)) {
+		undef $project;
 		die_error(undef, "Invalid project parameter");
 	}
-	if (!(-d "$projectroot/$project")) {
-		die_error(undef, "No such directory");
-	}
-	if (!(-e "$projectroot/$project/HEAD")) {
+	if (!(-d "$projectroot/$project") ||
+	    !(-e "$projectroot/$project/HEAD") ||
+	    ($export_ok && !(-e "$projectroot/$project/$export_ok"))) {
+		undef $project;
 		die_error(undef, "No such project");
 	}
 	$git_dir = "$projectroot/$project";
@@ -694,7 +698,8 @@ sub git_get_projects_list {
 		my $dir = $projects_list;
 		opendir my ($dh), $dir or return undef;
 		while (my $dir = readdir($dh)) {
-			if (-e "$projectroot/$dir/HEAD") {
+			if (-e "$projectroot/$dir/HEAD" && (!$export_ok ||
+			    -e "$projectroot/$dir/$export_ok")) {
 				my $pr = {
 					path => $dir,
 				};
@@ -716,7 +721,8 @@ sub git_get_projects_list {
 			if (!defined $path) {
 				next;
 			}
-			if (-e "$projectroot/$path/HEAD") {
+			if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
+			    -e "$projectroot/$path/$export_ok")) {
 				my $pr = {
 					path => $path,
 					owner => decode("utf8", $owner, Encode::FB_DEFAULT),
Signature · 1 line
1.4.2.g0ea2
#1 lore raw

PATCH 1/1 gitweb: option 'strict export'

With this option enabled gitweb will only give access to repositories which are also shown on the overview page.
---

Quoted 2 lines · Matthias Lederhofer

Perhaps there should be another option which allows only those repositories to be shown which are in $projects_list.

Here it is. This option is probably the one more likely to be used. It disallows access to repositories (using ?p=path/to/repository) that are not on the projects-list-page.

---
 Makefile           |    2 ++
 gitweb/gitweb.perl |   12 +++++++++++-
 2 files changed, 13 insertions(+), 1 deletions(-)
Makefile +2 -0
index 63df24c..0e17d4c 100644
@@ -133,6 +133,7 @@ GITWEB_CSS = gitweb.css
 GITWEB_LOGO = git-logo.png
 GITWEB_FAVICON = git-favicon.png
 GITWEB_EXPORT_OK =
+GITWEB_STRICT_EXPORT =
 
 export prefix bindir gitexecdir template_dir GIT_PYTHON_DIR
 
@@ -639,6 +640,7 @@ gitweb/gitweb.cgi: gitweb/gitweb.perl
 	    -e 's|++GITWEB_LOGO++|$(GITWEB_LOGO)|g' \
 	    -e 's|++GITWEB_FAVICON++|$(GITWEB_FAVICON)|g' \
 	    -e 's|++GITWEB_EXPORT_OK++|$(GITWEB_EXPORT_OK)|g' \
+	    -e 's|++GITWEB_STRICT_EXPORT++|$(GITWEB_STRICT_EXPORT)|g' \
 	    $< >$@+
 	chmod +x $@+
 	mv $@+ $@
gitweb/gitweb.perl +11 -1
index 3944d13..976f7ec 100755
@@ -57,6 +57,9 @@ our $projects_list = "++GITWEB_LIST++";
 # show repository only if this file exists
 our $export_ok = "++GITWEB_EXPORT_OK++";
 
+# only allow viewing of repositories also shown on the overview page
+our $strict_export = "++GITWEB_STRICT_EXPORT++";
+
 # list of git base URLs used for URL to where fetch project from,
 # i.e. full URL is "$git_base_url/$project"
 our @git_base_url_list = ("++GITWEB_BASE_URL++");
@@ -189,7 +192,8 @@ if (defined $project) {
 	}
 	if (!(-d "$projectroot/$project") ||
 	    !(-e "$projectroot/$project/HEAD") ||
-	    ($export_ok && !(-e "$projectroot/$project/$export_ok"))) {
+	    ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
+	    ($strict_export && !project_in_list($project))) {
 		undef $project;
 		die_error(undef, "No such project");
 	}
@@ -384,6 +388,12 @@ sub untabify {
 	return $line;
 }
 
+sub project_in_list {
+	my $project = shift;
+	my @list = git_get_projects_list();
+	return(@list && scalar(grep { $_->{'path'} eq $project } @list) != 0);
+}
+
 ## ----------------------------------------------------------------------
 ## HTML aware string manipulation
 
Signature · 1 line
1.4.2.g0ea2
#2 lore raw

[PATCH/current master] gitweb: do not use 'No such directory' error message

In reply to Matthias Lederhofer

undef $project; to prevent a file named description to be read.
---
Sorry, I patched an old version. Here is the patch for the current master/next.

---
 gitweb/gitweb.perl |   11 ++++-------
 1 files changed, 4 insertions(+), 7 deletions(-)
gitweb/gitweb.perl +4 -7
index a81c8d4..07ea1ea 100755
@@ -203,13 +203,10 @@ if (defined $project) {
 	$project = undef unless $project;
 }
 if (defined $project) {
-	if (!validate_input($project)) {
-		die_error(undef, "Invalid project parameter");
-	}
-	if (!(-d "$projectroot/$project")) {
-		die_error(undef, "No such directory");
-	}
-	if (!(-e "$projectroot/$project/HEAD")) {
+	if (!validate_input($project) ||
+	    !(-d "$projectroot/$project") ||
+	    !(-e "$projectroot/$project/HEAD")) {
+		undef $project;
 		die_error(undef, "No such project");
 	}
 	$git_dir = "$projectroot/$project";
Signature · 1 line
1.4.2.g0ea2
#3 lore raw

PATCH 1/1 gitweb: export options

$export_ok: If this variable evaluates to true it is checked if a file with this name exists in the repository. If it does not exist the repository cannot be viewed from gitweb. (Similar to git-daemon-export-ok for git-daemon).

$strict_export: If this variable evaluates to true only repositories listed on the project-list-page of gitweb can be accessed.

---
 Makefile           |    4 ++++
 gitweb/gitweb.perl |   23 ++++++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)
Makefile +4 -0
index 7b3114f..b9938ac 100644
@@ -126,6 +126,8 @@ GITWEB_CONFIG = gitweb_config.perl
 GITWEB_HOME_LINK_STR = projects
 GITWEB_SITENAME =
 GITWEB_PROJECTROOT = /pub/git
+GITWEB_EXPORT_OK =
+GITWEB_STRICT_EXPORT =
 GITWEB_BASE_URL =
 GITWEB_LIST =
 GITWEB_HOMETEXT = indextext.html
@@ -631,6 +633,8 @@ gitweb/gitweb.cgi: gitweb/gitweb.perl
 	    -e 's|++GITWEB_HOME_LINK_STR++|$(GITWEB_HOME_LINK_STR)|g' \
 	    -e 's|++GITWEB_SITENAME++|$(GITWEB_SITENAME)|g' \
 	    -e 's|++GITWEB_PROJECTROOT++|$(GITWEB_PROJECTROOT)|g' \
+	    -e 's|++GITWEB_EXPORT_OK++|$(GITWEB_EXPORT_OK)|g' \
+	    -e 's|++GITWEB_STRICT_EXPORT++|$(GITWEB_STRICT_EXPORT)|g' \
 	    -e 's|++GITWEB_BASE_URL++|$(GITWEB_BASE_URL)|g' \
 	    -e 's|++GITWEB_LIST++|$(GITWEB_LIST)|g' \
 	    -e 's|++GITWEB_HOMETEXT++|$(GITWEB_HOMETEXT)|g' \
gitweb/gitweb.perl +20 -3
index 07ea1ea..2a63c17 100755
@@ -54,6 +54,13 @@ our $favicon = "++GITWEB_FAVICON++";
 # source of projects list
 our $projects_list = "++GITWEB_LIST++";
 
+# show repository only if this file exists
+# (only effective if this variable evaluates to true)
+our $export_ok = "++GITWEB_EXPORT_OK++";
+
+# only allow viewing of repositories also shown on the overview page
+our $strict_export = "++GITWEB_STRICT_EXPORT++";
+
 # list of git base URLs used for URL to where fetch project from,
 # i.e. full URL is "$git_base_url/$project"
 our @git_base_url_list = ("++GITWEB_BASE_URL++");
@@ -205,7 +212,9 @@ if (defined $project) {
 if (defined $project) {
 	if (!validate_input($project) ||
 	    !(-d "$projectroot/$project") ||
-	    !(-e "$projectroot/$project/HEAD")) {
+	    !(-e "$projectroot/$project/HEAD") ||
+	    ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
+	    ($strict_export && !project_in_list($project))) {
 		undef $project;
 		die_error(undef, "No such project");
 	}
@@ -402,6 +411,12 @@ sub untabify {
 	return $line;
 }
 
+sub project_in_list {
+	my $project = shift;
+	my @list = git_get_projects_list();
+	return @list && scalar(grep { $_->{'path'} eq $project } @list);
+}
+
 ## ----------------------------------------------------------------------
 ## HTML aware string manipulation
 
@@ -714,7 +729,8 @@ sub git_get_projects_list {
 
 				my $subdir = substr($File::Find::name, $pfxlen + 1);
 				# we check related file in $projectroot
-				if (-e "$projectroot/$subdir/HEAD") {
+				if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
+				    -e "$projectroot/$subdir/$export_ok")) {
 					push @list, { path => $subdir };
 					$File::Find::prune = 1;
 				}
@@ -735,7 +751,8 @@ sub git_get_projects_list {
 			if (!defined $path) {
 				next;
 			}
-			if (-e "$projectroot/$path/HEAD") {
+			if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
+			    -e "$projectroot/$path/$export_ok")) {
 				my $pr = {
 					path => $path,
 					owner => decode("utf8", $owner, Encode::FB_DEFAULT),
Signature · 1 line
1.4.2.g0ea2
#4 lore raw

Re: [PATCH] gitweb: export options

Quoted 8 lines · Matthias Lederhofer

$export_ok: If this variable evaluates to true it is checked if a file with this name exists in the repository. If it does not exist the repository cannot be viewed from gitweb. (Similar to git-daemon-export-ok for git-daemon).

$strict_export: If this variable evaluates to true only repositories listed on the project-list-page of gitweb can be accessed.

Is this replacement for your earlier two patches?

[PATCH] gitweb: export-ok option
[PATCH] gitweb: option 'strict export'

What's the difference between this and the previous two?

I am asking this question because I am lazy and do not want to review two sets of similar patches ;-).

#5 lore raw

Re: [PATCH] gitweb: export options

Quoted 2 lines · Junio C Hamano

Matthias Lederhofer <[email protected]> writes:
Is this replacement for your earlier two patches?

Yes, it is, they did not apply on the current master. (I fetched the new heads, had a short look at the log but forgot to update my working heads :) ).

Quoted 4 lines

[PATCH] gitweb: export-ok option
[PATCH] gitweb: option 'strict export'

What's the difference between this and the previous two?

Now I decided to split it in another way: first remove the no-such-directory error and then implement the two new features. The patches should be equivalent to those before.

#6 lore raw

Re: [PATCH] gitweb: export options

Quoted 7 lines · Matthias Lederhofer
Quoted 2 lines · Junio C Hamano

Matthias Lederhofer <[email protected]> writes:
Is this replacement for your earlier two patches?

Yes, it is, they did not apply on the current master. (I fetched the new heads, had a short look at the log but forgot to update my working heads :) ).

Quoted 8 lines
Quoted 4 lines

[PATCH] gitweb: export-ok option
[PATCH] gitweb: option 'strict export'

What's the difference between this and the previous two?

Now I decided to split it in another way: first remove the no-such-directory error and then implement the two new features. The patches should be equivalent to those before.

I see.

The PATHINFO stuff Martin Waitz did mucks with $project somewhat later than this part of the patch, possibly bypassing your checks. Could you check what's in 'master' to see if it is reasonable and if not fix it up please?

#7 lore raw

PATCH 1/1 gitweb: fix warnings in PATH_INFO code and add export_ok/strict_export

---

Quoted 5 lines · Junio C Hamano

Matthias Lederhofer <[email protected]> writes:
The PATHINFO stuff Martin Waitz did mucks with $project somewhat later than this part of the patch, possibly bypassing your checks. Could you check what's in 'master' to see if it is reasonable and if not fix it up please?

This patch replaces the other two warning fixes by Jakub and me. I've put the whole thing in a sub-routine to keep the indentation level low.

---
 gitweb/gitweb.perl |   34 +++++++++++++++++++++-------------
 1 files changed, 21 insertions(+), 13 deletions(-)
gitweb/gitweb.perl +21 -13
index 497129a..0fb8638 100755
@@ -189,9 +189,6 @@ do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
 # version of the core git binary
 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
 
-# path to the current git repository
-our $git_dir;
-
 $projects_list ||= $projectroot;
 
 # ======================================================================
@@ -273,30 +270,41 @@ if (defined $searchtext) {
 }
 
 # now read PATH_INFO and use it as alternative to parameters
-our $path_info = $ENV{"PATH_INFO"};
-$path_info =~ s|^/||;
-$path_info =~ s|/$||;
-if (validate_input($path_info) && !defined $project) {
+sub evaluate_path_info {
+	return if defined $project;
+	my $path_info = $ENV{"PATH_INFO"};
+	return if !$path_info;
+	$path_info =~ s,(^/|/$),,gs;
+	$path_info = validate_input($path_info);
+	return if !$path_info;
 	$project = $path_info;
 	while ($project && !-e "$projectroot/$project/HEAD") {
 		$project =~ s,/*[^/]*$,,;
 	}
-	if (defined $project) {
-		$project = undef unless $project;
+	if (!$project ||
+	    ($export_ok && !-e "$projectroot/$project/$export_ok") ||
+	    ($strict_export && !project_in_list($project))) {
+		undef $project;
+		return;
 	}
+	# do not change any parameters if an action is given using the query string
+	return if $action;
 	if ($path_info =~ m,^$project/([^/]+)/(.+)$,) {
 		# we got "project.git/branch/filename"
 		$action    ||= "blob_plain";
-		$hash_base ||= $1;
-		$file_name ||= $2;
+		$hash_base ||= validate_input($1);
+		$file_name ||= validate_input($2);
 	} elsif ($path_info =~ m,^$project/([^/]+)$,) {
 		# we got "project.git/branch"
 		$action ||= "shortlog";
-		$hash   ||= $1;
+		$hash   ||= validate_input($1);
 	}
 }
+evaluate_path_info();
 
-$git_dir = "$projectroot/$project";
+# path to the current git repository
+our $git_dir;
+$git_dir = "$projectroot/$project" if $project;
 
 # dispatch
 my %actions = (
Signature · 1 line
1.4.2.1.ge767
#8 lore raw

Re: [PATCH] gitweb: fix warnings in PATH_INFO code and add export_ok/strict_export

Quoted 3 lines · Matthias Lederhofer

This patch replaces the other two warning fixes by Jakub and me. I've put the whole thing in a sub-routine to keep the indentation level low.

Looks much cleaner with this extra sub.

This does not seem have the "colon as a cue that the URL is also talking about a branch", Jakub's summary of the discussion between you two on #git channel. I think that proposal makes sense as well.

#9 lore raw

Re: [PATCH] gitweb: fix warnings in PATH_INFO code and add export_ok/strict_export

Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Matthias Lederhofer

This patch replaces the other two warning fixes by Jakub and me. I've put the whole thing in a sub-routine to keep the indentation level low.

Quoted 6 lines

Looks much cleaner with this extra sub.

This does not seem have the "colon as a cue that the URL is also talking about a branch", Jakub's summary of the discussion between you two on #git channel. I think that proposal makes sense as well.

Well, that is because this patch _predates_ the discussion (and proposal/conclusions).

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
#10 lore raw

Re: [PATCH] gitweb: fix warnings in PATH_INFO code and add export_ok/strict_export

Quoted 10 lines · Jakub Narebski
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Matthias Lederhofer

This patch replaces the other two warning fixes by Jakub and me. I've put the whole thing in a sub-routine to keep the indentation level low.

Looks much cleaner with this extra sub.

Quoted 7 lines
Quoted 4 lines

This does not seem have the "colon as a cue that the URL is also talking about a branch", Jakub's summary of the discussion between you two on #git channel. I think that proposal makes sense as well.

Well, that is because this patch _predates_ the discussion (and proposal/conclusions).

Yup, I know, I was not complaining.

I was saying that I do not oppose to a follow-up patch to add that "colon" stuff.

#11 lore raw
In reply to Matthias Lederhofer
Quoted 2 lines · Matthias Lederhofer

Perhaps there should be another option which allows only those repositories to be shown which are in $projects_list.

If $projects_list is a file (and not directory), only repositories specified there are shown.

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
#12 lore raw
Quoted 7 lines · Jakub Narebski
Quoted 2 lines · Matthias Lederhofer

Perhaps there should be another option which allows only those repositories to be shown which are in $projects_list.

If $projects_list is a file (and not directory), only repositories specified there are shown.

The main problem is not the list of projects. Every repository below $project_root can be viewed using ?p=path/to/repostory.

#13 lore raw
In reply to Jakub Narebski
Quoted 7 lines · Jakub Narebski
Quoted 2 lines · Matthias Lederhofer

Perhaps there should be another option which allows only those repositories to be shown which are in $projects_list.

If $projects_list is a file (and not directory), only repositories specified there are shown.

If you can _guess_ project's name, you can go directly to git_summary with it, and the page (or links that the page leads to) does not check $projects_list at all, no?

I think Matthias's patch addresses that issue.

#14 lore raw
In reply to Matthias Lederhofer
Quoted 11 lines · Matthias Lederhofer

Similar to git-daemon checking for git-daemon-export-ok this introduces an optional check before showing a repository in gitweb.

I also changed the 'No such directory' error message to 'No such project' because the user visiting my gitweb should not know what directories I have in the project root directory.

undef $project; is used to prevent displaying the description.
---
Perhaps there should be another option which allows only those repositories to be shown which are in $projects_list.

I don't know what this is for (well, except replacing 'No such directory' error message by 'No such project'). If you want only some of repositories to be present in gitweb, you simply use $projects_list _file_ (e.g. using new "project_index" action to generate it) and edit it to contain only the repositories you want to be shown in gitweb.

Unless of course the person who hosts gitweb, and the person who owns repository are uncommunicado, and you want the power of deciding if repository is present in gitweb or not to be placed in the hands of repository owner. Still, gitweb must be configured for this.

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git