Truly independent web browser
LibJS: Create functions in their own realm, not their prototype's
Shannon Booth43d17cec816d
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rsindex 64533531f29..b3d2601181b 100644--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs@@ -250,7 +250,7 @@ impl EcmascriptFunctionObject {shared_data.note_closure_created();let function = if private_environment.is_some() || home_object.is_some() || shared_data.is_class_constructor() {let function = allocate_object(vm, |defer_gc| EcmascriptFunctionObjectWithRareData {- base: Self::new(defer_gc, vm, shared_data, parent_environment, prototype),+ base: Self::new(defer_gc, vm, realm, shared_data, parent_environment, prototype),private_environment: NullableMember::new(private_environment),home_object: NullableMember::new(home_object),storage: EcmascriptFunctionObjectStorage::default(),@@ -259,7 +259,7 @@ impl EcmascriptFunctionObject {function.upcast()} else {allocate_object(vm, |defer_gc| {- Self::new(defer_gc, vm, shared_data, parent_environment, prototype)+ Self::new(defer_gc, vm, realm, shared_data, parent_environment, prototype)})};function.initialize(vm, realm);@@ -299,32 +299,28 @@ impl EcmascriptFunctionObject {fn new(_: &DeferGc<'_>,vm: &Vm,+ realm: Gc<Realm>,shared_data: Gc<SharedFunctionInstanceData>,parent_environment: Option<Gc<Environment>>,prototype: Gc<Object>,) -> Self {- let base =- FunctionObject::new_with_prototype(vm, Self::CLASS, prototype, MayInterfereWithIndexedPropertyAccess::No);- base.set_is_ecmascript_function_object();- base.class_bits.set(ecmascript_function_flags::IS_ECMASCRIPT_FUNCTION);-// OPTIMIZATION: Start from a premade shape: normal and async functions create their properties lazily, and// the other kinds already have theirs in spec order. Arrow functions use the same shape as other// functions of their kind, but never get a lazy prototype.- let realm = base.shape().realm();- let function_shape = match shared_data.kind() {+ let mut shape = match shared_data.kind() {FunctionKind::Normal => realm.normal_function_shape(),FunctionKind::Generator => realm.generator_function_shape(),FunctionKind::Async => realm.async_function_shape(),FunctionKind::AsyncGenerator => realm.async_generator_function_shape(),};- // NB: unsafe_set_shape(); the named storage gets room for the shape's properties when the function is allocated.- if function_shape.prototype() == Some(prototype) {- base.shape.set(function_shape);- } else {- base.shape- .set(function_shape.create_prototype_transition(vm, Some(prototype)));+ // NB: The function's [[Realm]] is the realm of its shape, so the shape comes from the function's realm even when+ // the prototype belongs to another one.+ if shape.prototype() != Some(prototype) {+ shape = shape.create_prototype_transition(vm, Some(prototype));}+ let base = FunctionObject::new_with_shape(Self::CLASS, shape, MayInterfereWithIndexedPropertyAccess::No);+ base.set_is_ecmascript_function_object();+ base.class_bits.set(ecmascript_function_flags::IS_ECMASCRIPT_FUNCTION);let function = Self { base };function.fields().shared_data.set(Some(shared_data));diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 5b165e229f3..f1668d5260a 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -3,6 +3,8 @@ Layout/input/pdf-viewer.pdfText/input/PDF/jbig2-image.html[LoadFromHttpServer]+; A data: iframe stays in this process only under an http: page.+Text/input/HTML/dynamic-function-foreign-prototype-realm.htmlText/input/wpt-import/web-locks/acquire.https.any.worker.htmlText/input/wpt-import/web-locks/signal.https.any.worker.htmlText/input/wpt-import/html/semantics/forms/the-input-element/input-untrusted-key-event.htmldiff --git a/Tests/LibWeb/Text/expected/HTML/dynamic-function-foreign-prototype-realm.txt b/Tests/LibWeb/Text/expected/HTML/dynamic-function-foreign-prototype-realm.txtnew file mode 100644index 00000000000..ada9eb42095--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/dynamic-function-foreign-prototype-realm.txt@@ -0,0 +1,9 @@+same-process cross-origin frame: true+same-origin Function: prototype from NewTarget: true, this is caller's global: true+same-origin GeneratorFunction: prototype from NewTarget: true, this is caller's global: true+same-origin AsyncFunction: prototype from NewTarget: true, this is caller's global: true+same-origin AsyncGeneratorFunction: prototype from NewTarget: true, this is caller's global: true+cross-origin Function: prototype from NewTarget: true, this is caller's global: true+cross-origin GeneratorFunction: prototype from NewTarget: true, this is caller's global: true+cross-origin AsyncFunction: prototype from NewTarget: true, this is caller's global: true+cross-origin AsyncGeneratorFunction: prototype from NewTarget: true, this is caller's global: truediff --git a/Tests/LibWeb/Text/input/HTML/dynamic-function-foreign-prototype-realm.html b/Tests/LibWeb/Text/input/HTML/dynamic-function-foreign-prototype-realm.htmlnew file mode 100644index 00000000000..e7d12f8fdfd--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/dynamic-function-foreign-prototype-realm.html@@ -0,0 +1,32 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ const sameOrigin = document.createElement("iframe");+ document.body.append(sameOrigin);+ const crossOrigin = document.createElement("iframe");+ crossOrigin.src = "data:text/html,<!DOCTYPE html>";+ await new Promise(resolve => {+ crossOrigin.onload = resolve;+ document.body.append(crossOrigin);+ });+ println(`same-process cross-origin frame: ${remoteFrameCount() === 0}`);++ const constructors = {+ Function: [Function, "return this", f => f()],+ GeneratorFunction: [Object.getPrototypeOf(function* () {}).constructor, "yield this", f => f().next().value],+ AsyncFunction: [Object.getPrototypeOf(async function () {}).constructor, "return this", f => f()],+ AsyncGeneratorFunction: [Object.getPrototypeOf(async function* () {}).constructor, "yield this", async f => (await f().next()).value],+ };+ for (const [label, prototype] of [["same-origin", sameOrigin.contentWindow], ["cross-origin", crossOrigin.contentWindow]]) {+ for (const [name, [constructor, body, thisOf]] of Object.entries(constructors)) {+ function NewTarget() {}+ NewTarget.prototype = prototype;+ const f = Reflect.construct(constructor, [body], NewTarget);+ const thisValue = await thisOf(f);+ println(`${label} ${name}: prototype from NewTarget: ${Object.getPrototypeOf(f) === prototype}, this is caller's global: ${thisValue === window}`);+ }+ }+ done();+ });+</script>