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.
This commit is contained in:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,200 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestLocalityUserGroupAccess(t *testing.T) {
|
||||
s, adminToken, domain := configFixture(t)
|
||||
ctx := context.Background()
|
||||
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/revisions/save", s.handleSaveRevisionAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/render/markdown", s.handleMarkdownRenderAPI)
|
||||
s.mux.HandleFunc("/o/", s.handleObjectView)
|
||||
s.mux.HandleFunc("/v/raw/", s.handleRawView)
|
||||
s.mux.HandleFunc("/", s.handleHome)
|
||||
admin, err := s.store.PrincipalForSession(domain, adminToken)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q, err := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "life", "Life", "", "/life")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st, err := s.store.CreateZoneAtPath(domain, admin.UUID, "street", q, "work", "Work", "", "/life/work")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spot, err := s.store.CreateSpotAtPath(domain, admin.UUID, st, "desk", "Desk", "", "/life/work/desk")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/life/work/desk/page")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rev, err := s.store.SaveRevisionContent(object, []byte("<h1>Protected content</h1>"), "", "und", "text/html", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := map[string]*store.Principal{}
|
||||
tokens := map[string]string{}
|
||||
for _, name := range []string{"alice", "bob", "charlie", "delegate"} {
|
||||
users[name], err = s.store.CreatePasswordAccount(domain, name, "a long test password", name, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tokens[name], err = s.store.CreateAuthSession(domain, users[name].UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
group, err := s.store.CreateGroup(domain, admin.UUID, "editors", "Editors", "", "members")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.AddGroupMemberByUsername(domain, group.UUID, "bob", "member"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
set := func(actor, kind, id, mode string, entries []store.LocalityAccessEntry) store.LocalityAccess {
|
||||
t.Helper()
|
||||
a, e := s.store.LocalityAccess(ctx, domain, actor, kind, id, nil)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
a.Mode = mode
|
||||
a.Entries = entries
|
||||
a, e = s.store.LocalityAccess(ctx, domain, actor, kind, id, &a)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return a
|
||||
}
|
||||
entry := func(kind, name, perm string) store.LocalityAccessEntry {
|
||||
return store.LocalityAccessEntry{Kind: kind, Name: name, Permission: perm}
|
||||
}
|
||||
set(admin.UUID, "quartier", q, "restricted", []store.LocalityAccessEntry{entry("user", "alice", "read"), entry("group", "editors", "read_write")})
|
||||
check := func(name, cap string, want bool) {
|
||||
t.Helper()
|
||||
err := s.store.Authorize(store.AuthorizationRequest{DomainUUID: domain, PrincipalUUID: users[name].UUID, Capability: cap, TargetType: "path", TargetID: "/life/work/desk/page"})
|
||||
if (err == nil) != want {
|
||||
t.Fatalf("%s %s=%v want %v", name, cap, err, want)
|
||||
}
|
||||
if cap == "read" {
|
||||
p, e := s.store.LoadReadPathPolicy(domain, users[name].UUID)
|
||||
if e != nil || p.Allows("/life/work/desk/page") != want {
|
||||
t.Fatalf("list policy %s=%v", name, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
check("alice", "read", true)
|
||||
check("alice", "write", false)
|
||||
check("bob", "read", true)
|
||||
check("bob", "write", true)
|
||||
check("bob", "delete", false)
|
||||
check("charlie", "read", false)
|
||||
if err = s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err == nil {
|
||||
t.Fatal("ACL could be bypassed by legacy mode")
|
||||
}
|
||||
call := func(token, method, path string, body any) *httptest.ResponseRecorder {
|
||||
b, _ := json.Marshal(body)
|
||||
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(string(b)))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
for _, path := range []string{"/life/work/desk/page", "/o/uuid:" + object, "/v/raw/uuid:" + object, "/v/raw/rev:" + rev.UUID} {
|
||||
w := call(tokens["charlie"], "GET", path, nil)
|
||||
if w.Code == 200 {
|
||||
t.Fatalf("denied alias leaked: %s", path)
|
||||
}
|
||||
}
|
||||
if w := call(tokens["alice"], "POST", "/_/api/v1/revisions/save", map[string]string{"object_uuid": object, "body": "bad", "lang": "und"}); w.Code != 403 {
|
||||
t.Fatalf("read-only save=%d", w.Code)
|
||||
}
|
||||
if w := call(tokens["bob"], "POST", "/_/api/v1/revisions/save", map[string]string{"object_uuid": object, "body": "<p>edited</p>", "lang": "und"}); w.Code != 200 {
|
||||
t.Fatalf("write save=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w := call(tokens["bob"], "POST", "/_/api/v1/render/markdown", map[string]any{"html": "<h1>Preview</h1>", "path": "/life/work/desk/page", "projection": true}); w.Code != 200 {
|
||||
t.Fatalf("scoped editor preview=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`UPDATE principals SET state='readonly' WHERE uuid=?`, users["bob"].UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check("bob", "read", true)
|
||||
check("bob", "write", false)
|
||||
if _, err = s.store.DB.Exec(`UPDATE principals SET state='active' WHERE uuid=?`, users["bob"].UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
set(admin.UUID, "spot", spot, "restricted", []store.LocalityAccessEntry{entry("user", "alice", "read_write"), entry("group", "editors", "read")})
|
||||
check("alice", "write", false)
|
||||
check("bob", "write", false)
|
||||
check("bob", "read", true)
|
||||
// Namespace guards apply even to direct SQL/CLI callers and prevent a private
|
||||
// binding from being moved or aliased out of its restrictive scope.
|
||||
for _, query := range []string{`UPDATE paths SET path='/escaped' WHERE object_uuid=?`, `DELETE FROM paths WHERE object_uuid=?`, `INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) SELECT domain_uuid,'/escape',object_uuid,0,0 FROM paths WHERE object_uuid=?`} {
|
||||
if _, err = s.store.DB.Exec(query, object); err == nil {
|
||||
t.Fatalf("namespace escaped: %s", query)
|
||||
}
|
||||
}
|
||||
if _, err = s.store.CreatePageAtPathInDomain(domain, "/life/work/desk/new"); err != nil {
|
||||
t.Fatalf("new documents blocked: %v", err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`UPDATE spots SET path_prefix='/escape' WHERE uuid=?`, spot); err == nil {
|
||||
t.Fatal("City boundary escaped")
|
||||
}
|
||||
// Membership removal is observed on the very next request.
|
||||
if _, err = s.store.DB.Exec(`DELETE FROM group_members WHERE group_uuid=? AND membership_uuid IN (SELECT uuid FROM domain_memberships WHERE principal_uuid=?)`, group.UUID, users["bob"].UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check("bob", "read", false)
|
||||
// Delegates may narrow but cannot invent a new positive grant.
|
||||
var member string
|
||||
if err = s.store.DB.QueryRow(`SELECT uuid FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=?`, domain, users["delegate"].UUID).Scan(&member); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g, err := s.store.CityGovernance(ctx, domain, admin.UUID, "quartier", q)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owners := []string{member}
|
||||
if _, err = s.store.ChangeCityGovernance(ctx, domain, admin.UUID, "quartier", q, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Owners: &owners}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delegated, err := s.store.LocalityAccess(ctx, domain, users["delegate"].UUID, "spot", spot, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delegated.Entries = []store.LocalityAccessEntry{entry("user", "charlie", "read_write")}
|
||||
if _, err = s.store.LocalityAccess(ctx, domain, users["delegate"].UUID, "spot", spot, &delegated); err != store.ErrGovernanceCeiling {
|
||||
t.Fatalf("delegate widened superior ACL: %v", err)
|
||||
}
|
||||
set(admin.UUID, "spot", spot, "inherit", nil)
|
||||
set(users["delegate"].UUID, "spot", spot, "restricted", []store.LocalityAccessEntry{entry("user", "charlie", "read_write")})
|
||||
check("charlie", "read", false)
|
||||
if _, err = s.store.LocalityAccess(ctx, domain, users["charlie"].UUID, "spot", spot, nil); err != store.ErrAccessDenied {
|
||||
t.Fatal("ordinary user altered ACL")
|
||||
}
|
||||
set(admin.UUID, "spot", spot, "inherit", nil)
|
||||
set(admin.UUID, "quartier", q, "inherit", nil)
|
||||
if _, err = s.store.DB.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,'/other-alias',?,0,0)`, domain, object); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a, err := s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.Mode = "restricted"
|
||||
a.Entries = []store.LocalityAccessEntry{entry("user", "alice", "read")}
|
||||
if _, err = s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, &a); err != store.ErrLocalityAccessAlias {
|
||||
t.Fatalf("cross-scope alias accepted: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user