Skip to content

Commit

Permalink
fix: use isArrayBuffer instead of isAnyArrayBuffer (nodejs#2586)
Browse files Browse the repository at this point in the history
* fix: use isArrayBuffer instead of isAnyArrayBuffer

* test: add

* fixup & test

* fixup

* fixup

* fix: use ArrayBuffer.isView
  • Loading branch information
tsctx authored and crysmags committed Feb 27, 2024
1 parent 548356c commit 5df5a25
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
10 changes: 2 additions & 8 deletions lib/fetch/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ webidl.converters.BlobPart = function (V, opts) {
return webidl.converters.Blob(V, { strict: false })
}

if (
ArrayBuffer.isView(V) ||
types.isAnyArrayBuffer(V)
) {
if (ArrayBuffer.isView(V) || types.isAnyArrayBuffer(V)) {
return webidl.converters.BufferSource(V, opts)
}
}
Expand Down Expand Up @@ -282,10 +279,7 @@ function processBlobParts (parts, options) {

// 3. Append the result of UTF-8 encoding s to bytes.
bytes.push(encoder.encode(s))
} else if (
types.isAnyArrayBuffer(element) ||
types.isTypedArray(element)
) {
} else if (ArrayBuffer.isView(element) || types.isArrayBuffer(element)) {
// 2. If element is a BufferSource, get a copy of the
// bytes held by the buffer source, and append those
// bytes to bytes.
Expand Down
2 changes: 1 addition & 1 deletion lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ webidl.converters.XMLHttpRequestBodyInit = function (V) {
return webidl.converters.Blob(V, { strict: false })
}

if (types.isArrayBuffer(V) || types.isTypedArray(V) || types.isDataView(V)) {
if (ArrayBuffer.isView(V) || types.isArrayBuffer(V)) {
return webidl.converters.BufferSource(V)
}

Expand Down
6 changes: 3 additions & 3 deletions lib/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,15 @@ webidl.converters.DataView = function (V, opts = {}) {
// https://webidl.spec.whatwg.org/#BufferSource
webidl.converters.BufferSource = function (V, opts = {}) {
if (types.isAnyArrayBuffer(V)) {
return webidl.converters.ArrayBuffer(V, opts)
return webidl.converters.ArrayBuffer(V, { ...opts, allowShared: false })
}

if (types.isTypedArray(V)) {
return webidl.converters.TypedArray(V, V.constructor)
return webidl.converters.TypedArray(V, V.constructor, { ...opts, allowShared: false })
}

if (types.isDataView(V)) {
return webidl.converters.DataView(V, opts)
return webidl.converters.DataView(V, opts, { ...opts, allowShared: false })
}

throw new TypeError(`Could not convert ${V} to a BufferSource.`)
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ webidl.converters.WebSocketSendData = function (V) {
return webidl.converters.Blob(V, { strict: false })
}

if (ArrayBuffer.isView(V) || types.isAnyArrayBuffer(V)) {
if (ArrayBuffer.isView(V) || types.isArrayBuffer(V)) {
return webidl.converters.BufferSource(V)
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/fetch/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,16 @@ test('endings=native', async () => {
assert.strictEqual(text, 'Hello\nWorld', `on ${process.platform} LF stays LF`)
}
})

test('not allow SharedArrayBuffer', () => {
const buffer = new SharedArrayBuffer(0)
assert.throws(() => {
// eslint-disable-next-line no-new
new File([buffer], 'text.txt')
}, TypeError)

assert.throws(() => {
// eslint-disable-next-line no-new
new File([new Uint8Array(buffer)], 'text.txt')
}, TypeError)
})
23 changes: 23 additions & 0 deletions test/websocket/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,27 @@ describe('Sending data to a server', () => {
})
})
})

test('Cannot send with SharedArrayBuffer', () => {
const sab = new SharedArrayBuffer(0)
const server = new WebSocketServer({ port: 0 })

const ws = new WebSocket(`ws://localhost:${server.address().port}`)

ws.addEventListener('open', () => {
ws.send(sab)
})

return new Promise((resolve) => {
server.on('connection', (ws) => {
ws.on('message', (data, isBinary) => {
assert.ok(!isBinary)
assert.deepStrictEqual(data, Buffer.from('[object SharedArrayBuffer]'))
ws.close(1000)
server.close()
resolve()
})
})
})
})
})

0 comments on commit 5df5a25

Please sign in to comment.