232 lines
8.3 KiB
Go
232 lines
8.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
const ObjectListCandidateLimit = 400
|
|
const objectListBindingLimit = 8000
|
|
|
|
var ErrObjectListBudget = errors.New("object listing authorization exceeds the bounded request budget")
|
|
|
|
type ObjectListPosition struct {
|
|
CreatedAt int64 `json:"created_at"`
|
|
UUID string `json:"uuid"`
|
|
}
|
|
|
|
type objectListCursor struct {
|
|
Purpose string `json:"purpose"`
|
|
Domain string `json:"domain"`
|
|
Principal string `json:"principal"`
|
|
Expires int64 `json:"expires"`
|
|
Position ObjectListPosition `json:"position"`
|
|
}
|
|
|
|
func (s *Store) EnsureObjectListingSchema() error {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
return db.ApplyPostgreSQLMigrations(context.Background(), s.DB, "store-object-listing", []db.PostgreSQLMigration{{Version: 1, Name: "metadata listing indexes", Statements: []string{
|
|
`CREATE INDEX idx_objects_metadata_page ON objects(COALESCE(created_at,0) DESC,uuid ASC)`,
|
|
`CREATE INDEX idx_paths_object_domain_page ON paths(object_uuid,domain_uuid,path)`,
|
|
}, Assertions: []string{`SELECT uuid,kind,created_at FROM objects WHERE 1=0`, `SELECT domain_uuid,path,object_uuid FROM paths WHERE 1=0`}}})
|
|
}
|
|
var available bool
|
|
if err := s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='objects') AND EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='paths')`).Scan(&available); err != nil {
|
|
return err
|
|
}
|
|
// Domain-only migration fixtures/installations may not have installed the
|
|
// Object schema yet. Their migration must not depend on the listing index.
|
|
if !available {
|
|
return nil
|
|
}
|
|
_, err := s.DB.Exec(`CREATE INDEX IF NOT EXISTS idx_objects_metadata_page ON objects(COALESCE(created_at,0) DESC,uuid ASC);
|
|
CREATE INDEX IF NOT EXISTS idx_paths_object_domain_page ON paths(object_uuid,domain_uuid,path)`)
|
|
return err
|
|
}
|
|
|
|
// Cursors are encrypted: a denied-only page must not expose a hidden Object's
|
|
// UUID or timestamp through a base64-encoded pagination bookmark.
|
|
func (s *Store) EncodeObjectListCursor(domain, principal string, position ObjectListPosition) (string, error) {
|
|
raw, err := json.Marshal(objectListCursor{Purpose: "object-metadata-v1", Domain: domain, Principal: principal, Expires: time.Now().Add(15 * time.Minute).Unix(), Position: position})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
encoded, err := s.encryptAuthSecret(raw)
|
|
return strings.NewReplacer("+", "-", "/", "_").Replace(encoded), err
|
|
}
|
|
|
|
func (s *Store) DecodeObjectListCursor(encoded, domain, principal string) (*ObjectListPosition, error) {
|
|
if encoded == "" {
|
|
return nil, nil
|
|
}
|
|
if len(encoded) > 2048 {
|
|
return nil, errors.New("invalid object-list cursor")
|
|
}
|
|
raw, err := s.decryptAuthSecret(strings.NewReplacer("-", "+", "_", "/").Replace(encoded))
|
|
var cursor objectListCursor
|
|
if err != nil || json.Unmarshal(raw, &cursor) != nil || cursor.Purpose != "object-metadata-v1" || cursor.Domain != domain || cursor.Principal != principal || cursor.Expires <= time.Now().Unix() || cursor.Position.UUID == "" || len(cursor.Position.UUID) > 128 || cursor.Position.CreatedAt < 0 {
|
|
return nil, errors.New("invalid object-list cursor")
|
|
}
|
|
return &cursor.Position, nil
|
|
}
|
|
|
|
// LoadObjectListReadGrants adds direct Object authority to the separate Path
|
|
// filter. Broad grants apply only to candidates already scoped by live Domain
|
|
// bindings, never to globally identified Objects in another Domain.
|
|
func (s *Store) LoadObjectListReadGrants(ctx context.Context, domain, principal string) (map[string]bool, bool, error) {
|
|
exact := make(map[string]bool)
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT g.target_type,g.target_id FROM principals p
|
|
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` 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=`+s.sqlParam(2)+` AND p.state IN ('active','readonly') AND g.target_type IN ('domain','object') LIMIT 4097`, domain, principal)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
defer rows.Close()
|
|
broad, count := false, 0
|
|
for rows.Next() {
|
|
count++
|
|
if count > 4096 {
|
|
return nil, false, ErrObjectListBudget
|
|
}
|
|
var kind, target string
|
|
if err := rows.Scan(&kind, &target); err != nil {
|
|
return nil, false, err
|
|
}
|
|
if kind == "domain" && target == domain || kind == "object" && target == "*" {
|
|
broad = true
|
|
} else if kind == "object" {
|
|
exact[target] = true
|
|
}
|
|
}
|
|
return exact, broad, rows.Err()
|
|
}
|
|
|
|
// ListObjectMetadataPage consumes at most 400 Object candidates and 8,000
|
|
// binding rows. The next position advances even when no candidate is readable.
|
|
// Paths stay internal and are never included in the returned metadata.
|
|
func (s *Store) ListObjectMetadataPage(ctx context.Context, domain string, after *ObjectListPosition, limit int, allowedPath func(string) bool, exact map[string]bool, broad bool) ([]Object, *ObjectListPosition, error) {
|
|
if limit <= 0 || limit > 200 {
|
|
limit = 100
|
|
}
|
|
if !broad && allowedPath == nil {
|
|
return nil, nil, ErrAccessDenied
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
type branch struct {
|
|
predicate string
|
|
args []any
|
|
}
|
|
branches := []branch{{args: []any{domain}}}
|
|
if after != nil {
|
|
// Separate the equality and older-time ranges so SQLite can seek by
|
|
// UUID within large same-second import batches instead of re-scanning
|
|
// every earlier UUID in that timestamp on each page.
|
|
branches = []branch{
|
|
{` AND COALESCE(o.created_at,0)=` + s.sqlParam(2) + ` AND o.uuid>` + s.sqlParam(3), []any{domain, after.CreatedAt, after.UUID}},
|
|
{` AND COALESCE(o.created_at,0)<` + s.sqlParam(2), []any{domain, after.CreatedAt}},
|
|
}
|
|
}
|
|
candidates := make([]Object, 0, ObjectListCandidateLimit+1)
|
|
for _, part := range branches {
|
|
remaining := ObjectListCandidateLimit + 1 - len(candidates)
|
|
if remaining <= 0 {
|
|
break
|
|
}
|
|
rows, err := tx.QueryContext(ctx, `SELECT o.uuid,COALESCE(o.kind,''),COALESCE(o.created_at,0) FROM objects o
|
|
WHERE EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=`+s.sqlParam(1)+` AND p.object_uuid=o.uuid)`+part.predicate+` ORDER BY COALESCE(o.created_at,0) DESC,o.uuid ASC LIMIT `+s.sqlParam(len(part.args)+1), append(part.args, remaining)...)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
for rows.Next() {
|
|
var object Object
|
|
if err := rows.Scan(&object.UUID, &object.Kind, &object.CreatedAt); err != nil {
|
|
rows.Close()
|
|
return nil, nil, err
|
|
}
|
|
candidates = append(candidates, object)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
more := len(candidates) > ObjectListCandidateLimit
|
|
if more {
|
|
candidates = candidates[:ObjectListCandidateLimit]
|
|
}
|
|
visible := make(map[string]bool)
|
|
args := []any{domain}
|
|
for _, object := range candidates {
|
|
if broad || exact[object.UUID] {
|
|
visible[object.UUID] = true
|
|
} else {
|
|
args = append(args, object.UUID)
|
|
}
|
|
}
|
|
if len(args) > 1 {
|
|
params := make([]string, len(args)-1)
|
|
for i := range params {
|
|
params[i] = s.sqlParam(i + 2)
|
|
}
|
|
rows, err := tx.QueryContext(ctx, `SELECT object_uuid,path FROM paths WHERE domain_uuid=`+s.sqlParam(1)+` AND object_uuid IN (`+strings.Join(params, ",")+`) LIMIT 8001`, args...)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
count := 0
|
|
for rows.Next() {
|
|
count++
|
|
if count > objectListBindingLimit {
|
|
rows.Close()
|
|
return nil, nil, ErrObjectListBudget
|
|
}
|
|
var id, path string
|
|
if err := rows.Scan(&id, &path); err != nil {
|
|
rows.Close()
|
|
return nil, nil, err
|
|
}
|
|
if allowedPath(path) {
|
|
visible[id] = true
|
|
}
|
|
if len(visible) == len(candidates) {
|
|
break
|
|
}
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
out := make([]Object, 0, min(limit, len(candidates)))
|
|
var next *ObjectListPosition
|
|
for i, object := range candidates {
|
|
if visible[object.UUID] {
|
|
out = append(out, object)
|
|
}
|
|
if len(out) == limit {
|
|
if i+1 < len(candidates) || more {
|
|
next = &ObjectListPosition{CreatedAt: object.CreatedAt, UUID: object.UUID}
|
|
}
|
|
return out, next, nil
|
|
}
|
|
}
|
|
if more && len(candidates) != 0 {
|
|
last := candidates[len(candidates)-1]
|
|
next = &ObjectListPosition{CreatedAt: last.CreatedAt, UUID: last.UUID}
|
|
}
|
|
return out, next, nil
|
|
}
|