Skip to main content

Security Best Practices

security on

Security is not a feature—it's a fundamental requirement. Building secure software protects user data, maintains trust, and prevents costly breaches. This chapter explores essential security practices, common vulnerabilities, and defensive strategies for building robust, secure applications throughout the software development lifecycle.

The Security Mindset​

Security must be integrated into every stage of development, not treated as an afterthought. This "shift-left" approach identifies and addresses vulnerabilities early, reducing risk and remediation costs.

Security is Everyone's Responsibility

Security isn't just the security team's job. Every developer, designer, and stakeholder must consider security implications in their work.

Common Vulnerabilities​

The Open Web Application Security Project (OWASP) maintains a list of the most critical web application security risks. The examples below use the familiar 2021 categories. Consult the OWASP Top 10:2025 for the current classification, including supply chain failures and mishandling of exceptional conditions. Neither list is an exhaustive security checklist.

1. Broken Access Control​

Users can access resources or perform actions beyond their intended permissions.

// Bad: No authorization check
app.get('/api/users/:id/profile', async (req, res) => {
const user = await User.findById(req.params.id);
res.json(user);
});

// Good: Verify user can access resource
app.get('/api/users/:id/profile', authenticateUser, async (req, res) => {
const requestedUserId = req.params.id;
const currentUserId = req.user.id;

// Users can only access their own profile unless they're admin
if (requestedUserId !== currentUserId && !req.user.isAdmin) {
return res.status(403).json({ error: 'Forbidden' });
}

const user = await User.findById(requestedUserId);
if (!user) return res.status(404).json({ error: 'User not found' });
res.json({ id: user.id, displayName: user.displayName });
});

// Better: Use authorization middleware
const authorize = (resource, action) => {
return async (req, res, next) => {
const hasPermission = await checkPermission(req.user, resource, action, req.params.id);

if (!hasPermission) {
return res.status(403).json({ error: 'Forbidden' });
}

next();
};
};

app.get('/api/users/:id/profile', authenticateUser, authorize('user:profile', 'read'), async (req, res) => {
const user = await User.findById(req.params.id);
if (!user) return res.status(404).json({ error: 'User not found' });
res.json({ id: user.id, displayName: user.displayName });
});

Prevention strategies:

  • Implement role-based access control (RBAC) or attribute-based access control (ABAC)
  • Deny access by default, explicitly grant permissions
  • Enforce authorization checks on every request
  • Log access control failures for monitoring

2. Cryptographic Failures​

Inadequate protection of sensitive data through weak or missing encryption.

// Bad: Storing passwords in plain text
const user = {
email: 'user@example.com',
password: 'Password123', // Never store plain text passwords!
};

// Bad: Weak hashing
const crypto = require('crypto');
const hash = crypto.createHash('md5').update(password).digest('hex'); // MD5 is broken

// Good: Use bcrypt for password hashing
const bcrypt = require('bcrypt');

async function hashPassword(password) {
const saltRounds = 12; // Adjust based on security requirements
return await bcrypt.hash(password, saltRounds);
}

async function verifyPassword(password, hash) {
return await bcrypt.compare(password, hash);
}

// Usage
const hashedPassword = await hashPassword('Password123');
const isValid = await verifyPassword('Password123', hashedPassword);

// Encrypting sensitive data at rest
const encryptData = (data, key) => {
const algorithm = 'aes-256-gcm';
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, key, iv);

let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');

const authTag = cipher.getAuthTag();

return {
encrypted,
iv: iv.toString('hex'),
authTag: authTag.toString('hex'),
};
};

