Skip to content

Commit 1726da9

Browse files
authored
permission: add path separator to loader check
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #47030 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 0b328b2 commit 1726da9

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ function readPackageScope(checkPath) {
423423
checkPath=StringPrototypeSlice(checkPath,0,separatorIndex);
424424
// Stop the search when the process doesn't have permissions
425425
// to walk upwards
426-
if(enabledPermission&&!permission.has('fs.read',checkPath)){
426+
if(enabledPermission&&!permission.has('fs.read',checkPath+sep)){
427427
returnfalse;
428428
}
429429
if(StringPrototypeEndsWith(checkPath,sep+'node_modules'))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
constfs=require('node:fs');
2+
3+
fs.readFile('/etc/passwd',()=>{});

‎test/parallel/test-cli-permission-deny-fs.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict';
22

3-
require('../common');
3+
constcommon=require('../common');
4+
5+
constfixtures=require('../common/fixtures');
46
const{ spawnSync }=require('child_process');
57
constassert=require('assert');
68
constfs=require('fs');
9+
constpath=require('path');
710

811
{
912
const{ status, stdout }=spawnSync(
@@ -126,3 +129,23 @@ const fs = require('fs');
126129
assert.strictEqual(status,1);
127130
assert.ok(!fs.existsSync('permission-deny-example.md'));
128131
}
132+
133+
{
134+
const{ root }=path.parse(process.cwd());
135+
constabs=(p)=>path.join(root,p);
136+
constfirstPath=abs(path.sep+process.cwd().split(path.sep,2)[1]);
137+
if(firstPath.startsWith('/etc')){
138+
common.skip('/etc as firstPath');
139+
}
140+
constfile=fixtures.path('permission','loader','index.js');
141+
const{ status, stderr }=spawnSync(
142+
process.execPath,
143+
[
144+
'--experimental-permission',
145+
`--allow-fs-read=${firstPath}`,
146+
file,
147+
]
148+
);
149+
assert.match(stderr.toString(),/resource:'.*?[\\/](?:etc|passwd)'/);
150+
assert.strictEqual(status,1);
151+
}

0 commit comments

Comments
 (0)