1- // Flags: --permission --allow-fs-read=* --allow-child-process
1+ // Flags: --permission --allow-fs-read=*
22'use strict' ;
33
44const common = require ( '../common' ) ;
@@ -10,46 +10,31 @@ 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 ).
13+ // When --permission is used without --allow-env, all env vars should be denied
14+ // (deny-by-default, consistent with other permission flags ).
1515{
16- assert . ok ( process . permission . has ( 'env' ) ) ;
16+ assert . ok ( ! process . permission . has ( 'env' ) ) ;
1717}
1818
1919{
20- // Environment variables should be readable
21- assert . ok ( typeof process . env . HOME === 'string' || process . env . HOME === undefined ) ;
20+ // Reading a denied variable should silently return undefined
21+ assert . strictEqual ( process . env . HOME , undefined ) ;
22+ assert . strictEqual ( process . env . PATH , undefined ) ;
2223}
2324
2425{
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 ;
26+ // Writing a denied variable should throw
27+ assert . throws ( ( ) => { process . env . __TEST_PERMISSION_ENV = 'test' ; } ,
28+ { code : 'ERR_ACCESS_DENIED' } ) ;
2929}
3030
3131{
32- // Object.keys should return env vars
33- const keys = Object . keys ( process . env ) ;
34- assert . ok ( keys . length > 0 ) ;
32+ // Deleting a denied variable should throw
33+ assert . throws ( ( ) => { delete process . env . __TEST_PERMISSION_ENV ; } ,
34+ { code : 'ERR_ACCESS_DENIED' } ) ;
3535}
3636
37- // Test that restriction activates when --allow-env is explicitly used
3837{
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 } ` ) ;
38+ // Enumerating should return empty
39+ assert . strictEqual ( Object . keys ( process . env ) . length , 0 ) ;
5540}
0 commit comments