Skip to content

Commit 7e1100e

Browse files
mattmccutchengitster
authored andcommitted
gitweb: add $prevent_xss option to prevent XSS by repository content
Add a gitweb configuration variable $prevent_xss that disables features to prevent content in repositories from launching cross-site scripting (XSS) attacks in the gitweb domain. Currently, this option makes gitweb ignore README.html (a better solution may be worked out in the future) and serve a blob_plain file of an untrusted type with "Content-Disposition: attachment", which tells the browser not to show the file at its original URL. The XSS prevention is currently off by default. Signed-off-by: Matt McCutchen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e46cc0 commit 7e1100e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

gitweb/README

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ not include variables usually directly set during build):
214214
Rename detection options for git-diff and git-diff-tree. By default
215215
('-M'); set it to ('-C') or ('-C', '-C') to also detect copies, or
216216
set it to () if you don't want to have renames detection.
217+
* $prevent_xss
218+
If true, some gitweb features are disabled to prevent content in
219+
repositories from launching cross-site scripting (XSS) attacks. Set this
220+
to true if you don't trust the content of your repositories. The default
221+
is false.
217222

218223

219224
Projects list file format
@@ -260,7 +265,9 @@ You can use the following files in repository:
260265
A .html file (HTML fragment) which is included on the gitweb project
261266
summary page inside <div> block element. You can use it for longer
262267
description of a project, to provide links (for example to project's
263-
homepage), etc.
268+
homepage), etc. This is recognized only if XSS prevention is off
269+
($prevent_xss is false); a way to include a readme safely when XSS
270+
prevention is on may be worked out in the future.
264271
* description (or gitweb.description)
265272
Short (shortened by default to 25 characters in the projects list page)
266273
single line description of a project (of a repository). Plain text file;

gitweb/gitweb.perl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ BEGIN
132132
# - one might want to include '-B' option, e.g. '-B', '-M'
133133
our @diff_opts = ('-M'); # taken from git_commit
134134

135+
# Disables features that would allow repository owners to inject script into
136+
# the gitweb domain.
137+
our $prevent_xss = 0;
138+
135139
# information about snapshot formats that gitweb is capable of serving
136140
our %known_snapshot_formats = (
137141
# name => {
@@ -4494,7 +4498,9 @@ sub git_summary {
44944498

44954499
print "</table>\n";
44964500

4497-
if (-s "$projectroot/$project/README.html") {
4501+
# If XSS prevention is on, we don't include README.html.
4502+
# TODO: Allow a readme in some safe format.
4503+
if (!$prevent_xss && -s "$projectroot/$project/README.html") {
44984504
print "<div class=\"title\">readme</div>\n" .
44994505
"<div class=\"readme\">\n";
45004506
insert_file("$projectroot/$project/README.html");
@@ -4739,10 +4745,21 @@ sub git_blob_plain {
47394745
$save_as .= '.txt';
47404746
}
47414747

4748+
# With XSS prevention on, blobs of all types except a few known safe
4749+
# ones are served with "Content-Disposition: attachment" to make sure
4750+
# they don't run in our security domain. For certain image types,
4751+
# blob view writes an <img> tag referring to blob_plain view, and we
4752+
# want to be sure not to break that by serving the image as an
4753+
# attachment (though Firefox 3 doesn't seem to care).
4754+
my $sandbox = $prevent_xss &&
4755+
$type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
4756+
47424757
print $cgi->header(
47434758
-type => $type,
47444759
-expires => $expires,
4745-
-content_disposition => 'inline; filename="' . $save_as . '"');
4760+
-content_disposition =>
4761+
($sandbox ? 'attachment' : 'inline')
4762+
. '; filename="' . $save_as . '"');
47464763
undef $/;
47474764
binmode STDOUT, ':raw';
47484765
print <$fd>;

0 commit comments

Comments
 (0)