Summary
Contentlayer can generate a JavaScript module using content file names in an import specifier without safely handling embedded single quotes. If an attacker can introduce a suitably named content file, the generated code may contain injected JavaScript that runs when the module is imported.
Affected context
The issue is associated with Contentlayer @contentlayer/core through 0.3.4, in the generation of the data export module by makeDataExportFile(). It becomes risky when content file names are attacker-controlled or can be modified by an untrusted contributor, and the generated module is subsequently imported by contentlayer dev or a Next.js development server.
Exploit preconditions
- Attack surface: Content files processed by Contentlayer's data export generation.
- Required access: Ability to add or modify a content file name, directly or through an input or contribution workflow.
- Trigger point: Generation and import of the resulting module during Contentlayer or Next.js development.
- Why exploitation is possible: A file name containing a single quote can break out of the generated import specifier if it is inserted into JavaScript source without safe encoding.
Why this happens
The underlying bug is unsafe construction of JavaScript source using a content file name as part of an import specifier. If untrusted text is interpolated without context-appropriate escaping or encoding, it can alter the generated program rather than remain data. This maps to CWE-94: Improper Control of Generation of Code (Code Injection).
Potential impact
- Injected JavaScript may execute in the development process when the generated module is imported.
- The code may access files, environment variables, or services available to that process.
- Development credentials or other secrets available to the process could be exposed.
- A compromised development environment could be used to affect related workflows or systems.
General mitigation guidance
Defensive patterns focus on keeping content-derived values as data rather than allowing them to change generated source.
- Use safe, context-aware encoding or structured code-generation APIs for values included in JavaScript.
- Prefer stable generated identifiers that are not derived directly from untrusted file names.
- Validate and restrict content file names according to a narrowly defined naming policy.
- Reduce trust in content supplied through external or collaborative workflows, and review changes that introduce or rename files.
- Run development processes with least privilege and limit access to secrets and sensitive resources.
How teams detect issues like this
- Use static analysis to identify string interpolation or concatenation that places file names or other untrusted values into generated JavaScript.
- Review code-generation paths for missing context-aware escaping and check whether validation occurs before source generation.
- Add policy checks for unexpected characters or naming patterns in content files, particularly in externally supplied changes.
- Include tests that verify unusual file names cannot alter the structure of generated modules.
- Automated code review tools may help flag unsafe source-generation patterns.

