From 899854037584dab596d7743aab6934fb3c66cf4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Sat, 7 Nov 2026 09:02:19 +0100 Subject: [PATCH] fix(fermata): a certificate file is judged by all of its text, not by its armor labels API_TOKEN=x, a lowercase private block or a raw key beside a certificate block no longer pass the read gate. Bag Attributes, subject= and issuer=, a CA bundle's comments and the head of x509 -text stay allowed, so real PEMs keep passing. --- src/core/secrets/parser.rs | 32 ++++++++++++++++++++++++---- tests/cli_hook_secret_formats.rs | 36 ++++++++++++++++++++++++++++++++ tests/known_limits.rs | 13 +++++------- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/core/secrets/parser.rs b/src/core/secrets/parser.rs index 5b84621..6effe58 100644 --- a/src/core/secrets/parser.rs +++ b/src/core/secrets/parser.rs @@ -297,11 +297,35 @@ fn parse_opaque(content: &str, source: &Path) -> Vec { values.into_iter().map(|value| SecretEntry { key: key.to_string(), value: value.to_string(), source: source.to_path_buf() }).collect() } -/// Whether `content` is PEM holding only certificates and public keys, which are no secret. +/// Whether `content` is PEM holding only certificates and public keys, which are no secret, +/// with nothing beside them but the text such files carry: see [`is_certificate_text`]. pub(crate) fn is_certificate_only(content: &str) -> bool { - let armor = regex::Regex::new(r"-----BEGIN ([A-Z0-9 ]+)-----").expect("valid regex"); - let mut labels = armor.captures_iter(content).map(|caps| caps[1].to_string()).peekable(); - labels.peek().is_some() && labels.all(|label| label.contains("CERTIFICATE") || label.contains("PUBLIC KEY")) + let (mut blocks, mut inside) = (0, false); + for line in content.lines().map(str::trim_end) { + if inside { + inside = !line.starts_with("-----END "); + if inside && line.starts_with("-----") { + return false; + } + } else if let Some(label) = line.strip_prefix("-----BEGIN ").and_then(|rest| rest.strip_suffix("-----")) { + if !label.contains("CERTIFICATE") && !label.contains("PUBLIC KEY") { + return false; + } + blocks += 1; + inside = true; + } else if !is_certificate_text(line) { + return false; + } + } + blocks > 0 && !inside +} + +/// A line real certificate files hold outside their blocks: `Bag Attributes` and its indented +/// attributes, `subject=` and `issuer=`, a CA bundle's comments, the head of `openssl x509 -text`. +fn is_certificate_text(line: &str) -> bool { + let is_comment = line.starts_with('#') && commented_assignment(line).is_none(); + let is_attribute = line.starts_with([' ', '\t']) && line.contains(':'); + line.is_empty() || is_comment || is_attribute || ["Bag Attributes", "Key Attributes", "Certificate:"].contains(&line) || line.starts_with("subject=") || line.starts_with("issuer=") } /// A quoted `.env` value: the text between the quotes, as written. diff --git a/tests/cli_hook_secret_formats.rs b/tests/cli_hook_secret_formats.rs index 8817623..e12c205 100644 --- a/tests/cli_hook_secret_formats.rs +++ b/tests/cli_hook_secret_formats.rs @@ -245,6 +245,42 @@ fn a_certificate_only_pem_passes_and_adds_no_entry() { row(&files, VALUE, Read::Passes, Elsewhere::NotMasked); } +/// Certificates with the text real files carry around them are certificate-only. Text that +/// is none of that makes the file a secret again. +#[test] +fn a_certificate_file_is_judged_by_all_of_its_text() { + const CERT: &str = "-----BEGIN CERTIFICATE-----\nFAKEFAKEfakeFAKEfake\n-----END CERTIFICATE-----\n"; + let denied = |out: Value| out["hookSpecificOutput"]["permissionDecision"] == "deny"; + for content in [ + CERT.to_string(), + CERT.replace('\n', "\r\n"), + format!("\n{CERT}\n{CERT}"), + format!("Bag Attributes\n friendlyName: fake-name\n localKeyID: 01 02 03\nsubject=/CN=fake\nissuer=/CN=fake\n{CERT}"), + format!("# Issuer: CN=Fake Root O=Fake\n# Label: \"Fake\"\n# SHA256 Fingerprint: 01:02\n{CERT}"), + format!("Certificate:\n Data:\n Version: 3 (0x2)\n{CERT}"), + ] { + let p = project("", "server.pem", content.as_bytes()); + assert!(!denied(p.pre_read("server.pem")), "{content:?}"); + assert!(!denied(p.pre_cat("server.pem")), "{content:?}"); + } + for content in [ + format!("{CERT}API_TOKEN=fake-beside-a-cert\n"), + format!("API_TOKEN=fake-beside-a-cert\n{CERT}"), + format!("# API_TOKEN=fake-beside-a-cert\n{CERT}"), + format!("{CERT}fake-beside-a-cert\n"), + format!("{CERT}-----begin private key-----\nfake-beside-a-cert\n"), + format!("{CERT}-----BEGIN PRIVATE KEY-----\nfake-beside-a-cert\n-----END PRIVATE KEY-----\n"), + "# -----BEGIN CERTIFICATE-----\nfake-beside-a-cert\n".to_string(), + // A block that holds another, or never ends, is no certificate. + "-----BEGIN CERTIFICATE-----\n-----BEGIN PRIVATE KEY-----\nfake\n-----END PRIVATE KEY-----\n".to_string(), + "-----BEGIN CERTIFICATE-----\nfake\n".to_string(), + ] { + let p = project("", "server.pem", content.as_bytes()); + assert!(denied(p.pre_read("server.pem")), "{content:?}"); + assert!(denied(p.pre_cat("server.pem")), "{content:?}"); + } +} + /// Binary, whatever the name: stopped on Read, and under `deny` no refusal of everything else. #[test] fn a_binary_key_file_is_stopped_and_refuses_nothing() { diff --git a/tests/known_limits.rs b/tests/known_limits.rs index c2b8c67..27faa32 100644 --- a/tests/known_limits.rs +++ b/tests/known_limits.rs @@ -453,20 +453,17 @@ fn a_new_file_named_to_a_verb_off_the_writing_list_is_not_checked() { assert!(denied(&p.bash("tool > vendor/new")), "control"); } -/// A key file holding only certificates passes, and that is read from its armor labels: -/// other text beside a certificate block is not looked at. +/// Text a certificate file may carry around its blocks is let through: a comment line and +/// an indented attribute. A value spelled as one of them passes. #[test] -fn a_certificate_file_is_judged_by_its_armor_labels() { +fn a_comment_or_an_attribute_line_beside_a_certificate_passes() { const CERT: &str = "-----BEGIN CERTIFICATE-----\nFAKEFAKEfakeFAKEfake\n-----END CERTIFICATE-----\n"; - for extra in ["API_TOKEN=fake-beside-a-cert\n", "-----begin private key-----\nfake-beside-a-cert\n", "fake-beside-a-cert\n"] { - let content = format!("{CERT}{extra}"); + for extra in ["# fake-beside-a-cert\n", " api_token: fake-beside-a-cert\n"] { + let content = format!("{extra}{CERT}"); let p = project(&[("server.pem", content.as_bytes())]); assert!(passes(&p.read("server.pem")), "{extra}"); - assert!(passes(&p.bash("cat server.pem")), "{extra}"); assert!(p.after_read("server.pem", &content).contains("fake-beside-a-cert"), "{extra}"); } - let p = project(&[("server.pem", b"# -----BEGIN CERTIFICATE-----\nfake-beside-a-cert\n")]); - assert!(passes(&p.read("server.pem"))); } /// Only `~` in a command is expanded. In Grep's `path` it stays text, so the file is not found.