Lab 09: Security Hardening

Time: 60 minutes | Level: Architect | Docker: docker run -it --rm node:20-alpine sh

Security is not an afterthought. This lab covers Node.js 20's permission model, WebCrypto API, key derivation, secure headers, and prototype pollution prevention — the foundations of production-hardened Node.js services.


Step 1: Node.js 20 Permission Model (--experimental-permission)

Node 20 introduces a capability-based permission model:

# Only allow reading /data and writing /tmp
node --experimental-permission \
  --allow-fs-read=/data \
  --allow-fs-write=/tmp \
  --allow-net=api.example.com \
  app.js

# Permission flags:
# --allow-fs-read=<path>     Allow file system read
# --allow-fs-write=<path>    Allow file system write
# --allow-net=<host>         Allow network access
# --allow-worker             Allow worker_threads
# --allow-child-process      Allow child_process.spawn
# --allow-wasi               Allow WASI

💡 The permission model follows the principle of least privilege. Deny all by default, explicitly grant only what's needed.


Step 2: AES-GCM Encryption with WebCrypto


Step 3: Ed25519 Digital Signatures

📸 Verified Output:


Step 4: Key Derivation — scrypt & PBKDF2

💡 Use scrypt or Argon2 for passwords. Use PBKDF2 for legacy compatibility. Never use MD5/SHA1 for password hashing.


Step 5: Prototype Pollution Prevention


Step 6: Secure HTTP Headers


Step 7: Secure Random & Constant-Time Comparison


Step 8: Capstone — Secure Request Handler

Build a complete secure request signing and verification system:


Summary

Security Control
API / Tool
Against

AES-GCM encryption

webcrypto.subtle.encrypt

Data exposure

Ed25519 signatures

subtle.sign/verify

Message tampering

scrypt / PBKDF2

crypto.scrypt, crypto.pbkdf2

Password cracking

Timing-safe compare

crypto.timingSafeEqual

Timing attacks

Prototype pollution

Object.create(null)

Injection attacks

CSP headers

Content-Security-Policy

XSS

HSTS

Strict-Transport-Security

MITM downgrade

Permission model

--experimental-permission

Privilege escalation

Last updated