DEV Community

Cover image for Getting Started with eslint-plugin-node-security
Ofri Peretz
Ofri Peretz

Posted on • Edited on

Getting Started with eslint-plugin-node-security

30+ rules for Node.js security. Cryptography. Process Isolation. File System Safety.

Quick Install

npm install --save-dev eslint-plugin-node-security
Enter fullscreen mode Exit fullscreen mode

Flat Config

// eslint.config.js
import nodeSecurity from 'eslint-plugin-node-security';

export default [nodeSecurity.configs.recommended];
Enter fullscreen mode Exit fullscreen mode

Run ESLint

npx eslint .
Enter fullscreen mode Exit fullscreen mode

You'll see output like:

src/auth/hash.ts
  15:27 error  πŸ”’ CWE-328 CVSS:7.5 | Weak hash algorithm: MD5
               [node-security/no-weak-hash-algorithm] Use crypto.createHash('sha256')

src/api/exec.ts
  10:5  error  πŸ”’ CWE-78 | Detected child process execution
               [node-security/detect-child-process] Avoid exec(), use spawn() or execFile()
Enter fullscreen mode Exit fullscreen mode

Rule Overview

Category Rules Examples
Cryptography 12 Weak hashes, static IVs, ECB mode
System & Process 5 exec(), eval(), unsafe require
File System 6 Zip Slip, TOCTOU, path injection
Best Practices 8 PII in logs, insecure temp storage

Quick Wins

1. Cryptography

// ❌ Weak hash
crypto.createHash('md5').update(data);

// βœ… Strong hash
crypto.createHash('sha256').update(data);
Enter fullscreen mode Exit fullscreen mode

2. System Security

// ❌ Shell injection risk
require('child_process').exec(`ls ${userInput}`);

// βœ… Safer execution
require('child_process').execFile('ls', [userInput]);
Enter fullscreen mode Exit fullscreen mode

3. File System

// ❌ Path traversal risk
fs.readFile(`/data/${userInput}`, cb);

// βœ… Validated path
if (isValid(userInput)) fs.readFile(path.join(ROOT, userInput), cb);
Enter fullscreen mode Exit fullscreen mode

Available Presets

import nodeSecurity from 'eslint-plugin-node-security';

export default [
    // Recommended (Low false positives, High impact)
    nodeSecurity.configs.recommended,

    // All Rules (Stricter auditing)
    nodeSecurity.configs.all
];
Enter fullscreen mode Exit fullscreen mode

Quick Reference

# Install
npm install --save-dev eslint-plugin-node-security

# Config (eslint.config.js)
import nodeSecurity from 'eslint-plugin-node-security';
export default [nodeSecurity.configs.recommended];

# Run
npx eslint .
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ npm: eslint-plugin-node-security
πŸ“– Full Rule List

⭐ Star on GitHub


πŸš€ grep -r exec( in your codebase!

GitHub | X | LinkedIn | Dev.to

Top comments (0)