Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fonts): construct CSS font faces through realm intrinsics

ldm0120d03cfbfc9
diff --git a/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs b/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs
index b6888f19d..29b9a77af 100644
--- a/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs
+++ b/moli-renderer-v8/src/native_bridge/document/css_state/font_faces.rs
@@ -13,7 +13,7 @@ use crate::{
StylesheetFontFaceDescriptor, StylesheetFontFaceProjection,
StylesheetFontFaceRuleProjection,
},
- util::{get_private_value, serialize_v8_iter_array, set_private_value, v8_string, v8str},
+ util::{get_private_value, serialize_v8_iter_array, set_private_value, v8_string},
};
use std::convert::TryFrom;
use std::rc::Rc;
@@ -66,9 +66,11 @@ fn construct_font_face_from_parts<'s>(
family: &str,
source: &str,
) -> Option<v8::Local<'s, v8::Value>> {
- let global = scope.get_current_context().global(scope);
- let ctor_value = global.get(scope, v8str(scope, "FontFace").into())?;
- let ctor = v8::Local::<v8::Function>::try_from(ctor_value).ok()?;
+ let ctor =
+ crate::context_bootstrap::exposed_interfaces::ensure_intrinsic_interface_constructor(
+ scope, "FontFace",
+ )
+ .ok()?;
let family = v8_string(scope, family)?;
let source = v8_string(scope, source)?;
ctor.new_instance(scope, &[family.into(), source.into()])
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/fonts.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/fonts.rs
index c1d37332f..a13b7ed91 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/fonts.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/fonts.rs
@@ -964,6 +964,58 @@ fn font_face_set_declared_slots_ignore_prototype_spoofing() {
r#"{"real":"loaded|function|1|true|1|1","fake":"TypeError|TypeError|TypeError|TypeError","documentFontsOwnerSlot":false,"documentFontsSlots":[],"descriptors":["status:function:get status:0:undefined:undefined:undefined:true:true:false","ready:function:get ready:0:undefined:undefined:undefined:true:true:false","size:function:get size:0:undefined:undefined:undefined:true:true:false"],"hasOwnSize":false,"ownSlots":[]}"#
);
}
+#[test]
+fn stylesheet_font_faces_use_intrinsics_after_public_constructor_replacement() {
+ for materialize_first in [false, true] {
+ for replacement in [
+ "{ configurable: true, get() { calls++; throw new Error('public getter'); } }",
+ "{ configurable: true, value: function() { calls++; throw new Error('public constructor'); } }",
+ ] {
+ let mut vm = new_storage_test_vm("https://font-face-intrinsic.test/");
+ let result = vm.eval(&format!(r#"
+(() => {{
+ let calls = 0;
+ const parent = document.body || document.documentElement || document;
+ const frame = parent.appendChild(document.createElement('iframe'));
+ return JSON.stringify([window, frame.contentWindow].map(w => {{
+ const original = {materialize_first} ? w.FontFace : null;
+ Object.defineProperty(w, 'FontFace', {replacement});
+ const d = w.document, fonts = d.fonts;
+ const style = d.createElement('style');
+ style.textContent = '@font-face {{ font-family: OriginalFace; src: local(OriginalFace); }}';
+ (d.head || d.documentElement || d).appendChild(style);
+ const first = Array.from(fonts)[0];
+ style.setAttribute('data-irrelevant', 'value');
+ const stable = Array.from(fonts)[0] === first;
+ style.sheet.insertRule('@font-face {{ font-family: Inserted; src: local(Inserted); }}', 1);
+ const faces = Array.from(fonts);
+ const result = {{
+ families: faces.map(face => face.family),
+ realm: faces.every(face => Object.getPrototypeOf(face).constructor instanceof w.Function),
+ prototype: !original || faces.every(face => Object.getPrototypeOf(face) === original.prototype),
+ stable: stable && faces[0] === first,
+ collection: fonts === d.fonts,
+ calls
+ }};
+ style.remove();
+ result.removed = fonts.size === 0;
+ return result;
+ }}));
+}})()
+"#)).expect("CSS font faces should bypass replaced public constructors");
+ let expected = serde_json::json!({
+ "families": ["OriginalFace", "Inserted"], "realm": true, "prototype": true,
+ "stable": true, "collection": true, "calls": 0, "removed": true,
+ });
+ assert_eq!(
+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),
+ serde_json::json!([expected, expected]),
+ "materialized: {materialize_first}, replacement: {replacement}"
+ );
+ }
+ }
+}
+
#[test]
fn document_fonts_tracks_connected_style_candidates() {
let mut vm = new_storage_test_vm("https://document-fonts-candidates.test/");
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs
index 54993a9ad..fe80333f9 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs
@@ -106,6 +106,10 @@ globalThis.realmFrame = document.createElement('iframe');
realmFrame.srcdoc = `<!doctype html><head><script>
globalThis.heldSheets = document.styleSheets;
globalThis.heldFonts = document.fonts;
+ globalThis.IntrinsicFontFace = FontFace;
+ Object.defineProperty(globalThis, 'FontFace', { get() {
+ throw new Error('parser must not read the public FontFace constructor');
+ }, configurable: true });
</` + `script><style>
@font-face { font-family: ChildParser; src: local('ChildParser'); }
div { color: green; }
@@ -144,7 +148,7 @@ realmFrame.srcdoc = `<!doctype html><head><script>
sheetRealm: sheets[0] instanceof w.CSSStyleSheet &&
sheets[0].cssRules[0] instanceof w.CSSFontFaceRule,
fontCount: faces.length,
- fontRealm: faces.every(face => face instanceof w.FontFace && !(face instanceof FontFace)),
+ fontRealm: faces.every(face => face instanceof w.IntrinsicFontFace && !(face instanceof FontFace)),
sheetListRealm: sheets instanceof w.StyleSheetList,
fontSetRealm: fonts instanceof w.FontFaceSet
};