Summary
protobufjs-cli's pbjs static code generation could emit unsafe JavaScript identifiers directly from schema-controlled names without proper sanitization.
When an attacker can influence the protobuf schema or JSON descriptor, they may craft names that inject arbitrary JavaScript into the generated output, leading to potential code execution when that code is built or run.
Affected context
The vulnerability appears in workflows where protobufjs-cli's pbjs tool is used to generate static JavaScript code from .proto schemas or JSON descriptors.
It becomes relevant when schema or descriptor files are sourced from, or modifiable by, untrusted or semi-trusted parties (for example, external contributors, automated conversion pipelines, or third-party specifications).
In these scenarios, attacker-controlled names for namespaces, enums, services, or related constructs can be embedded into the generated JavaScript and interpreted as code rather than inert identifiers.
Exploit preconditions
- Attack surface: The code generation pipeline using
pbjsto produce static JavaScript files. - Required access: Ability to create, modify, or supply protobuf schemas (
.proto) or JSON descriptors consumed bypbjs. - Trigger point: Running
pbjsto generate static JavaScript and then consuming or executing the generated code (e.g., bundling, serving, or running it in Node.js or the browser). - Why exploitation is possible: The generator embeds schema-derived names directly into JavaScript source without sufficient escaping or validation, allowing crafted names to break out of identifier contexts and introduce executable JavaScript.
Why this happens
The issue stems from treating schema-controlled names as safe identifiers in generated JavaScript, without enforcing identifier rules or applying proper escaping and validation.
This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where untrusted input influences code generation logic.
In this case, the static generator constructs JavaScript source by interpolating names (e.g., for namespaces, enums, or services) directly into the output; if those names contain characters or patterns that terminate identifiers or string literals, they can inject arbitrary code into the resulting file.
Potential impact
- Injection of arbitrary JavaScript into generated client or server code.
- Execution of attacker-controlled code when the generated module is loaded or executed.
- Compromise of build pipelines that automatically generate and run code from provided schemas.
- Potential lateral movement within environments that widely distribute or deploy generated artifacts.
General mitigation guidance
Effective mitigation focuses on ensuring that schema-derived names cannot alter the structure of generated JavaScript:
- Treat schema and descriptor inputs as untrusted unless they are tightly controlled and verified.
- Apply strict validation on all identifier-like fields before code generation; enforce character whitelists and language-specific identifier rules.
- Use escaping routines that ensure generated identifiers and strings cannot break out of their expected syntactic context.
- Separate code structure from data by avoiding raw string concatenation for code emission; use templating or AST-based code generators where possible.
- Run code generation in a constrained environment, following least-privilege principles for build and CI systems so that any compromise has limited blast radius.
How teams detect issues like this
Detection commonly involves review of both generator behavior and schema inputs:
- Static analysis or linters targeting the code generator to flag direct interpolation of untrusted names into code constructs (class names, property names, function declarations).
- Code review guidelines that explicitly look for raw string-based code emission and missing identifier sanitization in tooling.
- Schema validation policies (e.g., in CI/CD) that reject names containing characters outside an approved identifier set.
- Automated scanning of generated artifacts for suspicious patterns, such as unexpected control characters, injected statements, or malformed declarations.
- Use of security-focused build checks or SAST tools that can analyze generated code for injection markers and unusual syntactic constructs.

