subc_protocol/module_id.rs
1//! Shared module-id grammar for identity and declaration keys.
2
3/// Why `module_id` cannot serve as one literal store-path component.
4///
5/// This refuses path hazards rather than sanitizing identities. Colons are
6/// allowed for reserved-namespace children such as `mcp:worker`; platform-specific
7/// filename legality belongs to the store, not to the identity grammar.
8/// The daemon's copy in `registry.rs` is to be replaced by a call to this helper.
9pub fn module_id_path_hazard(module_id: &str) -> Result<(), String> {
10 if module_id.is_empty() {
11 return Err("empty".to_string());
12 }
13 if module_id.contains('/') || module_id.contains('\\') {
14 return Err("contains a path separator".to_string());
15 }
16 if module_id == "." || module_id == ".." {
17 return Err("is a dot path component".to_string());
18 }
19 if module_id.chars().any(|c| c.is_control()) {
20 return Err("contains a control character".to_string());
21 }
22 // The 255-byte store-path component limit counts UTF-8 bytes, not characters.
23 if module_id.len() > 255 {
24 return Err("is longer than 255 bytes".to_string());
25 }
26 Ok(())
27}