Full answer
By default a Node program can do everything the user can: read any file, run any command, open any network connection. A bug or a malicious dependency then has the same power. The permission model is a way to apply the principle of least privilege to the process itself.
Using it: node --permission app.js. With no other flags the process may not read or write files (beyond what Node needs to start), spawn processes, create workers, load native addons or use WASI. Add grants:
- --allow-fs-read=<path or pattern> and --allow-fs-write=<path>. Use * for all (not recommended). Paths can be repeated or comma separated.
- --allow-child-process: allow child_process spawn, exec and fork.
- --allow-worker: allow worker_threads.
- --allow-addons and --allow-wasi.
- Newer versions add network flags; check the documentation for the version you run.
Behaviour: a denied operation throws an error with code ERR_ACCESS_DENIED, and the error shows which permission and resource were refused. At run time, process.permission.has('fs.read', '/data') returns true or false, so code can adapt.
Limits to understand:
- It is a safety net against accidents and some attacks, not a full sandbox. A process with child-process permission, or with write access to its own code, can escape. Treat the grants as part of a defence in depth.
- It does not stop network access in older versions, and it does not restrict CPU or memory.
- Permissions are inherited as set at start-up. They cannot be widened later.
- Some patterns need extra care: the entry script and dependency folders must be readable for the app to start.
Good uses: running build scripts or third-party code with narrow rights, tools that process untrusted files, and hardening production services that know exactly which folders they use. Combine with containers, read-only file systems and OS users.
const { spawnSync } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const allowed = fs.mkdtempSync(path.join(os.tmpdir(), 'allowed-'));
const forbidden = fs.mkdtempSync(path.join(os.tmpdir(), 'forbidden-'));
fs.writeFileSync(path.join(allowed, 'notes.txt'), 'public notes');
fs.writeFileSync(path.join(forbidden, 'secret.txt'), 'private keys');
const childCode = `
const fs = require('node:fs');
const try_ = (label, fn) => {
try { console.log(label, '->', fn()); } catch (err) { console.log(label, '-> blocked with', err.code); }
};
try_('read allowed file ', () => fs.readFileSync(process.argv[1] + '/notes.txt', 'utf8'));
try_('read forbidden file', () => fs.readFileSync(process.argv[2] + '/secret.txt', 'utf8'));
try_('write in allowed folder', () => { fs.writeFileSync(process.argv[1] + '/new.txt', 'x'); return 'written'; });
try_('start a child process', () => require('node:child_process').spawnSync('echo', ['hi']).status);
console.log('process.permission.has(fs.read, allowed):', process.permission.has('fs.read', process.argv[1]));
console.log('process.permission.has(fs.write, allowed):', process.permission.has('fs.write', process.argv[1]));
console.log('process.permission.has(child):', process.permission.has('child'));
`;
const run = spawnSync(process.execPath, [
'--permission',
'--allow-fs-read=' + allowed, // reading only in this folder
'-e', childCode, allowed, forbidden,
], { encoding: 'utf8' });
console.log(run.stdout.trim());
console.log('child exit code:', run.status);
fs.rmSync(allowed, { recursive: true });
fs.rmSync(forbidden, { recursive: true });The child starts with --permission and read access to one folder only, so writing, the second folder and spawning processes are refused. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): read allowed file -> public notes read forbidden file -> blocked with ERR_ACCESS_DENIED write in allowed folder -> blocked with ERR_ACCESS_DENIED start a child process -> blocked with ERR_ACCESS_DENIED process.permission.has(fs.read, allowed): true process.permission.has(fs.write, allowed): false process.permission.has(child): false child exit code: 0
Key points
- --permission makes Node deny by default.
- Grant only needed paths with --allow-fs-read and --allow-fs-write.
- Denied calls throw ERR_ACCESS_DENIED.
- process.permission.has checks a grant.
- It is defence in depth, not a complete sandbox.
Common mistakes
- Believing the permission model replaces containers.
- Granting --allow-fs-read=* and gaining nothing.
- Forgetting that the app needs read access to its own code and dependencies.
- Allowing child processes and thinking the sandbox still holds.
Follow-ups
- How is this different from Deno permissions?
- How would you run an npm build script with minimal rights?
- What does ERR_ACCESS_DENIED tell you?
Tips
- Say 'deny by default' and 'least privilege'.
- Name the flags for fs, child process and worker.
- State clearly that it is not a full sandbox.