Skip to content

Commit 0179c6d

Browse files
aduh95UlisesGascon
authored andcommitted
worker: protect against user mutating well-known prototypes
PR-URL: #49270 Fixes: #49259 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 81ff68c commit 0179c6d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/internal/worker/io.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
const {
2323
kEmptyObject,
2424
kEnumerableProperty,
25+
setOwnProperty,
2526
} = require('internal/util');
2627

2728
const {
@@ -302,15 +303,15 @@ function setupPortReferencing(port, eventEmitter, eventName) {
302303
if (name === eventName) removeListener(eventEmitter.listenerCount(name));
303304
});
304305
const origNewListener = eventEmitter[kNewListener];
305-
eventEmitter[kNewListener] = function(size, type, ...args) {
306+
setOwnProperty(eventEmitter, kNewListener, function(size, type, ...args) {
306307
if (type === eventName) newListener(size - 1);
307308
return ReflectApply(origNewListener, this, arguments);
308-
};
309+
});
309310
const origRemoveListener = eventEmitter[kRemoveListener];
310-
eventEmitter[kRemoveListener] = function(size, type, ...args) {
311+
setOwnProperty(eventEmitter, kRemoveListener, function(size, type, ...args) {
311312
if (type === eventName) removeListener(size);
312313
return ReflectApply(origRemoveListener, this, arguments);
313-
};
314+
});
314315

315316
function newListener(size) {
316317
if (size === 0) {

test/parallel/test-worker-message-channel.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const common = require('../common');
33
const assert = require('assert');
44
const { MessageChannel, MessagePort, Worker } = require('worker_threads');
55

6+
// Asserts that freezing the EventTarget prototype does not make the internal throw.
7+
Object.freeze(EventTarget.prototype);
8+
69
{
710
const channel = new MessageChannel();
811

0 commit comments

Comments
 (0)