Skip to content

Commit

Permalink
worker: add brand checks for detached MessageEvent accessors
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #39773
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
jasnell authored and targos committed Sep 6, 2021
1 parent 71659fd commit 3b1ce93
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
44 changes: 39 additions & 5 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ function validateMessagePort(port, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'MessagePort', port);
}

function isMessageEvent(value) {
return value != null && kData in value;
}

class MessageEvent extends Event {
constructor(type, {
data = null,
Expand All @@ -131,19 +135,49 @@ class MessageEvent extends Event {

ObjectDefineProperties(MessageEvent.prototype, {
data: {
get() { return this[kData]; }, enumerable: true, configurable: true
get() {
if (!isMessageEvent(this))
throw new ERR_INVALID_THIS('MessageEvent');
return this[kData];
},
enumerable: true,
configurable: true,
},
origin: {
get() { return this[kOrigin]; }, enumerable: true, configurable: true
get() {
if (!isMessageEvent(this))
throw new ERR_INVALID_THIS('MessageEvent');
return this[kOrigin];
},
enumerable: true,
configurable: true,
},
lastEventId: {
get() { return this[kLastEventId]; }, enumerable: true, configurable: true
get() {
if (!isMessageEvent(this))
throw new ERR_INVALID_THIS('MessageEvent');
return this[kLastEventId];
},
enumerable: true,
configurable: true,
},
source: {
get() { return this[kSource]; }, enumerable: true, configurable: true
get() {
if (!isMessageEvent(this))
throw new ERR_INVALID_THIS('MessageEvent');
return this[kSource];
},
enumerable: true,
configurable: true,
},
ports: {
get() { return this[kPorts]; }, enumerable: true, configurable: true
get() {
if (!isMessageEvent(this))
throw new ERR_INVALID_THIS('MessageEvent');
return this[kPorts];
},
enumerable: true,
configurable: true,
},
});

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-messageevent-brandcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Flags: --expose-internals
'use strict';

require('../common');
const assert = require('assert');

const {
MessageEvent,
} = require('internal/worker/io');

[
'data',
'origin',
'lastEventId',
'source',
'ports',
].forEach((i) => {
assert.throws(() => Reflect.get(MessageEvent.prototype, i, {}), {
code: 'ERR_INVALID_THIS',
});
});

0 comments on commit 3b1ce93

Please sign in to comment.