Files
pages/internal/store/read_policy.go
T
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

102 lines
3.2 KiB
Go

package store
import (
"errors"
"strings"
)
// ReadPathPolicy is an immutable, request-scoped snapshot of positive Path
// read grants. Never share it across requests or use it to authorize writes.
type ReadPathPolicy struct {
access *localityAccessSnapshot
all bool
exact map[string]bool
subtrees map[string]bool
}
func (p *ReadPathPolicy) Allows(path string) bool {
if p == nil {
return false
}
return p.access.decide(path, "read", p.allowsBase(path))
}
func (p *ReadPathPolicy) allowsBase(path string) bool {
if p == nil || !strings.HasPrefix(path, "/") {
return false
}
if p.all || p.exact[path] {
return true
}
for at := strings.LastIndex(path, "/"); at > 0; at = strings.LastIndex(path[:at], "/") {
if p.subtrees[path[:at]] {
return true
}
}
return false
}
// LoadReadPathPolicy uses one bounded query, independent of the number of
// directory entries. It deliberately excludes Object grants: an Object grant
// does not reveal all of that Object's semantic aliases.
func (s *Store) LoadReadPathPolicy(domain, principal string) (*ReadPathPolicy, error) {
return loadReadPathPolicy(s.DB, domain, principal)
}
func loadReadPathPolicy(q pathQueryer, domain, principal string) (*ReadPathPolicy, error) {
p := &ReadPathPolicy{exact: map[string]bool{}, subtrees: map[string]bool{}}
if domain == "" || principal == "" {
return p, ErrAccessDenied
}
rows, err := q.Query(`
SELECT 'role',g.target_type,g.target_id FROM principals p
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active'
JOIN auth_role_bindings b ON b.membership_uuid=m.uuid
JOIN auth_roles r ON r.uuid=b.role_uuid AND r.domain_uuid=m.domain_uuid
JOIN auth_capability_grants g ON g.role_uuid=r.uuid AND g.capability='read'
WHERE p.uuid=? AND p.state IN ('active','readonly') AND g.target_type IN ('domain','path')
UNION ALL
SELECT 'group','path',gp.scope_path FROM principals p
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active'
JOIN group_members gm ON gm.membership_uuid=m.uuid
JOIN groups g ON g.uuid=gm.group_uuid AND g.domain_uuid=m.domain_uuid AND g.state='active'
JOIN group_permissions gp ON gp.group_uuid=g.uuid AND gp.permission IN ('read','read_write','read_write_once')
WHERE p.uuid=? AND p.state IN ('active','readonly')
LIMIT 4097`, domain, principal, domain, principal)
if err != nil {
return nil, err
}
defer rows.Close()
count := 0
for rows.Next() {
count++
if count > 4096 {
return nil, errors.New("too many read grants for bounded projection")
}
var source, kind, target string
if err := rows.Scan(&source, &kind, &target); err != nil {
return nil, err
}
if source == "role" {
if kind == "domain" && target == domain || kind == "path" && target == "*" {
p.all = true
} else if kind == "path" {
p.exact[target] = true
}
} else if target == "/" {
p.all = true
} else if strings.HasPrefix(target, "/") {
p.exact[target] = true
p.subtrees[strings.TrimRight(target, "/")] = true
}
}
if err = rows.Err(); err != nil {
return nil, err
}
if err = rows.Close(); err != nil {
return nil, err
}
p.access, err = loadLocalityAccess(q, domain, principal)
return p, err
}