Skip to content

Commit c0327ec

Browse files
committed
[security] Fix uninitialized memory disclosure in websocket.close()
When the `reason` argument for `websocket.close()` is a `TypedArray` instead of a string or `Buffer`, the function does not correctly overwrite the dirty buffer allocated via `Buffer.allocUnsafe()`. This results in the disclosure of uninitialized memory, potentially leaking sensitive data to the remote peer. Add stricter validation for the argument type.
1 parent ce2a3d6 commit c0327ec

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/sender.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
const { Duplex } = require('stream');
66
const { randomFillSync } = require('crypto');
7+
const {
8+
types: { isUint8Array }
9+
} = require('util');
710

811
const PerMessageDeflate = require('./permessage-deflate');
912
const { EMPTY_BUFFER, kWebSocket, NOOP } = require('./constants');
@@ -200,8 +203,10 @@ class Sender {
200203

201204
if (typeof data === 'string') {
202205
buf.write(data, 2);
203-
} else {
206+
} else if (isUint8Array(data)) {
204207
buf.set(data, 2);
208+
} else {
209+
throw new TypeError('Second argument must be a string or a Uint8Array');
205210
}
206211
}
207212

test/sender.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,16 @@ describe('Sender', () => {
439439
);
440440
});
441441

442+
it('throws an error if the second argument is invalid', () => {
443+
const mockSocket = new MockSocket();
444+
const sender = new Sender(mockSocket);
445+
446+
assert.throws(
447+
() => sender.close(1000, new Float32Array(20)),
448+
/^TypeError: Second argument must be a string or a Uint8Array$/
449+
);
450+
});
451+
442452
it('throws an error if the message is greater than 123 bytes', () => {
443453
const mockSocket = new MockSocket();
444454
const sender = new Sender(mockSocket);

0 commit comments

Comments
 (0)