aba89c4f5d
Every change is benchmark-verified (harness + before/after numbers in benches/, measured on this branch; reproduction commands in each doc): DAV / sync-client hot paths - PROPFIND dead-properties: one = ANY($1) query per 500-child page instead of one sequential query per child, and indexable `=` predicates instead of IS NOT DISTINCT FROM (seq scans). 2,000-child folder: 1.07-4.54 s of DB chatter -> 4-6 ms (258-773x). Applied to native + NC PROPFIND and both NC REPORT handlers. [benches/DEAD-PROPS.md] - Folder paging: keyset cursor (name > $last) + new partial index (folder_id, name) replaces LIMIT/OFFSET full-folder rescan per page. Full 20k-file walk: 1266 ms -> 77 ms (16.5x). New migration 20260917000000. [benches/PROPFIND-PAGING.md] - NC chroot / default-drive resolution: moka caches (30 s TTL, explicit invalidation on drive mutations) for find_default_for_user and the markerless chroot FolderDto. 2 uncached queries + 2 pool checkouts per NC/WebDAV/WOPI request -> sub-us moka hit (p50 0.7-3.6 ms -> ~1 us). [benches/CHROOT-CACHE.md] - Quota: PROPFINDs whose prop list never names a quota prop skip the 2-query resolution entirely (wants_quota()); the remaining lookups read 2 columns instead of the full auth.users row with its <=512 KiB avatar (11-16x, p50 3.4 ms -> 0.29 ms). Same narrow read now gates every upload quota check. [benches/QUOTA-PATH.md] CPU on the request path - ZIP exports (folder download, share ZIP, batch download): entries whose MIME says already-compressed (JPEG/MP4/zip/pdf/...) are Stored instead of Deflate - deflate ran inline on the tokio writer task at ~41 MB/s for ~0% size gain. Mixed media corpus: 4.31x wall and CPU, archive size unchanged. Shared predicate in common::mime_detect. [benches/ZIP-MEDIA.md] - Compression layers: tower-http's default maps to Brotli QUALITY 11 (verified in brotli-8.0.2 source and empirically: 90 ms per 64 KiB JSON response, 1.3 s per 700 KiB bundle). Both layers pinned to Precise(4): 99x less CPU for ~15% more bytes. SPA assets are now precompressed at build time (scripts/precompress.mjs, 77% smaller) and served via ServeDir::precompressed_br/gzip: 2016x less per-request work, and clients get the better q11 bytes. [benches/STATIC-PRECOMPRESSED.md] Batched / cached backend paths [benches/NPLUS1-AND-CACHES.md] - Content-search ReBAC re-verification: new AuthorizationEngine::check_files_read_batch (default = old loop; PgAclEngine override batches drive resolution + reuses role cache). 200 sequential point SELECTs per search -> 1-2 queries. - Batch-ZIP subtree downloads: drop per-file re-authz + per-file Recent recording (2 writes/file) for subtree entries already authorized at the root - mirrors the native folder-download path. ~6,000 statements removed from a 2,000-file archive. - CDC chunk manifests: immutable by content address, now moka-cached (weight-bounded 32 MiB, 60 s TTL, positive-only, invalidated on delete) - removes one manifest query (p50 0.44-4.4 ms) from every stream, range and full blob read. - People tab: grouped COUNT + batched cover lookup instead of dragging every face row with its 2 KiB embedding (10k faces: 30.4 ms & 21 MB -> 3.8 ms & 1.3 KB, 8.1x); merge() is one set-based UPDATE. [benches/PEOPLE-LIST.md] - Photos timeline cursor: raw timestamptz comparison instead of EXTRACT(EPOCH ...) wrapper + IS NULL OR disjunction - cursor is an index boundary again, deep scroll stops re-scanning skipped rows. - Public share landing: one atomic UPDATE ... access_count + 1 (was SELECT + full-row write-back: racy, lost updates, clobbered concurrent owner edits) - 3 round-trips -> 2 per visit. - move_to_trash: dead full-entity SELECT feeding a documented no-op removed from both branches; dead fields dropped from TrashService. - NFC normalization: is_nfc_quick fast path skips the decompose/recompose state machine for the ~100% already-NFC case (every row loaded from PG). Frontend - Large folders paint after page one (~200 items) via fetchFolderListing's new onPage hook instead of waiting for every sequential page. - Tested-and-reverted (kept for the record): cached Intl.Collator for name sorts - vitest showed it 2x SLOWER than V8's argument-less localeCompare fast path (5.6 ms vs 12.1 ms / 5k names). Sort order untouched. New bench harnesses under examples/ (bench feature): zip_media, dead_props, chroot_cache, quota_path, people_list, propfind_paging, static_precompress. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CBK1RdtzyP6759Muqe1K1w
237 lines
11 KiB
Rust
237 lines
11 KiB
Rust
//! Authorization port — the trait every service depends on for permission
|
|
//! decisions. Implementations: `PgAclEngine` (v1 default), `OpenFgaEngine`
|
|
//! (future). A `CachedAuthorizationEngine` decorator over either is planned
|
|
//! as a future optimization.
|
|
//!
|
|
//! Architectural rule (see CLAUDE.md):
|
|
//! **AuthZ is enforced exclusively in the application service layer.**
|
|
//! Handlers authenticate the caller and pass `caller_id` to the service;
|
|
//! they never call this trait directly.
|
|
|
|
use uuid::Uuid;
|
|
|
|
use crate::common::errors::DomainError;
|
|
use crate::domain::services::authorization::{
|
|
Grant, GrantCursor, IncomingGrantSummary, OutgoingResourceSummary, Permission, Resource,
|
|
ResourceKind, Role, Subject,
|
|
};
|
|
|
|
pub trait AuthorizationEngine: Send + Sync + 'static {
|
|
/// Returns true if `subject` has `permission` on `resource`, considering
|
|
/// owner short-circuit AND cascading from folder ancestors.
|
|
///
|
|
/// `check` never errors for "permission denied" — that's a `false` return.
|
|
/// `Err` is reserved for infrastructure failures (DB down, etc.).
|
|
async fn check(
|
|
&self,
|
|
subject: Subject,
|
|
permission: Permission,
|
|
resource: Resource,
|
|
) -> Result<bool, DomainError>;
|
|
|
|
/// Batched `check(subject, Read, File(id))` over a result page: returns
|
|
/// the subset of `file_ids` the subject may read. Semantically identical
|
|
/// to looping [`Self::check`] (the default does exactly that); the
|
|
/// `PgAclEngine` override resolves every file's drive in ONE query and
|
|
/// reuses the per-drive role cache, so verifying a 200-hit search page
|
|
/// costs 1 SQL round-trip instead of up to 200 sequential ones
|
|
/// (benches/SEARCH-REBAC.md).
|
|
async fn check_files_read_batch(
|
|
&self,
|
|
subject: Subject,
|
|
file_ids: &[Uuid],
|
|
) -> Result<std::collections::HashSet<Uuid>, DomainError> {
|
|
let mut allowed = std::collections::HashSet::with_capacity(file_ids.len());
|
|
for id in file_ids {
|
|
if self
|
|
.check(subject, Permission::Read, Resource::File(*id))
|
|
.await?
|
|
{
|
|
allowed.insert(*id);
|
|
}
|
|
}
|
|
Ok(allowed)
|
|
}
|
|
|
|
/// Convenience wrapper around `check`: returns `Ok(())` when allowed and
|
|
/// `DomainError::not_found` when denied (anti-enumeration — same error as
|
|
/// "resource doesn't exist" so attackers can't probe IDs by error shape).
|
|
async fn require(
|
|
&self,
|
|
subject: Subject,
|
|
permission: Permission,
|
|
resource: Resource,
|
|
) -> Result<(), DomainError> {
|
|
if self.check(subject, permission, resource).await? {
|
|
// Granted path: high-traffic (every authorized request hits
|
|
// this), so kept at `debug` and structured for grep-friendly
|
|
// filtering. Not an audit event — the audit trail focuses
|
|
// on denials and explicit mutations elsewhere.
|
|
tracing::debug!(
|
|
target: "oxicloud::authz",
|
|
event = "authz.allowed",
|
|
subject_type = subject.type_str(),
|
|
subject_id = %subject.id(),
|
|
permission = permission.as_str(),
|
|
resource_type = resource.type_str(),
|
|
resource_id = %resource.id(),
|
|
"👮🏻♂️ perms: ✔ Subject '{}' has permission to '{}' on resource '{}'",
|
|
subject,
|
|
permission,
|
|
resource
|
|
);
|
|
Ok(())
|
|
} else {
|
|
let (kind, id) = match resource {
|
|
Resource::Folder(id) => ("Folder", id),
|
|
Resource::File(id) => ("File", id),
|
|
Resource::Drive(id) => ("Drive", id),
|
|
Resource::Calendar(id) => ("Calendar", id),
|
|
Resource::AddressBook(id) => ("AddressBook", id),
|
|
Resource::Playlist(id) => ("Playlist", id),
|
|
};
|
|
// Audit-worthy: denials are the interesting signal. Routed
|
|
// through the `audit` tracing target so log aggregators can
|
|
// surface them separately from operational debug traffic.
|
|
// Span context (request_id, client_ip, user_id) is attached
|
|
// automatically by the request-scope span set in
|
|
// `interfaces/middleware/trace_span.rs`, so this log line
|
|
// doesn't need to duplicate those fields — they appear in
|
|
// the structured output of every log written inside the
|
|
// request span.
|
|
tracing::info!(
|
|
target: "audit",
|
|
event = "authz.denied",
|
|
subject_type = subject.type_str(),
|
|
subject_id = %subject.id(),
|
|
permission = permission.as_str(),
|
|
resource_type = resource.type_str(),
|
|
resource_id = %resource.id(),
|
|
"👮🏻♂️ perms: ⛔ Subject '{}' hasn't permission to '{}' on resource '{}'",
|
|
subject,
|
|
permission,
|
|
resource
|
|
);
|
|
Err(DomainError::not_found(kind, id.to_string()))
|
|
}
|
|
}
|
|
|
|
/// Resources explicitly granted to `subject`. Direct grants only — no
|
|
/// cascade expansion. Used by `GET /api/grants/incoming`.
|
|
async fn list_incoming_grants(&self, subject: Subject) -> Result<Vec<Grant>, DomainError>;
|
|
|
|
/// Cursor-paginated list of resources explicitly granted to `subject`,
|
|
/// optionally filtered by resource kind. Multiple permission rows for the
|
|
/// same resource are collapsed into one `IncomingGrantSummary`.
|
|
///
|
|
/// Ordered by `MIN(granted_at) DESC, resource_id DESC` — stable across
|
|
/// concurrent inserts because the cursor encodes both fields.
|
|
///
|
|
/// Pass `kinds = &[]` to return all resource kinds.
|
|
/// Returns `(summaries, next_cursor)` — `next_cursor` is `None` when the
|
|
/// last page has been reached.
|
|
async fn list_incoming_resources_paged(
|
|
&self,
|
|
subject: Subject,
|
|
kinds: &[ResourceKind],
|
|
limit: u32,
|
|
cursor: Option<GrantCursor>,
|
|
sort_by: &str,
|
|
reverse: bool,
|
|
) -> Result<(Vec<IncomingGrantSummary>, Option<GrantCursor>), DomainError>;
|
|
|
|
/// All grants on a specific resource (for "Manage sharing" UI). Caller
|
|
/// must verify the caller has `Share` on the resource before invoking.
|
|
async fn list_grants_on_resource(&self, resource: Resource) -> Result<Vec<Grant>, DomainError>;
|
|
|
|
/// Grants Outgoing — grants created by `granted_by`. Used by
|
|
/// `GET /api/grants/outgoing` ("things I've shared with others").
|
|
async fn list_outgoing_grants(&self, granted_by: Uuid) -> Result<Vec<Grant>, DomainError>;
|
|
|
|
/// Cursor-paginated list of resources that `granted_by` has shared with
|
|
/// others. Multiple permission rows for the same (subject, resource) pair
|
|
/// are collapsed into one `OutgoingGrantEntry`; multiple subjects on the
|
|
/// same resource are grouped into one `OutgoingResourceSummary`.
|
|
///
|
|
/// Returns `(summaries, next_cursor)`.
|
|
async fn list_outgoing_resources_paged(
|
|
&self,
|
|
granted_by: Uuid,
|
|
limit: u32,
|
|
cursor: Option<GrantCursor>,
|
|
sort_by: &str,
|
|
reverse: bool,
|
|
) -> Result<(Vec<OutgoingResourceSummary>, Option<GrantCursor>), DomainError>;
|
|
|
|
/// Update `expires_at` for every role grant belonging to `subject`.
|
|
/// Used by `share_service` when a token-share's expiry is refreshed —
|
|
/// the subject (token) maps to a small fixed set of role grants, so a
|
|
/// single UPDATE covers them. Resource-scoped expiry changes go through
|
|
/// `set_role` (which carries `expires_at` as part of its UPSERT).
|
|
async fn set_expiry_for_subject(
|
|
&self,
|
|
subject: Subject,
|
|
expires_at: Option<chrono::DateTime<chrono::Utc>>,
|
|
) -> Result<(), DomainError>;
|
|
|
|
/// Delete every row from `storage.role_grants` whose `expires_at` is
|
|
/// more than `grace_days` in the past. Returns the count of rows
|
|
/// removed.
|
|
///
|
|
/// The engine's `check` / `list_grants_*` paths already ignore
|
|
/// expired rows (they filter on `expires_at > NOW()` in-query), so
|
|
/// this is pure garbage collection — no live authorization decision
|
|
/// changes. The grace window preserves the audit / support answer
|
|
/// to "what happened to my access?" for a couple of weeks past
|
|
/// expiration.
|
|
///
|
|
/// Grace of `0` means "delete every row whose `expires_at` is in
|
|
/// the past, right now" — used by the admin `?force=true` trigger
|
|
/// endpoint to enable Hurl regression testing without waiting the
|
|
/// configured grace out.
|
|
///
|
|
/// Rows with `expires_at IS NULL` (permanent grants) are never
|
|
/// touched.
|
|
async fn purge_expired_grants(&self, grace_days: u32) -> Result<u64, DomainError>;
|
|
|
|
/// Revoke a single role grant by its UUID. Idempotent — returns `Ok(())`
|
|
/// whether or not the row existed. The id comes from a prior listing
|
|
/// or `find_grant_full_by_id` lookup.
|
|
async fn revoke(&self, grant_id: Uuid) -> Result<(), DomainError>;
|
|
|
|
/// Removes every grant whose `resource` matches. Called by lifecycle
|
|
/// hooks when a resource is permanently deleted. Returns the count of
|
|
/// rows removed.
|
|
async fn revoke_all_for_resource(&self, resource: Resource) -> Result<usize, DomainError>;
|
|
|
|
/// Removes every grant whose `subject` matches. Called when a user/token
|
|
/// /group is deleted. Returns the count of rows removed.
|
|
async fn revoke_all_for_subject(&self, subject: Subject) -> Result<usize, DomainError>;
|
|
|
|
// ── Role-keyed grant operations ────────────────────────────────────────
|
|
// These are the only grant write path. Lifecycle hook bulk-deletes
|
|
// (`revoke_all_for_*` above) wipe matching rows directly, so callers
|
|
// using those paths don't need to invoke `clear_role` separately.
|
|
|
|
/// Set the role for a `(subject, resource)` pair. Idempotent via the
|
|
/// UNIQUE `(subject_type, subject_id, resource_type, resource_id)`
|
|
/// constraint — `ON CONFLICT` updates the role + expires_at if they
|
|
/// changed, which is exactly the right semantics for an atomic role
|
|
/// change (e.g. promoting Viewer → Editor in one UPDATE with no race
|
|
/// window, no DELETE+INSERT).
|
|
async fn set_role(
|
|
&self,
|
|
granted_by: Uuid,
|
|
subject: Subject,
|
|
role: Role,
|
|
resource: Resource,
|
|
expires_at: Option<chrono::DateTime<chrono::Utc>>,
|
|
) -> Result<Grant, DomainError>;
|
|
|
|
/// Remove the role for a `(subject, resource)` pair. Idempotent —
|
|
/// succeeds whether or not the row existed. Called after `revoke`
|
|
/// succeeds to keep the two tables in sync during dual-write; after
|
|
/// cleanup this is the canonical role-revocation entry point.
|
|
async fn clear_role(&self, subject: Subject, resource: Resource) -> Result<(), DomainError>;
|
|
}
|