forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-permission-audit-fs-does-not-deny.js
More file actions
59 lines (51 loc) · 1.85 KB
/
Copy pathtest-permission-audit-fs-does-not-deny.js
File metadata and controls
59 lines (51 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
const common = require('../common');
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('This test only works on a main thread');
}
const assert = require('assert');
const { spawnSync } = require('child_process');
const { test } = require('node:test');
const fixtures = require('../common/fixtures');
const blockedFile = fixtures.path('permission', 'deny', 'protected-file.md');
function runAudit(mode) {
const childScript = `
const dc = require('node:diagnostics_channel');
const msgs = [];
dc.subscribe('node:permission-model:fs', (m) => msgs.push({
permission: m.permission,
resource: m.resource,
}));
try {
${mode === 'eval' ?
`eval('require("node:fs").readFileSync(process.env.BLOCKED_FILE)');` :
`require('node:fs').readFileSync(process.env.BLOCKED_FILE);`}
console.log('RESULT NO_THROW');
} catch (e) {
console.log('RESULT THREW ' + e.code);
}
console.log('AUDIT ' + JSON.stringify(msgs));
`;
const env = { ...process.env, BLOCKED_FILE: blockedFile };
const { status, stdout, stderr } = spawnSync(
process.execPath,
['--permission-audit', '-e', childScript],
{ encoding: 'utf8', env },
);
assert.strictEqual(status, 0, stderr);
const lines = stdout.split('\n');
assert.ok(lines.includes('RESULT NO_THROW'), stdout);
const auditLine = lines.find((l) => l.startsWith('AUDIT '));
assert.ok(auditLine, stdout);
const msgs = JSON.parse(auditLine.replace('AUDIT ', ''));
assert.strictEqual(msgs.length, 1);
assert.strictEqual(msgs[0].permission, 'FileSystemRead');
assert.ok(msgs[0].resource.endsWith('protected-file.md'));
}
test('permission-audit logs fs denial without throwing', () => {
runAudit('direct');
});
test('permission-audit logs fs denial without throwing (eval)', () => {
runAudit('eval');
});