- A glob in [files] or [policy] that does not compile costs only that pattern while open, with a note by name beside text output. A [files] list with none left leaves the built-in list. Under deny the refusal stays, and the channel filter stays strict. - A file named `credentials` is on the default list, at any depth. - A word with a separator counts only if the path is there, the shell may expand it, or a redirect names it: packages, branches and hosts' paths are not denied. - timeout, nice, ionice, stdbuf, xargs and busybox are prefixes; `2>&1` stays in its segment. - `bash|sh|zsh -c` and `eval` arguments are read as command lines. - A `<<` with no closing line is no heredoc. A commented line's own comment is dropped. Text holding NUL fails to parse. The unknown-field message says so. Durations are bound to under 6 characters. More source extensions pass the gate. - Removed: HarnessAdapter::post_tool_op (dead since 3C was dropped), tool_op back inline.
249 lines
9.2 KiB
Rust
249 lines
9.2 KiB
Rust
//! The Channels table of the known-failures build, for what concerns the manifest. The
|
|
//! channel filter (`dirigent_server` `channels/filter.rs`, `Sieve::load`) loads
|
|
//! `SecretsConfig`, forces `on_parse_error` to `Deny`, builds the `Manifest` and a
|
|
//! `Redactor::new`. `sieve` below does the same, so each test is one row of that table.
|
|
//!
|
|
//! Not covered: block 3C was dropped, so swallowed lines, Python triple quotes and shell
|
|
//! spellings have no rows here. All values are fake.
|
|
|
|
use std::fs;
|
|
use std::path::Path;
|
|
use std::sync::Once;
|
|
|
|
use dirigent_fermata::core::secrets::config::{ParseErrorAction, RedactionStyle};
|
|
use dirigent_fermata::core::secrets::{Manifest, Redactor, SecretsConfig};
|
|
|
|
const VALUE: &str = "fake-value-in-the-file-1f9a";
|
|
const MASK: &str = "*****";
|
|
const BINARY: &[u8] = &[0xff, 0xfe, 0x00, 0xc3, 0x28, 0x80];
|
|
|
|
/// `Sieve::load`: `Err` is a refusal. The redactor is the one the filter would use.
|
|
fn sieve(root: &Path) -> Result<Redactor, String> {
|
|
static HOME: Once = Once::new();
|
|
HOME.call_once(|| {
|
|
let home = Box::leak(Box::new(tempfile::tempdir().unwrap()));
|
|
std::env::set_var("HOME", home.path());
|
|
});
|
|
let mut config = SecretsConfig::load(root).map_err(|e| e.to_string())?;
|
|
config.enforcement.on_parse_error = ParseErrorAction::Deny;
|
|
let manifest = Manifest::build(&config, root).map_err(|e| e.to_string())?;
|
|
let redactor = Redactor::new(&manifest, RedactionStyle::Masked);
|
|
if manifest.is_empty() || redactor.has_secrets() {
|
|
Ok(redactor)
|
|
} else {
|
|
Err("the redactor cannot be built from these secrets".to_string())
|
|
}
|
|
}
|
|
|
|
fn root(files: &[(&str, &[u8])]) -> tempfile::TempDir {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
for (name, content) in files {
|
|
let path = dir.path().join(name);
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
fs::write(path, content).unwrap();
|
|
}
|
|
dir
|
|
}
|
|
|
|
fn load(files: &[(&str, &[u8])]) -> Result<Redactor, String> {
|
|
sieve(root(files).path())
|
|
}
|
|
|
|
fn refuses(files: &[(&str, &[u8])]) {
|
|
let names: Vec<&str> = files.iter().map(|f| f.0).collect();
|
|
assert!(load(files).is_err(), "{names:?} must refuse");
|
|
}
|
|
|
|
fn passes(files: &[(&str, &[u8])]) -> Redactor {
|
|
let names: Vec<&str> = files.iter().map(|f| f.0).collect();
|
|
load(files).unwrap_or_else(|e| panic!("{names:?} must not refuse: {e}"))
|
|
}
|
|
|
|
fn masked(files: &[(&str, &[u8])], text: &str) -> bool {
|
|
let out = passes(files).redact(text).text;
|
|
!out.contains(VALUE) && out.contains(MASK)
|
|
}
|
|
|
|
fn crosses(files: &[(&str, &[u8])], text: &str) -> bool {
|
|
passes(files).redact(text).text == text
|
|
}
|
|
|
|
fn pem(label: &str, body: &str) -> String {
|
|
format!("-----BEGIN {label}-----\n{body}\n-----END {label}-----\n")
|
|
}
|
|
|
|
// ---- New refusals ----------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_botsecrets_with_an_unknown_field_refuses() {
|
|
refuses(&[(".botsecrets", b"[polcy]\nx = 1\n")]);
|
|
refuses(&[(".botsecrets", b"[keys]\nbogus = [\"X\"]\n")]);
|
|
}
|
|
|
|
#[test]
|
|
fn enforcement_mode_refuses() {
|
|
refuses(&[(".botsecrets", b"[enforcement]\nmode = \"warn\"\n")]);
|
|
}
|
|
|
|
#[test]
|
|
fn mask_entire_file_refuses() {
|
|
refuses(&[(".botsecrets", b"[[file]]\npath = \"a.txt\"\nmask-entire-file = true\n")]);
|
|
}
|
|
|
|
/// UTF-16 with no byte order mark is valid UTF-8 holding NUL: it "parsed" into keys no
|
|
/// filter knows. A channel refuses it, as any secret file that cannot be parsed.
|
|
#[test]
|
|
fn a_utf16_file_with_no_bom_refuses() {
|
|
let utf16: Vec<u8> = format!("API_TOKEN={VALUE}\n").encode_utf16().flat_map(u16::to_le_bytes).collect();
|
|
refuses(&[(".env", &utf16)]);
|
|
}
|
|
|
|
/// A file named `credentials` is on the list, so a non-UTF-8 one refuses at any depth.
|
|
#[test]
|
|
fn a_non_utf8_file_named_credentials_refuses() {
|
|
refuses(&[("credentials", BINARY)]);
|
|
refuses(&[("sub/credentials", BINARY)]);
|
|
}
|
|
|
|
/// Only `*.tfstate` (JSON) can be broken as text. `*.tfvars`, `.aws/credentials` and
|
|
/// `secrets.{sh,ini,conf,cfg}` go through the lenient env reader, which accepts any
|
|
/// UTF-8 text, so for them the new refusal is the non-UTF-8 one. The plan's table says
|
|
/// "broken or non-UTF-8" for all of them: the "broken" half is false except for tfstate.
|
|
#[test]
|
|
fn a_broken_tfstate_refuses() {
|
|
refuses(&[("x.tfstate", b"{\"api_token\": \"fake")]);
|
|
refuses(&[("terraform.tfstate.backup", b"not json at all")]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_non_utf8_file_of_a_new_format_refuses() {
|
|
for name in ["x.tfstate", "terraform.tfvars", ".aws/credentials", "secrets.sh", "secrets.ini", "secrets.conf", "secrets.cfg"] {
|
|
refuses(&[(name, BINARY)]);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_broken_text_of_the_env_read_formats_does_not_refuse() {
|
|
passes(&[("terraform.tfvars", b"db_password = \"unterminated\n")]);
|
|
passes(&[("secrets.ini", b"[main\napi_token = x\n")]);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn a_symlinked_env_whose_target_will_not_parse_refuses() {
|
|
let dir = root(&[("real/target.bin", BINARY)]);
|
|
std::os::unix::fs::symlink(dir.path().join("real/target.bin"), dir.path().join(".env")).unwrap();
|
|
assert!(sieve(dir.path()).is_err());
|
|
}
|
|
|
|
// ---- Unchanged: no refusal ---------------------------------------------------
|
|
|
|
#[test]
|
|
fn binary_key_and_credential_files_do_not_refuse() {
|
|
for name in ["server.pem", "server.key", "bundle.p12", "bundle.pfx", ".netrc", ".htpasswd"] {
|
|
passes(&[(name, BINARY)]);
|
|
}
|
|
}
|
|
|
|
// ---- Fewer refusals ----------------------------------------------------------
|
|
|
|
#[test]
|
|
fn multi_document_yaml_does_not_refuse() {
|
|
let yaml = format!("api_token: {VALUE}\n---\nother_secret: {VALUE}-2\n");
|
|
passes(&[("secrets.yaml", yaml.as_bytes())]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_utf8_bom_in_json_or_env_does_not_refuse() {
|
|
let json = format!("\u{feff}{{\"api_token\": \"{VALUE}\"}}");
|
|
let env = format!("\u{feff}API_TOKEN={VALUE}\n");
|
|
passes(&[("service-account.json", json.as_bytes())]);
|
|
passes(&[(".env", env.as_bytes())]);
|
|
}
|
|
|
|
// ---- More masked -------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn commented_env_lines_are_masked() {
|
|
let env = format!("# API_TOKEN={VALUE}\n");
|
|
assert!(masked(&[(".env", env.as_bytes())], &format!("see {VALUE} here")));
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn a_symlinked_env_is_masked() {
|
|
let dir = root(&[("real/target.env", format!("API_TOKEN={VALUE}\n").as_bytes())]);
|
|
std::os::unix::fs::symlink(dir.path().join("real/target.env"), dir.path().join(".env")).unwrap();
|
|
let out = sieve(dir.path()).unwrap().redact(&format!("x {VALUE} y")).text;
|
|
assert!(!out.contains(VALUE) && out.contains(MASK));
|
|
}
|
|
|
|
#[test]
|
|
fn a_python_trailing_comment_and_annotation_are_masked() {
|
|
let py = format!("API_TOKEN = \"{VALUE}\" # fake\nDB_PASSWORD: str = \"{VALUE}-b\"\n");
|
|
let files = [("secrets.py", py.as_bytes())];
|
|
assert!(masked(&files, &format!("x {VALUE} y")));
|
|
assert!(masked(&files, &format!("x {VALUE}-b y")));
|
|
}
|
|
|
|
#[test]
|
|
fn values_of_the_new_formats_are_masked() {
|
|
let state = format!("{{\"outputs\":{{\"db_password\":{{\"value\":\"{VALUE}\"}}}}}}");
|
|
let vars = format!("db_password = \"{VALUE}\"\n");
|
|
let aws = format!("[default]\naws_secret_access_key = {VALUE}\n");
|
|
let sh = format!("export API_TOKEN={VALUE}\n");
|
|
let ini = format!("[main]\napi_token = {VALUE}\n");
|
|
for (name, content) in [
|
|
("x.tfstate", &state),
|
|
("terraform.tfvars", &vars),
|
|
(".aws/credentials", &aws),
|
|
("secrets.sh", &sh),
|
|
("secrets.ini", &ini),
|
|
("secrets.conf", &ini),
|
|
("secrets.cfg", &ini),
|
|
] {
|
|
assert!(masked(&[(name, content.as_bytes())], &format!("x {VALUE} y")), "{name}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_pem_private_body_is_masked() {
|
|
for (name, label) in [("server.pem", "PRIVATE KEY"), ("server.key", "RSA PRIVATE KEY"), ("id_rsa", "OPENSSH PRIVATE KEY")] {
|
|
let content = pem(label, VALUE);
|
|
assert!(masked(&[(name, content.as_bytes())], &format!("x {VALUE} y")), "{name}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_one_token_key_file_is_masked() {
|
|
let content = format!("{VALUE}\n");
|
|
assert!(masked(&[("config/master.key", content.as_bytes())], &format!("x {VALUE} y")));
|
|
}
|
|
|
|
// ---- Less masked -------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn plain_settings_cross() {
|
|
let env = "AUTH_ENABLED=true\nSESSION_COUNT=42\nTOKEN_TTL=3600\nAPI_TOKEN_EXPIRY=15m\n\
|
|
PASSWORD_POLICY=strict-rotation\nSECRET_BACKEND=vault-cluster\nAUTHOR_NAME=somebody-here\n\
|
|
AUTHORITY=authority-host-name\nAUTHORIZED_USERS=alice-and-bob-and-carol\nAUTHOR=jane-doe-the-author\n";
|
|
let files = [(".env", env.as_bytes())];
|
|
for text in ["true", "42", "3600", "15m", "strict-rotation", "vault-cluster", "somebody-here", "authority-host-name", "alice-and-bob-and-carol", "jane-doe-the-author"] {
|
|
assert!(crosses(&files, &format!("value {text} end")), "{text} must cross");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_long_number_under_a_setting_key_still_crosses_as_the_plan_says() {
|
|
// The plan: `API_TOKEN_TTL=604800` crosses (key ends in a setting word, 6 digits).
|
|
assert!(crosses(&[(".env", b"API_TOKEN_TTL=604800\n")], "ttl 604800 end"));
|
|
}
|
|
|
|
// ---- Unchanged: masked -------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_short_word_like_secret_is_still_masked() {
|
|
let out = passes(&[(".env", b"DB_PASSWORD=postgres\n")]).redact("login postgres end").text;
|
|
assert!(!out.contains("postgres") && out.contains(MASK));
|
|
}
|