package server import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "regexp" "strings" "testing" "golang.org/x/net/html" ) const projectionAttack = `

Readable heading

Formatting chapter

bad URL
forged controls
` 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, `