Repository navigation
Fix structuredClone WeakMap/WeakSet/Promise prototype mutations #60003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fe741ff
c1c6035
4eda213
e41db8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,16 @@ describe('structuredClone', () => { | |
| expectDataCloneError(() => structuredClone(Promise.resolve(4))); | ||
| }); | ||
|
|
||
| it('does not write to WeakMap, WeakSet, or Promise prototypes', () => { | ||
| Object.freeze(WeakMap.prototype); | ||
| Object.freeze(WeakSet.prototype); | ||
| Object.freeze(Promise.prototype); | ||
|
|
||
| expectDataCloneError(() => structuredClone(new WeakMap())); | ||
| expectDataCloneError(() => structuredClone(new WeakSet())); | ||
| expectDataCloneError(() => structuredClone(new Promise(() => {}))); | ||
|
Comment on lines
+124
to
+127
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possible comment worth it here clarifying i.e. mutated prototypes throw a loud: cannot add a new property (#60002) |
||
| }); | ||
|
|
||
| it('clones simple objects', () => { | ||
| const value = {foo: 'bar'}; | ||
| const clone = structuredClone(value); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -295,8 +295,6 @@ export default function structuredClone<T>(value: T): T { | |
| } | ||
| } | ||
|
|
||
| const NON_SERIALIZABLE_OBJECT_KEY = Symbol('nonSerializableObject'); | ||
|
|
||
| function cloneArrayBuffer(value: ArrayBuffer): ArrayBuffer { | ||
| const existingClone = memory.get(value); | ||
| if (existingClone instanceof ArrayBuffer) { | ||
|
|
@@ -392,16 +390,11 @@ function createErrorClone(value: Error): Error { | |
| } | ||
|
|
||
| function isNonSerializableObject<T extends interface {}>(obj: T): boolean { | ||
| // $FlowExpectedError[invalid-in-lhs] | ||
| return NON_SERIALIZABLE_OBJECT_KEY in obj; | ||
| } | ||
|
|
||
| function markClassAsNonSerializable<T>(cls: Class<T>): void { | ||
| // $FlowExpectedError[incompatible-use] | ||
| cls.prototype[NON_SERIALIZABLE_OBJECT_KEY] = true; | ||
| // Built-in prototypes must not be written to because Hardened JS | ||
| // environments (SES) freeze them, so the non-serializable | ||
| // built-ins are detected with `instanceof` instead of marking the | ||
| // prototypes with e.g. Symbol('nonSerializableObject'). | ||
|
Comment on lines
+393
to
+396
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a required comment, can be deduced from git history (blame / prev commit) but the human context is helpful |
||
| return ( | ||
| obj instanceof WeakMap || obj instanceof WeakSet || obj instanceof Promise | ||
| ); | ||
| } | ||
|
|
||
| // Non-serializable built-ins. | ||
| markClassAsNonSerializable(WeakMap); | ||
| markClassAsNonSerializable(WeakSet); | ||
| markClassAsNonSerializable(Promise); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: *NOT