Skip to content

Commit

Permalink
web: mark as uncloneable when possible (#3709)
Browse files Browse the repository at this point in the history
* web: mark as uncloneable when possible

This tells node that the marked instances from undici are not cloneable,
so that attempts to cloning those throw `DataCloneError`.

* test: add test cases for platform objects uncloneable

* fix: move to webidl

* fix: move it under webidl

* fixup: rename it to markAsUncloneable

* fix: mark more web instances uncloneable

* fixup

---------

Co-authored-by: Khafra <[email protected]>
(cherry picked from commit 1ab2382)
  • Loading branch information
jazelly committed Oct 17, 2024
1 parent 5344aa5 commit 429d59c
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/web/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Cache {
webidl.illegalConstructor()
}

webidl.util.markAsUncloneable(this)
this.#relevantRequestResponseList = arguments[1]
}

Expand Down
2 changes: 2 additions & 0 deletions lib/web/cache/cachestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CacheStorage {
if (arguments[0] !== kConstruct) {
webidl.illegalConstructor()
}

webidl.util.markAsUncloneable(this)
}

async match (request, options = {}) {
Expand Down
2 changes: 2 additions & 0 deletions lib/web/eventsource/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class EventSource extends EventTarget {
// 1. Let ev be a new EventSource object.
super()

webidl.util.markAsUncloneable(this)

const prefix = 'EventSource constructor'
webidl.argumentLengthCheck(arguments, 1, prefix)

Expand Down
2 changes: 2 additions & 0 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const File = globalThis.File ?? NativeFile
// https://xhr.spec.whatwg.org/#formdata
class FormData {
constructor (form) {
webidl.util.markAsUncloneable(this)

if (form !== undefined) {
throw webidl.errors.conversionFailed({
prefix: 'FormData constructor',
Expand Down
2 changes: 2 additions & 0 deletions lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ class Headers {
#headersList

constructor (init = undefined) {
webidl.util.markAsUncloneable(this)

if (init === kConstruct) {
return
}
Expand Down
1 change: 1 addition & 0 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ let patchMethodWarning = false
class Request {
// https://fetch.spec.whatwg.org/#dom-request
constructor (input, init = {}) {
webidl.util.markAsUncloneable(this)
if (input === kConstruct) {
return
}
Expand Down
1 change: 1 addition & 0 deletions lib/web/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Response {

// https://fetch.spec.whatwg.org/#dom-response
constructor (body = null, init = {}) {
webidl.util.markAsUncloneable(this)
if (body === kConstruct) {
return
}
Expand Down
2 changes: 2 additions & 0 deletions lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { types, inspect } = require('node:util')
const { markAsUncloneable } = require('node:worker_threads')
const { toUSVString } = require('../../core/util')

/** @type {import('../../../types/webidl').Webidl} */
Expand Down Expand Up @@ -86,6 +87,7 @@ webidl.util.Type = function (V) {
}
}

webidl.util.markAsUncloneable = markAsUncloneable || (() => {})
// https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
webidl.util.ConvertToInt = function (V, bitLength, signedness, opts) {
let upperBound
Expand Down
4 changes: 4 additions & 0 deletions lib/web/websocket/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MessageEvent extends Event {
constructor (type, eventInitDict = {}) {
if (type === kConstruct) {
super(arguments[1], arguments[2])
webidl.util.markAsUncloneable(this)
return
}

Expand All @@ -26,6 +27,7 @@ class MessageEvent extends Event {
super(type, eventInitDict)

this.#eventInit = eventInitDict
webidl.util.markAsUncloneable(this)
}

get data () {
Expand Down Expand Up @@ -112,6 +114,7 @@ class CloseEvent extends Event {
super(type, eventInitDict)

this.#eventInit = eventInitDict
webidl.util.markAsUncloneable(this)
}

get wasClean () {
Expand Down Expand Up @@ -142,6 +145,7 @@ class ErrorEvent extends Event {
webidl.argumentLengthCheck(arguments, 1, prefix)

super(type, eventInitDict)
webidl.util.markAsUncloneable(this)

type = webidl.converters.DOMString(type, prefix, 'type')
eventInitDict = webidl.converters.ErrorEventInit(eventInitDict ?? {})
Expand Down
2 changes: 2 additions & 0 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class WebSocket extends EventTarget {
constructor (url, protocols = []) {
super()

webidl.util.markAsUncloneable(this)

const prefix = 'WebSocket constructor'
webidl.argumentLengthCheck(arguments, 1, prefix)

Expand Down
33 changes: 33 additions & 0 deletions test/node-platform-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test } = require('node:test')
const { markAsUncloneable } = require('node:worker_threads')
const { Response, Request, FormData, Headers, ErrorEvent, MessageEvent, CloseEvent, EventSource, WebSocket } = require('..')
const { CacheStorage } = require('../lib/web/cache/cachestorage')
const { Cache } = require('../lib/web/cache/cache')
const { kConstruct } = require('../lib/core/symbols')

test('unserializable web instances should be uncloneable if node exposes the api', (t) => {
if (markAsUncloneable !== undefined) {
t = tspl(t, { plan: 11 })
const uncloneables = [
{ Uncloneable: Response, brand: 'Response' },
{ Uncloneable: Request, value: 'http://localhost', brand: 'Request' },
{ Uncloneable: FormData, brand: 'FormData' },
{ Uncloneable: MessageEvent, value: 'dummy event', brand: 'MessageEvent' },
{ Uncloneable: CloseEvent, value: 'dummy event', brand: 'CloseEvent' },
{ Uncloneable: ErrorEvent, value: 'dummy event', brand: 'ErrorEvent' },
{ Uncloneable: EventSource, value: 'http://localhost', brand: 'EventSource' },
{ Uncloneable: Headers, brand: 'Headers' },
{ Uncloneable: WebSocket, value: 'http://localhost', brand: 'WebSocket' },
{ Uncloneable: Cache, value: kConstruct, brand: 'Cache' },
{ Uncloneable: CacheStorage, value: kConstruct, brand: 'CacheStorage' }
]
uncloneables.forEach((platformEntity) => {
t.throws(() => structuredClone(new platformEntity.Uncloneable(platformEntity.value)),
DOMException,
`Cloning ${platformEntity.brand} should throw DOMException`)
})
}
})
6 changes: 6 additions & 0 deletions types/webidl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ interface WebidlUtil {
* Stringifies {@param V}
*/
Stringify (V: any): string

/**
* Mark a value as uncloneable for Node.js.
* This is only effective in some newer Node.js versions.
*/
markAsUncloneable (V: any): void
}

interface WebidlConverters {
Expand Down

0 comments on commit 429d59c

Please sign in to comment.