Read shows a file's bytes, a loaded program shows the decoded text. The manifest held only one. It now holds the file's text, the decoded text, the JSON- and \n-escaped forms, each line of a multi-line value from 8 characters, and numbers from 6. .env parses quoted values over lines and a comment after a value.
233 lines
10 KiB
Rust
233 lines
10 KiB
Rust
//! A value spelled in its own file differently from how it decodes. Read shows the
|
|
//! file's bytes, a program that loaded the file shows the decoded text, and every such
|
|
//! spelling has to be masked. All values here are fake.
|
|
|
|
use assert_cmd::Command;
|
|
use serde_json::{json, Value};
|
|
use std::fs;
|
|
use tempfile::TempDir;
|
|
|
|
/// Fake PEM body lines. `TAIL` is shorter than the line minimum.
|
|
const LINE_1: &str = "FAKEFAKEpemBODYlineONExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
const LINE_2: &str = "FAKEFAKEpemBODYlineTWOyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
|
|
const TAIL: &str = "abc=";
|
|
|
|
fn pem() -> String {
|
|
format!("-----BEGIN PRIVATE KEY-----\n{LINE_1}\n{LINE_2}\n{TAIL}\n-----END PRIVATE KEY-----\n")
|
|
}
|
|
|
|
fn escaped_pem() -> String {
|
|
pem().replace('\n', "\\n")
|
|
}
|
|
|
|
fn project(files: &[(&str, &str)]) -> (TempDir, TempDir) {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
fs::write(dir.path().join(".botignore"), "").unwrap();
|
|
for (name, content) in files {
|
|
fs::write(dir.path().join(name), content).unwrap();
|
|
}
|
|
(dir, tempfile::tempdir().unwrap())
|
|
}
|
|
|
|
fn hook(p: &(TempDir, TempDir), response: &Value) -> Value {
|
|
let output = Command::cargo_bin("fermata")
|
|
.unwrap()
|
|
.args(["hook", "--event", "post-tool-use", "--harness", "claude"])
|
|
.current_dir(p.0.path())
|
|
.env("HOME", p.1.path())
|
|
.write_stdin(json!({"tool_name": "Read", "tool_input": {}, "tool_response": response}).to_string())
|
|
.assert()
|
|
.success()
|
|
.get_output()
|
|
.clone();
|
|
serde_json::from_slice(&output.stdout).unwrap()
|
|
}
|
|
|
|
/// What the model gets from `Read` of a file holding `text`.
|
|
fn read(p: &(TempDir, TempDir), text: &str) -> String {
|
|
let response = json!({
|
|
"type": "text",
|
|
"file": {"filePath": "/p/f", "content": text, "numLines": 1, "startLine": 1, "totalLines": 1},
|
|
});
|
|
let out = hook(p, &response);
|
|
out["hookSpecificOutput"]["updatedToolOutput"]["file"]["content"]
|
|
.as_str()
|
|
.unwrap_or(text)
|
|
.to_string()
|
|
}
|
|
|
|
/// `Read` of the file itself shows none of `parts`, in a project that declares it.
|
|
fn file_read_hides(name: &str, content: &str, parts: &[&str]) {
|
|
let botsecrets = format!("[files]\npatterns = [\"{name}\"]\n[keys]\ninclude = [\"*\"]\n");
|
|
let p = project(&[(".botsecrets", &botsecrets), (name, content)]);
|
|
let shown = read(&p, content);
|
|
for part in parts {
|
|
assert!(!shown.contains(part), "{name}: {part:?} reached the model in:\n{shown}");
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The file's own bytes
|
|
// ---------------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_google_key_file_with_the_default_config() {
|
|
let key = escaped_pem();
|
|
let content = format!("{{\n \"type\": \"service_account\",\n \"private_key_id\": \"fakeid1234\",\n \"private_key\": \"{key}\"\n}}\n");
|
|
let p = project(&[(".botsecrets", ""), ("service-account.json", &content)]);
|
|
let shown = read(&p, &content);
|
|
assert!(!shown.contains("fakeid1234"), "{shown}");
|
|
assert!(!shown.contains(LINE_1) && !shown.contains(LINE_2), "{shown}");
|
|
assert!(shown.contains("service_account"), "other values stay: {shown}");
|
|
}
|
|
|
|
#[test]
|
|
fn json_escapes() {
|
|
file_read_hides("s.json", r#"{"k": "fake\"quoted\\value"}"#, &[r#"fake\"quoted\\value"#]);
|
|
file_read_hides("s.json", r#"{"k": "fake\u0041unicode"}"#, &[r"fake\u0041unicode"]);
|
|
file_read_hides("s.json", r#"{"k": "fake\/slash\tvalue"}"#, &[r"fake\/slash\tvalue"]);
|
|
}
|
|
|
|
#[test]
|
|
fn an_env_value_with_escapes() {
|
|
let content = format!("PRIVATE_KEY=\"{}\"\nAPI_TOKEN=plainfake123\n", escaped_pem());
|
|
file_read_hides(".env", &content, &[LINE_1, LINE_2, "plainfake123"]);
|
|
file_read_hides(".env", "K=\"fake\\tvalue\"\n", &["fake\\tvalue"]);
|
|
}
|
|
|
|
#[test]
|
|
fn an_env_value_over_real_lines() {
|
|
let content = format!("PRIVATE_KEY=\"{}\"\nAPI_TOKEN=plainfake123\n", pem());
|
|
file_read_hides(".env", &content, &[LINE_1, LINE_2, "plainfake123"]);
|
|
let single = format!("PRIVATE_KEY='{}'\nAPI_TOKEN=plainfake123\n", pem());
|
|
file_read_hides(".env", &single, &[LINE_1, LINE_2, "plainfake123"]);
|
|
}
|
|
|
|
#[test]
|
|
fn yaml_spellings() {
|
|
let block = format!("key: |\n {LINE_1}\n {LINE_2}\nother: x\n");
|
|
file_read_hides("s.yaml", &block, &[LINE_1, LINE_2]);
|
|
let folded = format!("key: >-\n {LINE_1}\n {LINE_2}\nother: x\n");
|
|
file_read_hides("s.yaml", &folded, &[LINE_1, LINE_2]);
|
|
let plain = format!("key: {LINE_1}\n {LINE_2}\nother: x\n");
|
|
file_read_hides("s.yaml", &plain, &[LINE_1, LINE_2]);
|
|
let nested = format!("a:\n - key: |\n {LINE_1}\n {LINE_2}\n");
|
|
file_read_hides("s.yaml", &nested, &[LINE_1, LINE_2]);
|
|
file_read_hides("s.yaml", "key: \"fake\\nyaml\\tvalue\"\n", &["fake\\nyaml\\tvalue"]);
|
|
file_read_hides("s.yaml", "key: 'fake''quoted value'\n", &["fake''quoted value"]);
|
|
let over_lines = "key: \"fake quoted\n over lines value\"\n";
|
|
file_read_hides("s.yaml", over_lines, &["fake quoted", "over lines value"]);
|
|
}
|
|
|
|
#[test]
|
|
fn toml_spellings() {
|
|
file_read_hides("s.toml", "key = \"fake\\tvalue\\\"x\"\n", &["fake\\tvalue\\\"x"]);
|
|
let lines = format!("key = \"\"\"\n{LINE_1}\n{LINE_2}\n\"\"\"\n");
|
|
file_read_hides("s.toml", &lines, &[LINE_1, LINE_2]);
|
|
let joined = format!("key = \"\"\"\\\n{LINE_1}\\\n{LINE_2}\"\"\"\n");
|
|
file_read_hides("s.toml", &joined, &[LINE_1, LINE_2]);
|
|
file_read_hides("s.toml", "# an 'odd' comment\nkey = 'fake literal' # \"note\n", &["fake literal"]);
|
|
}
|
|
|
|
#[test]
|
|
fn python_and_properties_spellings() {
|
|
let joined = format!("key={LINE_1}\\\n {LINE_2}\n");
|
|
file_read_hides("s.properties", &joined, &[LINE_1, LINE_2]);
|
|
let botsecrets = "[files]\npatterns = [\"s.py\"]\n[keys]\ninclude = [\"*\"]\n";
|
|
let p = project(&[(".botsecrets", botsecrets), ("s.py", "KEY = \"fake\\nvalue1\"\n")]);
|
|
assert_eq!(read(&p, "printed: fake\nvalue1"), "printed: *****");
|
|
assert_eq!(read(&p, "file: fake\\nvalue1"), "file: *****");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Numbers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn numbers_in_a_secret_file() {
|
|
file_read_hides("s.toml", "api_token = 987654321\n", &["987654321"]);
|
|
file_read_hides("s.json", "{\"api_token\": 987654321}", &["987654321"]);
|
|
file_read_hides("s.yaml", "api_token: 987654321\n", &["987654321"]);
|
|
file_read_hides("s.toml", "api_token = 987_654_321\n", &["987_654_321"]);
|
|
file_read_hides("s.json", "{\"api_token\": 123456789012345678901234567890}", &["123456789012345678901234567890"]);
|
|
file_read_hides("s.yaml", "pin: 0012345678\n", &["0012345678"]);
|
|
}
|
|
|
|
/// A short number under a matching key is a setting more often than a secret.
|
|
#[test]
|
|
fn a_short_number_stays() {
|
|
let botsecrets = "[files]\npatterns = [\"s.toml\"]\n[keys]\ninclude = [\"*\"]\n";
|
|
let p = project(&[(".botsecrets", botsecrets), ("s.toml", "cache_key_ttl = 36000\n")]);
|
|
assert_eq!(read(&p, "ttl is 36000"), "ttl is 36000");
|
|
let p = project(&[(".botsecrets", botsecrets), ("s.toml", "cache_key_ttl = 360000\n")]);
|
|
assert_eq!(read(&p, "ttl is 360000"), "ttl is *****");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// What a program prints
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// `.env` values as a shell or a dotenv loader decodes them.
|
|
#[test]
|
|
fn an_env_value_as_a_program_prints_it() {
|
|
let botsecrets = "[files]\npatterns = [\".env\"]\n";
|
|
for (line, printed) in [
|
|
("API_TOKEN=\"fakevalue\" # prod", "fakevalue"),
|
|
("API_TOKEN=\"fake\\\"value\"", "fake\"value"),
|
|
("API_TOKEN=fake\\ value", "fake value"),
|
|
("API_TOKEN='fake value' # prod", "fake value"),
|
|
("API_TOKEN=fakevalue # prod", "fakevalue"),
|
|
("API_TOKEN=\"fake$value\"", "fake$value"),
|
|
] {
|
|
let p = project(&[(".botsecrets", botsecrets), (".env", line)]);
|
|
assert_eq!(read(&p, &format!("echo: {printed}\n")), "echo: *****\n", "{line}");
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Part of a value
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// A Read with a line range, a grep, a diff: the value is not whole.
|
|
#[test]
|
|
fn each_line_of_a_value_is_masked_alone() {
|
|
let content = format!("PRIVATE_KEY=\"{}\"\n", pem());
|
|
let p = project(&[(".botsecrets", "[files]\npatterns = [\".env\"]\n"), (".env", &content)]);
|
|
assert_eq!(read(&p, &format!("3: {LINE_2}\n")), "3: *****\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_whole_value_gets_one_mask() {
|
|
let content = format!("PRIVATE_KEY=\"{}\"\n", pem());
|
|
let p = project(&[(".botsecrets", "[files]\npatterns = [\".env\"]\n"), (".env", &content)]);
|
|
assert_eq!(read(&p, &content), "PRIVATE_KEY=\"*****\"\n");
|
|
assert_eq!(read(&p, &format!("a {} b", pem().trim_end())), "a ***** b");
|
|
}
|
|
|
|
/// Short lines, PEM armor and layout appear in ordinary text. The price is stated in
|
|
/// `MIN_LINE_LEN`: a short tail read away from its value passes.
|
|
#[test]
|
|
fn short_lines_armor_and_layout_are_not_masked_alone() {
|
|
let value = format!("-----BEGIN PRIVATE KEY-----\n{LINE_1}\nprod\n--------\n1234567\n12345678\n-----END PRIVATE KEY-----");
|
|
let content = format!("PRIVATE_KEY=\"{value}\"\n");
|
|
let p = project(&[(".botsecrets", "[files]\npatterns = [\".env\"]\n"), (".env", &content)]);
|
|
for text in ["see -----BEGIN PRIVATE KEY----- here", "-----END PRIVATE KEY-----", "prod", "--------", "1234567"] {
|
|
assert_eq!(read(&p, text), text);
|
|
}
|
|
assert_eq!(read(&p, "12345678"), "*****");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// A malformed .env
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// An opening quote nobody closed must not swallow the keys below it.
|
|
#[test]
|
|
fn an_unclosed_quote_leaves_the_next_key_alone() {
|
|
let content = "BROKEN_TOKEN=\"unclosedfake\nAPI_TOKEN=\"nextfakevalue\"\nOTHER_TOKEN=lastfakevalue\n";
|
|
let botsecrets = "[files]\npatterns = [\".env\"]\n";
|
|
let p = project(&[(".botsecrets", botsecrets), (".env", content)]);
|
|
let shown = read(&p, "nextfakevalue lastfakevalue");
|
|
assert_eq!(shown, "***** *****");
|
|
}
|