e39a9560ba
GET /files and /artifacts/ now send the artifact sandbox CSP and nosniff through one helper, and /files serves HTML, XHTML, SVG, XML, XSL, JavaScript and multipart/x-mixed-replace as attachments, including when the type is sniffed from an extensionless file. The web proxy adds the same headers to every proxied /files and /artifacts/ response (defence in depth against an older server), and the peer relay shares the helper. show_html previews are unchanged by operator decision. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
140 lines
5.2 KiB
Go
140 lines
5.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestServeHomeFile(t *testing.T) {
|
|
root := t.TempDir()
|
|
home := filepath.Join(root, "home")
|
|
outside := filepath.Join(root, "outside.txt")
|
|
if err := os.Mkdir(home, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(home, "hello.txt"), []byte("hello file contents"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(home, "résumé"), []byte("plain"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(outside, []byte("outside"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(outside, filepath.Join(home, "escape")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
originalHomeDirectory := operatorHomeDirectory
|
|
operatorHomeDirectory = func() (string, error) { return home, nil }
|
|
t.Cleanup(func() { operatorHomeDirectory = originalHomeDirectory })
|
|
t.Run("operator home seam", func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
serveHomeFile(recorder, httptest.NewRequest(http.MethodGet, "/files?path=~/hello.txt", nil))
|
|
if recorder.Code != http.StatusOK || recorder.Body.String() != "hello file contents" {
|
|
t.Fatalf("response = %d %q", recorder.Code, recorder.Body.String())
|
|
}
|
|
})
|
|
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
want int
|
|
}{
|
|
{"happy path", "~/hello.txt", http.StatusOK},
|
|
{"directory refused", "~", http.StatusNotFound},
|
|
{"dot dot refused", filepath.Join(home, "..", "outside.txt"), http.StatusForbidden},
|
|
{"symlink refused", "~/escape", http.StatusForbidden},
|
|
{"missing file", "~/missing", http.StatusNotFound},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/files?path="+tc.path, nil)
|
|
recorder := httptest.NewRecorder()
|
|
serveHomeFileFrom(recorder, req, home)
|
|
if recorder.Code != tc.want {
|
|
t.Fatalf("status = %d, want %d", recorder.Code, tc.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("download disposition and range", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/files?path=~/hello.txt&download=1", nil)
|
|
req.Header.Set("Range", "bytes=0-4")
|
|
recorder := httptest.NewRecorder()
|
|
serveHomeFileFrom(recorder, req, home)
|
|
if recorder.Code != http.StatusPartialContent || recorder.Body.String() != "hello" {
|
|
t.Fatalf("response = %d %q, want 206 hello", recorder.Code, recorder.Body.String())
|
|
}
|
|
if got := recorder.Header().Get("Content-Disposition"); got != `attachment; filename="hello.txt"; filename*=UTF-8''hello.txt` {
|
|
t.Fatalf("disposition = %q", got)
|
|
}
|
|
if recorder.Header().Get("Content-Type") != "text/plain; charset=utf-8" {
|
|
t.Fatalf("content type = %q", recorder.Header().Get("Content-Type"))
|
|
}
|
|
})
|
|
|
|
t.Run("non ASCII disposition", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/files?path=~/r%C3%A9sum%C3%A9", nil)
|
|
recorder := httptest.NewRecorder()
|
|
serveHomeFileFrom(recorder, req, home)
|
|
if !strings.Contains(recorder.Header().Get("Content-Disposition"), "filename*=UTF-8''r%C3%A9sum%C3%A9") {
|
|
t.Fatal(recorder.Header().Get("Content-Disposition"))
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestServeHomeFileSandboxesScriptCapableTypes(t *testing.T) {
|
|
home := t.TempDir()
|
|
files := map[string]string{
|
|
"page.html": "<script>fetch('/connect')</script>",
|
|
"image.svg": `<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>`,
|
|
"page.xhtml": "<html xmlns='http://www.w3.org/1999/xhtml'></html>",
|
|
"feed.xml": "<?xml version='1.0'?><x/>",
|
|
"noext": "<!DOCTYPE html><html><script>alert(1)</script></html>",
|
|
"notes.txt": "plain",
|
|
"photo.png": "\x89PNG\r\n\x1a\n",
|
|
}
|
|
for name, body := range files {
|
|
if err := os.WriteFile(filepath.Join(home, name), []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for name := range files {
|
|
t.Run(name, func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
serveHomeFileFrom(recorder, httptest.NewRequest(http.MethodGet, "/files?path=~/"+name, nil), home)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", recorder.Code)
|
|
}
|
|
header := recorder.Header()
|
|
if csp := header.Get("Content-Security-Policy"); csp != untrustedContentPolicy || !strings.HasPrefix(csp, "sandbox;") {
|
|
t.Fatalf("csp = %q", csp)
|
|
}
|
|
if header.Get("X-Content-Type-Options") != "nosniff" {
|
|
t.Fatalf("nosniff = %q", header.Get("X-Content-Type-Options"))
|
|
}
|
|
wantAttachment := name != "notes.txt" && name != "photo.png"
|
|
if got := strings.HasPrefix(header.Get("Content-Disposition"), "attachment;"); got != wantAttachment {
|
|
t.Fatalf("disposition = %q (type %q), want attachment %v", header.Get("Content-Disposition"), header.Get("Content-Type"), wantAttachment)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestScriptCapableType(t *testing.T) {
|
|
for _, contentType := range []string{"text/html; charset=utf-8", "application/xhtml+xml", "image/svg+xml", "text/xml", "application/xml", "application/rss+xml", "TEXT/HTML"} {
|
|
if !scriptCapableType(contentType) {
|
|
t.Errorf("%q should be script-capable", contentType)
|
|
}
|
|
}
|
|
for _, contentType := range []string{"text/plain; charset=utf-8", "image/png", "application/pdf", "application/octet-stream", ""} {
|
|
if scriptCapableType(contentType) {
|
|
t.Errorf("%q should not be script-capable", contentType)
|
|
}
|
|
}
|
|
}
|