interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / crates / site-builder / src / ledger_public.rs

149 lines · 8572 bytes Raw Blame History
1//! The public ledger is a line-preserving projection, never a copy of the
2//! private coordination ledger. Row numbers remain stable for #L links.
3use std::borrow::Cow;
4use regex::Regex;
5use std::sync::LazyLock;
6
7struct Rules {
8 secret: Regex,
9 email: Regex,
10 uuid: Regex,
11 path: Regex,
12 root: Regex,
13 host: Regex,
14 ipv4: Regex,
15 ipv6: Regex,
16 port: Regex,
17 pid: Regex,
18}
19
20static RULES: LazyLock<Rules> = LazyLock::new(|| Rules {
21 secret: Regex::new(r"(?ix)(?:\b(?:password|passwd|secret|api[_-]?key|private[_-]?key|access[_-]?token|auth(?:orization)?|cookie|session[_-]?(?:key|token|id))\s*(?:=|:|\bis\b)\s*\S+|--(?:[a-z0-9]+[_-])*(?:password|passwd|secret|api[_-]?key|private[_-]?key|access[_-]?token|auth(?:orization)?|cookie|session[_-]?(?:key|token|id))\s+\S+|\b(?:sk-|ghp_|gho_|github_pat_|xox[baprs]-|AKIA)[a-z0-9_-]+|-----BEGIN\s+(?:[A-Z]+\s+)*PRIVATE\s+KEY|\bBearer\s+[a-z0-9._-]{8,}|[a-z][a-z0-9+.-]*://[^\s/@]+:[^\s/@]+@)").unwrap(),
22 email: Regex::new(r"(?i)\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}\b").unwrap(),
23 uuid: Regex::new(r"(?i)\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b").unwrap(),
24 // Only paths with a separate leading delimiter. Repository-relative
25 // evidence paths and public https:// links are intentional references.
26 path: Regex::new(r#"(?i)(^|[\s(<`"'\[=:,])(?:~[/\\][^\s<>"']*|/(?:[a-z0-9._-]+/)+[a-z0-9._-]*)"#).unwrap(),
27 // Machine-local roots are private even when prose glues them to the
28 // previous word ("brief/home/user/..."), so match them anywhere.
29 root: Regex::new(r#"(?:~[/\\]|/(?:home|root|tmp|var|etc|opt|srv|run|proc|mnt|media|Users|private)/)[^\s<>"'`)\]]*"#).unwrap(),
30 host: Regex::new(r"(?i)\b(?:localhost|[a-z0-9_-]+(?:\.[a-z0-9_-]+)*\.(?:local|internal|ts\.net))\b").unwrap(),
31 ipv4: Regex::new(r"\b(?:\d{1,3}\.){3}\d{1,3}\b").unwrap(),
32 ipv6: Regex::new(r"(?i)\[[0-9a-f:]{2,}\]").unwrap(),
33 port: Regex::new(r":18[0-9]{3}\b").unwrap(),
34 pid: Regex::new(r"(?i)\bpid\s*[0-9]+\b").unwrap(),
35});
36
37const HEADER: &str = "id\topened_utc\towner\tstatus\titem\tdone_when\tevidence\tupdated_utc";
38// RFC 8032 section 7.1 TEST 1 publishes this exact Ed25519 key. The
39// exception is value-bound, not a general exemption for RFC citations.
40const RFC8032_TEST1: &str = "secret is 9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60;";
41
42fn public_field(input: &str) -> Result<Cow<'_, str>, &'static str> {
43 let r = &*RULES;
44 if r.secret.find_iter(input).any(|m| {
45 !input.to_ascii_lowercase().contains("rfc 8032 7.1 test 1")
46 || !m.as_str().eq_ignore_ascii_case(RFC8032_TEST1)
47 }) {
48 return Err("credential-shaped value in ledger");
49 }
50 if r.email.is_match(input) { return Err("email-shaped value in ledger"); }
51 if input.chars().any(|c| c.is_control()) {
52 return Err("control character in ledger field");
53 }
54 if !r.path.is_match(input) && !r.root.is_match(input) && !r.uuid.is_match(input) && !r.host.is_match(input)
55 && !r.ipv4.is_match(input) && !r.ipv6.is_match(input) && !r.port.is_match(input)
56 && !r.pid.is_match(input) {
57 return Ok(Cow::Borrowed(input));
58 }
59 let value = r.path.replace_all(input, |caps: &regex::Captures<'_>| format!("{}[private path]", &caps[1]));
60 let value = r.root.replace_all(&value, "[private path]");
61 let value = r.uuid.replace_all(&value, "[private id]");
62 let value = r.host.replace_all(&value, "[private host]");
63 let value = r.ipv4.replace_all(&value, "[network address]");
64 let value = r.ipv6.replace_all(&value, "[network address]");
65 let value = r.port.replace_all(&value, ":[private port]");
66 let value = r.pid.replace_all(&value, "pid [private]");
67 Ok(Cow::Owned(value.into_owned()))
68}
69
70pub fn project(input: &str) -> Result<String, String> {
71 if !input.ends_with('\n') || input.contains('\r') { return Err("ledger must be newline-terminated UTF-8 TSV".into()); }
72 let mut lines = input.lines();
73 if lines.next() != Some(HEADER) { return Err("unexpected ledger header".into()); }
74 let mut output = String::with_capacity(input.len());
75 output.push_str(HEADER);
76 output.push('\n');
77 for (row, line) in lines.enumerate() {
78 let mut count = 0;
79 for field in line.split('\t') {
80 if count == 8 { return Err(format!("line {}: ledger row has more than eight columns", row + 2)); }
81 if count > 0 { output.push('\t'); }
82 output.push_str(&public_field(field).map_err(|reason| format!("line {}: {reason}", row + 2))?);
83 count += 1;
84 }
85 if count < 5 { return Err(format!("line {}: ledger row has fewer than five columns", row + 2)); }
86 output.push('\n');
87 }
88 Ok(output)
89}
90
91pub fn check_public(input: &str) -> Result<(), String> {
92 if project(input)? == input { Ok(()) } else { Err("ledger is not its public projection".into()) }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98 const HEADER_LINE: &str = "id\topened_utc\towner\tstatus\titem\tdone_when\tevidence\tupdated_utc\n";
99 #[test]
100 fn projects_paths_addresses_and_keeps_line_links() {
101 let row = "L001\t2026-10-19T16:43Z\tagent\tdone\tSee /home/person/private\tCheck ~/notes at 127.0.0.1:18091\trepo@abcdef1 evidence/run.log https://interplane.io/drakestapleton/omega-mv pid 12345 550e8400-e29b-41d4-a716-446655440000\t2026-10-19T17:00Z\n";
102 let output = project(&format!("{HEADER_LINE}{row}")).unwrap();
103 assert!(output.contains("See [private path]"));
104 assert!(output.contains("Check [private path] at [network address]:[private port]"));
105 assert!(output.contains("repo@abcdef1 evidence/run.log https://interplane.io/drakestapleton/omega-mv"));
106 assert!(output.contains("pid [private] [private id]"));
107 assert!(output.lines().nth(1).unwrap().starts_with("L001\t"));
108 assert!(check_public(&output).is_ok());
109 assert!(check_public(&format!("{HEADER_LINE}{row}")).is_err());
110 }
111 #[test]
112 fn projects_glued_private_roots_and_compact_pids() {
113 let row = "L003\tdate\tagent\topen\tworker pid2344210, brief/home/person/handoffs/x.md and queue~/state/a\t\tevidence/run.log https://interplane.io/drakestapleton/omega-mv\tdate\n";
114 let output = project(&format!("{HEADER_LINE}{row}")).unwrap();
115 assert!(!output.contains("/home/"), "{output}");
116 assert!(!output.contains("~/"), "{output}");
117 assert!(!output.contains("2344210"), "{output}");
118 assert!(output.contains("brief[private path] and queue[private path]"), "{output}");
119 assert!(output.contains("evidence/run.log https://interplane.io/drakestapleton/omega-mv"));
120 assert!(check_public(&output).is_ok());
121 }
122 #[test]
123 fn rejects_entire_ledger_on_identifiers_credentials_or_malformed_rows() {
124 for value in ["owner@example.org", "password=hunter22", "--api-key shortvalue", "ghp_abcdef123"] {
125 let input = format!("{HEADER_LINE}L001\tdate\tagent\topen\t{value}\t\t\tdate\n");
126 assert!(project(&input).is_err(), "{value}");
127 }
128 assert!(project(&format!("{HEADER_LINE}L001\tbad\n")).is_err());
129 assert!(project(&format!("{HEADER_LINE}L001\tdate\tagent\topen\tgood\t\t\tdate\n")).is_ok());
130 // now.rs accepts five-field rows and treats the trailing cells as
131 // absent. Keep their source line and still screen the fields present.
132 let short = format!("{HEADER_LINE}L002\tdate\tagent\topen\tSee /home/person/private\n");
133 let projected = project(&short).unwrap();
134 assert!(projected.contains("L002\tdate\tagent\topen\tSee [private path]\n"));
135 assert!(check_public(&projected).is_ok());
136 }
137 #[test]
138 fn rfc_8032_test_vector_is_public_but_other_secret_is_not() {
139 // RFC 8032 section 7.1 TEST 1; this key is explicitly published as
140 // a test vector, not an operational secret.
141 let known = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60";
142 let public = format!("{HEADER_LINE}L001\tdate\tagent\topen\tRFC 8032 7.1 TEST 1 secret is {known};\t\t\tdate\n");
143 assert!(project(&public).is_ok(), "published RFC test vector must remain publishable");
144 let unknown = "a4c618e50b7d92f3c4e1d690a5b7c8d9e0f1a2b3c4d5e6f789ab01cd23ef4567";
145 for item in [format!("secret is {unknown}"), format!("RFC 8032 7.1 TEST 1 secret is {unknown}")] {
146 assert!(project(&format!("{HEADER_LINE}L001\tdate\tagent\topen\t{item}\t\t\tdate\n")).is_err(), "{item}");
147 }
148 }
149}