diff --git a/CLAUDE.md b/CLAUDE.md index 723a595..69dc496 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -38,6 +38,7 @@ Developed in this monorepo; planned to be exported as a standalone repo in the f - **Withholding depends on the harness.** Claude Code ignores an `updatedToolOutput` that does not match a built-in tool's output schema and then uses the original, so `harness/claude.rs` keeps the object's shape (strings replaced, arrays cut to one item, `type`/`mode` tags kept). A tool whose schema has another enum field can still reject that. PreToolUse does not look at secret files, so the tool has already run. - **Redaction sees only a string `tool_response`.** Claude Code's built-in tools send objects. Those count as empty and pass unredacted under every setting; only the `deny` failure path above handles them. - **Errors never quote a file.** `ParseError::Format` and `SecretsConfigError::Parse` keep the format and line number and drop the parser's text, because it can quote the failing line. Patterns (`.botignore`, `[policy]`, `[files]`) can appear in messages: they are not values, and a deny reason names them too. +- **Editor files never refuse.** A discovered file named `*.swp`, `*.swo`, `*.swn`, `*.swx` or `*~` that cannot be read or parsed is passed over silently under every `on_parse_error`, in the hooks and in Dirigent's channel filter (which forces `deny`). If it can be parsed its values stay known and are redacted, as before. `.bak`, `.orig` and `.env.gpg` keep today's rule, since a hand-made copy can hold live values. A `[[file]]` override is meant, whatever its name, and keeps the rule too. Files with no parser format (`secrets.toml~`, `#secrets.toml#`, `secrets.toml.bak`) were and are opaque: not read, no refusal, not redacted. ## Out of scope (v0.2) diff --git a/docs/configuration.md b/docs/configuration.md index 3956b97..d5b1496 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -181,6 +181,8 @@ Messages name the file and the line, never the line's text. If the config itself cannot be read, the setting is unknown. Any layer with an uncommented line setting `deny` counts, even in a file that does not parse, and PreToolUse then refuses too. A layer whose bytes cannot be read counts as `deny`. Otherwise the hooks stay open. +Editor files never refuse: a discovered `*.swp`, `*.swo`, `*.swn`, `*.swx` or `*~` that cannot be read or parsed is passed over under every setting. If it can be parsed, its values are still redacted. `.bak` and `.env.gpg` follow the setting, and so does anything named in `[[file]]`. + ```toml [enforcement] mode = "strict" diff --git a/src/core/secrets/manifest.rs b/src/core/secrets/manifest.rs index e21d2f4..7a9ef80 100644 --- a/src/core/secrets/manifest.rs +++ b/src/core/secrets/manifest.rs @@ -52,6 +52,30 @@ pub struct Manifest { /// redaction (e.g. `"yes"`, `"on"`, `"42"`). const MIN_VALUE_LEN: usize = 4; +/// File name endings of the working files editors leave beside what they edit: +/// vim's swap files (`.swp`, then `.swo`, `.swn` when one exists; `.swx` is its +/// write probe) and the `~` backup of vim and emacs. +/// +/// `.bak`, `.orig` and the like are not here: nothing makes those but a person +/// copying a file by hand, and such a copy can hold live values. +const EDITOR_ARTEFACT_SUFFIXES: &[&str] = &[".swp", ".swo", ".swn", ".swx", "~"]; + +/// Whether `path` is an editor's working file. +/// +/// Such a file comes and goes while someone edits, and a swap file is binary, so +/// failing to read one says nothing about the project's secrets and must not stop +/// anything, under any `on_parse_error`. It is still read: what can be parsed from +/// it is a copy of real values and stays known to the redactor. +/// +/// Only names [`FileFormat::from_path`] gives a format reach the parser, in practice +/// `.env*`. `secrets.toml~` or emacs' `#secrets.toml#` have no format, so they are +/// opaque: never read, never a refusal, not redacted, like a `.pem`. +fn is_editor_artefact(path: &Path) -> bool { + path.file_name() + .and_then(|n| n.to_str()) + .is_some_and(|name| EDITOR_ARTEFACT_SUFFIXES.iter().any(|suffix| name.ends_with(suffix))) +} + /// Directories that are unconditionally skipped during file discovery. const SKIP_DIRS: &[&str] = &[".git", "node_modules", "target", "__pycache__", ".venv"]; @@ -68,6 +92,7 @@ impl Manifest { for path in &discovered { match parse_discovered_file(path) { Ok(file_entries) => entries.extend(file_entries), + Err(_) if is_editor_artefact(path) => {} Err(e) => match config.enforcement.on_parse_error { ParseErrorAction::Allow => { eprintln!( diff --git a/tests/cli_hook_fail_closed.rs b/tests/cli_hook_fail_closed.rs index 044c9ca..f732c3f 100644 --- a/tests/cli_hook_fail_closed.rs +++ b/tests/cli_hook_fail_closed.rs @@ -225,6 +225,16 @@ fn other_settings_stay_open_and_quote_nothing() { } } +/// An editor's file does not take down every tool output of a `deny` project. +#[test] +fn an_editors_swap_file_does_not_withhold_under_deny() { + let p = project(&[(".botsecrets", DENY), (".env", "API_TOKEN=value-on-the-failing-line\n")]); + fs::write(p.dir.path().join(".env.swp"), b"b0VIM \xff\xfe\x00\x01").unwrap(); + let (out, _) = p.post(json!(RESPONSE)); + let text = out["hookSpecificOutput"]["updatedToolOutput"].as_str().unwrap(); + assert!(text.starts_with("output of the tool") && !text.contains(VALUE), "{text}"); +} + // --------------------------------------------------------------------------- // PreToolUse // --------------------------------------------------------------------------- diff --git a/tests/core_secrets_manifest.rs b/tests/core_secrets_manifest.rs index 9cd36ea..8edfde4 100644 --- a/tests/core_secrets_manifest.rs +++ b/tests/core_secrets_manifest.rs @@ -327,3 +327,49 @@ fn opaque_file_formats_are_skipped_gracefully() { let keys: Vec<&str> = manifest.entries().iter().map(|e| e.key.as_str()).collect(); assert!(keys.contains(&"PASSWORD")); } + +// --------------------------------------------------------------------------- +// Editor artefacts +// --------------------------------------------------------------------------- + +const NOT_UTF8: &[u8] = b"b0VIM 9.1 \xff\xfe\x00\x01"; + +fn deny_config() -> SecretsConfig { + config_from_toml("[enforcement]\non_parse_error = \"deny\"\n") +} + +/// Stays strict by default: an editor's file that cannot be read stops nothing, and +/// one that can is still redacted. Anything else that cannot be read still refuses. +#[test] +fn an_editors_file_that_cannot_be_read_does_not_refuse_and_one_that_can_stays_known() { + let dir = tempfile::tempdir().unwrap(); + let config = deny_config(); + for name in [".env.swp", ".env.swo", ".env.swn", ".env.swx", ".env.local~"] { + fs::write(dir.path().join(name), NOT_UTF8).unwrap(); + } + fs::write(dir.path().join(".env.bak.swp"), "API_KEY=readable-swap-value-1234\n").unwrap(); + + let manifest = Manifest::build(&config, dir.path()).expect("editor files must not refuse"); + let values: Vec<&str> = manifest.entries().iter().map(|e| e.value.as_str()).collect(); + assert_eq!(values, ["readable-swap-value-1234"]); + + for name in [".env.gpg", ".env.bak", ".env.orig", ".env.local"] { + let file = dir.path().join(name); + fs::write(&file, NOT_UTF8).unwrap(); + assert!(Manifest::build(&config, dir.path()).is_err(), "{name} must still refuse"); + fs::remove_file(&file).unwrap(); + } +} + +/// A file named by hand is meant, whatever it is called. +#[test] +fn a_file_override_named_like_an_editors_file_still_refuses_and_a_found_one_does_not() { + let dir = tempfile::tempdir().unwrap(); + fs::write(dir.path().join(".env.swp"), NOT_UTF8).unwrap(); + Manifest::build(&deny_config(), dir.path()).expect("found by a pattern: no refusal"); + + let config = config_from_toml( + "[enforcement]\non_parse_error = \"deny\"\n[[file]]\npath = \".env.swp\"\nformat = \"env\"\n", + ); + assert!(Manifest::build(&config, dir.path()).is_err()); +}