Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(fonts): validate every decoded font table extent
ldm0c2a927f105cf
diff --git a/Cargo.lock b/Cargo.lockindex df7281e42..7f8f917dd 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2808,6 +2808,7 @@ dependencies = ["moli-image","moli-system-fonts","parley",+ "read-fonts","stylo","stylo_taffy","taffy",diff --git a/moli-layout/Cargo.toml b/moli-layout/Cargo.tomlindex cefce6d20..9b4987770 100644--- a/moli-layout/Cargo.toml+++ b/moli-layout/Cargo.toml@@ -11,6 +11,7 @@ kurbo = "=0.13.1"moli-image = { path = "../moli-image" }moli-system-fonts = { path = "../moli-system-fonts" }parley = "=0.11.1"+read-fonts = "0.41.0"stylo = "0.20"stylo_taffy = { git = "https://github.com/DioxusLabs/blitz", rev = "5081c65811a4396f5a99b2e0aca542a4a4a6606f", default-features = false, features = ["std", "block", "flexbox", "grid", "floats"] }taffy = { version = "=0.13.0", default-features = false, features = ["std", "block_layout", "flexbox", "grid", "float_layout", "content_size", "calc", "detailed_layout_info"] }diff --git a/moli-layout/src/text.rs b/moli-layout/src/text.rsindex 3bf92cd18..5bff63a53 100644--- a/moli-layout/src/text.rs+++ b/moli-layout/src/text.rs@@ -921,6 +921,23 @@ fn validate_registered_font(face: &WebFontFace,sfnt_bytes: Arc<[u8]>,) -> Result<(), WebFontRegistrationError> {+ // Fontique reads metadata lazily and can register a font whose unused+ // tables extend past the supplied bytes. Validate every directory entry+ // before accepting either a single SFNT font or a font collection.+ let file = read_fonts::FileRef::new(&sfnt_bytes)+ .map_err(|_| WebFontRegistrationError::UnsupportedPayload)?;+ for font in file.fonts() {+ let font = font.map_err(|_| WebFontRegistrationError::UnsupportedPayload)?;+ for table in font.table_directory().table_records() {+ let start = table.offset() as usize;+ let end = start+ .checked_add(table.length() as usize)+ .ok_or(WebFontRegistrationError::UnsupportedPayload)?;+ if sfnt_bytes.get(start..end).is_none() {+ return Err(WebFontRegistrationError::UnsupportedPayload);+ }+ }+ }let mut font_context = FontContext {collection: Collection::new(CollectionOptions {shared: false,@@ -973,6 +990,58 @@ mod tests {const TEST_WOFF: &[u8] = include_bytes!("../tests/fixtures/moli-ahem.woff");const TEST_WOFF2: &[u8] = include_bytes!("../tests/fixtures/moli-ahem.woff2");+ fn validate_test_font_bytes(bytes: &[u8]) -> Result<(), WebFontRegistrationError> {+ validate_registered_font(&WebFontFace::new("FontFace"), decode_web_font_bytes(bytes)?)+ }++ #[test]+ fn font_validation_checks_decoding_and_every_table_extent() {+ for bytes in [TEST_TTF, TEST_WOFF, TEST_WOFF2] {+ assert!(validate_test_font_bytes(bytes).is_ok());+ for size in [0, 4, 12, 44, bytes.len() - 10] {+ assert!(+ validate_test_font_bytes(&bytes[..size]).is_err(),+ "length {size}"+ );+ }+ }++ // Corrupt a table offset and length independently, including values+ // whose sum would overflow u32. Metadata-only registration must not+ // make an out-of-bounds table acceptable.+ for field in [20, 24] {+ let mut bytes = TEST_TTF.to_vec();+ bytes[field..field + 4].copy_from_slice(&u32::MAX.to_be_bytes());+ assert!(validate_test_font_bytes(&bytes).is_err());+ }+ }++ #[test]+ fn font_validation_checks_all_faces_in_a_collection() {+ let mut collection = b"ttcf\0\x01\0\0\0\0\0\x02\0\0\0\0\0\0\0\0".to_vec();+ for index in 0..2 {+ let offset = collection.len() as u32;+ collection[12 + index * 4..16 + index * 4].copy_from_slice(&offset.to_be_bytes());+ let mut font = TEST_TTF.to_vec();+ let count = u16::from_be_bytes([font[4], font[5]]) as usize;+ for table in 0..count {+ let start = 20 + table * 16;+ let relative = u32::from_be_bytes(font[start..start + 4].try_into().unwrap());+ font[start..start + 4].copy_from_slice(&(relative + offset).to_be_bytes());+ }+ collection.extend(font);+ }+ assert!(validate_test_font_bytes(&collection).is_ok());+ for index in 0..2 {+ let mut corrupt = collection.clone();+ let offset =+ u32::from_be_bytes(corrupt[12 + index * 4..16 + index * 4].try_into().unwrap())+ as usize;+ corrupt[offset + 20..offset + 24].copy_from_slice(&u32::MAX.to_be_bytes());+ assert!(validate_test_font_bytes(&corrupt).is_err());+ }+ }+#[test]fn missing_x_height_uses_the_blink_ascent_fallback() {assert!((resolved_inline_x_height(10.0, None) - 5.6).abs() < f32::EPSILON);