Skip to content

Commit

Permalink
permission: add path separator to loader check
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Mar 9, 2023
1 parent 629047d commit 05b939b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ function readPackageScope(checkPath) {
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
// Stop the search when the process doesn't have permissions
// to walk upwards
if (enabledPermission && !permission.has('fs.read', checkPath)) {
if (enabledPermission && !permission.has('fs.read', checkPath + sep)) {
return false;
}
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/permission/loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const fs = require('node:fs');

fs.readFile('/etc/passwd', () => {});
21 changes: 21 additions & 0 deletions test/parallel/test-cli-permission-deny-fs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

require('../common');

const fixtures = require('../common/fixtures');
const { spawnSync } = require('child_process');
const assert = require('assert');
const fs = require('fs');
const path = require('path');

{
const { status, stdout } = spawnSync(
Expand Down Expand Up @@ -126,3 +129,21 @@ const fs = require('fs');
assert.strictEqual(status, 1);
assert.ok(!fs.existsSync('permission-deny-example.md'));
}

{
const firstPath = path.sep + process.cwd().split(path.sep)[1];
const pwd = fixtures.path('permission', 'loader');
const file = path.join(pwd, 'index.js');
const { status, stderr } = spawnSync(
process.execPath,
[
'--experimental-permission',
`--allow-fs-read=${firstPath}`,
file,
]
);
assert.ok(
stderr.toString().includes('resource: \'/etc/passwd\''),
stderr);
assert.strictEqual(status, 1);
}

0 comments on commit 05b939b

Please sign in to comment.