Skip to content

Commit 2fd7284

Browse files
billtijasnell
authored andcommitted
test: add test for loading read-only modules
Adds a test-case to cover loading modules the user does not have permission to write to. Covers issue logged in #20112 PR-URL: #20138 Refs: #20112 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ffd57cd commit 2fd7284

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/parallel/test-module-readonly.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
if (!common.isWindows) {
6+
// TODO: Similar checks on *nix-like systems (e.g using chmod or the like)
7+
common.skip('test only runs on Windows');
8+
}
9+
10+
const assert = require('assert');
11+
const fs = require('fs');
12+
const path = require('path');
13+
const cp = require('child_process');
14+
15+
const tmpdir = require('../common/tmpdir');
16+
tmpdir.refresh();
17+
18+
// Create readOnlyMod.js and set to read only
19+
const readOnlyMod = path.join(tmpdir.path, 'readOnlyMod');
20+
const readOnlyModRelative = path.relative(__dirname, readOnlyMod);
21+
const readOnlyModFullPath = `${readOnlyMod}.js`;
22+
23+
fs.writeFileSync(readOnlyModFullPath, 'module.exports = 42;');
24+
25+
// Removed any inherited ACEs, and any explicitly granted ACEs for the
26+
// current user
27+
cp.execSync(
28+
`icacls.exe "${readOnlyModFullPath}" /inheritance:r /remove "%USERNAME%"`);
29+
30+
// Grant the current user read & execute only
31+
cp.execSync(`icacls.exe "${readOnlyModFullPath}" /grant "%USERNAME%":RX`);
32+
33+
let except = null;
34+
try {
35+
// Attempt to load the module. Will fail if write access is required
36+
require(readOnlyModRelative);
37+
} catch (err) {
38+
except = err;
39+
}
40+
41+
// Remove the expliclty granted rights, and reenable inheritance
42+
cp.execSync(
43+
`icacls.exe "${readOnlyModFullPath}" /remove "%USERNAME%" /inheritance:e`);
44+
45+
// Delete the test module (note: tmpdir should get cleaned anyway)
46+
fs.unlinkSync(readOnlyModFullPath);
47+
48+
assert.ifError(except);

0 commit comments

Comments
 (0)