@matheusbsbs / matheus-bagliano / commits / 403d0906e9
Fork 0

gitweb: Better git-unquoting and gitweb-quoting of pathnames

Extend unquote subroutine, which unquotes quoted and escaped filenames which git may return, to deal not only with octal char sequence quoting, but also quoting ordinary characters including '\"' and '\\' which are respectively quoted '"' and '\', and to deal also with C escape sequences including '\t' for TAB and '\n' for LF. Add esc_path subroutine for gitweb quoting and HTML escaping filenames (currently it does equivalent of ls' --hide-control-chars, which means showing undisplayable characters (including '\n' and '\t') as '?' (question mark) character, and use 'span' element with cntrl CSS class to help rendering them differently. Convert gitweb to use esc_path correctly to print pathnames. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Nov 8, 2006 at 11:48 UTC 403d0906e9876871a7913554be4caa93f21f1d15
2 files changed +60 -19
gitweb/gitweb.css
+7
@@ -16,6 +16,13 @@ a:hover, a:visited, a:active {
16 color: #880000;
17 }
18
19 +span.cntrl {
20 + border: dashed #aaaaaa;
21 + border-width: 1px;
22 + padding: 0px 2px 0px 2px;
23 + margin: 0px 2px 0px 2px;
24 +}
25 +
26 img.logo {
27 float: right;
28 border-width: 0px;
gitweb/gitweb.perl
+53 -19
@@ -584,12 +584,46 @@ sub esc_html ($;%) {
584 return $str;
585 }
586
587 +# quote control characters and escape filename to HTML
588 +sub esc_path {
589 + my $str = shift;
590 + $str = esc_html($str);
591 + $str =~ s|([[:cntrl:]])|<span class="cntrl">?</span>|g;
592 + return $str;
593 +}
594 +
595 # git may return quoted and escaped filenames
596 sub unquote {
597 my $str = shift;
598 +
599 + sub unq {
600 + my $seq = shift;
601 + my %es = ( # character escape codes, aka escape sequences
602 + 't' => "\t", # tab (HT, TAB)
603 + 'n' => "\n", # newline (NL)
604 + 'r' => "\r", # return (CR)
605 + 'f' => "\f", # form feed (FF)
606 + 'b' => "\b", # backspace (BS)
607 + 'a' => "\a", # alarm (bell) (BEL)
608 + 'e' => "\e", # escape (ESC)
609 + 'v' => "\013", # vertical tab (VT)
610 + );
611 +
612 + if ($seq =~ m/^[0-7]{1,3}$/) {
613 + # octal char sequence
614 + return chr(oct($seq));
615 + } elsif (exists $es{$seq}) {
616 + # C escape sequence, aka character escape code
617 + return $es{$seq}
618 + }
619 + # quoted ordinary character
620 + return $seq;
621 + }
622 +
623 if ($str =~ m/^"(.*)"$/) {
624 + # needs unquoting
625 $str = $1;
592 - $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
626 + $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
627 }
628 return $str;
629 }
@@ -1520,7 +1554,7 @@ sub git_header_html {
1554 if (defined $action) {
1555 $title .= "/$action";
1556 if (defined $file_name) {
1523 - $title .= " - " . esc_html($file_name);
1557 + $title .= " - " . esc_path($file_name);
1558 if ($action eq "tree" && $file_name !~ m|/$|) {
1559 $title .= "/";
1560 }
@@ -1791,20 +1825,20 @@ sub git_print_page_path {
1825 $fullname .= ($fullname ? '/' : '') . $dir;
1826 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1827 hash_base=>$hb),
1794 - -title => $fullname}, esc_html($dir));
1828 + -title => $fullname}, esc_path($dir));
1829 print " / ";
1830 }
1831 if (defined $type && $type eq 'blob') {
1832 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1833 hash_base=>$hb),
1800 - -title => $name}, esc_html($basename));
1834 + -title => $name}, esc_path($basename));
1835 } elsif (defined $type && $type eq 'tree') {
1836 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1837 hash_base=>$hb),
1804 - -title => $name}, esc_html($basename));
1838 + -title => $name}, esc_path($basename));
1839 print " / ";
1840 } else {
1807 - print esc_html($basename);
1841 + print esc_path($basename);
1842 }
1843 }
1844 print "<br/></div>\n";
@@ -1876,7 +1910,7 @@ sub git_print_tree_entry {
1910 print "<td class=\"list\">" .
1911 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1912 file_name=>"$basedir$t->{'name'}", %base_key),
1879 - -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1913 + -class => "list"}, esc_path($t->{'name'})) . "</td>\n";
1914 print "<td class=\"link\">";
1915 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1916 file_name=>"$basedir$t->{'name'}", %base_key)},
@@ -1903,7 +1937,7 @@ sub git_print_tree_entry {
1937 print "<td class=\"list\">";
1938 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1939 file_name=>"$basedir$t->{'name'}", %base_key)},
1906 - esc_html($t->{'name'}));
1940 + esc_path($t->{'name'}));
1941 print "</td>\n";
1942 print "<td class=\"link\">";
1943 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
@@ -1968,7 +2002,7 @@ sub git_difftree_body {
2002 print "<td>";
2003 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2004 hash_base=>$hash, file_name=>$diff{'file'}),
1971 - -class => "list"}, esc_html($diff{'file'}));
2005 + -class => "list"}, esc_path($diff{'file'}));
2006 print "</td>\n";
2007 print "<td>$mode_chng</td>\n";
2008 print "<td class=\"link\">";
@@ -1984,7 +2018,7 @@ sub git_difftree_body {
2018 print "<td>";
2019 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2020 hash_base=>$parent, file_name=>$diff{'file'}),
1987 - -class => "list"}, esc_html($diff{'file'}));
2021 + -class => "list"}, esc_path($diff{'file'}));
2022 print "</td>\n";
2023 print "<td>$mode_chng</td>\n";
2024 print "<td class=\"link\">";
@@ -2024,7 +2058,7 @@ sub git_difftree_body {
2058 print "<td>";
2059 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2060 hash_base=>$hash, file_name=>$diff{'file'}),
2027 - -class => "list"}, esc_html($diff{'file'}));
2061 + -class => "list"}, esc_path($diff{'file'}));
2062 print "</td>\n";
2063 print "<td>$mode_chnge</td>\n";
2064 print "<td class=\"link\">";
@@ -2064,11 +2098,11 @@ sub git_difftree_body {
2098 print "<td>" .
2099 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2100 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
2067 - -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
2101 + -class => "list"}, esc_path($diff{'to_file'})) . "</td>\n" .
2102 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2103 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2104 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
2071 - -class => "list"}, esc_html($diff{'from_file'})) .
2105 + -class => "list"}, esc_path($diff{'from_file'})) .
2106 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2107 "<td class=\"link\">";
2108 if ($action eq 'commitdiff') {
@@ -2191,7 +2225,7 @@ sub git_patchset_body {
2225 $file ||= $diffinfo->{'file'};
2226 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2227 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2194 - -class => "list"}, esc_html($file));
2228 + -class => "list"}, esc_path($file));
2229 $patch_line =~ s|a/.*$|a/$file|g;
2230 print "<div class=\"diff from_file\">$patch_line</div>\n";
2231
@@ -2203,7 +2237,7 @@ sub git_patchset_body {
2237 $file ||= $diffinfo->{'file'};
2238 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2239 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2206 - -class => "list"}, esc_html($file));
2240 + -class => "list"}, esc_path($file));
2241 $patch_line =~ s|b/.*|b/$file|g;
2242 print "<div class=\"diff to_file\">$patch_line</div>\n";
2243
@@ -3521,8 +3555,8 @@ sub git_blobdiff {
3555
3556 } else {
3557 while (my $line = <$fd>) {
3524 - $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3525 - $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3558 + $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
3559 + $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
3560
3561 print $line;
3562
@@ -3879,7 +3913,7 @@ sub git_search {
3913 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3914 hash=>$set{'id'}, file_name=>$set{'file'}),
3915 -class => "list"},
3882 - "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3916 + "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
3917 "<br/>\n";
3918 }
3919 print "</td>\n" .
@@ -4014,7 +4048,7 @@ XML
4048 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
4049 next;
4050 }
4017 - my $file = esc_html(unquote($7));
4051 + my $file = esc_path(unquote($7));
4052 $file = to_utf8($file);
4053 print "$file<br/>\n";
4054 }