Truly independent web browser
LibWeb: Throw a DataCloneError for an object with internal slots
Shannon Booth59208c0d45e0
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rsindex d09b17d6700..4be454bfa63 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs@@ -752,10 +752,13 @@ impl Serializer<'_> {else if value.is_function() {return Err(data_clone_error("Cannot serialize functions"));}- // FIXME: 23. Otherwise, if value has any internal slot other than [[Prototype]], [[Extensible]], or [[PrivateElements]], then throw a "DataCloneError" DOMException.-- // FIXME: 24. Otherwise, if value is an exotic object and value is not the %Object.prototype% intrinsic object associated with any realm, then throw a "DataCloneError" DOMException.-+ // 23. Otherwise, if value has any internal slot other than [[Prototype]], [[Extensible]], or [[PrivateElements]], then throw a "DataCloneError" DOMException.+ else if object.has_internal_slots() {+ return Err(data_clone_error("Cannot serialize an object with internal slots"));+ }+ // 24. Otherwise, if value is an exotic object and value is not the %Object.prototype% intrinsic object associated with any realm, then throw a "DataCloneError" DOMException.+ // NB: Step 19 serializes an Array, and step 23 throws for every other exotic object but an immutable prototype+ // exotic object, of which LibJS has only %Object.prototype%, and LibWeb only platform objects.// 25. Otherwise:else {// 1. Set serialized to { [[Type]]: "Object", [[Properties]]: a new empty List }.diff --git a/Tests/LibWeb/Text/expected/structured-clone-internal-slots-and-exotic-objects.txt b/Tests/LibWeb/Text/expected/structured-clone-internal-slots-and-exotic-objects.txtnew file mode 100644index 00000000000..4ba930ad38c--- /dev/null+++ b/Tests/LibWeb/Text/expected/structured-clone-internal-slots-and-exotic-objects.txt@@ -0,0 +1,11 @@+Proxy with a throwing ownKeys trap: DataCloneError+Proxy of a plain object: DataCloneError+WeakMap: DataCloneError+Promise: DataCloneError+generator object: DataCloneError+sloppy arguments object: DataCloneError+strict arguments object: DataCloneError+Object.prototype: 0 keys, prototype is Object.prototype: true+Map.prototype: 0 keys, prototype is Object.prototype: true+Math: 0 keys, prototype is Object.prototype: true+null-prototype object: a=1diff --git a/Tests/LibWeb/Text/input/structured-clone-internal-slots-and-exotic-objects.html b/Tests/LibWeb/Text/input/structured-clone-internal-slots-and-exotic-objects.htmlnew file mode 100644index 00000000000..1b0bf7522f2--- /dev/null+++ b/Tests/LibWeb/Text/input/structured-clone-internal-slots-and-exotic-objects.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ test(() => {+ const cases = [+ ["Proxy with a throwing ownKeys trap", () => new Proxy({}, { ownKeys() { throw new Error("trap"); } })],+ ["Proxy of a plain object", () => new Proxy({ a: 1 }, {})],+ ["WeakMap", () => new WeakMap()],+ ["Promise", () => Promise.resolve()],+ ["generator object", () => (function* () {})()],+ ["sloppy arguments object", function () { return arguments; }],+ ["strict arguments object", function () { "use strict"; return arguments; }],+ ];+ for (const [name, value] of cases) {+ try {+ structuredClone(value());+ println(`${name}: no exception`);+ } catch (error) {+ println(`${name}: ${error.name}`);+ }+ }++ for (const [name, value] of [["Object.prototype", Object.prototype], ["Map.prototype", Map.prototype], ["Math", Math]]) {+ const clone = structuredClone(value);+ println(`${name}: ${Object.keys(clone).length} keys, prototype is Object.prototype: ${Object.getPrototypeOf(clone) === Object.prototype}`);+ }++ const nullPrototypeClone = structuredClone(Object.assign(Object.create(null), { a: 1 }));+ println(`null-prototype object: a=${nullPrototypeClone.a}`);+ });+</script>