Skip to content

Commit

Permalink
[Fix] do not crash when --disable-proto=throw
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 16, 2024
1 parent 19f1da0 commit 6c367d9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 30 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/node-noproto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: 'Tests: node --disable-proto=throw'

on: [pull_request, push]

jobs:
throw:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>= 12.17'
type: minors
command: "NODE_OPTIONS='--disable-proto=throw' npx tape 'tests/**/*.js'"

delete:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>= 12.17'
type: minors
command: "NODE_OPTIONS='--disable-proto=delete' npx tape 'tests/**/*.js'"

node:
name: 'node --disable-proto'
needs: [throw, delete]
runs-on: ubuntu-latest
steps:
- run: true
26 changes: 1 addition & 25 deletions .github/workflows/node-twenties.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,8 @@ jobs:
type: minors
command: npm run tests-only

no-proto:
name: 'node, --disable-proto=delete'
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@v2
with:
disable-sudo: true
egress-policy: audit
allowed-endpoints: >
github.com:443
raw.githubusercontent.com:443
nodejs.org:443
iojs.org:443
registry.npmjs.org:443
actions-results-receiver-production.githubapp.com:443
- uses: actions/checkout@v4
with:
show-progress: false
- uses: ljharb/actions/node/install@main
- run: NODE_OPTIONS=--disable-proto=delete npm run tests-only
- uses: codecov/codecov-action@v3.1.5

finisher:
needs: [tests, no-proto]
needs: tests
name: 'node >= 20'
runs-on: ubuntu-latest
steps:
Expand Down
13 changes: 10 additions & 3 deletions get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
var callBind = require('call-bind-apply-helpers');
var gOPD = require('gopd');

// eslint-disable-next-line no-extra-parens, no-proto
var hasProtoAccessor = /** @type {{ __proto__?: typeof Array.prototype }} */ ([]).__proto__ === Array.prototype;
var hasProtoAccessor;
try {
// eslint-disable-next-line no-extra-parens, no-proto
hasProtoAccessor = /** @type {{ __proto__?: typeof Array.prototype }} */ ([]).__proto__ === Array.prototype;
} catch (e) {
if (!e || typeof e !== 'object' || !('code' in e) || e.code !== 'ERR_PROTO_ACCESS') {
throw e;
}
}

// eslint-disable-next-line no-extra-parens
var desc = hasProtoAccessor && gOPD && gOPD(Object.prototype, /** @type {keyof typeof Object.prototype} */ ('__proto__'));
var desc = !!hasProtoAccessor && gOPD && gOPD(Object.prototype, /** @type {keyof typeof Object.prototype} */ ('__proto__'));

var $Object = Object;
var $getPrototypeOf = $Object.getPrototypeOf;
Expand Down
8 changes: 7 additions & 1 deletion set.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ var $TypeError = require('es-errors/type');

/** @type {{ __proto__?: object | null }} */
var obj = {};
obj.__proto__ = null; // eslint-disable-line no-proto
try {
obj.__proto__ = null; // eslint-disable-line no-proto
} catch (e) {
if (!e || typeof e !== 'object' || !('code' in e) || e.code !== 'ERR_PROTO_ACCESS') {
throw e;
}
}

var hasProtoMutator = !('toString' in obj);

Expand Down
11 changes: 10 additions & 1 deletion test/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ test('setDunderProto', { skip: !setDunderProto }, function (t) {
});

test('no dunder proto', { skip: !!setDunderProto }, function (t) {
t.notOk('__proto__' in Object.prototype, 'no __proto__ in Object.prototype');
if ('__proto__' in Object.prototype) {
t['throws'](
// @ts-expect-error
function () { ({}).__proto__ = null; }, // eslint-disable-line no-proto
Error,
'throws when setting Object.prototype.__proto__'
);
} else {
t.notOk('__proto__' in Object.prototype, 'no __proto__ in Object.prototype');
}

t.end();
});

0 comments on commit 6c367d9

Please sign in to comment.