Summary
A code injection vulnerability in Faker's faker.helpers.fake method allows attacker-controlled templates to reach JavaScript's Function constructor through property resolution behavior. When untrusted fake templates are processed in browser or Node.js contexts, an attacker may execute arbitrary JavaScript code with the privileges of the hosting application.
Affected context
The issue can occur in applications that use Faker's faker.helpers.fake helper to process templates influenced by untrusted users, external data, or modifiable configuration. Risk increases when template evaluation can resolve a function-returning property and the hosting process exposes sensitive browser or Node.js capabilities.
Exploit preconditions
- Attack surface: Faker template processing in browser or Node.js applications
- Required access: Ability to supply or influence a fake template
- Trigger point: Invocation of
faker.helpers.fakeand its property-resolution logic - Why exploitation is possible: Attacker-controlled template expressions can reach the JavaScript
Functionconstructor when a resolved function returns another function
Why this happens
The underlying bug is an unsafe evaluation path in the fake-template helper. Property resolution permits attacker-controlled template content to traverse into executable JavaScript behavior, including access to the Function constructor. This maps to CWE-95, Improper Neutralization of Directives in Dynamically Evaluated Code, because data intended to describe fake values can instead be interpreted as executable code.
Potential impact
- Arbitrary JavaScript code may execute in the application context
- Sensitive process environment data could be exposed
- Browser-side credentials or accessible application data could be accessed
- Application integrity could be compromised
- The hosting process or user session could be used for further attacks
General mitigation guidance
Mitigation generally focuses on preventing untrusted template content from entering dynamic evaluation paths. Secure-by-default patterns include using fixed, non-evaluated generators; treating templates as data rather than executable expressions; restricting property resolution to an explicit allowlist of inert values; and separating trusted test-data generation from user-controlled input. Where dynamic behavior is unavoidable, applications can reduce input trust, isolate generation in a low-privilege process, limit browser and Node.js capabilities, and apply strict output and execution boundaries.
How teams detect issues like this
Detection can include static analysis for uses of faker.helpers.fake, dynamic template evaluation, Function construction, and unrestricted property-resolution helpers. Code review should examine whether templates originate from request data, files, environment variables, or other untrusted sources, and whether resolved values can become callable functions. Policy enforcement can prohibit dynamic code generation in production paths and require allowlisted template syntax. Automated code-review and dependency-analysis tools may help identify these patterns.

