git » gofer » commit 90e8cd5

dir: Use html/template for directory listings

author Alberto Bertogli
2026-10-30 20:20:34 UTC
committer Alberto Bertogli
2026-11-06 23:57:34 UTC
parent fe3d09538851d61806aa0a1048f452339f7fb5c2

dir: Use html/template for directory listings

Today we use text/template to render directory listings, and a manual
function to escape each name.

This is safe, but also prone to mistakes (and e.g. names with `&`
were broken).

So this patch moves to html/template which is more robust and safe by
default.

server/fileserver.go +2 -4
server/fileserver_test.go +57 -0

diff --git a/server/fileserver.go b/server/fileserver.go
index 118228f..7c8f423 100644
--- a/server/fileserver.go
+++ b/server/fileserver.go
@@ -2,7 +2,7 @@ package server
 
 import (
 	"errors"
-	"html"
+	"html/template"
 	"net/http"
 	"net/url"
 	"os"
@@ -10,7 +10,6 @@ import (
 	"slices"
 	"strconv"
 	"strings"
-	"text/template"
 )
 
 // FileServer implements an equivalent of http.FileServer, but with custom
@@ -135,7 +134,6 @@ func dirList(w http.ResponseWriter, req *http.Request, f http.File) {
 var tmplFuncs = template.FuncMap{
 	"humanize":   humanizeInt,
 	"pathEscape": pathEscape,
-	"htmlEscape": html.EscapeString,
 }
 
 var dirListTmpl = template.Must(
@@ -191,7 +189,7 @@ tbody tr:nth-child(even) {
   <tr>
     <td><code>
 	  <a href="{{.Name | pathEscape}}{{if .IsDir}}/{{end}}">
-	         {{- .Name | htmlEscape}}{{if .IsDir}}/{{end}}</a>
+	         {{- .Name}}{{if .IsDir}}/{{end}}</a>
     </code></td>
 	<td>{{if not .IsDir}}{{.Size | humanize}}{{end}}</td>
 	<td>{{.ModTime.Format "2006-01-02 15:04:05"}}</td>
diff --git a/server/fileserver_test.go b/server/fileserver_test.go
index 477ab10..3b32266 100644
--- a/server/fileserver_test.go
+++ b/server/fileserver_test.go
@@ -4,6 +4,9 @@ import (
 	"io/fs"
 	"net/http"
 	"net/http/httptest"
+	"os"
+	"path/filepath"
+	"strings"
 	"testing"
 )
 
@@ -59,3 +62,57 @@ func TestHumanizeInt(t *testing.T) {
 		}
 	}
 }
+
+// TestDirListEscaping checks that the links in directory listings are properly
+// escaped, including names that are special in URLs and in HTML.
+func TestDirListEscaping(t *testing.T) {
+	cases := []struct {
+		name string
+		want string
+	}{
+		{"plain", `<a href="plain">plain</a>`},
+		{"ñaca", `<a href="%C3%B1aca">ñaca</a>`},
+		{"a b", `<a href="a%20b">a b</a>`},
+		{"%41", `<a href="%2541">%41</a>`},
+
+		// Special in URLs.
+		{"#anchor", `<a href="%23anchor">#anchor</a>`},
+		{"?query", `<a href="%3Fquery">?query</a>`},
+
+		// These could be mistaken for a URL scheme.
+		{"x:y", `<a href="./x:y">x:y</a>`},
+		{"javascript:alert(1)",
+			`<a href="./javascript:alert%281%29">javascript:alert(1)</a>`},
+
+		// Special in HTML.
+		{"<a>", `<a href="%3Ca%3E">&lt;a&gt;</a>`},
+		{`a"b`, `<a href="a%22b">a&#34;b</a>`},
+		{"a'b", `<a href="a%27b">a&#39;b</a>`},
+		{"&amp;", `<a href="&amp;amp;">&amp;amp;</a>`},
+		{"<img src=x onerror=alert(1)>",
+			`<a href="%3Cimg%20src=x%20onerror=alert%281%29%3E">` +
+				`&lt;img src=x onerror=alert(1)&gt;</a>`},
+	}
+
+	root := t.TempDir()
+	for _, c := range cases {
+		err := os.WriteFile(filepath.Join(root, c.name), nil, 0o644)
+		if err != nil {
+			t.Fatal(err)
+		}
+	}
+
+	req := httptest.NewRequest("GET", "http://unused/", nil)
+	w := httptest.NewRecorder()
+	FileServer(http.Dir(root)).ServeHTTP(w, req)
+	body := w.Body.String()
+
+	for _, c := range cases {
+		if !strings.Contains(body, c.want) {
+			t.Errorf("%q: %q not found in the listing", c.name, c.want)
+		}
+	}
+	if strings.Contains(body, "ZgotmplZ") {
+		t.Errorf("html/template filtered out a value:\n%s", body)
+	}
+}