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

events: prevent undefined as an event name #21007

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ function enhanceStackTrace(err, own) {
}

EventEmitter.prototype.emit = function emit(type, ...args) {
validateEventName(type, 'eventName');

let doError = (type === 'error');

const events = this._events;
Expand Down Expand Up @@ -200,6 +202,7 @@ function _addListener(target, type, listener, prepend) {
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}

validateEventName(type, 'eventName');
events = target._events;
if (events === undefined) {
events = target._events = Object.create(null);
Expand Down Expand Up @@ -311,6 +314,7 @@ EventEmitter.prototype.removeListener =
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}

validateEventName(type, 'eventName');
events = this._events;
if (events === undefined)
return this;
Expand Down Expand Up @@ -480,3 +484,13 @@ function unwrapListeners(arr) {
}
return ret;
}

function validateEventName(name, paramName) {
if (name === undefined) {
const { ERR_INVALID_ARG_TYPE } = lazyErrors();
const err = new ERR_INVALID_ARG_TYPE(paramName, ['string', 'symbol'], name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there other possible paramNames? Otherwise I think it would be best to inline that string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.


Error.captureStackTrace(err, validateEventName);
throw err;
}
}
11 changes: 11 additions & 0 deletions test/parallel/test-event-emitter-add-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,14 @@ common.expectsError(() => {
message: 'The "listener" argument must be of type Function. ' +
'Received type object'
});

// Verify that the event type cannot be undefined.
common.expectsError(() => {
const ee = new EventEmitter();
ee.on(undefined, () => {});
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "eventName" argument must be one of type string or symbol. ' +
'Received type undefined'
});
10 changes: 10 additions & 0 deletions test/parallel/test-event-emitter-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ common.expectsError(
message: 'Unhandled error. ([object Object])'
}
);

// Verify that the event type cannot be undefined.
common.expectsError(() => {
EE.emit(undefined, new Error());
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "eventName" argument must be one of type string or symbol. ' +
'Received type undefined'
});
11 changes: 11 additions & 0 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ common.expectsError(() => {
'Received type object'
});

// Verify that the event type cannot be undefined.
common.expectsError(() => {
const ee = new EventEmitter();
ee.removeListener(undefined, () => {});
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "eventName" argument must be one of type string or symbol. ' +
'Received type undefined'
});

{
const ee = new EventEmitter();
const listener = () => {};
Expand Down