no-unsafe-deserialization
Detects unsafe deserialization of untrusted data. This rule is part of [`eslint-plugin-secure-coding`](https://www.npmjs.com/package/eslint-plugin-secure-coding
Keywords: unsafe deserialization, CWE-502, RCE, code execution, YAML, pickle, security
Detects unsafe deserialization of untrusted data. This rule is part of eslint-plugin-secure-coding.
💼 This rule is set to error in the recommended config.
Quick Summary
| Aspect | Details |
|---|---|
| CWE Reference | CWE-502 (Unsafe Deserialization) |
| Severity | Critical (CVSS 9.8) |
| Auto-Fix | 💡 Suggestions available |
| Category | Object & Prototype |
Vulnerability and Risk
Vulnerability: Unsafe deserialization happens when an application accepts serialized objects from untrusted sources and deserializes them without validation.
Risk: Serialized data can contain malicious payloads that, upon deserialization, execute arbitrary code (Remote Code Execution - RCE), modify application logic, or cause Denial of Service (DoS). This is often considered one of the most critical security risks.
Rule Details
Unsafe deserialization occurs when untrusted data is deserialized in a way that allows attackers to execute arbitrary code or manipulate application logic. This includes:
- Using
eval()orFunction()on untrusted data - YAML parsers that execute code
- JSON with prototype pollution
- Insecure serialization libraries
Why This Matters
| Issue | Impact | Solution |
|---|---|---|
| 💻 RCE | Full system compromise | Use safe deserializers |
| 🎭 Object Manipulation | Logic bypass | Validate before deserializing |
| 🔓 Auth Bypass | Unauthorized access | Use JSON.parse() for JSON data |
Examples
❌ Incorrect
// eval() for deserialization
const data = eval('(' + userInput + ')');
// Function constructor with user input
const fn = new Function('return ' + userInput);
const data = fn();
// YAML.load() without safe mode
import yaml from 'js-yaml';
const config = yaml.load(userYaml); // Can execute code!
// Unsafe serialization libraries
const obj = serialize.unserialize(userInput);✅ Correct
// Use JSON.parse() for JSON data
const data = JSON.parse(userInput);
// YAML with safeLoad
import yaml from 'js-yaml';
const config = yaml.safeLoad(userYaml);
// Or with explicit safe schema
const config = yaml.load(userYaml, { schema: yaml.SAFE_SCHEMA });
// Validate before deserialization
if (isValidJson(userInput)) {
const data = JSON.parse(userInput);
}
// Use safe serialization libraries
import { safeDeserialize } from 'safe-serialize';
const obj = safeDeserialize(userInput);Configuration
{
rules: {
'secure-coding/no-unsafe-deserialization': ['error', {
dangerousFunctions: ['eval', 'Function', 'serialize.unserialize'],
safeLibraries: ['safe-serialize', 'json5'],
validationFunctions: ['isValidJson', 'validateInput']
}]
}
}Options
| Option | Type | Default | Description |
|---|---|---|---|
dangerousFunctions | string[] | ['eval', 'Function', 'unserialize'] | Dangerous deserialization functions |
safeLibraries | string[] | ['safe-serialize'] | Safe deserialization libraries |
validationFunctions | string[] | ['isValidJson', 'validateInput'] | Input validation functions |
Error Message Format
The rule provides LLM-optimized error messages (Compact 2-line format) with actionable security guidance:
🔒 CWE-502 OWASP:A08 CVSS:9.8 | Deserialization of Untrusted Data detected | CRITICAL
Fix: Review and apply the recommended fix | https://owasp.org/Top10/A08_2021/Message Components
| Component | Purpose | Example |
|---|---|---|
| Risk Standards | Security benchmarks | CWE-502 OWASP:A08 CVSS:9.8 |
| Issue Description | Specific vulnerability | Deserialization of Untrusted Data detected |
| Severity & Compliance | Impact assessment | CRITICAL |
| Fix Instruction | Actionable remediation | Follow the remediation steps below |
| Technical Truth | Official reference | OWASP Top 10 |
Known False Negatives
The following patterns are not detected due to static analysis limitations:
Values from Variables
Why: Values stored in variables are not traced.
// ❌ NOT DETECTED - Value from variable
const value = userInput;
dangerousOperation(value);Mitigation: Validate all user inputs.
Wrapper Functions
Why: Custom wrappers not recognized.
// ❌ NOT DETECTED - Wrapper
myWrapper(userInput); // Uses dangerous API internallyMitigation: Apply rule to wrapper implementations.
Dynamic Invocation
Why: Dynamic calls not analyzed.
// ❌ NOT DETECTED - Dynamic
obj[method](userInput);Mitigation: Avoid dynamic method invocation.
Further Reading
- OWASP Deserialization - Testing guide
- CWE-502 - Official CWE entry
- js-yaml Security - YAML security
Related Rules
detect-eval-with-expression- eval() injectiondetect-object-injection- Prototype pollution
no-unlimited-resource-allocation
Detects unlimited resource allocation that could cause DoS. This rule is part of [`eslint-plugin-secure-coding`](https://www.npmjs.com/package/eslint-plugin-sec
no-unsafe-dynamic-require
Disallows dynamic `require()` calls with non-literal arguments that could lead to security vulnerabilities. This rule is part of [`eslint-plugin-secure-coding`]