const decryptData = (encryptedData, key) => {
const algorithm = 'aes-256-gcm';
const decipher = crypto.createDecipheriv(algorithm, key, Buffer.from(encryptedData.iv, 'hex'));

decipher.setAuthTag(Buffer.from(encryptedData.authTag, 'hex'));

let decrypted = decipher.update(encryptedData.encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');

return decrypted;
};

Prevention strategies:

  • Use strong, industry-standard algorithms (AES-256, RSA-2048+)
  • Never implement custom cryptography
  • Use bcrypt, scrypt, or Argon2 for password hashing
  • Encrypt sensitive data at rest and in transit (TLS 1.3)
  • Properly manage encryption keys (use key management services)

3. Injection​

Untrusted data is sent to an interpreter as part of a command or query.

// Bad: SQL Injection vulnerability
app.post('/api/login', async (req, res) => {
const { username, password } = req.body;

// NEVER DO THIS - vulnerable to SQL injection
const query = `SELECT * FROM users WHERE username = '${username}' AND password = '${password}'`;
const user = await db.query(query);

// Attacker can input: ' OR '1'='1
// Resulting query: SELECT * FROM users WHERE username = '' OR '1'='1' AND password = ''
});

// Good: Use parameterized queries
app.post('/api/login', async (req, res) => {
const { username, password } = req.body;

// Parameterized query prevents injection
const query = 'SELECT * FROM users WHERE username = $1';
const result = await db.query(query, [username]);

if (result.rows.length === 0) {
return res.status(401).json({ error: 'Invalid credentials' });
}

const user = result.rows[0];
const isValidPassword = await bcrypt.compare(password, user.password_hash);

if (!isValidPassword) {
return res.status(401).json({ error: 'Invalid credentials' });
}

// Generate session token
const token = generateAuthToken(user);
res.json({ token });
});

// Bad: Command Injection
const { exec } = require('child_process');

app.post('/api/convert', (req, res) => {
const { filename } = req.body;

// NEVER DO THIS - vulnerable to command injection
exec(`convert ${filename} output.png`, (error, stdout) => {
res.json({ success: true });
});

// Attacker can input: file.jpg; rm -rf /
});

// Good: Use safer alternatives
const { spawn } = require('child_process');
const path = require('node:path');
const crypto = require('node:crypto');

app.post('/api/convert', (req, res) => {
const { filename } = req.body;

// Validate input
if (typeof filename !== 'string' || !/^[a-zA-Z0-9][a-zA-Z0-9_.-]*\.(png|jpe?g)$/.test(filename)) {
return res.status(400).json({ error: 'Invalid filename' });
}

// Use spawn with array arguments (no shell interpretation)
// Resolve within a controlled directory; uploaded files must not be symlinks.
const input = path.join('/srv/uploads', filename);
const output = path.join('/srv/converted', `${crypto.randomUUID()}.png`);
const process = spawn('magick', [input, output], { shell: false });
process.on('error', () => {
if (!res.headersSent) res.status(500).json({ error: 'Conversion failed' });
});

process.on('close', (code) => {
if (res.headersSent) return;
if (code === 0) {
res.json({ success: true });
} else {
res.status(500).json({ error: 'Conversion failed' });
}
});
});

// Bad: NoSQL Injection
app.post('/api/login', async (req, res) => {
const { username, password } = req.body;

// Vulnerable to NoSQL injection
const user = await User.findOne({
username: username,
password: password,
});

// Attacker can send: { username: { $ne: null }, password: { $ne: null } }
});

// Good: Validate and sanitize input
app.post('/api/login', async (req, res) => {
const { username, password } = req.body;

// Validate types
if (typeof username !== 'string' || typeof password !== 'string') {
return res.status(400).json({ error: 'Invalid input' });
}

// Find user by username only
const user = await User.findOne({ username: username });

if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}

// Verify password separately
const isValid = await bcrypt.compare(password, user.passwordHash);

if (!isValid) {
return res.status(401).json({ error: 'Invalid credentials' });
}

res.json({ token: generateToken(user) });
});

Prevention strategies:

  • Use parameterized queries or ORMs
  • Validate and sanitize all user input
  • Use allowlists for input validation
  • Avoid shell commands; use native APIs
  • Apply principle of least privilege to database accounts

4. Insecure Design​

Fundamental flaws in application architecture and design.

Prevention strategies:

  • Conduct threat modeling during design phase
  • Implement security patterns (defense in depth, fail securely)
  • Establish secure development lifecycle
  • Use security architecture reviews
  • Build security requirements into user stories

5. Security Misconfiguration​

Improperly configured security settings, often due to defaults or incomplete configuration.

// Bad: Exposing sensitive information
app.use((err, req, res, next) => {
// Never expose stack traces in production
res.status(500).json({
error: err.message,
stack: err.stack, // Exposes internal details!
});
});

// Good: Safe error handling
app.use((err, req, res, next) => {
// Log full error server-side
logger.error('Server error:', {
error: err.message,
stack: err.stack,
url: req.url,
method: req.method,
});

// Return generic error to client
const isDevelopment = process.env.NODE_ENV === 'development';

res.status(500).json({
error: 'Internal server error',
...(isDevelopment && { details: err.message }),
});
});

// Bad: Insecure CORS configuration
app.use(
cors({
origin: '*', // Browsers reject wildcard origins with credentialed requests.
credentials: true,
}),
);

// Good: Restrictive CORS
app.use(
cors({
origin: process.env.ALLOWED_ORIGINS?.split(',') || ['https://myapp.com'],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
maxAge: 86400, // 24 hours
}),
);

// Security headers middleware
const helmet = require('helmet');

app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
scriptSrc: ["'self'"],
imgSrc: ["'self'", 'data:', 'https:'],
connectSrc: ["'self'"],
fontSrc: ["'self'"],
objectSrc: ["'none'"],
mediaSrc: ["'self'"],
frameSrc: ["'none'"],
},
},
hsts: {
maxAge: 31536000,
includeSubDomains: true,
preload: true,
},
referrerPolicy: { policy: 'strict-origin-when-cross-origin' },
}),
);

CORS controls browser access to responses; it does not replace authentication, authorization, or CSRF protection.

Security checklist:

# Security checklist (plain text, not a YAML configuration)
environment:
- [ ] Disable debug mode in production
- [ ] Remove default credentials
- [ ] Set secure cookie flags (httpOnly, secure, sameSite)
- [ ] Configure HTTPS/TLS properly
- [ ] Disable directory listing
- [ ] Remove unnecessary services/features

headers:
- [ ] Content-Security-Policy configured
- [ ] X-Frame-Options set to DENY or SAMEORIGIN
- [ ] X-Content-Type-Options set to nosniff
- [ ] Strict-Transport-Security enabled
- [ ] Referrer-Policy configured

dependencies:
- [ ] All dependencies up to date
- [ ] Vulnerability scan passed
- [ ] No known CVEs in dependencies
- [ ] Unused dependencies removed

access:
- [ ] Principle of least privilege applied
- [ ] Strong authentication required
- [ ] MFA enabled for admin accounts
- [ ] Rate limiting implemented
- [ ] Account lockout on failed attempts

6. Vulnerable and Outdated Components​

Using components with known vulnerabilities.

# Regular dependency scanning
npm audit
npm audit fix

# Automated scanning in CI/CD
# .github/workflows/security.yml
name: Security Scan

on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: '0 0 * * 0' # Weekly

jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7.0.1

- name: Run npm audit
run: npm audit --audit-level=moderate

