Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(sanitize): match credentials across invisible characters and redact more key blocks

AkitaOnRails642d8d6ee075
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6609cfa4..67159a12 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,21 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
+### Fixed
+
+- Hardened the privacy strip. It now matches credentials across invisible
+ formatting characters while storing text that matches nothing byte for
+ byte, so emoji sequences and scripts that need joiners are untouched. It
+ also redacts PGP private keys and private-key blocks that have no end line,
+ as long as a key-body line follows the header, so prose that only names a
+ header is kept. Tag characters outside an emoji flag, a byte-order mark
+ after the start of the text and the invisible math operators are stripped.
+ The allowlist no longer exempts private keys, and blank `[sanitize]
+ allowlist` entries are ignored with a warning at startup. The client scrub
+ shared by the relay and importer companions follows the same rules.
+ Reported by @FabioLeitao.
+ ([GHSA-jpcg-w5c5-wcv6](https://github.com/akitaonrails/ai-memory/security/advisories/GHSA-jpcg-w5c5-wcv6))
+
## [3.2.0] - 2026-10-21
### Added
diff --git a/companions/ai-memory-client/src/scrub.rs b/companions/ai-memory-client/src/scrub.rs
index bb9f4d3f..05552eb3 100644
--- a/companions/ai-memory-client/src/scrub.rs
+++ b/companions/ai-memory-client/src/scrub.rs
@@ -1,50 +1,147 @@
//! Local credential scrub shared by external capture clients.
+//!
+//! The server sanitizer (`ai-memory-core::sanitize`) re-scrubs everything
+//! these clients send; this copy keeps credentials out of the local spool,
+//! queue and preview files written before delivery. It follows the same
+//! rules for private keys and invisible characters: key blocks are matched
+//! first, including PGP armor and a block cut off before its end line, and
+//! invisible formatting characters do not hide a credential from the
+//! patterns.
use regex::Regex;
use std::sync::OnceLock;
pub fn sanitize_external_text(input: &str) -> String {
- let controls_removed: String = input
- .chars()
- .map(|ch| {
- if ch == '\n'
- || ch == '\r'
- || ch == '\t'
- || !(ch.is_control()
- || matches!(ch, '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}'))
- {
- ch
- } else {
- '\u{fffd}'
- }
- })
- .collect();
+ let mut offset = 0;
+ let mut controls_removed = String::with_capacity(input.len());
+ while let Some(ch) = input[offset..].chars().next() {
+ let next = offset + ch.len_utf8();
+ if ch == '\u{1f3f4}' {
+ let tags = emoji_tag_run_len(&input[next..]);
+ controls_removed.push_str(&input[offset..next + tags]);
+ offset = next + tags;
+ continue;
+ }
+ // Removed rather than replaced, before matching: a placeholder left
+ // inside a credential would split it out of reach of every pattern.
+ if ch == '\n'
+ || ch == '\r'
+ || ch == '\t'
+ || !(ch.is_control()
+ || matches!(ch, '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}')
+ || is_hidden_text_char(offset, ch))
+ {
+ controls_removed.push(ch);
+ }
+ offset = next;
+ }
secret_patterns()
.iter()
.fold(controls_removed, |text, rule| {
+ // Through invisible characters first, so a split credential is
+ // replaced whole; then the plain pass, which still finds a match
+ // that needs an invisible character as its word boundary.
+ let text = if text.contains(is_default_ignorable) {
+ redact_through_invisibles(&text, rule)
+ } else {
+ text
+ };
rule.regex
.replace_all(&text, |captures: &regex::Captures<'_>| {
- let matched = captures.get(0).map_or("", |matched| matched.as_str());
- if captures.get(3).is_some_and(|value| {
- value.as_str().chars().count() < 6
- || [
- "[REDACTED]",
- "[REDACTED CREDENTIAL]",
- "[REDACTED PRIVATE KEY]",
- ]
- .contains(&value.as_str())
- }) {
- matched.to_owned()
- } else {
- let mut replacement = String::new();
- captures.expand(rule.replacement, &mut replacement);
- replacement
- }
+ replacement(rule, captures)
})
.into_owned()
})
}
+fn replacement(rule: &RedactionRule, captures: &regex::Captures<'_>) -> String {
+ let matched = captures.get(0).map_or("", |matched| matched.as_str());
+ if captures.get(3).is_some_and(|value| {
+ value.as_str().chars().count() < 6
+ || [
+ "[REDACTED]",
+ "[REDACTED CREDENTIAL]",
+ "[REDACTED PRIVATE KEY]",
+ ]
+ .contains(&value.as_str())
+ }) {
+ matched.to_owned()
+ } else {
+ let mut replacement = String::new();
+ captures.expand(rule.replacement, &mut replacement);
+ replacement
+ }
+}
+
+/// Run `rule` over `text` with invisible characters removed and replace each
+/// match's span in `text`, invisible characters inside it included.
+fn redact_through_invisibles(text: &str, rule: &RedactionRule) -> String {
+ let mut shadow = String::with_capacity(text.len());
+ let mut origin = Vec::with_capacity(text.len());
+ for (index, ch) in text.char_indices() {
+ if !is_default_ignorable(ch) {
+ shadow.push(ch);
+ origin.extend(index..index + ch.len_utf8());
+ }
+ }
+ let mut output = String::with_capacity(text.len());
+ let mut copied = 0;
+ for captures in rule.regex.captures_iter(&shadow) {
+ let Some(found) = captures.get(0) else {
+ continue;
+ };
+ let start = origin.get(found.start()).copied().unwrap_or(text.len());
+ let end = match found.end().checked_sub(1).and_then(|last| origin.get(last)) {
+ Some(last) if found.end() > found.start() => last + 1,
+ _ => start,
+ };
+ let replaced = replacement(rule, &captures);
+ // A match the rule keeps stays byte for byte, invisible characters
+ // and all.
+ if start < copied || replaced == found.as_str() {
+ continue;
+ }
+ output.push_str(&text[copied..start]);
+ output.push_str(&replaced);
+ copied = end;
+ }
+ output.push_str(&text[copied..]);
+ output
+}
+
+/// Tag characters outside an emoji flag sequence, the invisible math
+/// operators, and a byte-order mark after the start of the text: characters
+/// whose only realistic use in captured text is hiding it.
+const fn is_hidden_text_char(offset: usize, ch: char) -> bool {
+ matches!(ch, '\u{2061}'..='\u{2064}' | '\u{e0000}'..='\u{e007f}')
+ || (ch == '\u{feff}' && offset > 0)
+}
+
+/// Unicode `Default_Ignorable_Code_Point` (UAX #44), the same set the server
+/// sanitizer ignores while matching.
+const fn is_default_ignorable(ch: char) -> bool {
+ matches!(ch,
+ '\u{ad}' | '\u{34f}' | '\u{61c}' | '\u{115f}' | '\u{1160}' | '\u{17b4}' | '\u{17b5}'
+ | '\u{180b}'..='\u{180f}' | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}'
+ | '\u{2060}'..='\u{206f}' | '\u{3164}' | '\u{fe00}'..='\u{fe0f}' | '\u{feff}'
+ | '\u{ffa0}' | '\u{fff0}'..='\u{fff8}' | '\u{1bca0}'..='\u{1bca3}'
+ | '\u{1d173}'..='\u{1d17a}' | '\u{e0000}'..='\u{e0fff}')
+}
+
+/// Byte length of the tag run that completes an emoji flag sequence at the
+/// start of `rest` (tag letters, then the cancel tag), or 0.
+fn emoji_tag_run_len(rest: &str) -> usize {
+ let mut len = 0;
+ for ch in rest.chars() {
+ match ch {
+ '\u{e0020}'..='\u{e007e}' => len += ch.len_utf8(),
+ '\u{e007f}' if len > 0 => return len + ch.len_utf8(),
+ _ => return 0,
+ }
+ }
+ 0
+}
+
struct RedactionRule {
regex: Regex,
replacement: &'static str,
@@ -56,11 +153,20 @@ fn secret_patterns() -> &'static [RedactionRule] {
vec![
RedactionRule {
regex: Regex::new(
- r"(?s)-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----.*?-----END [A-Z0-9 ]*PRIVATE KEY-----",
+ r"(?s)-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----.*?-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----",
)
.unwrap(),
replacement: "[REDACTED PRIVATE KEY]",
},
+ // A block with no end line, once at least one key-body line
+ // follows the header; the line end after it is kept.
+ RedactionRule {
+ regex: Regex::new(
+ r#"-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:(?:\r?\n|(?:\\r)?\\n)[ \t]*(?:(?:Proc-Type|DEK-Info|Version|Comment|Hash):[^\r\n\\]*)?)*?(?:(?:\r?\n|(?:\\r)?\\n)[ \t]*[A-Za-z0-9+/=]{16,}[ \t]*)+(?P<end>\r?(?m:$)|["'\\…]|\.\.\.)"#,
+ )
+ .unwrap(),
+ replacement: "[REDACTED PRIVATE KEY]${end}",
+ },
RedactionRule {
regex: Regex::new(
r"\b(?:github_pat_[A-Za-z0-9_]{20,}|gh[pousr]_[A-Za-z0-9]{20,}|sk-[A-Za-z0-9_-]{16,}|AKIA[0-9A-Z]{16})\b",
diff --git a/companions/ai-memory-client/tests/suite/mod.rs b/companions/ai-memory-client/tests/suite/mod.rs
index a2f48f87..a924f419 100644
--- a/companions/ai-memory-client/tests/suite/mod.rs
+++ b/companions/ai-memory-client/tests/suite/mod.rs
@@ -353,3 +353,64 @@ fn redaction_marker_suffix_never_exempts_a_credential() {
assert_eq!(sanitize_external_text(&control), control);
}
}
+
+/// The vectors the server sanitizer runs too (`ai-memory-core::sanitize`), so
+/// the two copies cannot drift: credentials split by an invisible character,
+/// private-key blocks with and without an end line, and text that must pass
+/// through unchanged.
+#[test]
+fn shared_sanitizer_vectors_hold() {
+ let vectors: Value = serde_json::from_str(include_str!(
+ "../../../../tests/fixtures/sanitizer_vectors.json"
+ ))
+ .unwrap();
+ let strings = |key: &str| -> Vec<String> {
+ vectors[key]
+ .as_array()
+ .unwrap()
+ .iter()
+ .map(|value| value.as_str().unwrap().to_string())
+ .collect()
+ };
+ for secret in strings("credentials") {
+ let (head, tail) = secret.split_at(secret.len() / 2);
+ for invisible in strings("invisibles") {
+ let out = sanitize_external_text(&format!("key {head}{invisible}{tail} end"));
+ assert!(
+ out.contains("[REDACTED") && !out.contains(head) && !out.contains(tail),
+ "{invisible:?} inside {secret}: {out:?}"
+ );
+ assert!(out.starts_with("key ") && out.ends_with(" end"), "{out:?}");
+ }
+ }
+ let body_line = vectors["key_body_line"].as_str().unwrap();
+ for block in vectors["key_blocks"].as_array().unwrap() {
+ let input = block["input"].as_str().unwrap();
+ let out = sanitize_external_text(input);
+ assert!(
+ out.contains("[REDACTED PRIVATE KEY]") && !out.contains(body_line),
+ "{input:?} -> {out:?}"
+ );
+ for kept in block["kept"].as_array().unwrap() {
+ assert!(out.contains(kept.as_str().unwrap()), "{input:?} -> {out:?}");
+ }
+ }
+ for text in strings("unchanged") {
+ assert_eq!(sanitize_external_text(&text), text);
+ }
+}
+
+#[test]
+fn characters_that_hide_text_or_split_a_credential_are_removed() {
+ let hidden: String = "hidden note"
+ .chars()
+ .map(|c| char::from_u32(0xe0000 + u32::from(c)).unwrap())
+ .collect();
+ assert_eq!(
+ sanitize_external_text(&format!("visible{hidden} text")),
+ "visible text"
+ );
+ // A control character inside a credential no longer keeps it whole.
+ let out = sanitize_external_text("sk-leak-012345\u{0}6789abcdef0123");
+ assert_eq!(out, "[REDACTED CREDENTIAL]");
+}
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index 5dc8670e..7aa0651a 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -1484,6 +1484,22 @@ pub struct RunProfile {
pub env: BTreeMap<String, String>,
}
+/// A blank `[sanitize] allowlist` entry is a substring of every match, so it
+/// would exempt everything from redaction. The sanitizer ignores such entries
+/// too; dropping them here is where the operator hears about it.
+fn drop_blank_sanitize_allowlist_entries(sanitize: &mut ai_memory_core::SanitizeConfig) {
+ let before = sanitize.allowlist.len();
+ sanitize.allowlist.retain(|entry| !entry.trim().is_empty());
+ let dropped = before - sanitize.allowlist.len();
+ if dropped > 0 {
+ tracing::warn!(
+ dropped,
+ "ignoring blank [sanitize] allowlist entries: an empty entry would \
+ exempt every match from redaction"
+ );
+ }
+}
+
fn validate_run_profiles(run: &RunSettings) -> Result<()> {
const MAX_PROFILES: usize = 128;
const MAX_PROFILE_NAME: usize = 64;
@@ -2450,6 +2466,7 @@ impl Config {
config.runtime_env = runtime_env;
validate_run_profiles(&config.run)?;
+ drop_blank_sanitize_allowlist_entries(&mut config.sanitize);
// The idle auto-end tick turns `idle_end_after_hours` into a
// microsecond cutoff: a value above a year is a configuration
@@ -3746,6 +3763,28 @@ mod tests {
/// adopt it, or `serve` on the same data dir would start requiring that
/// key and accepting it as its bearer. Control: a key named by
/// `[auth].bearer_token_file` is adopted.
+ /// A blank allowlist entry would turn redaction off; loading drops it and
+ /// keeps the real entries.
+ #[test]
+ fn config_load_drops_blank_sanitize_allowlist_entries() {
+ let tmp = TempDir::new().unwrap();
+ let dd = tmp.path().join("data");
+ std::fs::create_dir_all(&dd).unwrap();
+ let config_path = dd.join("config.toml");
+ std::fs::write(
+ &config_path,
+ "[sanitize]\nallowlist = [\"\", \" \", \"KEEP_ME\"]\n",
+ )
+ .unwrap();
+ let cfg = Config::load_with_runtime_env(
+ Some(&config_path),
+ Some(dd.clone()),
+ RuntimeEnv::default(),
+ )
+ .unwrap();
+ assert_eq!(cfg.sanitize.allowlist, vec!["KEEP_ME".to_string()]);
+ }
+
#[test]
fn config_load_never_adopts_the_stored_hook_key() {
let tmp = TempDir::new().unwrap();
diff --git a/crates/ai-memory-core/src/sanitize.rs b/crates/ai-memory-core/src/sanitize.rs
index 43050ae9..8dce3c67 100644
--- a/crates/ai-memory-core/src/sanitize.rs
+++ b/crates/ai-memory-core/src/sanitize.rs
@@ -27,9 +27,27 @@
//! `[sanitize].allowlist` — the allowlist is checked *per match*, so a
//! pattern still runs but an allowlisted span survives unchanged.
//!
+//! Private keys come first: complete PEM and PGP armor blocks, and a block
+//! cut off before its end line (`head` of a key file, a truncated tool
+//! result) as long as at least one key-body line follows the header. A
+//! sentence that only names a header has no body line and survives. The
+//! allowlist never exempts a private key.
+//!
+//! Invisible formatting characters (Unicode `Default_Ignorable_Code_Point`:
+//! zero-width spaces and joiners, soft hyphens, variation selectors, …) do
+//! not hide a credential from the patterns. When the text carries any, each
+//! pattern also runs over a copy with them removed and the match is mapped
+//! back to the original span, invisible characters included. Text that
+//! matches nothing is stored byte for byte, so emoji sequences and scripts
+//! that need joiners (Persian, the Indic scripts) are never altered.
+//!
//! Terminal escape sequences, NUL and bidi overrides are stripped rather
//! than redacted: they are not secrets, but stored text is replayed to a
-//! terminal by the CLI, and a NUL makes the markdown file binary.
+//! terminal by the CLI, and a NUL makes the markdown file binary. Tag
+//! characters outside an emoji flag sequence, a byte-order mark after the
+//! start of the text, and the invisible math operators are stripped too:
+//! captured prose has no use for them, and they can carry text a reader
+//! never sees.
//!
//! ## What we deliberately do not catch
//!
@@ -37,6 +55,9 @@
//! be safely redacted without knowing their structure — too many false
//! positives. Operators who care about that level of paranoia should
//! add a custom pattern via `extra_patterns`.
+//!
+//! Look-alike characters (full-width `sk-…`, other confusables) are not
+//! folded: matching does not apply NFKC normalization.
use std::sync::Arc;
@@ -64,10 +85,107 @@ const fn is_stripped_control(c: char) -> bool {
| '\u{7f}'..='\u{9f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}')
}
+/// Characters whose only realistic use in captured text is hiding it: the
+/// tag block (also used by emoji flag sequences, which [`emoji_tag_run_len`]
+/// keeps), the invisible math operators, and a byte-order mark anywhere but
+/// the start of the text (`U+2060` replaced it as a zero-width no-break
+/// space).
+const fn is_hidden_text_char(offset: usize, c: char) -> bool {
+ matches!(c, '\u{2061}'..='\u{2064}' | '\u{e0000}'..='\u{e007f}')
+ || (c == '\u{feff}' && offset > 0)
+}
+
+/// Unicode `Default_Ignorable_Code_Point` (UAX #44, `DerivedCoreProperties`):
+/// characters that render as nothing. Matching ignores them; storage keeps
+/// them unless [`is_stripped_control`] or [`is_hidden_text_char`] says
+/// otherwise.
+const fn is_default_ignorable(c: char) -> bool {
+ matches!(c,
+ '\u{ad}' | '\u{34f}' | '\u{61c}' | '\u{115f}' | '\u{1160}' | '\u{17b4}' | '\u{17b5}'
+ | '\u{180b}'..='\u{180f}' | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}'
+ | '\u{2060}'..='\u{206f}' | '\u{3164}' | '\u{fe00}'..='\u{fe0f}' | '\u{feff}'
+ | '\u{ffa0}' | '\u{fff0}'..='\u{fff8}' | '\u{1bca0}'..='\u{1bca3}'
+ | '\u{1d173}'..='\u{1d17a}' | '\u{e0000}'..='\u{e0fff}')
+}
+
+/// Byte length of the tag characters that close an emoji flag sequence
+/// (`U+1F3F4`, tag letters, `U+E007F` cancel tag) at the start of `rest`, or
+/// 0 when `rest` does not start with a well-formed run.
+fn emoji_tag_run_len(rest: &str) -> usize {
+ let mut len = 0;
+ for c in rest.chars() {
+ match c {
+ '\u{e0020}'..='\u{e007e}' => len += c.len_utf8(),
+ '\u{e007f}' if len > 0 => return len + c.len_utf8(),
+ _ => return 0,
+ }
+ }
+ 0
+}
+
+/// `text` with every [`is_default_ignorable`] character removed, plus the
+/// original byte offset of each byte kept, so a match found in the shadow
+/// maps back to the span it covers in `text`.
+struct Shadow {
+ text: String,
+ origin: Vec<usize>,
+}
+
+impl Shadow {
+ fn new(text: &str) -> Self {
+ let mut shadow = String::with_capacity(text.len());
+ let mut origin = Vec::with_capacity(text.len());
+ for (index, c) in text.char_indices() {
+ if is_default_ignorable(c) {
+ continue;
+ }
+ shadow.push(c);
+ origin.extend(index..index + c.len_utf8());
+ }
+ Self {
+ text: shadow,
+ origin,
+ }
+ }
+
+ /// The span of `text` a shadow match `start..end` covers: from its first
+ /// visible character through its last, invisible characters between them
+ /// included.
+ fn original_span(&self, text_len: usize, start: usize, end: usize) -> (usize, usize) {
+ let from = self.origin.get(start).copied().unwrap_or(text_len);
+ let to = match end.checked_sub(1).and_then(|last| self.origin.get(last)) {
+ Some(last) if end > start => last + 1,
+ _ => from,
+ };
+ (from, to)
+ }
+}
+
/// Compile-time list of redaction patterns. Order is intentional:
/// more-specific patterns first. False positives are acceptable —
/// better to redact a stray hash than to leak a credential.
const BUILTIN_PATTERNS: &[(&str, &str)] = &[
+ // Private keys run first, while their base64 body lines are still intact
+ // for the token rules below to leave alone.
+ //
+ // A complete PEM or PGP armor block (`… PRIVATE KEY BLOCK-----` is the PGP
+ // form) — multi-line, lazy match.
+ (
+ r"(?s)-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----.*?-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----",
+ "private_key",
+ ),
+ // A block with no end line: the head of a key file, or a tool result cut
+ // short before the hook saw it. It needs at least one key-body line (16+
+ // base64 characters filling the rest of the line) after the header,
+ // optionally behind armor headers (`Proc-Type:`, `Version:`, …) and blank
+ // lines, so prose that only names the header keeps it. Lines break on a
+ // real newline or a JSON-escaped one. The line end after the last body
+ // line sits outside the `secret` group: it has to be there, but it is not
+ // redacted.
+ (
+ r#"(?P<secret>-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----(?:(?:\r?\n|(?:\\r)?\\n)[ \t]*(?:(?:Proc-Type|DEK-Info|Version|Comment|Hash):[^\r\n\\]*)?)*?(?:(?:\r?\n|(?:\\r)?\\n)[ \t]*[A-Za-z0-9+/=]{16,}[ \t]*)+)(?:\r?(?m:$)|["'\\…]|\.\.\.)"#,
+ "private_key",
+ ),
// Bearer-style tokens.
(r#"(?i)bearer\s+[A-Za-z0-9._\-+/=]{16,}"#, "bearer_token"),
// Vendor-prefixed API keys.
@@ -148,11 +266,6 @@ const BUILTIN_PATTERNS: &[(&str, &str)] = &[
// credential from a short encoded fragment; config that long is pasted as
// JSON, not base64.
(r"eyJ[A-Za-z0-9_\-+/]{40,}={0,2}", "base64_json_token"),
- // PEM private key blocks — multi-line, lazy match.
- (
- r"(?s)-----BEGIN [A-Z ]*PRIVATE KEY-----.*?-----END [A-Z ]*PRIVATE KEY-----",
- "private_key",
- ),
// URL-embedded credentials: scheme://user:pass@host.
(
r"[a-zA-Z][a-zA-Z0-9+\-.]*://[^:/\s]+:[^@\s]+@[^\s]+",
@@ -238,11 +351,23 @@ pub struct Sanitizer {
}
struct SanitizerInner {
- patterns: Vec<(Regex, &'static str)>,
+ patterns: Vec<Pattern>,
allowlist: Vec<String>,
escapes: Regex,
}
+struct Pattern {
+ re: Regex,
+ label: &'static str,
+ /// A built-in rule whose match must include context that is not itself
+ /// secret (a line end) redacts only its `secret` capture group.
+ secret_group: bool,
+}
+
+/// The one label the allowlist never exempts: no project codename belongs
+/// inside a private key, and a short entry would otherwise match key bodies.
+const PRIVATE_KEY_LABEL: &str = "private_key";
+
impl std::fmt::Debug for Sanitizer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sanitizer")
@@ -277,15 +402,32 @@ impl Sanitizer {
pub fn new(cfg: &SanitizeConfig) -> Result<Self, regex::Error> {
let mut patterns = Vec::with_capacity(BUILTIN_PATTERNS.len() + cfg.extra_patterns.len());
for (p, label) in BUILTIN_PATTERNS {
- patterns.push((Regex::new(p)?, *label));
+ let re = Regex::new(p)?;
+ let secret_group = re.capture_names().any(|name| name == Some("secret"));
+ patterns.push(Pattern {
+ re,
+ label: *label,
+ secret_group,
+ });
}
for p in &cfg.extra_patterns {
- patterns.push((Regex::new(p)?, "custom"));
+ patterns.push(Pattern {
+ re: Regex::new(p)?,
+ label: "custom",
+ secret_group: false,
+ });
}
Ok(Self {
inner: Arc::new(SanitizerInner {
patterns,
- allowlist: cfg.allowlist.clone(),
+ // A blank entry is a substring of every match and would turn
+ // redaction off entirely. `Config::load` warns about it.
+ allowlist: cfg
+ .allowlist
+ .iter()
+ .filter(|entry| !entry.trim().is_empty())
+ .cloned()
+ .collect(),
escapes: Regex::new(ESCAPE_SEQUENCES)?,
}),
})
@@ -306,39 +448,118 @@ impl Sanitizer {
// Before the redaction passes: an escape inside a secret would
// otherwise split it out of reach of every pattern below.
let mut out = self.strip_control(input);
- for (re, label) in &self.inner.patterns {
- out = re
- .replace_all(&out, |caps: &regex::Captures<'_>| {
- let m = caps.get(0).map(|m| m.as_str()).unwrap_or_default();
- if self.inner.allowlist.iter().any(|a| m.contains(a)) {
- m.to_string()
- } else {
- debug!(pattern = re.as_str(), "sanitize: redacted match");
- format!("[REDACTED:{label}]")
- }
- })
- .into_owned();
+ for pattern in &self.inner.patterns {
+ // When invisible characters are present, the pass that looks
+ // through them goes first, so a split secret is redacted whole
+ // rather than losing only the part before the split. The plain
+ // pass then still finds a match that needs an invisible character
+ // as its word boundary.
+ if out.contains(is_default_ignorable) {
+ let shadow = Shadow::new(&out);
+ out = self.redact(&out, Some(&shadow), pattern);
+ }
+ out = self.redact(&out, None, pattern);
+ }
+ out
+ }
+
+ /// One pattern over `text`, or over `shadow` with each match mapped back
+ /// to the span of `text` it covers.
+ fn redact(&self, text: &str, shadow: Option<&Shadow>, pattern: &Pattern) -> String {
+ let view = shadow.map_or(text, |shadow| shadow.text.as_str());
+ let spans: Vec<regex::Match<'_>> = if pattern.secret_group {
+ pattern
+ .re
+ .captures_iter(view)
+ .filter_map(|caps| caps.name("secret").or_else(|| caps.get(0)))
+ .collect()
+ } else {
+ pattern.re.find_iter(view).collect()
+ };
+ let mut out = String::new();
+ let mut copied = 0;
+ let mut redacted = false;
+ for m in spans {
+ if self.allowlisted(pattern.label, m.as_str()) {
+ continue;
+ }
+ let (start, end) = match shadow {
+ Some(shadow) => shadow.original_span(text.len(), m.start(), m.end()),
+ None => (m.start(), m.end()),
+ };
+ if start < copied {
+ continue;
+ }
+ debug!(pattern = pattern.re.as_str(), "sanitize: redacted match");
+ out.push_str(&text[copied..start]);
+ out.push_str("[REDACTED:");
+ out.push_str(pattern.label);
+ out.push(']');
+ copied = end;
+ redacted = true;
}
+ if !redacted {
+ return text.to_string();
+ }
+ out.push_str(&text[copied..]);
out
}
- /// Drop terminal escape sequences and the control characters that have
- /// no place in a page.
+ /// Whether an allowlist entry exempts `matched`. Compared with invisible
+ /// characters removed, so they neither break nor fake an entry.
+ fn allowlisted(&self, label: &str, matched: &str) -> bool {
+ if label == PRIVATE_KEY_LABEL || self.inner.allowlist.is_empty() {
+ return false;
+ }
+ let visible: String;
+ let matched = if matched.contains(is_default_ignorable) {
+ visible = matched
+ .chars()
+ .filter(|c| !is_default_ignorable(*c))
+ .collect();
+ visible.as_str()
+ } else {
+ matched
+ };
+ self.inner
+ .allowlist
+ .iter()
+ .any(|entry| matched.contains(entry.as_str()))
+ }
+
+ /// Drop terminal escape sequences, the control characters that have no
+ /// place in a page, and characters that hide text.
///
/// Captured text is replayed to a terminal by `ai-memory read-page` and
/// `search`, where an escape rewrites the screen or the window title, and
/// a bidi override reverses what the reader sees. A NUL additionally
/// makes the markdown file binary, which costs it `grep` and git diffs.
+ /// Tag characters spell out text no reader sees; the ones that finish an
+ /// emoji flag (England, Scotland, Wales) stay.
fn strip_control(&self, input: &str) -> String {
let stripped = self.inner.escapes.replace_all(input, "");
- if stripped.contains(is_stripped_control) {
- stripped
- .chars()
- .filter(|c| !is_stripped_control(*c))
- .collect()
- } else {
- stripped.into_owned()
+ if !stripped
+ .char_indices()
+ .any(|(offset, c)| is_stripped_control(c) || is_hidden_text_char(offset, c))
+ {
+ return stripped.into_owned();
}
+ let mut out = String::with_capacity(stripped.len());
+ let mut offset = 0;
+ while let Some(c) = stripped[offset..].chars().next() {
+ let next = offset + c.len_utf8();
+ if c == '\u{1f3f4}' {
+ let tags = emoji_tag_run_len(&stripped[next..]);
+ out.push_str(&stripped[offset..next + tags]);
+ offset = next + tags;
+ continue;
+ }
+ if !is_stripped_control(c) && !is_hidden_text_char(offset, c) {
+ out.push(c);
+ }
+ offset = next;
+ }
+ out
}
}
@@ -1195,6 +1416,189 @@ mod tests {
);
}
+ /// One line of a fake key body. Short on purpose: a complete fake block
+ /// stays under the length the secret scanner's private-key rule needs.
+ const FAKE_KEY_LINE: &str = "b3BlbnNzaC1rZXktdjEAAAAA";
+
+ /// The vectors the client companion's scrub runs too
+ /// (`companions/ai-memory-client`), so the two copies cannot drift.
+ #[test]
+ fn shared_sanitizer_vectors_hold() {
+ let vectors: serde_json::Value = serde_json::from_str(include_str!(
+ "../../../tests/fixtures/sanitizer_vectors.json"
+ ))
+ .unwrap();
+ let strings = |key: &str| -> Vec<String> {
+ vectors[key]
+ .as_array()
+ .unwrap()
+ .iter()
+ .map(|value| value.as_str().unwrap().to_string())
+ .collect()
+ };
+ for secret in strings("credentials") {
+ let (head, tail) = secret.split_at(secret.len() / 2);
+ for invisible in strings("invisibles") {
+ let out = s().scrub(&format!("key {head}{invisible}{tail} end"));
+ assert!(
+ out.contains("[REDACTED:") && !out.contains(head) && !out.contains(tail),
+ "{invisible:?} inside {secret}: {out:?}"
+ );
+ assert!(out.starts_with("key ") && out.ends_with(" end"), "{out:?}");
+ }
+ }
+ let body_line = vectors["key_body_line"].as_str().unwrap();
+ for block in vectors["key_blocks"].as_array().unwrap() {
+ let input = block["input"].as_str().unwrap();
+ let out = s().scrub(input);
+ assert!(
+ out.contains("[REDACTED:private_key]") && !out.contains(body_line),
+ "{input:?} -> {out:?}"
+ );
+ for kept in block["kept"].as_array().unwrap() {
+ assert!(out.contains(kept.as_str().unwrap()), "{input:?} -> {out:?}");
+ }
+ }
+ for text in strings("unchanged") {
+ assert_eq!(s().scrub(&text), text);
+ }
+ }
+
+ #[test]
+ fn invisible_character_inside_an_operator_pattern_match_is_redacted() {
+ let sn = Sanitizer::new(&SanitizeConfig {
+ extra_patterns: vec![r"CANARY-[0-9]{6}".to_string()],
+ allowlist: vec![],
+ })
+ .unwrap();
+ assert_eq!(
+ sn.scrub("found CANARY-12\u{200b}3456 here"),
+ "found [REDACTED:custom] here"
+ );
+ }
+
+ /// An invisible character neither breaks an allowlist entry (the span is
+ /// still exempt) nor lets a secret that is not allowlisted through.
+ #[test]
+ fn invisible_characters_neither_break_nor_bypass_the_allowlist() {
+ let sn = Sanitizer::new(&SanitizeConfig {
+ extra_patterns: vec![],
+ allowlist: vec!["KEEP_ME".to_string()],
+ })
+ .unwrap();
+ for kept in [
+ "SERVICE_TOKEN=KEEP_ME_visible_value",
+ "SERVICE_TOKEN=KEEP\u{200b}_ME_visible_value",
+ ] {
+ assert_eq!(sn.scrub(kept), kept, "allowlisted span must survive");
+ }
+ let out = sn.scrub("SERVICE_TOKEN=s3cretva\u{200b}lue0123");
+ assert!(
+ out.contains("[REDACTED:env_secret]") && !out.contains("lue0123"),
+ "a secret without the allowlist entry must still be redacted: {out:?}"
+ );
+ }
+
+ #[test]
+ fn characters_that_hide_text_are_stripped() {
+ let hidden: String = "hidden note"
+ .chars()
+ .map(|c| char::from_u32(0xe0000 + u32::from(c)).unwrap())
+ .collect();
+ let out = s().scrub(&format!("visible{hidden} text"));
+ assert_eq!(out, "visible text");
+
+ assert_eq!(s().scrub("a\u{2062}b\u{feff}c"), "abc");
+ // A flag start with no cancel tag is not a flag: its tags go.
+ assert_eq!(s().scrub("\u{1f3f4}\u{e0067}\u{e0062} x"), "\u{1f3f4} x");
+ }
+
+ #[test]
+ fn pgp_private_key_block_is_redacted() {
+ let block = format!(
+ "before\n-----BEGIN PGP PRIVATE KEY BLOCK-----\nVersion: 1\n\n\
+ {FAKE_KEY_LINE}\n-----END PGP PRIVATE KEY BLOCK-----\nafter"
+ );
+ assert_eq!(s().scrub(&block), "before\n[REDACTED:private_key]\nafter");
+ }
+
+ #[test]
+ fn private_key_block_without_an_end_line_is_redacted() {
+ let line = FAKE_KEY_LINE;
+ for (input, expected) in [
+ (
+ format!("$ head -3 key.pem\n-----BEGIN PRIVATE KEY-----\n{line}\n{line}\n$ ls"),
+ "$ head -3 key.pem\n[REDACTED:private_key]\n$ ls".to_string(),
+ ),
+ (
+ format!("-----BEGIN RSA PRIVATE KEY-----\r\n{line}\r\n{line}\r\n"),
+ "[REDACTED:private_key]\r\n".to_string(),
+ ),
+ (
+ format!(r#"{{"stdout":"-----BEGIN OPENSSH PRIVATE KEY-----\n{line}\n{line}"}}"#),
+ r#"{"stdout":"[REDACTED:private_key]"}"#.to_string(),
+ ),
+ (
+ format!(
+ " -----BEGIN EC PRIVATE KEY-----\n {line}\n {}",
+ &line[..20]
+ ),
+ " [REDACTED:private_key]".to_string(),
+ ),
+ (
+ format!(
+ "-----BEGIN RSA PRIVATE KEY-----\nProc-Type: 4,ENCRYPTED\n\
+ DEK-Info: AES-128-CBC,0123456789ABCDEF\n\n{line}\n"
+ ),
+ "[REDACTED:private_key]\n".to_string(),
+ ),
+ ] {
+ assert_eq!(s().scrub(&input), expected, "input: {input:?}");
+ }
+ }
+
+ /// A key body line can contain `eyJ…`, which a token rule would redact
+ /// in part, leaving the rest of the key unrecognizable to the key rules.
+ #[test]
+ fn key_rules_run_before_token_rules_can_split_a_key_body() {
+ let first = format!("MIIEeyJ{}", "A".repeat(50));
+ let input = format!("-----BEGIN PRIVATE KEY-----\n{first}\n{FAKE_KEY_LINE}\n");
+ assert_eq!(s().scrub(&input), "[REDACTED:private_key]\n");
+ }
+
+ #[test]
+ fn allowlist_never_exempts_a_private_key() {
+ let sn = Sanitizer::new(&SanitizeConfig {
+ extra_patterns: vec![],
+ allowlist: vec!["b3Bl".to_string()],
+ })
+ .unwrap();
+ let complete =
+ format!("-----BEGIN PRIVATE KEY-----\n{FAKE_KEY_LINE}\n-----END PRIVATE KEY-----");
+ assert_eq!(sn.scrub(&complete), "[REDACTED:private_key]");
+ let unterminated = format!("-----BEGIN PRIVATE KEY-----\n{FAKE_KEY_LINE}\n");
+ assert_eq!(sn.scrub(&unterminated), "[REDACTED:private_key]\n");
+ }
+
+ #[test]
+ fn blank_allowlist_entries_are_ignored() {
+ let input = "token=ghp_FAKEfakeFAKEfakeFAKEfake012345678";
+ let sn = Sanitizer::new(&SanitizeConfig {
+ extra_patterns: vec![],
+ allowlist: vec![String::new(), " ".to_string()],
+ })
+ .unwrap();
+ assert_eq!(sn.scrub(input), "token=[REDACTED:github_token]");
+
+ // Control: a real entry still exempts its span.
+ let sn = Sanitizer::new(&SanitizeConfig {
+ extra_patterns: vec![],
+ allowlist: vec!["ghp_FAKE".to_string()],
+ })
+ .unwrap();
+ assert_eq!(sn.scrub(input), input);
+ }
+
/// JSON is the shape most captured tool payloads arrive in, and the
/// quote before the value used to put it outside the value class, so the
/// same secret was redacted in YAML and stored verbatim in JSON.
diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rs
index dbdc3639..da623759 100644
--- a/crates/ai-memory-hooks/src/router.rs
+++ b/crates/ai-memory-hooks/src/router.rs
@@ -20731,6 +20731,48 @@ pub(crate) mod tests {
);
}
+ /// End to end through `/hook`: a credential with an invisible formatting
+ /// character inside it reaches the store redacted, in the title and the
+ /// body, and the prose around it is kept.
+ #[tokio::test]
+ async fn user_prompt_secret_split_by_an_invisible_character_is_redacted() {
+ let tmp = TempDir::new().unwrap();
+ let state = make_state(&tmp).await;
+ // FAKE shape: `sk-` plus 26 characters, split after the first 10.
+ let (head, tail) = ("sk-FAKEfakeFA", "KEfake0123456789");
+ let prompt = format!("use {head}\u{200B}{tail} for the call");
+ let sid = SessionId::new();
+ let env = HookEnvelope::from_query_and_body(
+ HookQuery {
+ event: "user-prompt".into(),
+ agent: Some("claude-code".into()),
+ ..Default::default()
+ },
+ serde_json::json!({
+ "session_id": sid.to_string(),
+ "cwd": "/repo",
+ "prompt": prompt,
+ }),
+ );
+ process(&state, env, None, Vec::new()).await.unwrap();
+
+ let observations = state.reader.observations_for_session(sid).await.unwrap();
+ let obs = observations
+ .iter()
+ .find(|o| o.kind == ObservationKind::UserPrompt)
+ .expect("user prompt observation was recorded");
+ for stored in [&obs.title, &obs.body] {
+ assert!(
+ !stored.contains(head) && !stored.contains(tail),
+ "a fragment of the split key was stored: {stored:?}"
+ );
+ assert!(
+ stored.contains("[REDACTED:api_key]") && stored.contains("for the call"),
+ "the key must be redacted and the prose kept: {stored:?}"
+ );
+ }
+ }
+
/// Shared shape for the #1114 body-side regressions: a secret straddling
/// a per-event byte cap used to be cut in half at excerpt extraction,
/// before the sanitizer ever ran, so the surviving prefix was too short
diff --git a/docs/security-boundaries.md b/docs/security-boundaries.md
index 2d435ee8..bc6f453d 100644
--- a/docs/security-boundaries.md
+++ b/docs/security-boundaries.md
@@ -68,7 +68,7 @@ means the boundary is not built yet.
| 6b | Native `_meta` routing coordinate is routing-only (never grants identity) | `ai-memory-mcp/src/server.rs` `attach_native_session` reads only `_meta["ai.opencode/sessionID"]` into `ActorKey.session_id` (trimmed, non-empty-string-validated); `user` still comes from the authenticated `ActorContext`, so a forged `_meta` cannot cross a user/scope boundary — it only selects a session slot behind the existing `active_project` guard | `server.rs` `native_session_metadata_is_routing_only_and_preserves_precedence`, `native_session_metadata_validates_strings_without_granting_identity`; `autoscope_multiuser.rs` `native_metadata_routes_concurrent_reads_and_writes_to_hook_workspaces` (#864) | STRONG |
| 6c | Per-user slot namespaces stay per operator (`[slots] per_user`) | `ai-memory-mcp/src/server.rs` `place_slot_mutation`, shared by `place_slot_write` (`memory_write_page`) and `place_slot_delete` (`memory_delete_page`): a non-admin's generic `_slots/<name>.md` is redirected to their own `_slots/u-<operator>/` namespace, a path inside another operator's namespace is refused, admins bypass, and nothing changes with the flag off (#1083) | `slot_identity.rs` `oidc_operator_cannot_delete_another_operators_personal_slot`, `oidc_operator_deleting_shared_slot_path_deletes_their_personal_slot` (shared slot survives), legitimate controls `oidc_operator_can_delete_their_own_personal_slot_explicitly`, `admin_can_delete_shared_and_personal_slots` | STRONG |
| 6d | MCP page restore is admin-only and cannot bypass the write boundary | `ai-memory-mcp/src/server.rs` `memory_restore_page`: `require_admin_capability` gates listing and restoring alike (git keeps text a root deleted, purged or let expire), the same rung as `/admin/restore-page`; scope resolves once with the no-create mutation resolver (`effective_ids_for_mutation_args_with_actor`), so a restricted project needs a write grant even past the gate; a restore writes through `write_page_in_scope` (the `memory_write_page` body) into that resolved scope, so the sanitizer, admission chain, slot placement, attribution and supersession apply, and `checkpoint_write_metadata` re-validates the checkpoint's metadata | `tests/suite/restore_page_authz.rs`: `a_db_user_with_a_write_grant_cannot_list_or_restore` (an `aim_` user who can write the project is refused for both; root control restores), `a_foreign_restricted_project_is_refused_without_a_grant` (no grant on a restricted project, plus an unknown bearer, refused without leaking the version; root control), `a_personal_slot_restores_to_its_exact_path_and_only_for_root` (the slot owner is refused; root restores the exact `_slots/u-…` path, never the shared slot); `server.rs` `memory_restore_page_refuses_unknown_explicit_scope_and_bad_rev`, `memory_restore_page_round_trips_frontmatter_metadata`, legitimate control `memory_restore_page_lists_checkpoints_and_brings_back_a_deleted_page` | STRONG. Bite checks: removing the admin gate fails the write-grant and slot tests; removing it and also resolving without the viewer fails the restricted-project test; dropping the checkpoint metadata fails the round-trip test |
-| 7 | Sanitizer trust boundary (invariant #6) | `ai-memory-core/src/sanitize.rs` `Sanitized<T>` (private field, only `sanitize()` ctor); `Sanitized::new` scrubs the title *before* the 80-char display cap; `ai-memory-mcp/src/server.rs` `sanitize_feedback_reason` scrubs the reason *before* the 500-char cap; `ai-memory-hooks/src/router.rs` `durable_body` scrubs the full excerpt *before* the per-event body caps (`payload::durable_body_cap`), which no longer run at excerpt extraction; `ai-memory-hooks/src/payload.rs` `cap_object_fields` scrubs with the built-in sanitizer *before* the client spool cap; `WriterHandle::insert_observation` requires `Sanitized`; `ops` crate-private | Structural (compile-time) + `store/src/lib.rs` `insert_observation_boundary_scrubs_before_disk` + sanitizer scrub unit tests + `ai-memory-core::sanitize` `title_is_scrubbed_before_the_80_char_cap_runs` + `ai-memory-hooks::router` `issue_980_user_prompt_title_is_sanitized_before_truncated` (a secret straddling the 80-char boundary is redacted, not stored truncated; #980) + `ai-memory-mcp::server` `feedback_reason_is_scrubbed_before_the_500_char_cap` (#1109) + `ai-memory-hooks::router` `issue_1114_user_prompt_body_is_sanitized_before_the_16kib_cap`, `issue_1114_post_compaction_body_is_sanitized_before_the_16kib_cap`, `issue_1114_tool_body_is_sanitized_before_the_2kb_cap`, `tool_body_cap_keeps_the_utf8_boundary` + `ai-memory-hooks::payload` `client_body_cap_scrubs_before_truncating` (each places a secret straddling the cap and asserts a `[REDACTED:…]` marker survives with no raw fragment; #1114) | STRONG (structural) |
+| 7 | Sanitizer trust boundary (invariant #6) | `ai-memory-core/src/sanitize.rs` `Sanitized<T>` (private field, only `sanitize()` ctor); `Sanitized::new` scrubs the title *before* the 80-char display cap; `ai-memory-mcp/src/server.rs` `sanitize_feedback_reason` scrubs the reason *before* the 500-char cap; `ai-memory-hooks/src/router.rs` `durable_body` scrubs the full excerpt *before* the per-event body caps (`payload::durable_body_cap`), which no longer run at excerpt extraction; `ai-memory-hooks/src/payload.rs` `cap_object_fields` scrubs with the built-in sanitizer *before* the client spool cap; `WriterHandle::insert_observation` requires `Sanitized`; `ops` crate-private; `Sanitizer::scrub` matches each pattern through invisible formatting characters (a `Shadow` copy without `Default_Ignorable_Code_Point` characters, each match mapped back to its original span) and stores non-matching text byte for byte, `strip_control` removes stray tag characters (an emoji flag's tag run is kept), a non-leading U+FEFF and U+2061–2064, the private-key rules run first and cover PGP armor and a block with no end line once a key-body line follows the header, the allowlist never exempts `private_key` and ignores blank entries (`ai-memory-cli/src/config.rs` `drop_blank_sanitize_allowlist_entries` drops and warns at load); `companions/ai-memory-client/src/scrub.rs` `sanitize_external_text` mirrors the invisible-character, hidden-text and key rules | Structural (compile-time) + `store/src/lib.rs` `insert_observation_boundary_scrubs_before_disk` + sanitizer scrub unit tests + `ai-memory-core::sanitize` `title_is_scrubbed_before_the_80_char_cap_runs` + `ai-memory-hooks::router` `issue_980_user_prompt_title_is_sanitized_before_truncated` (a secret straddling the 80-char boundary is redacted, not stored truncated; #980) + `ai-memory-mcp::server` `feedback_reason_is_scrubbed_before_the_500_char_cap` (#1109) + `ai-memory-hooks::router` `issue_1114_user_prompt_body_is_sanitized_before_the_16kib_cap`, `issue_1114_post_compaction_body_is_sanitized_before_the_16kib_cap`, `issue_1114_tool_body_is_sanitized_before_the_2kb_cap`, `tool_body_cap_keeps_the_utf8_boundary` + `ai-memory-hooks::payload` `client_body_cap_scrubs_before_truncating` (each places a secret straddling the cap and asserts a `[REDACTED:…]` marker survives with no raw fragment; #1114) + `ai-memory-core::sanitize` `shared_sanitizer_vectors_hold` (credentials split by each invisible-character family, complete, PGP and end-line-less key blocks, and controls that must stay byte-identical: emoji ZWJ sequences, keycaps, a subdivision flag, Persian ZWNJ, Indic ZWJ, Thai ZWSP, Hebrew RLM, a soft hyphen, prose and code naming a key header; the same `tests/fixtures/sanitizer_vectors.json` drives `ai-memory-client` `shared_sanitizer_vectors_hold`), `invisible_character_inside_an_operator_pattern_match_is_redacted`, `invisible_characters_neither_break_nor_bypass_the_allowlist`, `characters_that_hide_text_are_stripped`, `pgp_private_key_block_is_redacted`, `private_key_block_without_an_end_line_is_redacted`, `key_rules_run_before_token_rules_can_split_a_key_body`, `allowlist_never_exempts_a_private_key`, `blank_allowlist_entries_are_ignored` + `ai-memory-hooks::router` `user_prompt_secret_split_by_an_invisible_character_is_redacted` + `ai-memory-cli::config` `config_load_drops_blank_sanitize_allowlist_entries` + `ai-memory-client` `characters_that_hide_text_or_split_a_credential_are_removed` (GHSA-jpcg-w5c5-wcv6) | STRONG (structural) |
| 7a | Eval observations: server provenance and reason privacy | `ai-memory-consolidate/src/auto_improve.rs` constructs private results from the existing execution, ignores extra model/scorer result fields and all eval results on report deserialization, hashes the evaluated request/bodies, and scrubs reasons before the 2,048-character cap and pending persistence; changed bodies cannot receive the earlier result | `auto_improve::tests::structured_eval_records_server_observation`; `tests/suite/eval_results.rs` `eval_reason_is_sanitized_before_pending_storage_with_legitimate_control` attempts forged deserialized reports and IDs/digests and malicious reasons with secrets/control characters, checks report and stored evidence before sidecar rendering, and preserves a legitimate reason and external evidence without independent results; fails with reason scrubbing or the report deserialization guard removed | STRONG |
| 7b | New public page metadata is bounded and cannot supply scope, author or admission controls; admin kind retains its legacy adapter | `ai-memory-core/src/page.rs` `PageWriteMetadata::into_frontmatter`; MCP validates all four fields; admin consumes legacy kind explicitly and shares entities/abstract/relations validation before scope creation; both use `Wiki::write_page` for sanitization/admission/authenticated attribution | `admin_write_page.rs` `write_page_metadata_rejects_invalid_payload_before_scope_creation` (invalid shapes, unknown relation kinds, raw-input over-limit values, untrimmed relation scopes and escaping paths refused; legitimate control admitted), `write_page_metadata_preserves_admin_auth_and_sanitization` (anonymous/DB-user refused, root control admitted with scrubbed metadata and authenticated attribution); `server.rs` `memory_write_page_metadata_rejects_invalid_before_scope_creation`, `memory_write_page_metadata_preserves_scope_author_and_sanitization` (foreign user denied; writer control admitted, forged frontmatter/author ignored, secrets scrubbed, invalid rewrite leaves disk and SQL latest unchanged); core `page_write_metadata_scope_components_refuse_surrounding_whitespace` and `page_write_metadata_limits_apply_before_trimming_and_normalization`; admin `write_page_metadata_admin_legacy_kind_matches_base` compares long/control-character kind behavior with the base adapter; entity-bound refusal bite-checked with its validation removed | STRONG |
| 7c | A compaction checkpoint cannot bypass `consolidate` admission by deferring its provider rewrite (#1229) | `ai-memory-hooks/src/router.rs` `write_checkpoint_page` gives the PreCompact / PostCompaction rule-based write the `consolidate` admission operation whenever a consolidator is configured, and `process_authorized` queues the durable rewrite only after that write succeeds; the worker's rewrite runs admission again through `Consolidator::consolidate_session` | `router.rs` `checkpoint_admission_refusal_writes_no_page_and_queues_no_rewrite` (a policy refusing `consolidate` leaves no page and no queued job; control: a policy gating only `handoff_begin` writes the page and queues the job), `checkpoint_preserves_the_consolidate_admission_operation` (the hook path sends exactly one `consolidate` admission call and never calls the provider); both bite-checked with the admission context removed | STRONG |
diff --git a/tests/fixtures/sanitizer_vectors.json b/tests/fixtures/sanitizer_vectors.json
new file mode 100644
index 00000000..68a53cb2
--- /dev/null
+++ b/tests/fixtures/sanitizer_vectors.json
@@ -0,0 +1,83 @@
+{
+ "_comment": "Shared by the server sanitizer (crates/ai-memory-core/src/sanitize.rs) and the client scrub (companions/ai-memory-client/src/scrub.rs) so the two copies cannot drift. Every value is a FAKE shape.",
+ "credentials": [
+ "sk-leak-0123456789abcdef0123",
+ "ghp_FAKE0123456789ABCDEFGHIJ0123",
+ "AKIAFAKEFAKEFAKEFAKE"
+ ],
+ "invisibles": [
+ "\u200b",
+ "\u200c",
+ "\u200d",
+ "\u200e",
+ "\u2060",
+ "\u2063",
+ "\ufeff",
+ "\u00ad",
+ "\u034f",
+ "\u115f",
+ "\u17b4",
+ "\u180e",
+ "\u3164",
+ "\ufe0f",
+ "\uffa0",
+ "\udb40\udc01",
+ "\udb40\udd00"
+ ],
+ "key_body_line": "b3BlbnNzaC1rZXktdjEAAAAA",
+ "key_blocks": [
+ {
+ "input": "before\n-----BEGIN PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\n-----END PRIVATE KEY-----\nafter",
+ "kept": [
+ "before\n",
+ "\nafter"
+ ]
+ },
+ {
+ "input": "before\n-----BEGIN PGP PRIVATE KEY BLOCK-----\nVersion: 1\n\nb3BlbnNzaC1rZXktdjEAAAAA\n-----END PGP PRIVATE KEY BLOCK-----\nafter",
+ "kept": [
+ "before\n",
+ "\nafter"
+ ]
+ },
+ {
+ "input": "$ head -3 key.pem\n-----BEGIN PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\nb3BlbnNzaC1rZXktdjEAAAAA\n$ ls",
+ "kept": [
+ "$ head -3 key.pem\n",
+ "\n$ ls"
+ ]
+ },
+ {
+ "input": "-----BEGIN RSA PRIVATE KEY-----\r\nb3BlbnNzaC1rZXktdjEAAAAA\r\nb3BlbnNzaC1rZXktdjEAAAAA\r\n",
+ "kept": [
+ "\r\n"
+ ]
+ },
+ {
+ "input": "{\"stdout\":\"-----BEGIN OPENSSH PRIVATE KEY-----\\nb3BlbnNzaC1rZXktdjEAAAAA\\nb3BlbnNzaC1rZXktdjEAAAAA\"}",
+ "kept": [
+ "{\"stdout\":\"",
+ "\"}"
+ ]
+ },
+ {
+ "input": "-----BEGIN RSA PRIVATE KEY-----\nProc-Type: 4,ENCRYPTED\nDEK-Info: AES-128-CBC,0123456789ABCDEF\n\nb3BlbnNzaC1rZXktdjEAAAAA\n",
+ "kept": []
+ }
+ ],
+ "unchanged": [
+ "family \ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc67 emoji",
+ "keycaps 1\ufe0f\u20e3 #\ufe0f\u20e3",
+ "flag \ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc73\udb40\udc63\udb40\udc74\udb40\udc7f Scotland",
+ "\u0645\u06cc\u200c\u062e\u0648\u0627\u0647\u0645",
+ "\u0915\u094d\u200d\u0937",
+ "\u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35\u200b\u0e04\u0e23\u0e31\u0e1a",
+ "\u05e9\u05dc\u05d5\u05dd\u200f world",
+ "co\u00adoperate",
+ "\ufeffstarts with a byte-order mark",
+ "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nmQENBFakeFakeFakeFakeFakeFakeFake\n-----END PGP PUBLIC KEY BLOCK-----",
+ "The file starts with -----BEGIN OPENSSH PRIVATE KEY----- and that is expected.",
+ "-----BEGIN RSA PRIVATE KEY-----\nThat header marks a PKCS#1 key.",
+ "const HEADER: &str = \"-----BEGIN RSA PRIVATE KEY-----\";\nparseHeaderLineFromInput(x)"
+ ]
+}