Diffinery

homeflare/celldpublic

38 lines · 1.9 KBRaw History
1//! Cell-scope predicates, reified sans-IO.
2//!
3//! A cell scope names one Durable Object: `Class:instance`, or a bare instance
4//! that the runtime prefixes with the single configured class. The scope is
5//! then used as a path component and as an object-store key — `db_path` joins
6//! it under the data directory, and the replication client builds
7//! `cells/<scope>/ltx/e<epoch>` from it — so its charset is a SECURITY fence,
8//! exactly as `peer::valid_identity`'s is. Admit `/` and a scope carries its
9//! own path segments: it escapes the data directory through `..` and escapes
10//! the bucket prefix the same way.
11//!
12//! The gate lives here, and the callers that accept a scope from the network
13//! apply it before the scope reaches storage.
14
15/// Is a cell scope well-formed? Non-empty, at most 512 bytes, and only ASCII
16/// alphanumerics plus `_ - . : $`.
17///
18/// The charset is the fence. `/` and `\` are excluded so the scope can never be
19/// more than one path component, which is what makes `..` inert: `Class:..` is
20/// a literal directory name, while `Class:../..` would traverse. Control bytes
21/// and NUL are excluded with everything else outside the set.
22///
23/// `:` is admitted because it is the class/instance separator and an instance
24/// may itself contain one. `$` is admitted because it is a legal JavaScript
25/// identifier character, so it is a legal exported class name. Both are inert
26/// in a path component.
27///
28/// The 512-byte bound is generous next to the two shapes that actually occur —
29/// a class name and a 64-character hex id, or a class name and a
30/// developer-chosen instance name — and it bounds what a single request can
31/// make the storage layer allocate.
32pub fn valid_cell_scope(scope: &str) -> bool {
33 !scope.is_empty()
34 && scope.len() <= 512
35 && scope
36 .bytes()
37 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.' | b':' | b'$'))
38}