feat: add browser upload queue and trusted sessions with bounded projections
This commit is contained in:
1 parent
84d52390f9
commit
b90aef79d0
79 files changed
+1962
-196
No files matched your search
@@ -0,0 +1,111 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/grammaratur"
|
||||
)
|
||||
|
||||
func TestCSVMacroParsingAndBounds(t *testing.T) {
|
||||
for _, raw := range []string{`{{csv:/data/a.csv}}`, `{{csv:"/data/日本語 table.csv" range=B2:D4 header=false}}`} {
|
||||
if _, err := parseCSVMacro(raw); err != nil {
|
||||
t.Fatal(raw, err)
|
||||
}
|
||||
}
|
||||
for _, raw := range []string{`{{csv:https://example.org/a.csv}}`, `{{csv://evil/a.csv}}`, `{{csv:/a/../secret}}`, `{{csv:/a range=A1:C201}}`, `{{csv:/a range=A1:AG2}}`, `{{csv:/a range=A2001:B2002}}`, `{{csv:/a range=D2:A1}}`, `{{csv:/a header=yes}}`, `{{csv:/a header=true header=false}}`, `{{csv:/a unsupported=1}}`, `{{csv:/a rev=bad}}`} {
|
||||
if _, err := parseCSVMacro(raw); err == nil {
|
||||
t.Fatal("accepted", raw)
|
||||
}
|
||||
}
|
||||
o, _ := parseCSVMacro(`{{csv:/data/a.csv range=A1:C3}}`)
|
||||
body := "\ufeffname,description,formula\r\n日本語,\"a,b\r\nnext\",=1+1\r\n<script>,{{children:/private}},end\r\n"
|
||||
got, err := csvMacroTable([]byte(body), o)
|
||||
if err != nil || !strings.Contains(got, "<th>name</th>") || !strings.Contains(got, "a,b\nnext") || !strings.Contains(got, "<script>") || !strings.Contains(got, "{{children:/private}}") {
|
||||
t.Fatalf("CSV: %s %v", got, err)
|
||||
}
|
||||
checkInertProjection(t, got)
|
||||
for _, bad := range [][]byte{[]byte("a,\"unterminated"), {0xff}, []byte("a,\x00"), []byte(strings.Repeat("x", maxCSVMacroBytes+1)), []byte(strings.Repeat("x", (16<<10)+1)), []byte(strings.Repeat("a,", 101))} {
|
||||
if _, err := csvMacroTable(bad, o); err == nil {
|
||||
t.Fatal("invalid/large CSV accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVMacroProjectionAuthorizationAndRevision(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/data/report.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := s.store.SaveRevisionContent(object, []byte("name,value\r\noriginal,1\r\n"), "", "und", "text/csv", "report.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := httptest.NewRequest("GET", "http://localhost/guide", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
macro := `{{csv:/data/report.csv range=A1:B2}}`
|
||||
got := s.safeHTMLProjection(r, "<p>"+macro+"</p><code>"+macro+"</code>", "/guide", true)
|
||||
if strings.Count(got, `class="csv-projection table-scroll"`) != 1 || !strings.Contains(got, "original") || !strings.Contains(got, "<code>"+macro+"</code>") || !strings.Contains(got, first.UUID) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
second, err := s.store.SaveRevisionContent(object, []byte("name,value\nupdated,2\n"), "", "und", "text/csv", "report.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = s.safeHTMLProjection(r, macro, "/guide", true)
|
||||
if !strings.Contains(got, "updated") || !strings.Contains(got, second.UUID) || strings.Contains(got, "original") {
|
||||
t.Fatal("stale projection", got)
|
||||
}
|
||||
got = s.safeHTMLProjection(r, `{{csv:/data/report.csv rev=`+first.UUID+`}}`, "/guide", true)
|
||||
if !strings.Contains(got, "original") || strings.Contains(got, "updated") {
|
||||
t.Fatal("pinned revision", got)
|
||||
}
|
||||
other, err := s.store.CreatePageAtPathInDomain(domain, "/data/other.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreign, err := s.store.SaveRevisionContent(other, []byte("SECRET_DIFFERENT_OBJECT"), "", "und", "text/csv", "other.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = s.safeHTMLProjection(r, `{{csv:/data/report.csv rev=`+foreign.UUID+`}}`, "/guide", true)
|
||||
if strings.Contains(got, "SECRET") || strings.Contains(got, "<table") {
|
||||
t.Fatal("foreign revision leaked", got)
|
||||
}
|
||||
// A fresh request with no credentials must not reuse another reader's table.
|
||||
denied := httptest.NewRequest("GET", "http://localhost/guide", nil)
|
||||
got = s.safeHTMLProjection(denied, macro, "/guide", true)
|
||||
if strings.Contains(got, "updated") || strings.Contains(got, "<table") {
|
||||
t.Fatal("unauthorized table", got)
|
||||
}
|
||||
public := r.WithContext(context.WithValue(r.Context(), anonymousProjectionKey{}, true))
|
||||
if got = s.safeHTMLProjection(public, macro, "/guide", true); strings.Contains(got, "<table") {
|
||||
t.Fatal("anonymous CSV expanded", got)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(r.Context())
|
||||
cancel()
|
||||
if got = s.expandCSVMacro(r.WithContext(ctx), macro); strings.Contains(got, "<table") {
|
||||
t.Fatal("cancelled projection ran")
|
||||
}
|
||||
// Authoring must preserve the macro, never bake projected rows into HTML.
|
||||
source := "<p>" + macro + "</p>"
|
||||
md, err := grammaratur.HTMLToMarkdownAuthoring(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(grammaratur.MarkdownAuthoringHTML(md), macro) || strings.Contains(md, "updated") {
|
||||
t.Fatal("authoring expanded CSV", md)
|
||||
}
|
||||
// Four distinct CSV reads per projection, then leave authored syntax literal.
|
||||
input := strings.Repeat(macro, 2)
|
||||
for _, col := range []string{"A1:A2", "B1:B2", "A1:B1", "A2:B2"} {
|
||||
input += `{{csv:/data/report.csv range=` + col + `}}`
|
||||
}
|
||||
got = s.safeHTMLProjection(r, input, "/guide", true)
|
||||
if strings.Count(got, "<table>") != 5 || !strings.Contains(got, "{{csv:") {
|
||||
t.Fatal("CSV budget not enforced", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user