Skip to content
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
18 changes: 18 additions & 0 deletions packages/zone.js/lib/common/to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ export function patchToString(Zone: ZoneType): void {
// override Function.prototype.toString to make zone.js patched function
// look like native function
Zone.__load_patch('toString', (global: any) => {
// In hardened environments (Node --frozen-intrinsics, SES lockdown, or
// any host that has frozen/sealed Function.prototype or Object.prototype),
// overwriting these toString methods will throw. We check both up-front
// and bail atomically so we never leave a half-applied patch: applying
// only one of the two would make Promise objects stringify inconsistently.
//
// Trade-off when bailing: patched functions reveal as their wrapper source
// via Function.prototype.toString rather than the original native source.
// Callers that sniff toString output for "[native code]" detection may
// see different results. This is preferred over crashing module load.
const functionDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, 'toString');
const objectDescriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'toString');
const canPatch = (descriptor: PropertyDescriptor | undefined) =>
!descriptor || (descriptor.writable !== false && descriptor.configurable !== false);
if (!canPatch(functionDescriptor) || !canPatch(objectDescriptor)) {
return;
}

// patch Func.prototype.toString to let them look like native
const originalFunctionToString = Function.prototype.toString;

Expand Down
31 changes: 21 additions & 10 deletions packages/zone.js/lib/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ export function patchNode(Zone: ZoneType): void {
});

Zone.__load_patch('console', (global: any, Zone: ZoneType) => {
// Skip entirely under Node --frozen-intrinsics or any host that froze console.
// Without instrumentation, console calls run in whatever zone they were called
// from rather than being hoisted to Zone.root — acceptable degradation; the
// alternative is crashing at load.
if (!Object.isExtensible(console)) {
return;
}
const consoleMethods = [
'dir',
'log',
Expand All @@ -154,17 +161,21 @@ export function patchNode(Zone: ZoneType): void {
'trace',
];
consoleMethods.forEach((m: string) => {
const originalMethod = ((console as any)[Zone.__symbol__(m)] = (console as any)[m]);
if (originalMethod) {
(console as any)[m] = function () {
const args = ArraySlice.call(arguments);
if (Zone.current === Zone.root) {
return originalMethod.apply(this, args);
} else {
return Zone.root.run(originalMethod, this, args);
}
};
const descriptor = Object.getOwnPropertyDescriptor(console, m);
// Only patch if we can both stash the original and overwrite the method.
if (!descriptor || descriptor.writable === false || descriptor.configurable === false) {
return;
}
const originalMethod = ((console as any)[Zone.__symbol__(m)] = (console as any)[m]);
if (!originalMethod) return;
(console as any)[m] = function () {
const args = ArraySlice.call(arguments);
if (Zone.current === Zone.root) {
return originalMethod.apply(this, args);
} else {
return Zone.root.run(originalMethod, this, args);
}
};
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/zone.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"electrontest": "cd test/extra && node electron.js",
"jest:test": "jest --config ./test/jest/jest.config.js",
"jest:nodetest": "jest --config ./test/jest/jest.node.config.js",
"frozen-intrinsics:test": "node --frozen-intrinsics ./test/node/frozen_intrinsics_load.js",
"vitest:test": "vitest ./test/vitest/*.spec.js --exclude ./test/vitest/*-globals.spec.js",
"vitest-globals:test": "vitest ./test/vitest/*-globals.spec.js --globals",
"promisefinallytest": "mocha ./test/promise/promise.finally.spec.mjs",
Expand Down
27 changes: 27 additions & 0 deletions packages/zone.js/test/jest/frozen-intrinsics.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
'use strict';

const {spawnSync} = require('child_process');
const path = require('path');

describe('zone.js/node under --frozen-intrinsics', () => {
it('should load without throwing', () => {
const scriptPath = path.resolve(__dirname, '../node/frozen_intrinsics_load.js');
const result = spawnSync(process.execPath, ['--frozen-intrinsics', scriptPath], {
encoding: 'utf-8',
});

if (result.status !== 0) {
throw new Error(
`zone.js/node threw under --frozen-intrinsics.\nstdout: ${result.stdout}\nstderr: ${result.stderr}`,
);
}
expect(result.status).toBe(0);
});
});
21 changes: 21 additions & 0 deletions packages/zone.js/test/node/frozen_intrinsics_load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
'use strict';

// This file is run as a subprocess by the frozen-intrinsics jest spec:
// node --frozen-intrinsics frozen_intrinsics_load.js
//
// A zero exit code means zone.js/node loaded without throwing under frozen
// intrinsics. Any thrown TypeError (e.g. "Cannot assign to read only property")
// would produce a non-zero exit and fail the test.
const path = require('path');
const bundlePath = path.resolve(
__dirname,
'../../../../dist/bin/packages/zone.js/npm_package/bundles/zone-node.umd.js',
);
require(bundlePath);
Loading