Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/boringssl/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,22 @@ pub unsafe extern "C" fn Bun__X509__checkHost(
0
}

/// Node's `IsSafeAltName` for IA5/Latin-1 entries: true when `name` contains
/// only printable ASCII with no `"`, `\\`, `,`, or `'`, so it can be rendered
/// in a subjectAltName list without JSON-style quoting.
#[inline]
fn is_safe_alt_name(name: &[u8]) -> bool {
name.iter()
.all(|&c| matches!(c, b' '..=b'~') && !matches!(c, b'"' | b'\\' | b',' | b'\''))
}

/// Certificate name bytes (IA5 / Latin-1) for error messages.
struct NameBytes<'a>(&'a [u8]);

impl core::fmt::Display for NameBytes<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use core::fmt::Write;
if X509::is_safe_alt_name(self.0, false) {
if is_safe_alt_name(self.0) {
for &byte in self.0 {
f.write_char(char::from(byte))?;
}
Expand Down Expand Up @@ -730,3 +739,34 @@ pub fn check_server_identity(ssl_ptr: &mut boring::SSL, hostname: &[u8]) -> bool
.peer_leaf_certificate()
.is_some_and(|x509| check_x509_server_identity(x509, hostname))
}

#[cfg(test)]
mod tests {
use super::{NameBytes, is_safe_alt_name};

#[test]
fn safe_alt_name_matches_node() {
assert!(is_safe_alt_name(b"good.example.com"));
assert!(is_safe_alt_name(b"http://example.com/a%2Cb"));
assert!(is_safe_alt_name(b" ~"));
for &c in b"\"\\,'" {
assert!(!is_safe_alt_name(&[c]));
}
assert!(!is_safe_alt_name(b"ab\x1f"));
assert!(!is_safe_alt_name(b"ab\x7f"));
assert!(!is_safe_alt_name(b"ex\xe4mple.com"));
}

#[test]
fn name_bytes_quoting() {
let fmt = |s: &[u8]| NameBytes(s).to_string();
assert_eq!(fmt(b"good.example.com"), "good.example.com");
assert_eq!(
fmt(b"good.example.com, DNS:evil.example.com"),
r#""good.example.com\u002c DNS:evil.example.com""#
);
assert_eq!(fmt(b"\"quoted\""), r#""\"quoted\"""#);
assert_eq!(fmt(b"ex\xe4mple.com"), r#""ex\u00e4mple.com""#);
assert_eq!(fmt(b"a\\b"), r#""a\\b""#);
}
}
Loading