Files
pages/internal/server/html_projection_test.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

212 lines
9.2 KiB
Go

package server
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"golang.org/x/net/html"
)
const projectionAttack = `<h1 id="chapter">Readable heading</h1><p><strong>Formatting</strong> <a href="#chapter">chapter</a></p>
<script>document.documentElement.dataset.attack='script';fetch('/sentinel',{method:'POST'})</script>
<script src="/static/attack.js"></script><img src="/v/raw/path:/missing.png" onerror="document.documentElement.dataset.attack='event'">
<a href="java&#x09;script:document.documentElement.dataset.attack='url'">bad URL</a>
<svg onload="document.documentElement.dataset.attack='svg'"><a><animate attributeName="href" values="javascript:alert(1)"/></a></svg>
<math><mtext><table><mglyph><style><!--</style><img title="--><img src=x onerror=alert(1)>"></math>
<iframe srcdoc="<script>parent.document.documentElement.dataset.attack='frame'</script>"></iframe>
<form id="editor" name="PagesEditor" action="/sentinel"><input name="saveNow"><button>attack</button></form>
<div class="children-async" data-children-src="/sentinel" style="position:fixed;inset:0">forged controls</div>
<style>body{display:none}</style><base href="https://invalid.example/"><meta http-equiv="refresh" content="0;url=/sentinel">
<img src="https://invalid.example/tracker"><img src="/e/accidental-write"><img src="/static/%2e%2e/e/accidental-write"><object data="/v/raw/path:/attack.html"></object>`
func checkInertProjection(t *testing.T, body string) {
t.Helper()
doc, err := html.Parse(strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
var walk func(*html.Node)
walk = func(n *html.Node) {
if n.Type == html.ElementNode {
switch n.Data {
case "script", "style", "svg", "math", "iframe", "form", "input", "button", "object", "embed", "base", "meta":
t.Fatalf("active element %s: %s", n.Data, body)
}
for _, a := range n.Attr {
if strings.HasPrefix(a.Key, "on") || a.Key == "style" || a.Key == "name" || a.Key == "srcdoc" {
t.Fatalf("active attr %+v", a)
}
if a.Key == "id" && !strings.HasPrefix(a.Val, "pw-doc-") {
t.Fatalf("unscoped ID %s", a.Val)
}
if (a.Key == "href" || a.Key == "src") && strings.Contains(strings.ToLower(a.Val), "javascript") {
t.Fatalf("active URL %+v", a)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(doc)
}
func TestHTMLProjectionCorpusAndMacros(t *testing.T) {
s, _, _ := configFixture(t)
r := httptest.NewRequest("GET", "http://localhost/guide", nil)
for _, body := range []string{projectionAttack, `<noscript><p title="</noscript><img src=x onerror=alert(1)>">`, `<a href="jav&#x61;script:alert(1)">bad</a><img src="//invalid.example/x">`, `<svg><foreignObject><iframe srcdoc="oops"></iframe></foreignObject></svg>`} {
got := s.safeHTMLProjection(r, body, "/guide", true)
checkInertProjection(t, got)
}
got := s.safeHTMLProjection(r, `<p>{{children:/guide}}</p><code>{{children:/literal}}</code><img alt="{{children:/attribute}}" src="/static/test.png"><section data-children-src="/sentinel" class="children-async">fake</section>`, "/guide", true)
checkInertProjection(t, got)
if strings.Count(got, `data-children-src=`) != 1 || strings.Contains(got, `data-children-src="/sentinel"`) || !strings.Contains(got, `{{children:/literal}}`) {
t.Fatalf("macro trust=%s", got)
}
got = s.safeHTMLProjection(r, projectionAttack, "/guide", false)
if !strings.Contains(got, "<strong>Formatting</strong>") || !strings.Contains(got, `href="#`+documentAnchor("chapter")) || strings.Contains(got, "https://invalid.example") || strings.Contains(got, `src="/e/`) {
t.Fatalf("format/resources=%s", got)
}
for _, body := range []string{strings.Repeat("x", maxProjectionInput+1), strings.Repeat("<div>", 129), strings.Repeat("{{children}}", 33)} {
got = s.safeHTMLProjection(r, body, "/guide", true)
if !strings.Contains(got, "projection-warning") {
t.Fatal("limit did not fail closed")
}
checkInertProjection(t, got)
}
ctx, cancel := context.WithCancel(r.Context())
cancel()
if got = s.safeHTMLProjection(r.WithContext(ctx), projectionAttack, "/guide", true); !strings.Contains(got, "projection-warning") {
t.Fatal("cancellation ignored")
}
}
func TestHTMLProjectionFailedMacroRemainsText(t *testing.T) {
s, _, _ := configFixture(t)
// Force a locality lookup failure in this temporary fixture. Decoded text
// must not acquire the final policy's application-owned macro attributes.
if _, err := s.store.DB.Exec(`ALTER TABLE zones RENAME TO unavailable_zones`); err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", "http://localhost/guide", nil)
for _, tail := range []string{"", "{{children:/guide}}"} {
body := `<p>{{spot ignored="&lt;section class='children-async' data-children-src='/sentinel'&gt;fake&lt;/section&gt;` + tail + `"}}</p>`
got := s.safeHTMLProjection(r, body, "/guide", true)
if strings.Contains(got, "<section") || !strings.Contains(got, "&lt;section") {
t.Fatalf("unresolved macro promoted authored markup: %s", got)
}
checkInertProjection(t, got)
}
}
func TestHTMLProjectionAPIKeepsCanonicalSource(t *testing.T) {
s, _, domain := configFixture(t)
object, err := s.store.CreatePageAtPathInDomain(domain, "/guide")
if err != nil {
t.Fatal(err)
}
revID, err := s.store.SaveRevisionContent(object, []byte(projectionAttack), "", "en", "text/html", "guide.html")
if err != nil {
t.Fatal(err)
}
before, err := s.store.GetRevision(revID.UUID)
if err != nil {
t.Fatal(err)
}
for _, mode := range []string{"html", "markdown"} {
payload := map[string]any{"projection": true, "path": "/guide", mode: projectionAttack}
b, _ := json.Marshal(payload)
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/render/markdown", bytes.NewReader(b))
s.handleMarkdownRenderAPI(w, r)
var response struct {
HTML string `json:"html"`
}
if err = json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
checkInertProjection(t, response.HTML)
if w.Header().Get("Cache-Control") != "private, no-store" {
t.Fatal("preview cache policy")
}
}
// The canonical conversion remains an authoring API, not a display result.
b, _ := json.Marshal(map[string]any{"markdown": projectionAttack})
w := httptest.NewRecorder()
s.handleMarkdownRenderAPI(w, httptest.NewRequest("POST", "http://localhost/_/api/v1/render/markdown", bytes.NewReader(b)))
if !strings.Contains(w.Body.String(), "dataset.attack") {
t.Fatal("canonical conversion was sanitized")
}
after, err := s.store.GetRevision(revID.UUID)
if err != nil || after.Body != before.Body || after.DocumentHash != before.DocumentHash {
t.Fatalf("canonical changed: %v", err)
}
}
func TestHTMLProjectionEndpointsAndCSP(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/", s.handleHome)
s.mux.HandleFunc("/o/", s.handleObjectView)
s.mux.HandleFunc("/r/view/", s.handleRevisionView)
s.mux.HandleFunc("/v/preview/", s.handlePreview)
s.mux.HandleFunc("/_/view/print/", s.handlePrint)
s.mux.HandleFunc("/u/", s.handleUserProfile)
object, err := s.store.CreatePageAtPathInDomain(domain, "/guide")
if err != nil {
t.Fatal(err)
}
rev, err := s.store.SaveRevisionContent(object, []byte(projectionAttack), "", "en", "text/html", "guide.html")
if err != nil {
t.Fatal(err)
}
userObject, err := s.store.CreatePageAtPathInDomain(domain, "/u/admin")
if err != nil {
t.Fatal(err)
}
if _, err = s.store.SaveRevisionContent(userObject, []byte(projectionAttack), "", "en", "text/html", "profile.html"); err != nil {
t.Fatal(err)
}
if err = s.store.SetConfigInDomain(domain, "home_path", "/guide"); err != nil {
t.Fatal(err)
}
for _, route := range []string{"/guide", "/", "/o/uuid:" + object, "/r/view/" + rev.UUID, "/v/preview/guide", "/_/view/print/path:/guide", "/u/admin"} {
w := configRequest(s, token, "GET", route, nil)
if w.Code != 200 {
t.Fatalf("%s=%d %s", route, w.Code, w.Body.String())
}
csp := w.Header().Get("Content-Security-Policy")
if !strings.Contains(csp, "script-src-attr 'none'") || !strings.Contains(csp, "'strict-dynamic'") || !strings.Contains(csp, "object-src 'none'") {
t.Fatalf("CSP %s=%s", route, csp)
}
if strings.Contains(w.Body.String(), `<script>document.documentElement.dataset.attack`) || strings.Contains(w.Body.String(), `onerror="document.documentElement.dataset.attack`) {
t.Fatalf("unsafe source in %s", route)
}
for _, m := range regexp.MustCompile(`<script\b[^>]*>`).FindAllString(w.Body.String(), -1) {
if !strings.Contains(m, `nonce="`) {
t.Fatalf("untrusted application script: %s", m)
}
}
}
if _, err = s.store.SaveRevisionContent(object, []byte(`<script>PLAIN_SENTINEL</script>`), "", "en", "text/plain", "code.txt"); err != nil {
t.Fatal(err)
}
w := configRequest(s, token, "GET", "/o/uuid:"+object, nil)
if w.Code != 200 || strings.Contains(w.Body.String(), `<script>PLAIN_SENTINEL`) || !strings.Contains(w.Body.String(), "&lt;script&gt;PLAIN_SENTINEL") {
t.Fatal("object MIME confusion")
}
}
func browserFixtureScript(headers http.Header, script []byte) string {
nonce := ""
if m := regexp.MustCompile(`'nonce-([^']+)'`).FindStringSubmatch(headers.Get("Content-Security-Policy")); len(m) == 2 {
nonce = m[1]
}
return `<script nonce="` + nonce + `">` + string(script) + `</script>`
}