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‎

Lines changed: 20 additions & 11 deletions
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
constkRejection=SymbolFor('nodejs.rejection');
4653

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

297-
consterrStack=err.stack.split('\n').slice(1);
298-
constownStack=own.stack.split('\n').slice(1);
304+
consterrStack=ArrayPrototypeSlice(
305+
StringPrototypeSplit(err.stack,'\n'),1);
306+
constownStack=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-
returnerr.stack+sep+ownStack.join('\n');
315+
returnerr.stack+sep+ArrayPrototypeJoin(ownStack,'\n');
307316
}
308317

309318
EventEmitter.prototype.emit=functionemit(type, ...args){
@@ -327,7 +336,7 @@ EventEmitter.prototype.emit = function emit(type, ...args){
327336
constcapture={};
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(typeofemitter.listenerCount==='function'){
621630
returnemitter.listenerCount(type);
622631
}
623-
returnlistenerCount.call(emitter,type);
632+
returnFunctionPrototypeCall(listenerCount,emitter,type);
624633
};
625634

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

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

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

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

0 commit comments

Comments
(0)