Skip to content

Commit 2054efa

Browse files
marsonyadanielleadams
authored andcommitted
events: refactor to use primordials in lib/events
Replace code that's vulnerable to Prototype Pollution with Primordials. PR-URL: #38117 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c576311 commit 2054efa

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

lib/events.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
'use strict';
2323

2424
const {
25+
ArrayPrototypeIndexOf,
26+
ArrayPrototypeJoin,
27+
ArrayPrototypeShift,
2528
ArrayPrototypeSlice,
29+
ArrayPrototypeSplice,
2630
Boolean,
2731
Error,
2832
ErrorCaptureStackTrace,
33+
FunctionPrototypeBind,
34+
FunctionPrototypeCall,
2935
MathMin,
3036
NumberIsNaN,
3137
ObjectCreate,
@@ -38,9 +44,10 @@ const {
3844
PromiseResolve,
3945
ReflectOwnKeys,
4046
String,
47+
StringPrototypeSplit,
4148
Symbol,
4249
SymbolFor,
43-
SymbolAsyncIterator
50+
SymbolAsyncIterator,
4451
} = primordials;
4552
const kRejection = SymbolFor('nodejs.rejection');
4653

@@ -265,7 +272,7 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
265272
function identicalSequenceRange(a, b) {
266273
for (let i = 0; i < a.length - 3; i++) {
267274
// Find the first entry of b that matches the current entry of a.
268-
const pos = b.indexOf(a[i]);
275+
const pos = ArrayPrototypeIndexOf(b, a[i]);
269276
if (pos !== -1) {
270277
const rest = b.length - pos;
271278
if (rest > 3) {
@@ -294,16 +301,18 @@ function enhanceStackTrace(err, own) {
294301
} catch {}
295302
const sep = `\nEmitted 'error' event${ctorInfo} at:\n`;
296303

297-
const errStack = err.stack.split('\n').slice(1);
298-
const ownStack = own.stack.split('\n').slice(1);
304+
const errStack = ArrayPrototypeSlice(
305+
StringPrototypeSplit(err.stack, '\n'), 1);
306+
const ownStack = ArrayPrototypeSlice(
307+
StringPrototypeSplit(own.stack, '\n'), 1);
299308

300309
const { 0: len, 1: off } = identicalSequenceRange(ownStack, errStack);
301310
if (len > 0) {
302-
ownStack.splice(off + 1, len - 2,
303-
' [... lines matching original stack trace ...]');
311+
ArrayPrototypeSplice(ownStack, off + 1, len - 2,
312+
' [... lines matching original stack trace ...]');
304313
}
305314

306-
return err.stack + sep + ownStack.join('\n');
315+
return err.stack + sep + ArrayPrototypeJoin(ownStack, '\n');
307316
}
308317

309318
EventEmitter.prototype.emit = function emit(type, ...args) {
@@ -327,7 +336,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
327336
const capture = {};
328337
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
329338
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
330-
value: enhanceStackTrace.bind(this, er, capture),
339+
value: FunctionPrototypeBind(enhanceStackTrace, this, er, capture),
331340
configurable: true
332341
});
333342
} catch {}
@@ -620,7 +629,7 @@ EventEmitter.listenerCount = function(emitter, type) {
620629
if (typeof emitter.listenerCount === 'function') {
621630
return emitter.listenerCount(type);
622631
}
623-
return listenerCount.call(emitter, type);
632+
return FunctionPrototypeCall(listenerCount, emitter, type);
624633
};
625634

626635
EventEmitter.prototype.listenerCount = listenerCount;
@@ -858,7 +867,7 @@ function on(emitter, event, options) {
858867
}
859868

860869
function eventHandler(...args) {
861-
const promise = unconsumedPromises.shift();
870+
const promise = ArrayPrototypeShift(unconsumedPromises);
862871
if (promise) {
863872
promise.resolve(createIterResult(args, false));
864873
} else {
@@ -869,7 +878,7 @@ function on(emitter, event, options) {
869878
function errorHandler(err) {
870879
finished = true;
871880

872-
const toError = unconsumedPromises.shift();
881+
const toError = ArrayPrototypeShift(unconsumedPromises);
873882

874883
if (toError) {
875884
toError.reject(err);

0 commit comments

Comments
 (0)