An app's OWN store had no ceiling of any kind — only the group-SHARED
collections did, which is the inversion of what you'd expect. And
`authz::script_gate` returns `Ok(())` when there is no principal, so a
PUBLIC UNAUTHENTICATED route could reach `files::collection(..).create(..)`
and write blobs in a loop until the disk filled. `PICLOUD_FILES_MAX_FILE_SIZE_BYTES`
caps ONE file at 100 MB; nothing capped the count. Same for KV keys and docs
against Postgres.
Ceilings: `PICLOUD_APP_KV_MAX_ROWS` / `PICLOUD_APP_DOCS_MAX_ROWS` (100k) and
`PICLOUD_APP_FILES_MAX_TOTAL_BYTES` (10 GiB).
They are enforced DIFFERENTLY from the group ones, on purpose — porting the
group design as-is would have been a serious regression:
* KV/docs check a ROW count, on INSERT only, with NO lock. `kv::set` is the
hottest write path in the system; taking a per-app advisory lock on every
set would serialize an app's entire data plane, and a `SUM(...)` scan would
make write cost grow with the app's stored size. What the lock buys is
small — unlocked overshoot is bounded by write concurrency (32) against a
ceiling of 100k, ~0.03%. These are anti-DoS rails, not billing. An update
adds no row and is bounded by the per-value cap, so it pays nothing at all.
* No per-app BYTE ceiling for KV/docs: every value is already capped at
`PICLOUD_KV_MAX_VALUE_BYTES`, so `max_rows x max_value_bytes` is ALREADY a
finite bound. A second scan would buy nothing.
* FILES are the exception and DO lock + sum: one blob may be 100 MB, so 32
racing uploads could overshoot by gigabytes of real disk, and a file write
is heavy enough that the lock and the SUM are lost in the noise. Checked on
create AND update — an update that skipped the ceiling would be a free
bypass (the same bug just fixed for group files: grow a 1-byte file to
100 MB, repeat).
`group_quota` is renamed `quota`: it now serves both owners, and the module
doc is where the two enforcement strategies are contrasted.
Pinned by tests/atomic_write.rs: the key ceiling refuses a new key while
still allowing an update at the ceiling (rejecting updates would brick an app
the moment it filled up — worse than the DoS the rail exists to stop), and 10
concurrent uploads cannot exceed the disk ceiling, nor can an update grow past it.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
210 lines
6.4 KiB
Rust
210 lines
6.4 KiB
Rust
//! `GroupFilesService` — the §11.6 shared group-collection FILES contract.
|
|
//!
|
|
//! The cross-app-sharing counterpart to [`crate::FilesService`], analogous to
|
|
//! [`crate::GroupKvService`] / [`crate::GroupDocsService`]. A script reaches it
|
|
//! via the explicit `files::shared_collection("name")` handle. The owner of the
|
|
//! data is not `cx.app_id`: the impl resolves the **owning group** by walking the
|
|
//! calling app's ancestor chain for the nearest group that declares the
|
|
//! collection shared with `kind='files'`. That ancestry walk is the isolation
|
|
//! boundary — the trait surface takes **no** group id (owner derivation stays in
|
|
//! the impl).
|
|
//!
|
|
//! Trust model (§11.6): **reads are open** to any subtree script (anonymous
|
|
//! public HTTP included); **writes require an authenticated principal** with
|
|
//! editor+ on the owning group. Same shape as group KV/docs.
|
|
|
|
use async_trait::async_trait;
|
|
use thiserror::Error;
|
|
use uuid::Uuid;
|
|
|
|
use crate::{FileMeta, FileUpdate, FilesListPage, NewFile, SdkCallCx};
|
|
|
|
#[async_trait]
|
|
pub trait GroupFilesService: Send + Sync {
|
|
async fn create(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
new: NewFile,
|
|
) -> Result<Uuid, GroupFilesError>;
|
|
|
|
async fn head(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
id: &str,
|
|
) -> Result<Option<FileMeta>, GroupFilesError>;
|
|
|
|
async fn get(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
id: &str,
|
|
) -> Result<Option<Vec<u8>>, GroupFilesError>;
|
|
|
|
async fn update(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
id: &str,
|
|
upd: FileUpdate,
|
|
) -> Result<(), GroupFilesError>;
|
|
|
|
async fn delete(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
id: &str,
|
|
) -> Result<bool, GroupFilesError>;
|
|
|
|
async fn list(
|
|
&self,
|
|
cx: &SdkCallCx,
|
|
collection: &str,
|
|
cursor: Option<&str>,
|
|
limit: u32,
|
|
) -> Result<FilesListPage, GroupFilesError>;
|
|
}
|
|
|
|
/// Stub for test bundles that don't exercise shared files collections.
|
|
#[derive(Debug, Default, Clone, Copy)]
|
|
pub struct NoopGroupFilesService;
|
|
|
|
#[async_trait]
|
|
impl GroupFilesService for NoopGroupFilesService {
|
|
async fn create(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_new: NewFile,
|
|
) -> Result<Uuid, GroupFilesError> {
|
|
Err(GroupFilesError::Backend(
|
|
"group files is not wired in".into(),
|
|
))
|
|
}
|
|
|
|
async fn head(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_id: &str,
|
|
) -> Result<Option<FileMeta>, GroupFilesError> {
|
|
Err(GroupFilesError::Backend(
|
|
"group files is not wired in".into(),
|
|
))
|
|
}
|
|
|
|
async fn get(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_id: &str,
|
|
) -> Result<Option<Vec<u8>>, GroupFilesError> {
|
|
Err(GroupFilesError::Backend(
|
|
"group files is not wired in".into(),
|
|
))
|
|
}
|
|
|
|
async fn update(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_id: &str,
|
|
_upd: FileUpdate,
|
|
) -> Result<(), GroupFilesError> {
|
|
Err(GroupFilesError::Backend(
|
|
"group files is not wired in".into(),
|
|
))
|
|
}
|
|
|
|
async fn delete(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_id: &str,
|
|
) -> Result<bool, GroupFilesError> {
|
|
Err(GroupFilesError::Backend(
|
|
"group files is not wired in".into(),
|
|
))
|
|
}
|
|
|
|
async fn list(
|
|
&self,
|
|
_cx: &SdkCallCx,
|
|
_collection: &str,
|
|
_cursor: Option<&str>,
|
|
_limit: u32,
|
|
) -> Result<FilesListPage, GroupFilesError> {
|
|
Err(GroupFilesError::Backend(
|
|
"group files is not wired in".into(),
|
|
))
|
|
}
|
|
}
|
|
|
|
/// Failure modes surfaced to the Rhai bridge. Mirrors [`crate::FilesError`] plus
|
|
/// the §11.6 `CollectionNotShared` boundary.
|
|
#[derive(Debug, Error)]
|
|
pub enum GroupFilesError {
|
|
#[error("invalid collection name: {0}")]
|
|
InvalidCollection(String),
|
|
|
|
/// No group on the calling app's ancestor chain declares this collection
|
|
/// shared with `kind='files'` — the structural "not shared with you"
|
|
/// boundary (also the outcome for a foreign app).
|
|
#[error("collection {0:?} is not a shared group files collection for this app")]
|
|
CollectionNotShared(String),
|
|
|
|
#[error("missing required field: {0}")]
|
|
MissingField(&'static str),
|
|
|
|
#[error("file too large: {size} bytes exceeds limit of {limit} bytes")]
|
|
TooLarge { size: usize, limit: usize },
|
|
|
|
/// The owning group's shared-files store is at its total-bytes quota
|
|
/// (`PICLOUD_GROUP_FILES_MAX_TOTAL_BYTES`); the write is rejected.
|
|
#[error("group files: quota exceeded ({actual} bytes; limit {limit})")]
|
|
QuotaExceeded { limit: u64, actual: u64 },
|
|
|
|
#[error("file name too long: {0} bytes exceeds 255")]
|
|
NameTooLong(usize),
|
|
|
|
#[error("content_type too long: {0} bytes exceeds 127")]
|
|
ContentTypeTooLong(usize),
|
|
|
|
#[error("file not found")]
|
|
NotFound,
|
|
|
|
#[error("file content corrupted (checksum mismatch)")]
|
|
Corrupted,
|
|
|
|
/// For reads this is only raised when `cx.principal.is_some()`; for WRITES
|
|
/// it is also raised when the principal is absent (writes fail closed).
|
|
#[error("forbidden")]
|
|
Forbidden,
|
|
|
|
#[error("group files backend error: {0}")]
|
|
Backend(String),
|
|
}
|
|
|
|
/// Map the shared [`crate::FilesError`] — returned by `validate_collection`
|
|
/// (re-exported as `validate_files_collection`) and the `NewFile`/`FileUpdate`
|
|
/// validators that the group-files service reuses — onto the group error. Lives
|
|
/// here (not in manager-core) so the orphan rule is satisfied.
|
|
impl From<crate::FilesError> for GroupFilesError {
|
|
fn from(e: crate::FilesError) -> Self {
|
|
use crate::FilesError as F;
|
|
match e {
|
|
F::InvalidCollection(c) => Self::InvalidCollection(c),
|
|
F::MissingField(f) => Self::MissingField(f),
|
|
F::TooLarge { size, limit } => Self::TooLarge { size, limit },
|
|
F::NameTooLong(n) => Self::NameTooLong(n),
|
|
F::ContentTypeTooLong(n) => Self::ContentTypeTooLong(n),
|
|
F::NotFound => Self::NotFound,
|
|
F::Corrupted => Self::Corrupted,
|
|
F::Forbidden => Self::Forbidden,
|
|
F::QuotaExceeded { limit, actual } => Self::QuotaExceeded { limit, actual },
|
|
F::Backend(s) => Self::Backend(s),
|
|
}
|
|
}
|
|
}
|