Diffinery

homeflare/celldpublic

51 lines · 2.6 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/// The fleet-wide storage limit for one cell scope. Every data filesystem must
16/// support a path component of this size, so scope validity is the same on
17/// every node that can own the cell.
18pub const MAX_CELL_SCOPE: usize = 255;
19
20/// Is a cell scope well-formed? Non-empty, at most `MAX_CELL_SCOPE` bytes, not
21/// `.` or `..`, and only ASCII alphanumerics plus `_ - . : $`.
22///
23/// The charset is the fence. `/` and `\` are excluded so the scope can never be
24/// more than one path component, which is what makes an embedded `..` inert:
25/// `Class:..` is a literal directory name, while `Class:../..` would traverse.
26/// Control bytes and NUL are excluded with everything else outside the set.
27///
28/// One path component is not sufficient on its own, because `.` and `..` are
29/// themselves single components that the filesystem resolves. A bare `..`
30/// clears the charset, and the callers that take a scope without a class prefix
31/// — `/cell/`, `/evict/`, the peer routes, and the wake index — hand it to
32/// `db_path` unchanged, so `data_dir.join("..")` lands above the data
33/// directory. Both names are therefore rejected.
34///
35/// `:` is admitted because it is the class/instance separator and an instance
36/// may itself contain one. `$` is admitted because it is a legal JavaScript
37/// identifier character, so it is a legal exported class name. Both are inert
38/// in a path component.
39///
40/// The bound is also the minimum `NAME_MAX` that celld accepts for a data
41/// filesystem. The runtime checks that capability at startup. Therefore, a
42/// scope that passes this gate fits every node in the fleet.
43pub fn valid_cell_scope(scope: &str) -> bool {
44 !scope.is_empty()
45 && scope.len() <= MAX_CELL_SCOPE
46 && scope != "."
47 && scope != ".."
48 && scope
49 .bytes()
50 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.' | b':' | b'$'))
51}