Summary
protobufjs-cli versions prior to 1.3.2 and 2.5.0 can generate JavaScript files that embed attacker-controlled code when given crafted JSON descriptor input, due to incomplete sanitization of unsafe names in static and static-module code generation.
If an attacker can supply or influence the JSON descriptors used by pbjs, the resulting generated code may execute malicious payloads when the output file is later imported or run and the affected APIs are invoked.
Affected context
This vulnerability occurs in development or build environments that:
- Use
protobufjs-clito generate static or static-module JavaScript code - Accept or process pre-parsed JSON descriptors (instead of parsing
.protofiles directly) - Treat generated JavaScript as trusted application code and execute or import it without additional review
The risk materializes when the JSON descriptor content can be influenced by untrusted sources or mixed-trust pipelines, and the generated files are later executed in Node.js, browsers, or bundlers as part of an application.
Exploit preconditions
- Attack surface: Code-generation pipeline using
protobufjs-clistatic / static-module targets. - Required access: Ability to provide, modify, or influence the JSON descriptor input consumed by
pbjsduring generation. - Trigger point: Running
pbjsstatic or static-module code generation on the crafted JSON descriptor, followed by executing or importing the generated JavaScript and invoking affected APIs. - Why exploitation is possible: The name-handling and escaping logic for identifiers and references in the code generator was incomplete, allowing attacker-controlled values from the JSON descriptor to be emitted as raw JavaScript references or code fragments, which are then treated as executable code when the generated file runs.
Why this happens
The underlying issue is an incomplete defense against code injection in code generators, mapped to CWE-94: Improper Control of Generation of Code ('Code Injection').
The protobufjs-cli static code generator takes schema information from JSON descriptors and interpolates it into JavaScript templates.
Because some identifier or reference fields derived from the descriptor were not fully validated, normalized, or safely escaped, crafted values could break out of the intended syntactic context and inject arbitrary expressions into the output.
This pattern reflects a broader bug class where template-based generators treat partially untrusted metadata as inert text, but use it in contexts (identifiers, property paths, or expression positions) that the JavaScript engine interprets as executable program code.
Potential impact
- Generated JavaScript may execute attacker-controlled code when imported or run.
- Compromise of build or deployment environments that automatically run or bundle generated code.
- Execution of arbitrary commands or actions in Node.js environments using the generated code.
- Data exfiltration, privilege escalation, or lateral movement via malicious logic hidden in generated APIs.
- Supply-chain style attacks where compromised descriptors poison downstream consumers of the generated artifacts.
General mitigation guidance
Mitigation centers on treating code generation input as potentially hostile and hardening generator templates:
- Normalize and strictly validate schema-derived names and references before injecting them into code templates, enforcing conservative character sets and structural constraints.
- Use context-aware escaping routines for all variable substitutions that influence identifiers, property paths, or expressions in generated JavaScript.
- Separate trusted and untrusted stages of schema processing; only allow code generation from descriptors that have passed validation and originate from trusted sources.
- Reduce implicit trust in generated artifacts by incorporating review or signing steps, especially when descriptors come from third parties or automated pipelines.
- Apply least-privilege and sandboxing where generated code is executed (e.g., constrained runtimes, limited I/O) to minimize blast radius if a similar issue reappears.
How teams detect issues like this
Effective detection approaches can combine static analysis, build-time checks, and review practices:
- Use static analysis or grep-based rules to scan
protobufjs-clitemplates or output for unsafe interpolation patterns, such as direct insertion of descriptor-derived values into executable contexts without sanitization. - Add CI jobs that inspect generated JavaScript for unexpected constructs (e.g.,
eval,Function, process or fs access) in code that should only consist of serialization logic. - During security code review, flag any code-generation step that accepts JSON descriptors from mixed-trust sources and verify that identifiers and references are strictly validated.
- Employ automated SAST tools that recognize JavaScript code injection patterns and can inspect both generator source code and committed generated artifacts.
- Establish policy controls that prevent unchecked third-party or user-controlled schema descriptors from feeding into production code-generation pipelines.

