151 lines
5.8 KiB
Go
151 lines
5.8 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"html"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const csvEditorFixtureBody = "\ufeff\"name\",description\r\n日本語,\"a,b\r\nnext\"\r\n"
|
|
|
|
func TestCSVEditorRevisionAndBounds(t *testing.T) {
|
|
s, token, domain, _ := editorFixture(t)
|
|
object, err := s.store.CreatePageAtPathInDomain(domain, "/table.csv")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
first, err := s.store.SaveRevisionContent(object, []byte(csvEditorFixtureBody), "", "und", "text/csv; charset=utf-8", "table.csv")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, query := range []string{"", "?edittype=markdown", "?lang=und"} {
|
|
w := configRequest(s, token, "GET", "/e/-/table.csv"+query, nil)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `data-csv="true"`) || !strings.Contains(w.Body.String(), `value="code"`) || strings.Contains(w.Body.String(), `data-editor-action="markdown"`) {
|
|
t.Fatalf("editor %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
head, _ := s.store.GetRevisionForExactLanguage(object, "und")
|
|
if head.UUID != first.UUID {
|
|
t.Fatal("GET changed revision")
|
|
}
|
|
save := func(auth, base, body string) int {
|
|
data, _ := json.Marshal(map[string]any{"object_uuid": object, "body": body, "lang": "und", "content_type": "text/csv; charset=utf-8", "filename": "table.csv", "base_revision_uuid": base})
|
|
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(data))
|
|
r.Header.Set("Authorization", "Bearer "+auth)
|
|
r.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w.Code
|
|
}
|
|
changed := strings.Replace(csvEditorFixtureBody, "日本語", "更新", 1)
|
|
if code := save(token, first.UUID, changed); code != 200 {
|
|
t.Fatal("save", code)
|
|
}
|
|
if code := save(token, first.UUID, "stale"); code != 409 {
|
|
t.Fatal("stale save", code)
|
|
}
|
|
head, _ = s.store.GetRevisionForExactLanguage(object, "und")
|
|
if head.Body != changed || head.ContentType != "text/csv; charset=utf-8" || head.Filename != "table.csv" {
|
|
t.Fatal("CSV altered")
|
|
}
|
|
state := configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+"&lang=und&revision="+first.UUID, nil)
|
|
var content struct {
|
|
Body string
|
|
Editable bool
|
|
}
|
|
if err = json.Unmarshal(state.Body.Bytes(), &content); err != nil || state.Code != 200 || !content.Editable || content.Body != csvEditorFixtureBody {
|
|
t.Fatal("restore", state.Code, err)
|
|
}
|
|
reader, err := s.store.CreatePasswordAccount(domain, "csvreader", "a long test password", "Reader", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.store.SetPrincipalState(domain, reader.UUID, "readonly"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
denied, _ := s.store.CreateAuthSession(domain, reader.UUID)
|
|
if code := save(denied, head.UUID, "forbidden"); code != 403 {
|
|
t.Fatal("readonly save", code)
|
|
}
|
|
// Initial CSV is embedded as an escaped JS string to preserve CRLF. A
|
|
// field must not terminate that script or become executable markup.
|
|
if _, err = s.store.SaveRevisionContent(object, []byte("\"</script><script>alert(1)</script>\",value\r\n"), "", "und", "text/csv", "table.csv"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
escaped := configRequest(s, token, "GET", "/e/-/table.csv", nil)
|
|
if escaped.Code != 200 || strings.Contains(escaped.Body.String(), "<script>alert(1)</script>") || !strings.Contains(escaped.Body.String(), `\u003c/script\u003e`) {
|
|
t.Fatal("CSV script string was not escaped")
|
|
}
|
|
for _, input := range []struct {
|
|
body []byte
|
|
code int
|
|
}{{[]byte(strings.Repeat("x", maxCSVMacroBytes+1)), 413}, {[]byte{0xff, 0xfe}, 415}, {[]byte("a,\x00"), 415}} {
|
|
rev, e := s.store.SaveRevisionContent(object, input.body, "", "und", "text/csv", "table.csv")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
w := configRequest(s, token, "GET", "/e/-/table.csv", nil)
|
|
if w.Code != input.code {
|
|
t.Fatalf("editor bounds %d wanted %d", w.Code, input.code)
|
|
}
|
|
w = configRequest(s, token, "GET", "/_/api/v1/editor/state?uuid="+object+"&lang=und&revision="+rev.UUID, nil)
|
|
if w.Code != input.code {
|
|
t.Fatalf("state bounds %d wanted %d", w.Code, input.code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCSVEditorChromium(t *testing.T) {
|
|
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
|
t.Skip("opt-in Chromium")
|
|
}
|
|
for _, width := range []string{"1280", "390"} {
|
|
t.Run(width, func(t *testing.T) {
|
|
s, token, domain, _ := editorFixture(t)
|
|
object, err := s.store.CreatePageAtPathInDomain(domain, "/table.csv")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.store.SaveRevisionContent(object, []byte(csvEditorFixtureBody), "", "und", "text/csv; charset=utf-8", "table.csv"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
script, err := os.ReadFile("internal/server/testdata/csv-editor.js")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Host = "localhost"
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
if r.URL.Path == "/e/-/table.csv" {
|
|
rec := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
|
for k, v := range rec.Header() {
|
|
w.Header()[k] = v
|
|
}
|
|
w.WriteHeader(rec.Code)
|
|
_, policy, _ := strings.Cut(rec.Header().Get("Content-Security-Policy"), "'nonce-")
|
|
nonce, _, _ := strings.Cut(policy, "'")
|
|
w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", `<script nonce="`+html.EscapeString(nonce)+`">`+string(script)+"</script></body>", 1)))
|
|
return
|
|
}
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
}))
|
|
defer server.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
|
defer cancel()
|
|
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/e/-/table.csv", t.TempDir(), "/tmp/pageswiki-csv-editor-"+width+".png", width, "850").CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("browser: %v %s", err, out)
|
|
}
|
|
})
|
|
}
|
|
}
|