Files
pages/internal/server/html_projection.go
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

332 lines
11 KiB
Go

package server
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
stdhtml "html"
"io"
"net/http"
"net/url"
pathpkg "path"
"regexp"
"strings"
"time"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
const htmlProjectionVersion = "controlled-v2"
const maxProjectionInput = 1 << 20
const maxProjectionOutput = 4 << 20
var projectionMacroRE = regexp.MustCompile(`\{\{(?:children[^}]*|(?:city-breadcrumbs|spot-pages|quartier|street|spot|nearby|city)(?:\s+[^}]*)?)\}\}`)
var languageClassRE = regexp.MustCompile(`^language-[a-zA-Z0-9_+-]{1,40}$`)
func projectionPolicy(generated bool) *bluemonday.Policy {
p := bluemonday.NewPolicy()
p.AllowStandardURLs()
p.RequireNoReferrerOnLinks(true)
p.AllowElements("p", "br", "hr", "div", "span", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote", "pre", "code", "b", "strong", "i", "em", "u", "s", "del", "ins", "sub", "sup", "small", "mark", "abbr", "kbd", "samp", "var", "ul", "ol", "li", "dl", "dt", "dd", "table", "thead", "tbody", "tfoot", "tr", "th", "td", "caption", "colgroup", "col", "figure", "figcaption", "details", "summary", "section", "article")
p.AllowAttrs("href").OnElements("a")
p.AllowAttrs("title", "lang", "dir").Globally()
p.AllowAttrs("id").Matching(regexp.MustCompile(`^[^\s]{1,256}$`)).Globally()
p.AllowAttrs("class").Matching(languageClassRE).OnElements("code")
p.AllowAttrs("src", "alt").OnElements("img")
p.AllowAttrs("width", "height", "colspan", "rowspan", "span", "start", "value").Matching(regexp.MustCompile(`^[0-9]{1,4}$`)).OnElements("img", "td", "th", "col", "colgroup", "ol", "li")
p.AllowAttrs("open").Matching(regexp.MustCompile(`^(open)?$`)).OnElements("details")
if generated {
// Only the server's macro renderer can introduce these attributes. The
// authored pass does not allow data-*, arbitrary classes, forms or IDs
// outside the document namespace after tree normalization.
p.AllowElements("nav")
p.AllowAttrs("class", "role", "aria-label").Globally()
p.AllowAttrs("data-children-path", "data-children-exclude", "data-children-src").OnElements("section")
p.AllowAttrs("loading", "referrerpolicy").OnElements("img")
}
return p
}
var authoredHTMLPolicy = projectionPolicy(false)
var generatedHTMLPolicy = projectionPolicy(true)
func projectionFallback(body, message string) string {
if len(body) > 64<<10 {
body = body[:64<<10]
}
return `<p class="projection-warning" role="status">` + stdhtml.EscapeString(message) + `</p><pre>` + stdhtml.EscapeString(body) + `</pre>`
}
func projectionInputOK(ctx context.Context, body string) bool {
if len(body) > maxProjectionInput {
return false
}
z := html.NewTokenizer(strings.NewReader(body))
z.SetMaxBuf(maxProjectionInput)
depth := 0
for count := 0; count < 16384; count++ {
if count%128 == 0 && ctx.Err() != nil {
return false
}
switch z.Next() {
case html.ErrorToken:
return z.Err() == io.EOF
case html.StartTagToken:
name, _ := z.TagName()
switch string(name) {
case "br", "hr", "img", "meta", "link", "input", "wbr", "source", "area", "base", "embed", "param", "track", "col":
default:
depth++
}
if depth > 128 {
return false
}
case html.EndTagToken:
if depth > 0 {
depth--
}
}
}
return false
}
func documentAnchor(id string) string { return fmt.Sprintf("pw-doc-%x", sha256.Sum256([]byte(id))) }
// extractApprovedExternalActiveResources runs before the authored allowlist,
// which intentionally knows nothing about script or link elements. An
// approved element becomes unpredictable text; its canonical server-authored
// tag is restored only after the generated-output allowlist has also run.
func extractApprovedExternalActiveResources(body string, policy externalResourcePolicy) (string, map[string]string) {
var seed [18]byte
if _, err := rand.Read(seed[:]); err != nil {
return body, nil
}
prefix := "PWEXT-" + base64.RawURLEncoding.EncodeToString(seed[:]) + "-"
fragments := map[string]string{}
var output strings.Builder
z := html.NewTokenizer(strings.NewReader(body))
z.SetMaxBuf(maxProjectionInput)
skippingApprovedScript := false
for {
tokenType := z.Next()
if tokenType == html.ErrorToken {
break
}
if skippingApprovedScript {
if tokenType == html.EndTagToken {
token := z.Token()
if strings.EqualFold(token.Data, "script") {
skippingApprovedScript = false
}
}
continue
}
if tokenType != html.StartTagToken && tokenType != html.SelfClosingTagToken {
output.Write(z.Raw())
continue
}
token := z.Token()
name := strings.ToLower(token.Data)
attrs := map[string]string{}
for _, attr := range token.Attr {
attrs[strings.ToLower(attr.Key)] = attr.Val
}
kind, resourceURL := "", ""
switch name {
case "script":
kind, resourceURL = "script", attrs["src"]
case "link":
if strings.EqualFold(strings.TrimSpace(attrs["rel"]), "stylesheet") {
kind, resourceURL = "stylesheet", attrs["href"]
}
}
approval, approved := policy.approved(kind, resourceURL)
if !approved {
output.Write(z.Raw())
continue
}
placeholder := prefix + fmt.Sprint(len(fragments))
var node *html.Node
if kind == "script" {
node = &html.Node{Type: html.ElementNode, Data: "script", DataAtom: atom.Script, Attr: []html.Attribute{
{Key: "src", Val: approval.URL}, {Key: "integrity", Val: approval.Integrity},
{Key: "crossorigin", Val: "anonymous"}, {Key: "defer", Val: "defer"},
}}
if tokenType == html.StartTagToken {
skippingApprovedScript = true
}
} else {
node = &html.Node{Type: html.ElementNode, Data: "link", DataAtom: atom.Link, Attr: []html.Attribute{
{Key: "rel", Val: "stylesheet"}, {Key: "href", Val: approval.URL},
{Key: "integrity", Val: approval.Integrity}, {Key: "crossorigin", Val: "anonymous"},
}}
}
var fragment strings.Builder
if html.Render(&fragment, node) != nil {
output.Write(z.Raw())
continue
}
fragments[placeholder] = fragment.String()
output.WriteString(placeholder)
}
return output.String(), fragments
}
// safeHTMLProjection is the only authored-HTML display boundary. It never
// writes Documents/Revisions and never fetches an external resource. Canonical
// Markdown conversion deliberately does not call it.
func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand bool) string {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
r = r.WithContext(ctx)
fallback := func() string {
return projectionFallback(body, translate(s.getLocale(s.domainUUIDForRequest(r)), "viewer.html_projection_unavailable"))
}
if !projectionInputOK(r.Context(), body) {
return fallback()
}
resourcePolicy := s.externalResourcePolicy(s.domainUUIDForRequest(r))
body, externalFragments := extractApprovedExternalActiveResources(body, resourcePolicy)
safe := authoredHTMLPolicy.Sanitize(body)
root := &html.Node{Type: html.ElementNode, Data: "div", DataAtom: atom.Div}
nodes, err := html.ParseFragment(strings.NewReader(safe), root)
if err != nil {
return fallback()
}
var output strings.Builder
macros, count := 0, 0
var emit func(*html.Node, int, bool) bool
emit = func(n *html.Node, depth int, literal bool) bool {
count++
if count > 16384 || depth > 128 || output.Len() > maxProjectionOutput || r.Context().Err() != nil {
return false
}
if n.Type == html.TextNode {
if !expand || literal {
output.WriteString(stdhtml.EscapeString(n.Data))
return true
}
at := 0
for _, loc := range projectionMacroRE.FindAllStringIndex(n.Data, 33) {
if ctx.Err() != nil {
return false
}
macros++
if macros > 32 {
return false
}
output.WriteString(stdhtml.EscapeString(n.Data[at:loc[0]]))
macro := n.Data[loc[0]:loc[1]]
expanded := macro
// Dispatch exactly one macro. A failed City macro must never
// be reparsed as a nested children request on its raw text.
if isAnonymousProjection(r) {
expanded = s.expandPublicMacro(r, macro, path)
} else if strings.HasPrefix(macro, "{{children") {
expanded = s.expandChildrenMacros(macro, path)
} else {
expanded = s.expandCityMacrosContext(ctx, macro, path, s.domainUUIDForRequest(r), s.pathReadFilter(r))
}
if expanded == macro {
// Failed/unresolved macros are still authored text. Do not
// promote decoded entities into trusted generated elements.
output.WriteString(stdhtml.EscapeString(macro))
} else {
output.WriteString(expanded)
}
at = loc[1]
}
output.WriteString(stdhtml.EscapeString(n.Data[at:]))
return true
}
if n.Type != html.ElementNode {
return true
}
attrs := n.Attr[:0]
for _, a := range n.Attr {
switch a.Key {
case "id":
a.Val = documentAnchor(a.Val)
case "href":
if strings.HasPrefix(a.Val, "#") {
a.Val = "#" + documentAnchor(strings.TrimPrefix(a.Val, "#"))
}
case "src":
u, e := url.Parse(a.Val)
if e != nil || strings.ContainsAny(a.Val, "\\\r\n\t") {
continue
}
if u.IsAbs() || u.Host != "" {
approval, ok := resourcePolicy.approved("image", a.Val)
if !ok || n.Data != "img" {
continue
}
a.Val = approval.URL
attrs = append(attrs, a)
continue
}
u.Path = pathpkg.Clean(u.Path)
// Images are passive read projections, not arbitrary authenticated
// GETs against editors, login flows or management endpoints.
if !strings.HasPrefix(u.Path, "/v/raw/") && !strings.HasPrefix(u.Path, "/_/obj/") && !strings.HasPrefix(u.Path, "/static/") && !strings.HasPrefix(u.Path, "/o/path:/system/static/") {
continue
}
u.RawPath = ""
a.Val = u.String()
}
attrs = append(attrs, a)
}
n.Attr = attrs
if n.Data == "img" {
n.Attr = append(n.Attr, html.Attribute{Key: "loading", Val: "lazy"}, html.Attribute{Key: "referrerpolicy", Val: "no-referrer"})
}
// Render a shallow element to let net/html escape its attributes; never
// interpolate authored attributes into the macro output context.
shallow := &html.Node{Type: html.ElementNode, Data: n.Data, DataAtom: n.DataAtom, Attr: n.Attr}
var tag strings.Builder
if html.Render(&tag, shallow) != nil {
return false
}
opening := tag.String()
closing := "</" + n.Data + ">"
void := !strings.HasSuffix(opening, closing)
if !void {
opening = strings.TrimSuffix(opening, closing)
}
output.WriteString(opening)
for child := n.FirstChild; child != nil; child = child.NextSibling {
if !emit(child, depth+1, literal || n.Data == "code" || n.Data == "pre" || n.Data == "a") {
return false
}
}
if !void {
output.WriteString(closing)
}
return true
}
for _, n := range nodes {
if !emit(n, 0, false) {
return fallback()
}
}
if output.Len() > maxProjectionOutput {
return fallback()
}
projected := output.String()
if expand && !isAnonymousProjection(r) {
projected = s.renderDiagramsInHTML(r, path, projected)
}
// A second allowlist pass guards all generated URL/HTML output too. Active
// external resources only exist as random text at this boundary, so macro
// output cannot manufacture an approved script or stylesheet tag.
projected = generatedHTMLPolicy.Sanitize(projected)
for token, fragment := range externalFragments {
projected = strings.ReplaceAll(projected, token, fragment)
}
return projected
}