- name: Run Snyk security scan
uses: snyk/actions/node@b075801f26076be9bb50521154cafab73f44da0c
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=high

- name: OWASP Dependency Check
uses: dependency-check/Dependency-Check_Action@1e54355a8b4c8abaa8cc7d0b70aa655a3bb15a6c
with:
project: 'myapp'
path: '.'
format: 'HTML'

7. Identification and Authentication Failures​

Weak authentication or session management.

// Authentication implementation
const jwt = require('jsonwebtoken');
const speakeasy = require('speakeasy');

class AuthService {
// A 15-character minimum also supports password-only authentication.
static PASSWORD_MIN_LENGTH = 15;

static validatePassword(password) {
if (typeof password !== 'string' || [...password].length < this.PASSWORD_MIN_LENGTH) {
throw new Error(`Password must be at least ${this.PASSWORD_MIN_LENGTH} characters`);
}

// Check against common passwords
if (this.isCommonPassword(password)) {
throw new Error('Password is too common');
}
}

// Multi-factor authentication
static generateMFASecret(user) {
const secret = speakeasy.generateSecret({
name: `MyApp (${user.email})`,
length: 32,
});

return {
secret: secret.base32,
qrCode: secret.otpauth_url,
};
}

static verifyMFAToken(secret, token) {
return speakeasy.totp.verify({
secret: secret,
encoding: 'base32',
token: token,
window: 1, // Allow 30s time drift
});
}

// Secure session token generation
static generateToken(user) {
const payload = {
userId: user.id,
email: user.email,
roles: user.roles,
iat: Math.floor(Date.now() / 1000),
};

return jwt.sign(payload, process.env.JWT_SECRET, {
expiresIn: '15m', // Short-lived access token
algorithm: 'HS256',
issuer: 'myapp.com',
audience: 'myapp-api',
});
}

static generateRefreshToken(user) {
const payload = {
userId: user.id,
type: 'refresh',
iat: Math.floor(Date.now() / 1000),
};

return jwt.sign(payload, process.env.REFRESH_TOKEN_SECRET, {
expiresIn: '7d', // Longer-lived refresh token
algorithm: 'HS256',
issuer: 'myapp.com',
audience: 'myapp-refresh',
jwtid: require('node:crypto').randomUUID(),
});
}

static verifyToken(token) {
try {
return jwt.verify(token, process.env.JWT_SECRET, {
algorithms: ['HS256'],
issuer: 'myapp.com',
audience: 'myapp-api',
});
} catch (error) {
throw new Error('Invalid or expired token');
}
}
}

// Rate limiting for authentication endpoints
const rateLimit = require('express-rate-limit');

const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 5, // 5 requests per window
message: 'Too many login attempts, please try again later',
standardHeaders: true,
legacyHeaders: false,
handler: (req, res) => {
logger.warn('Rate limit exceeded', {
ip: req.ip,
endpoint: req.path,
});

res.status(429).json({
error: 'Too many requests',
retryAfter: Math.max(0, Math.ceil((req.rateLimit.resetTime.getTime() - Date.now()) / 1000)),
});
},
});

