Summary
Markdown Preview Enhanced before 0.8.28 evaluates the contents of Bitfield fenced code blocks as JavaScript using vm.runInNewContext(), enabling execution of attacker-controlled code on the server.
When a crafted markdown document containing a malicious bitfield block is rendered or exported, the extension may run arbitrary code under the privileges of the hosting process.
Affected context
This vulnerability appears when Markdown Preview Enhanced processes markdown files that use Bitfield fenced code blocks and are rendered in an environment where server-side JavaScript evaluation occurs.
It is most relevant in contexts such as desktop applications, editor backends, or server-side rendering/export pipelines where markdown content may originate from or pass through less-trusted sources (shared documents, collaborative repositories, or imported files).
The issue manifests because the extension treats what should be declarative configuration data as executable code during preview or export.
Exploit preconditions
- Attack surface: Markdown document ingestion and rendering (preview, export, or automated processing).
- Required access: Ability to supply or modify a markdown document that will be opened or processed by Markdown Preview Enhanced (e.g., commit to a repo, send a file, or influence shared content).
- Trigger point: Viewing, previewing, or exporting a markdown file that contains a specially crafted Bitfield fenced code block.
- Why exploitation is possible: The Bitfield block contents are passed to interpretJS(), which uses vm.runInNewContext() to execute the text as JavaScript instead of treating it as passive data, allowing arbitrary script execution.
Why this happens
The core issue stems from treating user-controlled markdown content as executable JavaScript in a server-side context.
Bitfield fenced code blocks, which conceptually represent data (register or bitfield definitions), were handed to interpretJS(), which evaluates the content through vm.runInNewContext().
This creates a direct code injection path, matching CWE-94: Improper Control of Generation of Code ('Code Injection'), where an attacker crafts input that is executed as code rather than being parsed as structured data.
The fix—switching to JSON5.parse() for parsing bitfield definitions—reflects the correct abstraction: these blocks are configuration-like data, not scripts, and should never invoke a general-purpose code execution engine.
Potential impact
- Execution of arbitrary JavaScript with the privileges of the markdown rendering/export process
- Reading, modifying, or deleting files accessible to the host application
- Exfiltration of sensitive data such as API keys, project files, or credentials stored on disk or in environment variables
- Installation of additional malicious tooling or persistence mechanisms on the host system, depending on available APIs
- Lateral movement to other systems or services reachable from the compromised environment
General mitigation guidance
Effective mitigation focuses on removing the code execution surface from data-oriented features and constraining what untrusted content can trigger:
- Treat all user-supplied markdown and fenced code blocks as untrusted, especially in shared or automated pipelines.
- Replace any generic "evaluate this text as code" mechanism with strict data parsers (e.g., JSON/JSON5 or schema-validated formats) for configuration-like blocks.
- Introduce explicit separation between "data" blocks and "script" blocks, and default to non-executable handling unless a narrowly scoped, sandboxed behavior is required.
- When dynamic evaluation is truly necessary, run it in a hardened sandbox with minimized capabilities, no filesystem or network by default, and explicit allowlists for exposed APIs.
- Apply least-privilege principles to the host process: limit file system access, network permissions, and OS-level privileges to reduce blast radius if a rendering path is abused.
How teams detect issues like this
Organizations can look for this pattern and similar issues through multiple layers of review and automation:
- Static analysis: Search for uses of vm.runInNewContext(), eval(), Function(), or similar dynamic evaluation constructs where the source string is influenced by markdown or other user-controlled content.
- Code review: Flag any feature where "presentation" inputs (markdown, templates, documentation) are passed into generic interpreters instead of being parsed by a restricted data model; review custom markdown extensions and fenced-block handlers carefully.
- Policy enforcement: Define secure coding guidelines that prohibit or tightly regulate dynamic code evaluation mechanisms, especially in content processing pipelines.
- Automated tooling: Use linters or security scanners that include rules for dangerous JavaScript runtime APIs and integrate them into CI/CD to catch regressions before release.

