Skip to content

Commit

Permalink
bootstrap: freeze more intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Apr 17, 2021
1 parent 14aed60 commit 14591b7
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 105 deletions.
236 changes: 131 additions & 105 deletions lib/internal/freeze_intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
'use strict';

const {
AggregateError,
AggregateErrorPrototype,
Array,
ArrayBuffer,
ArrayBufferPrototype,
ArrayIteratorPrototype,
ArrayPrototype,
ArrayPrototypeForEach,
ArrayPrototypePush,
Expand All @@ -45,6 +48,8 @@ const {
ErrorPrototype,
EvalError,
EvalErrorPrototype,
FinalizationRegistry,
FinalizationRegistryPrototype,
Float32Array,
Float32ArrayPrototype,
Float64Array,
Expand Down Expand Up @@ -84,9 +89,11 @@ const {
Set,
SetPrototype,
String,
StringIteratorPrototype,
StringPrototype,
Symbol,
SymbolIterator,
SymbolMatchAll,
SyntaxError,
SyntaxErrorPrototype,
TypeError,
Expand All @@ -105,12 +112,15 @@ const {
URIErrorPrototype,
WeakMap,
WeakMapPrototype,
WeakRef,
WeakRefPrototype,
WeakSet,
WeakSetPrototype,
decodeURI,
decodeURIComponent,
encodeURI,
encodeURIComponent,
globalThis,
} = primordials;

module.exports = function() {
Expand All @@ -124,52 +134,37 @@ module.exports = function() {
} = require('timers');

const intrinsicPrototypes = [
// Anonymous Intrinsics
// IteratorPrototype
ObjectGetPrototypeOf(
ObjectGetPrototypeOf(new Array()[SymbolIterator]())
),
// ArrayIteratorPrototype
ObjectGetPrototypeOf(new Array()[SymbolIterator]()),
// StringIteratorPrototype
ObjectGetPrototypeOf(new String()[SymbolIterator]()),
// MapIteratorPrototype
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
// SetIteratorPrototype
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
// GeneratorFunction
ObjectGetPrototypeOf(function* () {}),
// AsyncFunction
ObjectGetPrototypeOf(async function() {}),
// AsyncGeneratorFunction
ObjectGetPrototypeOf(async function* () {}),
// TypedArray
TypedArrayPrototype,

// 19 Fundamental Objects
ObjectPrototype, // 19.1
FunctionPrototype, // 19.2
BooleanPrototype, // 19.3
// 21 Fundamental Objects
ObjectPrototype, // 20.1
FunctionPrototype, // 20.2
BooleanPrototype, // 20.3

ErrorPrototype, // 19.5
ErrorPrototype, // 20.5
AggregateErrorPrototype,
EvalErrorPrototype,
RangeErrorPrototype,
ReferenceErrorPrototype,
SyntaxErrorPrototype,
TypeErrorPrototype,
URIErrorPrototype,

// 20 Numbers and Dates
NumberPrototype, // 20.1
DatePrototype, // 20.3
// 21 Numbers and Dates
NumberPrototype, // 21.1
BigIntPrototype, // 21.2
DatePrototype, // 21.4

// 21 Text Processing
StringPrototype, // 21.1
RegExpPrototype, // 21.2
// 22 Text Processing
StringPrototype, // 22.1
StringIteratorPrototype, // 22.1.5
RegExpPrototype, // 22.2
// 22.2.7 RegExpStringIteratorPrototype
ObjectGetPrototypeOf(/e/[SymbolMatchAll]()),

// 22 Indexed Collections
ArrayPrototype, // 22.1
// 23 Indexed Collections
ArrayPrototype, // 23.1
ArrayIteratorPrototype, // 23.1.5

TypedArrayPrototype, // 23.2
Int8ArrayPrototype,
Uint8ArrayPrototype,
Uint8ClampedArrayPrototype,
Expand All @@ -182,55 +177,50 @@ module.exports = function() {
BigInt64ArrayPrototype,
BigUint64ArrayPrototype,

// 23 Keyed Collections
MapPrototype, // 23.1
SetPrototype, // 23.2
WeakMapPrototype, // 23.3
WeakSetPrototype, // 23.4

// 24 Structured Data
ArrayBufferPrototype, // 24.1
DataViewPrototype, // 24.3
PromisePrototype, // 25.4
// 24 Keyed Collections
MapPrototype, // 24.1
// 24.1.5 MapIteratorPrototype
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
SetPrototype, // 24.2
// 24.2.5 SetIteratorPrototype
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
WeakMapPrototype, // 24.3
WeakSetPrototype, // 24.4

// 25 Structured Data
ArrayBufferPrototype, // 25.1
SharedArrayBuffer.prototype, // 25.2
DataViewPrototype, // 25.3

// 26 Managing Memory
WeakRefPrototype, // 26.1
FinalizationRegistryPrototype, // 26.2

// 27 Control Abstraction Objects
// 27.1 Iteration
ObjectGetPrototypeOf(ArrayIteratorPrototype), // 27.1.2 IteratorPrototype
// 27.1.3 AsyncIteratorPrototype
ObjectGetPrototypeOf(ObjectGetPrototypeOf(ObjectGetPrototypeOf(
(async function*() {})()
))),
PromisePrototype, // 27.2

// Other APIs / Web Compatibility
console.Console.prototype,
BigIntPrototype,
WebAssembly.Module.prototype,
WebAssembly.Instance.prototype,
WebAssembly.Table.prototype,
WebAssembly.Memory.prototype,
WebAssembly.CompileError.prototype,
WebAssembly.LinkError.prototype,
WebAssembly.RuntimeError.prototype,
SharedArrayBuffer.prototype,
];
const intrinsics = [
// Anonymous Intrinsics
// ThrowTypeError
// 10.2.4.1 ThrowTypeError
ObjectGetOwnPropertyDescriptor(FunctionPrototype, 'caller').get,
// IteratorPrototype
ObjectGetPrototypeOf(
ObjectGetPrototypeOf(new Array()[SymbolIterator]())
),
// ArrayIteratorPrototype
ObjectGetPrototypeOf(new Array()[SymbolIterator]()),
// StringIteratorPrototype
ObjectGetPrototypeOf(new String()[SymbolIterator]()),
// MapIteratorPrototype
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
// SetIteratorPrototype
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
// GeneratorFunction
ObjectGetPrototypeOf(function* () {}),
// AsyncFunction
ObjectGetPrototypeOf(async function() {}),
// AsyncGeneratorFunction
ObjectGetPrototypeOf(async function* () {}),
// TypedArray
TypedArray,

// 18 The Global Object
// 19 The Global Object
// 19.2 Function Properties of the Global Object
eval,
// eslint-disable-next-line node-core/prefer-primordials
isFinite,
Expand All @@ -240,38 +230,46 @@ module.exports = function() {
parseFloat,
// eslint-disable-next-line node-core/prefer-primordials
parseInt,
// 19.2.6 URI Handling Functions
decodeURI,
decodeURIComponent,
encodeURI,
encodeURIComponent,

// 19 Fundamental Objects
Object, // 19.1
Function, // 19.2
Boolean, // 19.3
Symbol, // 19.4
// 20 Fundamental Objects
Object, // 20.1
Function, // 20.2
Boolean, // 20.3
Symbol, // 20.4

Error, // 19.5
Error, // 20.5
AggregateError,
EvalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError,

// 20 Numbers and Dates
Number, // 20.1
// 21 Numbers and Dates
Number, // 21.1
BigInt, // 21.2
// eslint-disable-next-line node-core/prefer-primordials
Math, // 20.2
Date, // 20.3

// 21 Text Processing
String, // 21.1
RegExp, // 21.2

// 22 Indexed Collections
Array, // 22.1

Math, // 21.3
Date, // 21.4

// 22 Text Processing
String, // 22.1
StringIteratorPrototype, // 22.1.5
RegExp, // 22.2
// 22.2.7 RegExpStringIteratorPrototype
ObjectGetPrototypeOf(/e/[SymbolMatchAll]()),

// 23 Indexed Collections
Array, // 23.1
ArrayIteratorPrototype, // 23.1.5
// 23.2 TypedArray
TypedArray,
Int8Array,
Uint8Array,
Uint8ClampedArray,
Expand All @@ -284,23 +282,47 @@ module.exports = function() {
BigInt64Array,
BigUint64Array,

// 23 Keyed Collections
Map, // 23.1
Set, // 23.2
WeakMap, // 23.3
WeakSet, // 23.4

// 24 Structured Data
ArrayBuffer, // 24.1
DataView, // 24.3
// 24 Keyed Collections
Map, // 24.1
// 24.1.5 MapIteratorPrototype
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
Set, // 24.2
// 24.2.5 SetIteratorPrototype
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
WeakMap, // 24.3
WeakSet, // 24.4

// 25 Structured Data
ArrayBuffer, // 25.1
SharedArrayBuffer, // 25.2
DataView, // 25.3
Atomics, // 25.4
// eslint-disable-next-line node-core/prefer-primordials
JSON, // 24.5
Promise, // 25.4
JSON, // 25.5

// 26 Managing Memory
WeakRef, // 26.1
FinalizationRegistry, // 26.2

// 27 Control Abstraction Objects
// 27.1 Iteration
ObjectGetPrototypeOf(ArrayIteratorPrototype), // 27.1.2 IteratorPrototype
// 27.1.3 AsyncIteratorPrototype
ObjectGetPrototypeOf(ObjectGetPrototypeOf(ObjectGetPrototypeOf(
(async function*() {})()
))),
Promise, // 27.2
// 27.3 GeneratorFunction
ObjectGetPrototypeOf(function* () {}),
// 27.4 AsyncGeneratorFunction
ObjectGetPrototypeOf(async function* () {}),
// 27.7 AsyncFunction
ObjectGetPrototypeOf(async function() {}),

// 26 Reflection
// 28 Reflection
// eslint-disable-next-line node-core/prefer-primordials
Reflect, // 26.1
Proxy, // 26.2
Reflect, // 28.1
Proxy, // 28.2

// B.2.1
escape,
Expand All @@ -314,10 +336,7 @@ module.exports = function() {
setInterval,
setTimeout,
console,
BigInt,
Atomics,
WebAssembly,
SharedArrayBuffer,
];

if (typeof Intl !== 'undefined') {
Expand All @@ -337,6 +356,13 @@ module.exports = function() {
const frozenSet = new WeakSet();
ArrayPrototypeForEach(intrinsics, deepFreeze);

// 19.1 Value Properties of the Global Object
ObjectDefineProperty(globalThis, 'globalThis', {
configurable: false,
writable: false,
value: globalThis,
});

// Objects that are deeply frozen.
function deepFreeze(root) {
/**
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-freeze-intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ assert.throws(
assert.strictEqual(Object.getOwnPropertyDescriptor(o, 'toString').enumerable,
true);
}

// Ensure we can not override globalThis
{
assert.throws(() => { globalThis.globalThis = null; },
{ name: 'TypeError' });
assert.notStrictEqual(globalThis, null);
}

0 comments on commit 14591b7

Please sign in to comment.