diff --git a/crates/tw-redact/src/redact.rs b/crates/tw-redact/src/redact.rs index 7570e2c6..125636f5 100644 --- a/crates/tw-redact/src/redact.rs +++ b/crates/tw-redact/src/redact.rs @@ -130,7 +130,7 @@ mod tests { #[test] fn nothing_to_redact_means_the_text_comes_back_byte_identical() { - // **走官方端点不该脱敏**,那条路径上这个函数每次都要跑, + // **绝大多数请求一处都不命中**,而这个函数每个请求都要跑, // 所以它在「没命中」时必须是一次纯粹的拷贝。 let t = "帮我看看这个 .env 文件\n"; let r = redact(t, &all()); @@ -148,6 +148,24 @@ mod tests { assert_eq!(restore(&r.text, &r.ledger), t); } + #[test] + fn a_key_at_the_start_of_a_line_is_replaced_and_the_body_stays_json() { + // 请求体里的换行是 `\n` 两个字符。行首的 key 曾经和那个 `n` 粘在 + // 一起、认不出来 —— 而贴进对话的凭据文件里,key 偏偏常在行首 + let body = serde_json::json!({ + "messages": [{ "role": "user", "content": format!("keys:\n{KEY}\nthanks") }] + }) + .to_string(); + let r = redact(&body, &all()); + assert!(!r.text.contains(KEY), "{}", r.text); + let v: serde_json::Value = serde_json::from_str(&r.text).expect("the body is still JSON"); + assert_eq!( + v["messages"][0]["content"], "keys:\n<>\nthanks", + "{v}" + ); + assert_eq!(restore(&r.text, &r.ledger), body); + } + #[test] fn the_same_value_twice_gets_the_same_placeholder() { // 一把 key 出现三次却换成三个不同的占位符,会让模型以为那是三个 diff --git a/crates/tw-redact/src/rules.rs b/crates/tw-redact/src/rules.rs index f481dbe6..158b8513 100644 --- a/crates/tw-redact/src/rules.rs +++ b/crates/tw-redact/src/rules.rs @@ -421,9 +421,31 @@ fn classify_token(tok: &str, set: &RuleSet) -> Option<&'static str> { /// /// **在 token 边界上匹配。**`xsk-ant-…` 里的 `sk-ant-…` 不是一把密钥, /// 而按子串找会把它算上 —— 那种误报没法解释。 +/// +/// **JSON 转义不属于任何 token。**扫的是请求体,而请求体是 JSON:换行在 +/// 里面是 `\n` 两个字符。按字符切的话,行首的 `sk-ant-…` 会和那个 `n` +/// 粘成 `nsk-ant-…`,前缀就对不上了 —— 贴进对话的凭据文件里,密钥偏偏 +/// 常在行首。`\t` 同理;`\uXXXX` 也一样(Python 写的客户端默认把中文 +/// 都转成这种写法,`密钥:sk-…` 里的冒号就成了 `\uff1a`)。所以转义序列 +/// 整个算作分隔。 fn for_each_token(text: &str, mut f: impl FnMut(&str, Range)) { let mut start: Option = None; - for (i, c) in text.char_indices() { + let mut chars = text.char_indices().peekable(); + while let Some((i, c)) = chars.next() { + if c == '\\' { + if let Some(s) = start.take() { + f(&text[s..i], s..i); + } + // 反斜杠后面那个字符是转义的一部分;`\u` 再带四位十六进制 + if let Some((_, 'u')) = chars.next() { + for _ in 0..4 { + if chars.next_if(|(_, h)| h.is_ascii_hexdigit()).is_none() { + break; + } + } + } + continue; + } if is_tok(c) { start.get_or_insert(i); } else if let Some(s) = start.take() { @@ -799,6 +821,29 @@ mod tests { assert!(got[0].1.starts_with("sk-ant-api03-"), "{}", got[0].1); } + #[test] + fn a_key_right_after_a_json_escape_is_still_found() { + // 扫的是请求体原文:换行、制表符在里面是 `\n`、`\t`,中文可能是 + // `\uXXXX`。转义里的那个字母不能和后面的密钥粘成一个 token + let key = "sk-ant-api03-AAAAAAAAAAAAAAAAAAAAAAAAAA"; + let jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.c2lnbmF0dXJl"; + for (t, rule, value) in [ + (format!(r"keys:\n{key}\nnext"), "anthropic-api-key", key), + (format!(r"key\t{key}"), "anthropic-api-key", key), + ( + format!(r"\u5bc6\u94a5\uff1a{key}"), + "anthropic-api-key", + key, + ), + (format!(r"token:\n{jwt}"), "jwt", jwt), + // 转义的反斜杠:解出来是 `C:\sk-ant-…`,反斜杠后面照样是边界 + (format!(r"C:\\{key}"), "anthropic-api-key", key), + ] { + let got = found(&t); + assert_eq!(got, vec![(rule.to_string(), value.to_string())], "{t}"); + } + } + #[test] fn a_key_glued_to_other_characters_is_not_a_key() { // 在 token 边界上匹配。`xsk-ant-…` 里的那一段不是一把密钥,