// Package schutzaria evaluates trusted, request-scoped authorization facts. // It does not authenticate callers, load grants, resolve aliases, or commit // publication. Callers must supply the correct Domain/principal snapshot. package schutzaria import "strings" const Component = "schutzaria" // AccessLayer is the union of matching subjects at one locality. Layers at // the same Path and at ancestors intersect; Grant cannot override a denial. type AccessLayer struct { Path string Read, Write, Grant bool } // LocalityAccess is immutable after construction and must not be cached // across requests. Admin bypasses caps only; it does not create base grants. type LocalityAccess struct { admin bool paths map[string][]AccessLayer } func NewLocalityAccess(admin bool, layers []AccessLayer) *LocalityAccess { p := &LocalityAccess{admin: admin, paths: make(map[string][]AccessLayer)} for _, layer := range layers { p.paths[layer.Path] = append(p.paths[layer.Path], layer) } return p } // RequiresPathChecks permits the existing Object-alias fast path only when // no local cap applies or the authenticated actor has recovery authority. // Foreign-Domain restrictions must be checked separately before this shortcut. func (p *LocalityAccess) RequiresPathChecks() bool { return p != nil && !p.admin && len(p.paths) != 0 } func (p *LocalityAccess) Decide(path, capability string, basis bool) bool { if !strings.HasPrefix(path, "/") { return false } if p == nil || p.admin { return basis } grant := false for at := path; ; { for _, layer := range p.paths[at] { allowed := layer.Write if capability == "read" { allowed = layer.Read } if !allowed { return false } if layer.Grant && (capability == "read" || capability == "write" || capability == "create" || capability == "upload") { grant = true } } if at == "/" { break } i := strings.LastIndex(at, "/") if i <= 0 { at = "/" } else { at = at[:i] } } return basis || grant } // ReadPathPolicy contains only positive Path grants plus locality caps. // Object grants must never expose all semantic aliases. A nil/zero policy denies. type ReadPathPolicy struct { access *LocalityAccess all bool exact, subtrees map[string]bool } // NewReadPathPolicy copies input slices; subsequent mutations cannot broaden // an existing snapshot. Paths are already normalized by the application. func NewReadPathPolicy(all bool, exact, subtrees []string, access *LocalityAccess) *ReadPathPolicy { p := &ReadPathPolicy{all: all, access: access, exact: make(map[string]bool), subtrees: make(map[string]bool)} for _, path := range exact { p.exact[path] = true } for _, path := range subtrees { p.subtrees[path] = true } return p } 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 }