Files
pruefwerk/compliance-agent/src/tenant_path.rs
T
sharang cc3a9c7130
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m10s
CI / Deploy MCP (push) Successful in 3m49s
CI / Deploy Dashboard (push) Successful in 5m32s
CI / Deploy Agent (push) Successful in 6m52s
feat(platform): isolate tenants on disk, in the runner API and on webhooks (#255) (#295)
2026-09-21 06:53:36 +00:00

121 lines
4.3 KiB
Rust

//! Filesystem path segments that carry a tenant boundary.
//!
//! Everything the agent writes for a target — git clones, werkbank artifact
//! blobs — lives under a directory named after the owning tenant. Two tenants
//! that onboard a target with the same name, or a blob with the same digest,
//! must never land on the same path: that is how one tenant ends up fetching
//! into (or reading out of) another tenant's checkout.
//!
//! Both segment builders here collapse their input to a single safe path
//! component, so a `..` or a `/` in a tenant id or a target name can never
//! escape or nest. [`tenant_segment`] additionally keeps the mapping
//! *injective*, which [`name_segment`] does not need to be.
use sha2::{Digest, Sha256};
/// Characters that survive into a path segment verbatim.
fn is_safe(c: char) -> bool {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.'
}
/// Map anything outside `[A-Za-z0-9._-]` to `_` and trim leading/trailing
/// `.`/`_`, so the result is one path component and never `.`/`..`.
fn collapse(raw: &str) -> String {
let mapped: String = raw
.chars()
.map(|c| if is_safe(c) { c } else { '_' })
.collect();
mapped.trim_matches(|c| c == '.' || c == '_').to_string()
}
/// The directory segment for a **tenant**.
///
/// Collapsing is lossy — `a/b`, `a:b` and `a_b` all map to `a_b` — and for a
/// tenant that would be a shared directory, i.e. exactly the isolation bug this
/// exists to prevent. So whenever collapsing changed anything (or emptied the
/// input), a digest of the *raw* id is appended: distinct tenant ids then always
/// get distinct directories. Well-formed ids (UUIDs, slugs) are unchanged and
/// stay readable.
pub fn tenant_segment(tenant_id: &str) -> String {
let collapsed = collapse(tenant_id);
if !collapsed.is_empty() && collapsed == tenant_id {
return collapsed;
}
let digest = hex::encode(Sha256::digest(tenant_id.as_bytes()));
let stem = if collapsed.is_empty() {
"t"
} else {
&collapsed
};
format!("{stem}-{}", &digest[..16])
}
/// The directory segment for a **name inside a tenant** (a target/repo name).
///
/// Names can carry slashes or other path-hostile characters (a target named
/// after a repo path, say); those would otherwise nest or break the path. Only
/// collision-free *across* tenants — which the enclosing [`tenant_segment`]
/// guarantees.
pub fn name_segment(name: &str) -> String {
let collapsed = collapse(name);
if collapsed.is_empty() {
"repo".to_string()
} else {
collapsed
}
}
#[cfg(test)]
mod tests {
use super::{name_segment, tenant_segment};
#[test]
fn name_segment_collapses_path_hostile_names() {
assert_eq!(name_segment("zephyr-example-app"), "zephyr-example-app");
assert_eq!(
name_segment("ChristianRinn/bare_metal_stm32f411xe"),
"ChristianRinn_bare_metal_stm32f411xe"
);
assert_eq!(name_segment("../../etc/passwd"), "etc_passwd");
assert_eq!(name_segment("a b:c"), "a_b_c");
assert_eq!(name_segment("///"), "repo");
}
#[test]
fn tenant_segment_passes_well_formed_ids_through() {
assert_eq!(
tenant_segment("3f2b9c1e-0a5d-4c88-9f1a-7b2c3d4e5f60"),
"3f2b9c1e-0a5d-4c88-9f1a-7b2c3d4e5f60"
);
assert_eq!(tenant_segment("acme_gmbh"), "acme_gmbh");
}
#[test]
fn tenant_segment_sanitises_traversal() {
let seg = tenant_segment("../../etc");
assert!(!seg.contains('/'), "{seg} must be one path component");
assert!(!seg.contains(".."), "{seg} must not traverse");
assert!(seg.starts_with("etc-"), "{seg}");
}
#[test]
fn tenant_segment_is_injective_where_collapsing_is_not() {
// These three collapse to the same string; they must not share a dir.
let a = tenant_segment("acme/eu");
let b = tenant_segment("acme:eu");
let c = tenant_segment("acme_eu");
assert_ne!(a, b);
assert_ne!(a, c);
assert_ne!(b, c);
// …and an id that needs no collapsing is left alone.
assert_eq!(c, "acme_eu");
}
#[test]
fn tenant_segment_never_empty() {
assert!(!tenant_segment("").is_empty());
assert!(!tenant_segment("///").is_empty());
assert_ne!(tenant_segment(""), tenant_segment("///"));
}
}