Permissions and isolation
Prerequisite: node-dependency-and-security-maintenance. Objective: Compare authority versus sandbox. Each example is independent. Use Node.js 22.16.0 in a disposable directory; save as main.mjs and run node main.mjs. No external service is contacted.
How it works
Separate process identity, filesystem permissions, network policy and runtime capabilities. For Node 22.16.0, use --permission with explicitly granted file paths for reviewed code. Check feature and platform documentation before changing the flags. Even when a test denies a file read, stronger independently reviewed OS or VM isolation is necessary for untrusted workloads. The first example models a service policy; the second runs a benign child on two lab-owned files. It does not attempt to bypass restrictions.
Model an allowlist
const allowed=new Set(['catalog:read']);console.log(allowed.has('catalog:read'),allowed.has('orders:delete'));Expected output:
true falseThe explicit set grants one capability and denies the other.
Observe an actual permission denial
import {writeFileSync} from 'node:fs';import {spawnSync} from 'node:child_process';import {resolve} from 'node:path';writeFileSync('allowed.txt','sample');writeFileSync('denied.txt','sample');writeFileSync('child.cjs',`const fs=require('node:fs'); console.log(fs.readFileSync('allowed.txt','utf8'));try{fs.readFileSync('denied.txt')}catch(e){console.log(e.code)}`);const r=spawnSync(process.execPath,['--permission','--allow-fs-read='+resolve('child.cjs'),'--allow-fs-read='+resolve('allowed.txt'),resolve('child.cjs')],{encoding:'utf8',timeout:2000});if(r.status!==0)throw Error(r.stderr);process.stdout.write(r.stdout);Expected output:
sampleERR_ACCESS_DENIEDThe reviewed child reads the granted fixture and is denied the other one.
Common mistake
Never rely on Node permissions alone to execute arbitrary learner or third-party code.
Practice
List the minimum read, write and network privileges for a catalog service and identify which controls require OS-level enforcement.
Reference candidate: node-permissions-and-isolation-lookup (route unverified). Documentation anchor: https://nodejs.org/download/release/latest-jod/docs/api/permissions.html (related documentation, not exact-version validation).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Permissions and isolation — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Who is the permission model intended to constrain?
What does the lab denial prove?
Which is a least-privilege default?