Sub topic 12
Security Models Compared
This chapter assumes you know how to load scripts in a page, run files with node, and use the built-in fs module from earlier chapters. It also assumes you are comfortable with async/await.
The browser and Node.js run the same language, but they trust your code very differently. A browser treats every script as possibly hostile, because it came from a website the user visited. Node.js treats the script as a program the user chose to run, so by default it gets the same access as the user who started it.
The browser sandbox
A sandbox is a restricted environment that limits what code can touch. Browser JavaScript runs inside one, and these rules apply:
- No direct system access. Scripts cannot open arbitrary files, start processes, or open raw network sockets. They can read a file only when the user picks it, for example with
<input type="file">. - Same-origin policy. An origin is the scheme, host, and port of a URL. A script can freely read responses from its own origin. Reading a response from another origin requires that server to allow it through CORS (Cross-Origin Resource Sharing) headers such as
Access-Control-Allow-Origin. - Permission prompts. Sensitive APIs such as geolocation, the camera, and notifications ask the user first.
- Content Security Policy (CSP). This is an HTTP header that lets the site limit where scripts may load from, which reduces the damage an injected script can do.
// Run in a page's console. Pass a URL on another origin that sends no CORS headers.
async function tryCrossOrigin(url) {
try {
const response = await fetch(url);
console.log('allowed, status', response.status);
} catch (err) {
// The request may reach the server, but the browser hides the response.
console.log('blocked by the browser:', err.name); // TypeError
}
}
tryCrossOrigin('https://api.other-site.test/data');Node.js: full user privileges
A Node.js script can read and write any file the user can, run shell commands with child_process, and open any network connection. Node.js applies no same-origin policy and shows no permission prompts. That access is what makes Node.js useful for servers and tooling. It also means an npm dependency you install runs with your privileges too.
Node.js has an opt-in permission model, enabled with the --permission flag (it was called --experimental-permission before Node.js 22.13 and 23.5). When the flag is on, file system access, child processes, and workers are denied unless you grant them.
// perm.mjs
import { readFile } from 'node:fs/promises';
console.log('can read /etc:', process.permission?.has('fs.read', '/etc') ?? 'model off');
try {
const text = await readFile('/etc/hosts', 'utf8');
console.log('read', text.length, 'chars');
} catch (err) {
console.log('blocked:', err.code);
}Run it two ways on Linux or macOS:
node perm.mjs
# can read /etc: model off
# read 213 chars
node --permission --allow-fs-read=./ perm.mjs
# can read /etc: false
# blocked: ERR_ACCESS_DENIEDSide by side
| Concern | Browser | Node.js (default) |
|---|---|---|
| Who is trusted | Nobody; every site is untrusted | The user who ran the script |
| File system | Only files the user selects | Everything the user can access |
| Network | Same-origin policy and CORS | Unrestricted |
| Processes | None | `child_process` |
| Opt-in restrictions | CSP, permissions policy | `--permission` flags |
Common mistakes
- Expecting CORS to block calls made from Node.js. CORS is enforced only by browsers.
- Treating a browser fetch failure as a server problem. Check the console for a CORS message first.
- Assuming npm packages are harmless. Install scripts and imported code run with your full privileges.
- Using
--permissionto run untrusted code safely. The flag reduces accidents but does not isolate the process.
Exercise
A script fetches https://api.example.test/data. In a browser on a different origin it fails with a CORS error, but the same code run with Node.js 20 succeeds. Why, and what change makes the browser version work?
Show answer
The same-origin policy and CORS are enforced only by the browser. Node.js does no CORS checks, so it receives the response. To make the browser version work, the API server must send an Access-Control-Allow-Origin header that includes the page's origin. Another option is to route the request through a server on the page's own origin.
Exercise
You run node --permission app.mjs and get ERRACCESSDENIED when it reads ./config/settings.json. What command grants read access to only that folder?
Show answer
node --permission --allow-fs-read=./config/ app.mjs. Depending on the Node.js version, you may also need to grant read access to the folder containing app.mjs so the entry script itself can load.