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.
200 lines
8.2 KiB
Go
200 lines
8.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestAnonymousApprovedPublication(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
|
s.mux.HandleFunc("/", s.handleHome)
|
|
ctx := context.Background()
|
|
admin, err := s.store.PrincipalForSession(domain, token)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "public", "Public", "", "/public")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st, err := s.store.CreateZoneAtPath(domain, admin.UUID, "street", q, "private", "Private", "", "/public/private")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
create := func(path, body, ct string) (string, *store.Revision) {
|
|
t.Helper()
|
|
o, e := s.store.CreatePageAtPathInDomain(domain, path)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
v, e := s.store.SaveRevisionContent(o, []byte(body), "", "und", ct, "")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return o, v
|
|
}
|
|
object, rev := create("/public/page", "<h1>Approved page</h1><script>window.stolen=true</script><p>{{children:/private}}</p>", "text/html")
|
|
create("/public/movie", "FAKE-MEDIA-CONTENT", "video/mp4")
|
|
create("/public/private/secret", "<h1>PRIVATE-SENTINEL</h1>", "text/html")
|
|
create("/outside", "<h1>OUTSIDE-SENTINEL</h1>", "text/html")
|
|
setPolicy := func(kind, id, value string) {
|
|
t.Helper()
|
|
g, e := s.store.CityGovernance(ctx, domain, admin.UUID, kind, id)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_, e = s.store.ChangeCityGovernance(ctx, domain, admin.UUID, kind, id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &value, ConfirmRelaxation: true}, false)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
setPolicy("domain", domain, "public")
|
|
setPolicy("quartier", q, "public")
|
|
setPolicy("street", st, "authenticated_acl")
|
|
url := governanceEndpoint("quartier", q) + "/publication"
|
|
call := func(method, path, bearer string, body any, headers map[string]string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
b, _ := json.Marshal(body)
|
|
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(string(b)))
|
|
if bearer != "" {
|
|
r.Header.Set("Authorization", bearer)
|
|
}
|
|
r.Header.Set("Content-Type", "application/json")
|
|
for k, v := range headers {
|
|
r.Header.Set(k, v)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
preview := func() store.LocalityPublication {
|
|
t.Helper()
|
|
w := call("GET", url, "Bearer "+token, nil, nil)
|
|
if w.Code != 200 {
|
|
t.Fatalf("preview %d %s", w.Code, w.Body.String())
|
|
}
|
|
var p store.LocalityPublication
|
|
if err = json.Unmarshal(w.Body.Bytes(), &p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
approve := func(p store.LocalityPublication) {
|
|
t.Helper()
|
|
w := call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil)
|
|
if w.Code != 200 {
|
|
t.Fatalf("approve %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
denied := func(path string) {
|
|
t.Helper()
|
|
w := call("GET", path, "", nil, nil)
|
|
if w.Code == 200 || strings.Contains(w.Body.String(), "PRIVATE-SENTINEL") {
|
|
t.Fatalf("leaked %s: %d", path, w.Code)
|
|
}
|
|
}
|
|
denied("/public/page")
|
|
p := preview()
|
|
if !p.Eligible || len(p.Entries) != 2 || len(p.Excluded) != 1 {
|
|
t.Fatalf("impact=%+v", p)
|
|
}
|
|
if w := call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Enabled: true, Confirm: true}, nil); w.Code != 400 {
|
|
t.Fatalf("unapproved=%d", w.Code)
|
|
}
|
|
approve(p)
|
|
w := call("GET", "/public/page", "", nil, nil)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), "Approved page") || !strings.Contains(w.Body.String(), `property="og:title"`) || strings.Contains(w.Body.String(), "window.stolen") || !strings.Contains(w.Body.String(), "data-children-src") || strings.Contains(w.Body.String(), "auth-menu") || strings.Contains(w.Body.String(), "PRIVATE-SENTINEL") || strings.Contains(w.Body.String(), " download") {
|
|
t.Fatalf("public viewer %d %s", w.Code, w.Body.String())
|
|
}
|
|
if w.Header().Get("Content-Security-Policy") == "" || !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
|
t.Fatal("public CSP/cache missing")
|
|
}
|
|
for _, path := range []string{"/public/private/secret", "/outside", "/o/uuid:" + object, "/v/raw/uuid:" + object, "/v/raw/rev:" + rev.UUID, "/_/obj/sha256:" + rev.DocumentHash, "/e/public/page", "/v/search?q=Approved"} {
|
|
denied(path)
|
|
}
|
|
for _, bearer := range []string{"Bearer invalid", "Basic invalid"} {
|
|
if w = call("GET", "/public/page", bearer, nil, nil); w.Code == 200 {
|
|
t.Fatal("invalid credentials downgraded")
|
|
}
|
|
}
|
|
if w = call("GET", "/public/page", "", nil, map[string]string{"Cookie": authCookieName + "=expired"}); w.Code == 200 {
|
|
t.Fatal("expired cookie downgraded")
|
|
}
|
|
if w = call("GET", "/v/raw/path:/public/movie", "", nil, map[string]string{"Range": "bytes=0-3"}); w.Code != 206 || w.Body.String() != "FAKE" {
|
|
t.Fatalf("range=%d %s", w.Code, w.Body.String())
|
|
}
|
|
if w = call("GET", "/v/raw/path:/public/page", "", nil, nil); w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Disposition"), "attachment") {
|
|
t.Fatalf("raw HTML unsafe %d", w.Code)
|
|
}
|
|
if w = call("HEAD", "/v/raw/path:/public/movie", "", nil, nil); w.Code != 200 || w.Body.Len() != 0 {
|
|
t.Fatal("raw HEAD")
|
|
}
|
|
if err = s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err == nil {
|
|
t.Fatal("publication downgraded to legacy")
|
|
}
|
|
// A new revision invalidates publication before any conditional 304 check.
|
|
if _, err = s.store.SaveRevisionContent(object, []byte("<h1>NOT-APPROVED</h1>"), "", "und", "text/html", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
denied("/public/page")
|
|
if w = call("GET", "/v/raw/path:/public/page", "", nil, map[string]string{"If-None-Match": `"` + rev.DocumentHash + `"`}); w.Code == 304 || w.Code == 200 {
|
|
t.Fatal("revoked conditional access")
|
|
}
|
|
approve(preview())
|
|
create("/public/new", "UNAPPROVED-NEW", "text/plain")
|
|
denied("/public/new")
|
|
// Stale impact token cannot approve intervening edits.
|
|
p = preview()
|
|
if _, err = s.store.SaveRevisionContent(object, []byte("changed again"), "", "und", "text/html", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil); w.Code != 400 {
|
|
t.Fatalf("stale impact %d", w.Code)
|
|
}
|
|
approve(preview())
|
|
if _, err = s.store.DB.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,?,?,?,0)`, domain, "/alias", object, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
denied("/public/page")
|
|
denied("/alias")
|
|
if _, err = s.store.DB.Exec(`DELETE FROM paths WHERE domain_uuid=? AND path='/alias'`, domain); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
denied("/public/page")
|
|
approve(preview())
|
|
setPolicy("domain", domain, "authenticated_acl")
|
|
denied("/public/page")
|
|
setPolicy("domain", domain, "public")
|
|
denied("/public/page")
|
|
approve(preview())
|
|
// Revocation is explicit and does not change the ceiling/draft.
|
|
p = preview()
|
|
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Enabled: false}, nil); w.Code != 200 {
|
|
t.Fatalf("revoke=%d", w.Code)
|
|
}
|
|
denied("/public/page")
|
|
if w = call("PATCH", url, "Bearer "+token, store.PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}, nil); w.Code != 409 {
|
|
t.Fatal("pre-revocation consent replayed")
|
|
}
|
|
approve(preview())
|
|
a, e := s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, nil)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
a.Mode = "restricted"
|
|
a.Entries = []store.LocalityAccessEntry{{Kind: "user", Name: "admin", Permission: "read_write"}}
|
|
if _, e = s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, &a); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
denied("/public/page")
|
|
if p = preview(); p.Eligible || len(p.Entries) != 0 {
|
|
t.Fatalf("ACL publicly approved %+v", p)
|
|
}
|
|
}
|