Skip to content
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

fix: use isArrayBuffer instead of isAnyArrayBuffer #2586

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
tsctx marked this conversation as resolved.
Show resolved Hide resolved
// 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()
})
})
})
})
})