diff --git a/src/core/secrets/parser.rs b/src/core/secrets/parser.rs index 6effe58..966ae2b 100644 --- a/src/core/secrets/parser.rs +++ b/src/core/secrets/parser.rs @@ -300,11 +300,13 @@ fn parse_opaque(content: &str, source: &Path) -> Vec { /// 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 (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("-----") { + let lines: Vec<&str> = content.lines().map(str::trim_end).collect(); + let (mut blocks, mut end) = (0, None); + for (at, line) in lines.iter().copied().enumerate() { + if let Some(closing) = &end { + if line == closing { + end = None; + } else if line.starts_with("-----") { return false; } } else if let Some(label) = line.strip_prefix("-----BEGIN ").and_then(|rest| rest.strip_suffix("-----")) { @@ -312,20 +314,22 @@ pub(crate) fn is_certificate_only(content: &str) -> bool { return false; } blocks += 1; - inside = true; - } else if !is_certificate_text(line) { + end = Some(format!("-----END {label}-----")); + } else if !is_certificate_text(line, lines.get(at + 1).copied()) { return false; } } - blocks > 0 && !inside + blocks > 0 && end.is_none() } /// 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=") +/// attributes, `subject=` and `issuer=`, a CA bundle's comments and titles over a `====` line, +/// and `openssl x509 -text`, whose indented lines have no one shape. An assignment is none of it. +fn is_certificate_text(line: &str, next: Option<&str>) -> bool { + let is_rule = |text: &str| text.len() > 2 && text.chars().all(|c| c == '='); + let assignment = commented_assignment(line).is_some(); + let indented_or_comment = line.starts_with(['#', ' ', '\t']) || next.is_some_and(is_rule); + line.is_empty() || is_rule(line) || (indented_or_comment && !assignment) || ["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 e12c205..cf593ea 100644 --- a/tests/cli_hook_secret_formats.rs +++ b/tests/cli_hook_secret_formats.rs @@ -257,7 +257,10 @@ fn a_certificate_file_is_judged_by_all_of_its_text() { 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}"), + // Real output of `openssl req -x509 ... | openssl x509 -text`. + include_str!("fixtures/x509-text.pem").to_string(), + // curl's cacert.pem. + format!("##\n## Bundle of CA Root Certificates\n##\n\nFake Root CA\n============\n{CERT}"), ] { let p = project("", "server.pem", content.as_bytes()); assert!(!denied(p.pre_read("server.pem")), "{content:?}"); @@ -267,6 +270,9 @@ fn a_certificate_file_is_judged_by_all_of_its_text() { 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!(" API_TOKEN=fake-beside-a-cert\n{CERT}"), + format!("API_TOKEN=fake-beside-a-cert\n=======\n{CERT}"), + "-----BEGIN CERTIFICATE-----\nfake\n-----END PRIVATE KEY-----\n".to_string(), 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"), diff --git a/tests/fixtures/x509-text.pem b/tests/fixtures/x509-text.pem new file mode 100644 index 0000000..8c7ea22 --- /dev/null +++ b/tests/fixtures/x509-text.pem @@ -0,0 +1,60 @@ +Certificate: + Data: + Version: 3 (0x2) + Serial Number: + 72:fd:e7:8f:65:18:ed:4a:f6:0d:4c:30:ac:c4:d4:1a:26:47:ac:5b + Signature Algorithm: sha256WithRSAEncryption + Issuer: CN=fake.invalid + Validity + Not Before: Nov 7 08:22:41 2026 GMT + Not After : Nov 8 08:22:41 2026 GMT + Subject: CN=fake.invalid + Subject Public Key Info: + Public Key Algorithm: rsaEncryption + Public-Key: (1024 bit) + Modulus: + 00:c0:17:2f:44:a7:b0:38:20:81:6f:2e:f0:8d:b5: + 58:4b:5a:e3:e9:1e:e9:5e:08:c0:f8:e6:54:4d:b1: + 68:cc:e1:79:0a:7b:45:06:18:5d:5b:ce:b3:2b:33: + 93:78:89:26:d8:ff:7d:84:70:e1:7e:60:58:5e:57: + b6:9f:8d:6b:ee:2c:75:ad:c4:77:9a:fd:c7:5f:e8: + 75:63:21:34:31:1d:32:6f:8d:36:58:b9:5e:3e:f2: + 02:e3:8f:ba:40:4d:21:a6:85:75:73:43:9e:37:07: + 9e:85:6f:43:41:0f:b2:42:e8:d7:d0:8a:b1:49:e2: + 87:f1:5d:fc:d3:40:b2:46:bd + Exponent: 65537 (0x10001) + X509v3 extensions: + X509v3 Subject Key Identifier: + F1:FD:BA:7A:25:6C:EA:4F:9C:9B:F1:5A:0E:11:A2:5C:6F:EF:BD:5A + X509v3 Authority Key Identifier: + F1:FD:BA:7A:25:6C:EA:4F:9C:9B:F1:5A:0E:11:A2:5C:6F:EF:BD:5A + X509v3 Basic Constraints: critical + CA:TRUE + X509v3 Key Usage: + Digital Signature, Key Encipherment + X509v3 Extended Key Usage: + TLS Web Server Authentication + Signature Algorithm: sha256WithRSAEncryption + Signature Value: + 84:9a:b8:03:32:0b:8f:16:f8:fb:5d:8f:97:9f:cf:a1:37:85: + 7c:75:e2:47:df:ca:1b:6c:d2:02:96:5c:8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TIFICATE-----