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.
This commit is contained in:
2026-11-07 09:02:19 +01:00
parent f78fed95ef
commit 8998540375
3 changed files with 69 additions and 12 deletions
+28 -4
View File
@@ -297,11 +297,35 @@ fn parse_opaque(content: &str, source: &Path) -> Vec<SecretEntry> {
values.into_iter().map(|value| SecretEntry { key: key.to_string(), value: value.to_string(), source: source.to_path_buf() }).collect() 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 { pub(crate) fn is_certificate_only(content: &str) -> bool {
let armor = regex::Regex::new(r"-----BEGIN ([A-Z0-9 ]+)-----").expect("valid regex"); let (mut blocks, mut inside) = (0, false);
let mut labels = armor.captures_iter(content).map(|caps| caps[1].to_string()).peekable(); for line in content.lines().map(str::trim_end) {
labels.peek().is_some() && labels.all(|label| label.contains("CERTIFICATE") || label.contains("PUBLIC KEY")) 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. /// A quoted `.env` value: the text between the quotes, as written.
+36
View File
@@ -245,6 +245,42 @@ fn a_certificate_only_pem_passes_and_adds_no_entry() {
row(&files, VALUE, Read::Passes, Elsewhere::NotMasked); 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. /// Binary, whatever the name: stopped on Read, and under `deny` no refusal of everything else.
#[test] #[test]
fn a_binary_key_file_is_stopped_and_refuses_nothing() { fn a_binary_key_file_is_stopped_and_refuses_nothing() {
+5 -8
View File
@@ -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"); assert!(denied(&p.bash("tool > vendor/new")), "control");
} }
/// A key file holding only certificates passes, and that is read from its armor labels: /// Text a certificate file may carry around its blocks is let through: a comment line and
/// other text beside a certificate block is not looked at. /// an indented attribute. A value spelled as one of them passes.
#[test] #[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"; 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"] { for extra in ["# fake-beside-a-cert\n", " api_token: fake-beside-a-cert\n"] {
let content = format!("{CERT}{extra}"); let content = format!("{extra}{CERT}");
let p = project(&[("server.pem", content.as_bytes())]); let p = project(&[("server.pem", content.as_bytes())]);
assert!(passes(&p.read("server.pem")), "{extra}"); 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}"); 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. /// Only `~` in a command is expanded. In Grep's `path` it stays text, so the file is not found.