app.post('/api/auth/login', authLimiter, async (req, res) => {
const { email, password, mfaToken } = req.body;
if (
typeof email !== 'string' ||
typeof password !== 'string' ||
(mfaToken !== undefined && typeof mfaToken !== 'string')
) {
return res.status(400).json({ error: 'Invalid input' });
}

try {
// Find user
const user = await User.findOne({ email });

if (!user || (await isAccountLocked(user.id))) {
// Use a generic error to reduce response-based account enumeration.
return res.status(401).json({ error: 'Invalid credentials' });
}

// Verify password
const isValidPassword = await bcrypt.compare(password, user.passwordHash);

if (!isValidPassword) {
// Log failed attempt
await logFailedLogin(user.id, req.ip);

// Check if account should be locked
if (await shouldLockAccount(user.id)) {
await lockAccount(user.id);
return res.status(401).json({ error: 'Invalid credentials' });
}

return res.status(401).json({ error: 'Invalid credentials' });
}

// Check if MFA is enabled
if (user.mfaEnabled) {
if (!mfaToken) {
return res.status(401).json({ error: 'MFA token required' });
}

const isValidMFA = AuthService.verifyMFAToken(user.mfaSecret, mfaToken);

if (!isValidMFA) {
return res.status(401).json({ error: 'Invalid MFA token' });
}
}

// Generate tokens
const accessToken = AuthService.generateToken(user);
const refreshToken = AuthService.generateRefreshToken(user);

// Persist only a hash of the refresh token, plus expiry and reuse-detection metadata.
const refreshHash = require('node:crypto').createHash('sha256').update(refreshToken).digest('hex');
await storeRefreshToken(user.id, refreshHash, req.ip, req.headers['user-agent']);

// Log successful login
await logSuccessfulLogin(user.id, req.ip);

res.json({
accessToken,
refreshToken,
expiresIn: 900, // 15 minutes
});
} catch (error) {
logger.error('Login error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});

Password policy should favor length and a blocklist of common or compromised passwords. Do not require arbitrary character mixtures or scheduled password changes without evidence of compromise. NIST specifies at least 15 characters for a password used alone and permits an eight-character minimum when the password is part of MFA; allow at least 64 characters. Use a maintained Argon2id implementation for new password storage. Legacy bcrypt implementations commonly limit inputs to 72 bytes, so do not silently truncate long passwords. See NIST password guidance and OWASP password storage guidance.

The login example assumes application-specific helpers for lock expiry, atomic failed-attempt tracking, and token persistence. Generic errors reduce response-based account enumeration; timing differences also need attention. MFA verification must reject reuse of a previously accepted code and enforce its own attempt limit.

8. Software and Data Integrity Failures​

Insecure CI/CD pipelines, unsigned software, or unverified updates.

# Security checks for a Node.js application; signing/deployment is configured separately.
name: Security Checks
on:
push:
branches: [main]
pull_request:

permissions:
contents: read

jobs:
security-checks:
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write
steps:
- uses: actions/checkout@v7.0.1
with:
fetch-depth: 0
- uses: actions/setup-node@v7.0.0
with:
node-version: '24'
cache: npm
- run: npm ci
- run: npm audit --audit-level=high
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: javascript-typescript
- name: Analyze source
uses: github/codeql-action/analyze@v4
- name: Dependency review
if: github.event_name == 'pull_request'
uses: actions/dependency-review-action@v5.0.0
- name: Secret scan
uses: trufflesecurity/trufflehog@v3.97.9
with:
path: ./
extra_args: --results=verified,unknown

build:
needs: security-checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7.0.1
- uses: actions/setup-node@v7.0.0
with:
node-version: '24'
cache: npm
- run: npm ci
- run: npm run build

9. Security Logging and Monitoring Failures​

Insufficient logging and monitoring to detect and respond to security incidents.

// Comprehensive security logging
const winston = require('winston');

const securityLogger = winston.createLogger({
level: 'info',
format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
defaultMeta: { service: 'security' },
transports: [
new winston.transports.File({ filename: 'security.log' }),
new winston.transports.File({ filename: 'security-error.log', level: 'error' }),
],
});

// Security event types to log
const SecurityEvents = {
AUTH_ATTEMPT: 'auth.attempt',
AUTH_SUCCESS: 'auth.success',
AUTH_FAILURE: 'auth.failure',
AUTH_LOCKOUT: 'auth.lockout',
ACCESS_DENIED: 'access.denied',
PRIVILEGE_ESCALATION: 'privilege.escalation',
DATA_ACCESS: 'data.access',
DATA_MODIFICATION: 'data.modification',
DATA_DELETION: 'data.deletion',
CONFIG_CHANGE: 'config.change',
SUSPICIOUS_ACTIVITY: 'suspicious.activity',
};

function logSecurityEvent(eventType, details) {
securityLogger.info({
eventType,
timestamp: new Date().toISOString(),
...details,
});
}

// Middleware to log security events
app.use((req, res, next) => {
// Log all authentication attempts
if (req.path.startsWith('/api/auth/')) {
logSecurityEvent(SecurityEvents.AUTH_ATTEMPT, {
ip: req.ip,
path: req.path,
userAgent: req.headers['user-agent'],
});
}

// Track on response
res.on('finish', () => {
if (res.statusCode === 401 || res.statusCode === 403) {
logSecurityEvent(SecurityEvents.ACCESS_DENIED, {
ip: req.ip,
path: req.path,
method: req.method,
statusCode: res.statusCode,
userId: req.user?.id,
});
}
});

next();
});

// Anomaly detection
class AnomalyDetector {
constructor(windowMs = 60_000, maxKeys = 10_000) {
if (!Number.isSafeInteger(windowMs) || windowMs <= 0 || !Number.isSafeInteger(maxKeys) || maxKeys <= 0) {
throw new RangeError('Window and capacity must be positive integers');
}
this.windowMs = windowMs;
this.maxKeys = maxKeys;
this.requestCounts = new Map();
}

trackRequest(ip, endpoint) {
const key = `${ip}:${endpoint}`;
const now = Date.now();
let state = this.requestCounts.get(key);
if (!state || now >= state.expiresAt) {
this.requestCounts.delete(key);
if (this.requestCounts.size >= this.maxKeys) {
this.requestCounts.delete(this.requestCounts.keys().next().value);
}
state = { count: 0, expiresAt: now + this.windowMs, alerted: false };
this.requestCounts.set(key, state);
}

state.count++;
if (state.count > 100 && !state.alerted) {
state.alerted = true;
this.flagSuspiciousActivity(ip, endpoint, state.count);
}
}

flagSuspiciousActivity(ip, endpoint, count) {
logSecurityEvent(SecurityEvents.SUSPICIOUS_ACTIVITY, {
ip,
endpoint,
requestCount: count,
severity: 'high',
});

// Notify security team
this.notifySecurityTeam({
type: 'Potential DDoS or brute force',
ip,
endpoint,
count,
});
}

notifySecurityTeam(alert) {
// Send to monitoring system, Slack, PagerDuty, etc.
console.log('SECURITY ALERT:', alert);
}
}

This bounded, single-process counter illustrates an alerting heuristic. It resets each key's window and sends at most one alert per window; eviction or process restarts lose history. Use shared monitoring for multiple instances and investigate signals before blocking traffic.

10. Server-Side Request Forgery (SSRF)​

Application fetches remote resources without validating user-supplied URLs.

// Bad: SSRF vulnerability
app.post('/api/fetch', async (req, res) => {
const { url } = req.body;

// Attacker can access internal services
// Example: http://localhost:6379/
// Or: http://169.254.169.254/latest/meta-data/
const response = await fetch(url);
const data = await response.text();

res.send(data);
});

// Good: Select a server-configured endpoint instead of accepting arbitrary URLs.
// Configure network egress rules to block private and metadata services as well.
const endpoints = Object.freeze({
catalog: 'https://api.example.com/catalog',
status: 'https://status.example.com/summary',
});

app.post('/api/fetch', async (req, res) => {
const { resource } = req.body;
if (typeof resource !== 'string' || !Object.hasOwn(endpoints, resource)) {
return res.status(400).json({ error: 'Unknown resource' });
}

try {
const response = await fetch(endpoints[resource], {
signal: AbortSignal.timeout(5000),
redirect: 'error',
});
if (!response.ok || !response.body) throw new Error('Upstream request failed');

const chunks = [];
let size = 0;
for await (const chunk of response.body) {
size += chunk.byteLength;
if (size > 10 * 1024 * 1024) throw new Error('Response too large');
chunks.push(Buffer.from(chunk));
}

// Treat upstream content as text instead of executing it as HTML.
res.type('text/plain').send(Buffer.concat(chunks).toString('utf8'));
} catch (error) {
logger.error('Upstream fetch failed');
res.status(502).json({ error: 'Failed to fetch resource' });
}
});

A hostname allowlist alone does not prevent DNS rebinding. For applications that must accept user-supplied URLs, validate both IPv4 and IPv6 destinations, bind the connection to the validated address, restrict network egress, and revalidate every redirect. See the OWASP SSRF prevention guidance.

Authentication and Authorization​

JSON Web Tokens (JWT) Best Practices​

// Secure JWT implementation
const jwt = require('jsonwebtoken');
const crypto = require('crypto');

class JWTService {
constructor() {
// Use strong secrets (32+ bytes random)
this.accessTokenSecret = process.env.JWT_ACCESS_SECRET;
this.refreshTokenSecret = process.env.JWT_REFRESH_SECRET;

if (!this.accessTokenSecret || !this.refreshTokenSecret) {
throw new Error('JWT secrets must be configured');
}
}

createAccessToken(user) {
return jwt.sign(
{
sub: user.id, // Subject
email: user.email,
roles: user.roles,
type: 'access',
},
this.accessTokenSecret,
{
expiresIn: '15m',
algorithm: 'HS256',
issuer: 'myapp.com',
audience: 'myapp-api',
jwtid: crypto.randomBytes(16).toString('hex'), // Unique token ID
},
);
}

createRefreshToken(user) {
return jwt.sign(
{
sub: user.id,
type: 'refresh',
},
this.refreshTokenSecret,
{
expiresIn: '7d',
algorithm: 'HS256',
issuer: 'myapp.com',
audience: 'myapp-refresh',
jwtid: crypto.randomBytes(16).toString('hex'),
},
);
}

verifyAccessToken(token) {
try {
const payload = jwt.verify(token, this.accessTokenSecret, {
algorithms: ['HS256'],
issuer: 'myapp.com',
audience: 'myapp-api',
});

if (payload.type !== 'access') {
throw new Error('Invalid token type');
}

return payload;
} catch (error) {
throw new Error('Invalid or expired token');
}
}

verifyRefreshToken(token) {
try {
const payload = jwt.verify(token, this.refreshTokenSecret, {
algorithms: ['HS256'],
issuer: 'myapp.com',
audience: 'myapp-refresh',
});

if (payload.type !== 'refresh') {
throw new Error('Invalid token type');
}

return payload;
} catch (error) {
throw new Error('Invalid or expired refresh token');
}
}
}

// Token blacklist for revocation (ioredis API; use a connected client)
class TokenBlacklist {
constructor(redis) {
this.redis = redis;
}

async revoke(jti, expiresIn) {
// Store token ID with expiration
await this.redis.setex(`blacklist:${jti}`, expiresIn, '1');
}

async isRevoked(jti) {
const result = await this.redis.get(`blacklist:${jti}`);
return result !== null;
}
}

// Authentication middleware
async function authenticateToken(req, res, next) {
const authHeader = req.headers['authorization'];
const token = /^Bearer +([^\s]+)$/i.exec(authHeader ?? '')?.[1];

if (!token) {
res.set('WWW-Authenticate', 'Bearer realm="myapp-api"');
return res.status(401).json({ error: 'No token provided' });
}

try {
const payload = jwtService.verifyAccessToken(token);

// Check if token is blacklisted
if (await tokenBlacklist.isRevoked(payload.jti)) {
res.set('WWW-Authenticate', 'Bearer realm="myapp-api"');
return res.status(401).json({ error: 'Token has been revoked' });
}

req.user = payload;
next();
} catch (error) {
res.set('WWW-Authenticate', 'Bearer realm="myapp-api"');
return res.status(401).json({ error: 'Invalid token' });
}
}

Keep access and refresh signing secrets separate. Rotate refresh tokens and store their hashes with expiry and reuse detection in a durable store. Revocation-cache failures need an explicit policy; sensitive operations should fail closed. The examples omit token issuance routes and persistence setup.

OAuth 2.0 and OpenID Connect​

OAuth 2.0 delegates access; OpenID Connect adds authentication. Use a maintained client rather than decoding JWTs and treating their contents as verified. This example uses the openid-client 6 API with a trusted HTTPS issuer and a pre-registered callback URL. Application-specific database and logging helpers are omitted.

import * as client from 'openid-client';

const redirectUri = 'https://app.example.com/auth/callback';
const config = await client.discovery(
new URL(process.env.OIDC_ISSUER),
process.env.OIDC_CLIENT_ID,
process.env.OIDC_CLIENT_SECRET,
);
// Also validate ID token signatures against the issuer's published keys.
client.enableNonRepudiationChecks(config);

app.get('/auth/login', async (req, res, next) => {
try {
const verifier = client.randomPKCECodeVerifier();
const state = client.randomState();
const nonce = client.randomNonce();
req.session.oidc = { verifier, state, nonce };

const url = client.buildAuthorizationUrl(config, {
redirect_uri: redirectUri,
scope: 'openid profile email',
code_challenge: await client.calculatePKCECodeChallenge(verifier),
code_challenge_method: 'S256',
state,
nonce,
});
// Persist the server-side session before leaving the application.
await new Promise((resolve, reject) => req.session.save((err) => (err ? reject(err) : resolve())));
res.redirect(url.href);
} catch (error) {
next(error);
}
});

app.get('/auth/callback', async (req, res, next) => {
const pending = req.session.oidc;
if (!pending?.state || !pending?.nonce || !pending?.verifier) {
return res.status(400).json({ error: 'No pending login' });
}

delete req.session.oidc;
try {
await new Promise((resolve, reject) => req.session.save((err) => (err ? reject(err) : resolve())));
// Use the configured origin, never a client-supplied Host header.
const callbackUrl = new URL(req.originalUrl, redirectUri);
const tokens = await client.authorizationCodeGrant(config, callbackUrl, {
pkceCodeVerifier: pending.verifier,
expectedState: pending.state,
expectedNonce: pending.nonce,
idTokenExpected: true,
});
const claims = tokens.claims();
const user = await findOrCreateUser({
issuer: claims.iss,
subject: claims.sub,
email: claims.email_verified === true ? claims.email : undefined,
name: claims.name,
});

// Rotate the session ID after authentication to prevent session fixation.
await new Promise((resolve, reject) => req.session.regenerate((err) => (err ? reject(err) : resolve())));
req.session.userId = user.id;
await new Promise((resolve, reject) => req.session.save((err) => (err ? reject(err) : resolve())));
res.redirect('/dashboard');
} catch (error) {
next(error); // Central error handling returns a generic authentication error.
}
});

The library validates the issuer, audience, expiration, nonce, state, and PKCE exchange; signature checks are explicitly enabled above. Identify accounts by the pair of issuer and subject, and do not automatically link accounts by email. Use secure, HTTP-only session cookies, a suitable SameSite policy, short login-attempt lifetimes, and a shared session store that consumes each attempt atomically when handling concurrent callbacks. See the client checks and OpenID Connect validation rules.

Input Validation and Sanitization​

// Input validation library
const validator = require('validator');
const sanitizeHtml = require('sanitize-html');

class InputValidator {
// Email validation
static isValidEmail(email) {
return validator.isEmail(email, {
allow_utf8_local_part: false,
require_tld: true,
});
}

// URL validation
static isValidURL(url) {
return validator.isURL(url, {
protocols: ['https'],
require_protocol: true,
require_valid_protocol: true,
});
}

// Phone number validation
static isValidPhone(phone) {
return validator.isMobilePhone(phone, 'any', {
strictMode: true,
});
}

// Alphanumeric validation
static isAlphanumeric(str) {
return validator.isAlphanumeric(str, 'en-US');
}

// UUID validation
static isValidUUID(uuid) {
return validator.isUUID(uuid, 4);
}

// HTML sanitization
static sanitizeHTML(dirty) {
return sanitizeHtml(dirty, {
allowedTags: ['b', 'i', 'em', 'strong', 'a', 'p', 'br'],
allowedAttributes: {
a: ['href'],
},
allowedSchemes: ['https'],
});
}

// Display-label sanitization only; this does not establish filesystem safety.
static sanitizeFilename(filename) {
return filename.replace(/[^a-zA-Z0-9._-]/g, '').substring(0, 255);
}
}

// Validate one explicit request source, rather than falling back between sources.
function validateRequest(schema, source = 'body') {
return (req, res, next) => {
const input = req[source];
if (!input || typeof input !== 'object' || Array.isArray(input)) {
return res.status(400).json({ error: 'Invalid input' });
}

for (const [field, rules] of Object.entries(schema)) {
const value = input[field];
const missing = value === undefined || value === null || value === '';
if (missing && !rules.required) continue;

let valid = !missing;
if (rules.type === 'number') {
valid = valid && typeof value === 'number' && Number.isFinite(value);
if (rules.min !== undefined) valid = valid && value >= rules.min;
if (rules.max !== undefined) valid = valid && value <= rules.max;
} else {
valid = valid && typeof value === 'string';
if (valid && rules.type === 'email') valid = InputValidator.isValidEmail(value);
if (valid && rules.type === 'url') valid = InputValidator.isValidURL(value);
if (valid && rules.maxLength !== undefined) valid = value.length <= rules.maxLength;
if (valid && rules.pattern) valid = rules.pattern.test(value);
}

if (!valid) return res.status(400).json({ error: 'Validation failed', field });
}
next();
};
}

// Usage
app.post(
'/api/users',
validateRequest({
email: { required: true, type: 'email' },
username: { required: true, pattern: /^[a-zA-Z0-9_]{3,20}$/ },
age: { required: true, type: 'number', min: 18, max: 120 },
website: { type: 'url' },
}),
async (req, res) => {
// Request is validated
const { email, username, age, website } = req.body;
const user = await createUser({ email, username, age, website });
res.json(user);
},
);

Validation does not replace parameterized database queries, object-level authorization, or output encoding. Construct an allowlisted persistence object after validation instead of passing the entire request body to an ORM; this prevents mass assignment of fields such as roles or administrative privileges.

Secure Configuration Management​

// Environment-based configuration
class SecureConfig {
constructor() {
this.env = process.env.NODE_ENV || 'development';
this.loadConfig();
}

loadConfig() {
// Load from environment variables
this.config = {
app: {
port: parseInt(process.env.PORT || '3000'),
host: process.env.HOST || 'localhost',
env: this.env,
},
database: {
host: this.requireEnv('DB_HOST'),
port: parseInt(this.requireEnv('DB_PORT')),
database: this.requireEnv('DB_NAME'),
username: this.requireEnv('DB_USER'),
password: this.requireEnv('DB_PASSWORD'),
ssl: this.env === 'production',
},
security: {
jwtSecret: this.requireEnv('JWT_SECRET'),
refreshTokenSecret: this.requireEnv('REFRESH_TOKEN_SECRET'),
encryptionKey: this.requireEnv('ENCRYPTION_KEY'),
sessionSecret: this.requireEnv('SESSION_SECRET'),
},
features: {
rateLimiting: {
enabled: true,
maxRequests: 100,
windowMs: 15 * 60 * 1000,
},
mfa: {
enabled: this.env === 'production',
required: false,
},
},
};

// Validate configuration
this.validateConfig();
}

requireEnv(key) {
const value = process.env[key];

if (!value) {
throw new Error(`Missing required environment variable: ${key}`);
}

return value;
}

validateConfig() {
// Ensure secrets are strong enough
if (this.config.security.jwtSecret.length < 32) {
throw new Error('JWT secret must be at least 32 characters');
}

// In production, enforce stricter requirements
if (this.env === 'production') {
if (!this.config.database.ssl) {
throw new Error('Database SSL must be enabled in production');
}
}
}

get(path) {
return path.split('.').reduce((obj, key) => obj?.[key], this.config);
}
}

// Secrets management with AWS Secrets Manager
const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager');

class SecretsManager {
constructor() {
this.client = new SecretsManagerClient({ region: process.env.AWS_REGION });
this.cache = new Map();
}

async getSecret(secretName) {
// Check cache first
if (this.cache.has(secretName)) {
return this.cache.get(secretName);
}

try {
const command = new GetSecretValueCommand({ SecretId: secretName });
const response = await this.client.send(command);

const secret = JSON.parse(response.SecretString);

// Cache for 5 minutes
this.cache.set(secretName, secret);
setTimeout(() => this.cache.delete(secretName), 5 * 60 * 1000);

return secret;
} catch (error) {
logger.error('Failed to retrieve secret:', error);
throw new Error('Failed to retrieve secret');
}
}
}

Security Testing​

Static Application Security Testing (SAST)​

# ESLint security plugin
npm install --save-dev eslint eslint-plugin-security
// eslint.config.cjs
const security = require('eslint-plugin-security');
module.exports = [security.configs.recommended];

The ESLint security plugin uses ESLint's flat configuration. Its findings need review because a suspicious pattern can be safe in context.

# Run security linting
npx eslint src/

# SonarQube scanning
sonar-scanner \
-Dsonar.projectKey=myapp \
-Dsonar.sources=src \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.token="$SONAR_TOKEN"

# Semgrep security scanning
semgrep --config=auto src/

Dynamic Application Security Testing (DAST)​

# OWASP ZAP scanning
docker run -t -v "$PWD:/zap/wrk:rw" ghcr.io/zaproxy/zaproxy:stable zap-baseline.py \
-t https://myapp.example.com \
-r zap-report.html

# Burp Suite scanning (via API)
curl -X POST https://burp-api.example.com/scan \
-H "Authorization: Bearer $API_KEY" \
-d '{
"target": "https://myapp.example.com",
"scope": {
"include": ["https://myapp.example.com/*"],
"exclude": ["https://myapp.example.com/logout"]
}
}'

Penetration Testing Checklist​

## Authentication & Session Management

- [ ] Test for weak passwords
- [ ] Test for account enumeration
- [ ] Test for brute force protection
- [ ] Test session timeout
- [ ] Test concurrent login
- [ ] Test logout functionality
- [ ] Test remember me functionality
- [ ] Test password reset flow

## Authorization

- [ ] Test horizontal privilege escalation
- [ ] Test vertical privilege escalation
- [ ] Test direct object references
- [ ] Test forced browsing
- [ ] Test missing authorization checks

## Input Validation

- [ ] Test for SQL injection
- [ ] Test for XSS (reflected, stored, DOM)
- [ ] Test for command injection
- [ ] Test for path traversal
- [ ] Test for XXE
- [ ] Test for SSRF

## Configuration

- [ ] Test for default credentials
- [ ] Test for directory listing
- [ ] Test for verbose error messages
- [ ] Test for security headers
- [ ] Test for insecure HTTP methods
- [ ] Test SSL/TLS configuration

## Business Logic

- [ ] Test workflow bypass
- [ ] Test race conditions
- [ ] Test negative values
- [ ] Test overflow/underflow
- [ ] Test business rule validation

Incident Response​

Security Incident Response Plan​

This skeleton illustrates incident routing. Pattern matches are candidate signals with false positives and false negatives, so validate them before classifying an incident. Production integrations must redact sensitive event data, retain records in bounded storage, establish trusted client addresses, and require a reviewed policy before blocking or rate limiting a source.

// Incident detection and response
class IncidentResponseSystem {
constructor() {
this.incidents = [];
this.severityLevels = {
CRITICAL: 1,
HIGH: 2,
MEDIUM: 3,
LOW: 4,
};
}

async detectIncident(event) {
// Analyze security event
const incident = this.analyzeEvent(event);

if (incident) {
await this.respondToIncident(incident);
}
}

analyzeEvent(event) {
// Check for known attack patterns
const patterns = [
{ type: 'SQL_INJECTION', regex: /(\bUNION\b|\bSELECT\b.*\bFROM\b)/i },
{ type: 'XSS', regex: /<script|javascript:|onerror=/i },
{ type: 'PATH_TRAVERSAL', regex: /\.\.[\/\\]/ },
{ type: 'COMMAND_INJECTION', regex: /[;&|`$()]/ },
];

for (const pattern of patterns) {
if (pattern.regex.test(event.data)) {
return {
id: crypto.randomUUID(),
type: pattern.type,
severity: this.severityLevels.HIGH,
timestamp: new Date(),
source: event.ip,
details: event,
};
}
}

return null;
}

async respondToIncident(incident) {
// Log incident
logger.error('Security incident detected', incident);

// Store incident
this.incidents.push(incident);
await this.storeIncident(incident);

// Take action based on severity
if (incident.severity === this.severityLevels.CRITICAL) {
await this.handleCriticalIncident(incident);
} else if (incident.severity === this.severityLevels.HIGH) {
await this.handleHighSeverityIncident(incident);
}

// Notify security team
await this.notifySecurityTeam(incident);
}

async handleCriticalIncident(incident) {
// Block source IP immediately
await this.blockIP(incident.source);

// Alert on-call engineer
await this.alertPagerDuty(incident);

// Initiate incident response procedure
await this.initiateIncidentResponse(incident);
}

async handleHighSeverityIncident(incident) {
// Rate limit source IP
await this.rateLimitIP(incident.source);

// Send alert to security channel
await this.alertSlack(incident);
}

async blockIP(ip) {
// Add to firewall blocklist
await firewall.block(ip);

// Add to application blocklist
await redis.setex(`blocked:${ip}`, 86400, '1'); // 24 hours

logger.info(`Blocked IP ${ip}`);
}

async notifySecurityTeam(incident) {
await emailService.send({
to: process.env.SECURITY_TEAM_EMAIL,
subject: `Security Incident: ${incident.type}`,
body: `
Incident ID: ${incident.id}
Type: ${incident.type}
Severity: ${incident.severity}
Source: ${incident.source}
Timestamp: ${incident.timestamp}

Details: ${JSON.stringify(incident.details, null, 2)}
`,
});
}
}

Post-Incident Review Template​

# Security Incident Post-Mortem

## Incident Summary

- **Incident ID**: INC-2024-001
- **Date**: 2024-01-15
- **Severity**: High
- **Status**: Resolved
- **Duration**: 2 hours

## Timeline

- 14:00 - Incident detected by monitoring system
- 14:05 - Security team notified
- 14:15 - Incident response initiated
- 14:30 - Root cause identified
- 15:00 - Vulnerability patched
- 16:00 - Incident resolved

## Impact

- Number of affected users: 1,234
- Data compromised: None
- Service downtime: 0 minutes
- Financial impact: $0

## Root Cause

The incident was caused by an outdated dependency with a known SQL injection vulnerability (CVE-2024-XXXXX).

## Response Actions

1. Blocked malicious IP addresses
2. Applied emergency patch
3. Forced password resets for affected accounts
4. Notified affected users

## Lessons Learned

- Dependency scanning needs to run more frequently
- Need automated alerts for critical vulnerabilities
- Incident response time was good but can be improved

## Action Items

- [ ] Implement daily dependency scans (Owner: Security Team, Due: 2024-01-20)
- [ ] Set up automated vulnerability alerts (Owner: DevOps, Due: 2024-01-18)
- [ ] Update incident response playbook (Owner: Security Lead, Due: 2024-01-25)
- [ ] Conduct security training for developers (Owner: Security Team, Due: 2024-02-01)

Best Practices Summary​

Do's​

✅ Apply defense in depth: Multiple layers of security controls

✅ Follow principle of least privilege: Grant minimum necessary permissions

✅ Validate all input: Never trust user input

✅ Use parameterized queries: Prevent injection attacks

✅ Encrypt sensitive data: Both at rest and in transit

✅ Implement strong authentication: MFA, strong passwords, session management

✅ Log security events: Comprehensive logging for incident response

✅ Keep dependencies updated: Regular security patches

✅ Conduct security testing: SAST, DAST, penetration testing

✅ Have incident response plan: Be prepared for security incidents

Don'ts​

❌ Don't store secrets in code: Use environment variables or secret managers

❌ Don't trust client-side validation: Always validate on server

❌ Don't use weak cryptography: Avoid MD5, SHA1, weak ciphers

❌ Don't expose sensitive information: Stack traces, debug info in production

❌ Don't ignore security warnings: From tools, audits, or advisories

❌ Don't implement custom crypto: Use battle-tested libraries

❌ Don't skip security updates: Especially critical patches

❌ Don't use default credentials: Change all defaults immediately

Conclusion​

Security is an ongoing process, not a one-time task. Building secure software requires:

  1. Security-first mindset throughout the organization
  2. Defense in depth with multiple security layers
  3. Secure coding practices to prevent common vulnerabilities
  4. Regular security testing to identify and fix issues
  5. Continuous monitoring to detect and respond to threats
  6. Incident response preparedness for when breaches occur
  7. Ongoing education to stay current with evolving threats

Start with the fundamentals: authentication, authorization, input validation, and encryption. Build on these with advanced techniques like threat modeling, security testing, and monitoring. Remember that security is everyone's responsibility, and small improvements compound over time.

References​