Skip to content

Commit 75707f4

Browse files
aduh95codebytere
authored andcommitted
lib,tools: enforce access to prototype from primordials
PR-URL: #36025 Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 3be5e86 commit 75707f4

4 files changed

Lines changed: 100 additions & 54 deletions

File tree

‎lib/internal/freeze_intrinsics.js‎

Lines changed: 88 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,42 @@
2525
const{
2626
Array,
2727
ArrayBuffer,
28+
ArrayBufferPrototype,
29+
ArrayPrototype,
2830
ArrayPrototypeForEach,
31+
ArrayPrototypePush,
2932
BigInt,
3033
BigInt64Array,
34+
BigInt64ArrayPrototype,
35+
BigIntPrototype,
3136
BigUint64Array,
37+
BigUint64ArrayPrototype,
3238
Boolean,
39+
BooleanPrototype,
3340
DataView,
41+
DataViewPrototype,
3442
Date,
43+
DatePrototype,
3544
Error,
45+
ErrorPrototype,
3646
EvalError,
47+
EvalErrorPrototype,
3748
Float32Array,
49+
Float32ArrayPrototype,
3850
Float64Array,
51+
Float64ArrayPrototype,
3952
Function,
53+
FunctionPrototype,
4054
Int16Array,
55+
Int16ArrayPrototype,
4156
Int32Array,
57+
Int32ArrayPrototype,
4258
Int8Array,
43-
JSON,
59+
Int8ArrayPrototype,
4460
Map,
45-
Math,
61+
MapPrototype,
4662
Number,
63+
NumberPrototype,
4764
Object,
4865
ObjectDefineProperty,
4966
ObjectFreeze,
@@ -52,28 +69,44 @@ const {
5269
ObjectGetOwnPropertyNames,
5370
ObjectGetOwnPropertySymbols,
5471
ObjectGetPrototypeOf,
72+
ObjectPrototype,
5573
ObjectPrototypeHasOwnProperty,
5674
Promise,
75+
PromisePrototype,
5776
RangeError,
77+
RangeErrorPrototype,
5878
ReferenceError,
59-
Reflect,
79+
ReferenceErrorPrototype,
6080
ReflectOwnKeys,
6181
RegExp,
82+
RegExpPrototype,
83+
SafeSet,
6284
Set,
85+
SetPrototype,
6386
String,
87+
StringPrototype,
6488
Symbol,
6589
SymbolIterator,
6690
SyntaxError,
91+
SyntaxErrorPrototype,
6792
TypeError,
93+
TypeErrorPrototype,
6894
TypedArray,
6995
TypedArrayPrototype,
7096
Uint16Array,
97+
Uint16ArrayPrototype,
7198
Uint32Array,
99+
Uint32ArrayPrototype,
72100
Uint8Array,
101+
Uint8ArrayPrototype,
73102
Uint8ClampedArray,
103+
Uint8ClampedArrayPrototype,
74104
URIError,
105+
URIErrorPrototype,
75106
WeakMap,
107+
WeakMapPrototype,
76108
WeakSet,
109+
WeakSetPrototype,
77110
}=primordials;
78111

79112
module.exports=function(){
@@ -110,55 +143,55 @@ module.exports = function() {
110143
TypedArrayPrototype,
111144

112145
// 19 Fundamental Objects
113-
Object.prototype,// 19.1
114-
Function.prototype,// 19.2
115-
Boolean.prototype,// 19.3
116-
117-
Error.prototype,// 19.5
118-
EvalError.prototype,
119-
RangeError.prototype,
120-
ReferenceError.prototype,
121-
SyntaxError.prototype,
122-
TypeError.prototype,
123-
URIError.prototype,
146+
ObjectPrototype,// 19.1
147+
FunctionPrototype,// 19.2
148+
BooleanPrototype,// 19.3
149+
150+
ErrorPrototype,// 19.5
151+
EvalErrorPrototype,
152+
RangeErrorPrototype,
153+
ReferenceErrorPrototype,
154+
SyntaxErrorPrototype,
155+
TypeErrorPrototype,
156+
URIErrorPrototype,
124157

125158
// 20 Numbers and Dates
126-
Number.prototype,// 20.1
127-
Date.prototype,// 20.3
159+
NumberPrototype,// 20.1
160+
DatePrototype,// 20.3
128161

129162
// 21 Text Processing
130-
String.prototype,// 21.1
131-
RegExp.prototype,// 21.2
163+
StringPrototype,// 21.1
164+
RegExpPrototype,// 21.2
132165

133166
// 22 Indexed Collections
134-
Array.prototype,// 22.1
135-
136-
Int8Array.prototype,
137-
Uint8Array.prototype,
138-
Uint8ClampedArray.prototype,
139-
Int16Array.prototype,
140-
Uint16Array.prototype,
141-
Int32Array.prototype,
142-
Uint32Array.prototype,
143-
Float32Array.prototype,
144-
Float64Array.prototype,
145-
BigInt64Array.prototype,
146-
BigUint64Array.prototype,
167+
ArrayPrototype,// 22.1
168+
169+
Int8ArrayPrototype,
170+
Uint8ArrayPrototype,
171+
Uint8ClampedArrayPrototype,
172+
Int16ArrayPrototype,
173+
Uint16ArrayPrototype,
174+
Int32ArrayPrototype,
175+
Uint32ArrayPrototype,
176+
Float32ArrayPrototype,
177+
Float64ArrayPrototype,
178+
BigInt64ArrayPrototype,
179+
BigUint64ArrayPrototype,
147180

148181
// 23 Keyed Collections
149-
Map.prototype,// 23.1
150-
Set.prototype,// 23.2
151-
WeakMap.prototype,// 23.3
152-
WeakSet.prototype,// 23.4
182+
MapPrototype,// 23.1
183+
SetPrototype,// 23.2
184+
WeakMapPrototype,// 23.3
185+
WeakSetPrototype,// 23.4
153186

154187
// 24 Structured Data
155-
ArrayBuffer.prototype,// 24.1
156-
DataView.prototype,// 24.3
157-
Promise.prototype,// 25.4
188+
ArrayBufferPrototype,// 24.1
189+
DataViewPrototype,// 24.3
190+
PromisePrototype,// 25.4
158191

159192
// Other APIs / Web Compatibility
160193
console.Console.prototype,
161-
BigInt.prototype,
194+
BigIntPrototype,
162195
WebAssembly.Module.prototype,
163196
WebAssembly.Instance.prototype,
164197
WebAssembly.Table.prototype,
@@ -171,7 +204,7 @@ module.exports = function() {
171204
constintrinsics=[
172205
// Anonymous Intrinsics
173206
// ThrowTypeError
174-
ObjectGetOwnPropertyDescriptor(Function.prototype,'caller').get,
207+
ObjectGetOwnPropertyDescriptor(FunctionPrototype,'caller').get,
175208
// IteratorPrototype
176209
ObjectGetPrototypeOf(
177210
ObjectGetPrototypeOf(newArray()[SymbolIterator]())
@@ -224,6 +257,7 @@ module.exports = function() {
224257

225258
// 20 Numbers and Dates
226259
Number,// 20.1
260+
// eslint-disable-next-line node-core/prefer-primordials
227261
Math,// 20.2
228262
Date,// 20.3
229263

@@ -255,10 +289,12 @@ module.exports = function() {
255289
// 24 Structured Data
256290
ArrayBuffer,// 24.1
257291
DataView,// 24.3
292+
// eslint-disable-next-line node-core/prefer-primordials
258293
JSON,// 24.5
259294
Promise,// 25.4
260295

261296
// 26 Reflection
297+
// eslint-disable-next-line node-core/prefer-primordials
262298
Reflect,// 26.1
263299
Proxy,// 26.2
264300

@@ -281,19 +317,21 @@ module.exports = function() {
281317
];
282318

283319
if(typeofIntl!=='undefined'){
284-
intrinsicPrototypes.push(Intl.Collator.prototype);
285-
intrinsicPrototypes.push(Intl.DateTimeFormat.prototype);
286-
intrinsicPrototypes.push(Intl.ListFormat.prototype);
287-
intrinsicPrototypes.push(Intl.NumberFormat.prototype);
288-
intrinsicPrototypes.push(Intl.PluralRules.prototype);
289-
intrinsicPrototypes.push(Intl.RelativeTimeFormat.prototype);
290-
intrinsics.push(Intl);
320+
ArrayPrototypePush(intrinsicPrototypes,
321+
Intl.Collator.prototype,
322+
Intl.DateTimeFormat.prototype,
323+
Intl.ListFormat.prototype,
324+
Intl.NumberFormat.prototype,
325+
Intl.PluralRules.prototype,
326+
Intl.RelativeTimeFormat.prototype,
327+
);
328+
ArrayPrototypePush(intrinsics,Intl);
291329
}
292330

293-
intrinsicPrototypes.forEach(enableDerivedOverrides);
331+
ArrayPrototypeForEach(intrinsicPrototypes,enableDerivedOverrides);
294332

295333
constfrozenSet=newWeakSet();
296-
intrinsics.forEach(deepFreeze);
334+
ArrayPrototypeForEach(intrinsics,deepFreeze);
297335

298336
// Objects that are deeply frozen.
299337
functiondeepFreeze(root){
@@ -306,7 +344,7 @@ module.exports = function() {
306344
*/
307345
functioninnerDeepFreeze(node){
308346
// Objects that we have frozen in this round.
309-
constfreezingSet=newSet();
347+
constfreezingSet=newSafeSet();
310348

311349
// If val is something we should be freezing but aren't yet,
312350
// add it to freezingSet.

‎lib/internal/process/warning.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const{
44
ArrayIsArray,
55
Error,
6+
ErrorPrototypeToString,
67
ErrorCaptureStackTrace,
78
String,
89
}=primordials;
@@ -81,10 +82,10 @@ function onWarning(warning) {
8182
if(trace&&warning.stack){
8283
msg+=`${warning.stack}`;
8384
}else{
84-
consttoString=
85+
msg+=
8586
typeofwarning.toString==='function' ?
86-
warning.toString : Error.prototype.toString;
87-
msg+=`${toString.apply(warning)}`;
87+
`${warning.toString()}` :
88+
ErrorPrototypeToString(warning);
8889
}
8990
if(typeofwarning.detail==='string'){
9091
msg+=`\n${warning.detail}`;

‎test/parallel/test-eslint-prefer-primordials.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,13 @@ new RuleTester({
163163
options: [{name: 'Map',into: 'Safe'}],
164164
errors: [{message: /const{SafeMap}=primordials/}]
165165
},
166+
{
167+
code: `
168+
const { Function } = primordials;
169+
const noop = Function.prototype;
170+
`,
171+
options: [{name: 'Function'}],
172+
errors: [{message: /const{FunctionPrototype}=primordials/}]
173+
},
166174
]
167175
});

‎tools/eslint-rules/prefer-primordials.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ module.exports = {
7575
acc.set(
7676
option.name,
7777
(option.ignore||[])
78-
.concat(['prototype'])
7978
.reduce((acc,name)=>acc.set(name,{
8079
ignored: true
8180
}),newMap())

0 commit comments

Comments
 (0)