1- // Flags: --permission --allow-fs-read=*
1+ // Flags: --permission --allow-fs-read=* --allow-child-process
22'use strict' ;
33
44const common = require ( '../common' ) ;
@@ -10,25 +10,46 @@ if (!isMainThread) {
1010
1111const assert = require ( 'assert' ) ;
1212
13+ // When --permission is used without --allow-env, env vars should be
14+ // freely accessible (backward compatible behavior).
1315{
14- assert . ok ( ! process . permission . has ( 'env' ) ) ;
16+ assert . ok ( process . permission . has ( 'env' ) ) ;
1517}
1618
1719{
18- assert . strictEqual ( process . env . HOME , undefined ) ;
19- assert . strictEqual ( process . env . PATH , undefined ) ;
20+ // Environment variables should be readable
21+ assert . ok ( typeof process . env . HOME === 'string' || process . env . HOME === undefined ) ;
2022}
2123
2224{
23- assert . throws ( ( ) => {
24- process . env . TEST_VAR = 'value' ;
25- } , common . expectsError ( {
26- code : 'ERR_ACCESS_DENIED' ,
27- permission : 'EnvVar' ,
28- } ) ) ;
25+ // Setting env vars should work
26+ process . env . __TEST_PERMISSION_ENV = 'test' ;
27+ assert . strictEqual ( process . env . __TEST_PERMISSION_ENV , 'test' ) ;
28+ delete process . env . __TEST_PERMISSION_ENV ;
2929}
3030
3131{
32+ // Object.keys should return env vars
3233 const keys = Object . keys ( process . env ) ;
33- assert . strictEqual ( keys . length , 0 ) ;
34+ assert . ok ( keys . length > 0 ) ;
35+ }
36+
37+ // Test that restriction activates when --allow-env is explicitly used
38+ {
39+ const { spawnSync } = require ( 'child_process' ) ;
40+ const { status, stderr } = spawnSync ( process . execPath , [
41+ '--permission' ,
42+ '--allow-fs-read=*' ,
43+ '--allow-env=__NONEXISTENT_VAR__' ,
44+ '-e' ,
45+ `
46+ const assert = require('assert');
47+ assert.ok(!process.permission.has('env'));
48+ assert.strictEqual(process.env.HOME, undefined);
49+ assert.strictEqual(process.env.PATH, undefined);
50+ assert.throws(() => { process.env.X = '1'; }, { code: 'ERR_ACCESS_DENIED' });
51+ assert.strictEqual(Object.keys(process.env).length, 0);
52+ ` ,
53+ ] ) ;
54+ assert . strictEqual ( status , 0 , `child stderr: ${ stderr } ` ) ;
3455}
0 commit comments