Summary
protobufjs compiles Protocol Buffer definitions into JavaScript conversion functions, and prior to versions 7.5.6 and 8.0.2 its toObject generator could emit attacker-controlled expressions.
A crafted descriptor with a non-string default value for a bytes field could inject arbitrary JavaScript into the generated conversion code, potentially allowing code execution during serialization or object conversion where untrusted schemas are used.
Affected context
This vulnerability affects applications that use protobufjs to compile Protocol Buffer descriptors into JavaScript code, particularly when:
- Descriptors (schema definitions) are loaded dynamically or supplied from external/untrusted sources
- The
toObjectconversion functions are generated and executed at runtime - Schema authors can control default values for
bytesfields without strict validation
In typical, closed, build-time-only setups where all .proto files are trusted and controlled, the risk is significantly lower, but environments that accept or transform descriptors at runtime are exposed.
Exploit preconditions
- Attack surface: The Protobuf schema/descriptor ingestion interface (e.g., dynamic loading of
.protofiles, JSON descriptors, or plugin-provided schemas). - Required access: Ability to supply or modify Protocol Buffer descriptors consumed by protobufjs, including the default value of a
bytesfield. - Trigger point: Execution of the generated
toObjectconversion function for affected message types after code generation. - Why exploitation is possible: The code generator incorporates the default value of a
bytesfield into JavaScript source without enforcing that it is a safe string literal, enabling injection of arbitrary JavaScript expressions into the generated conversion function.
Why this happens
The underlying flaw is an instance of CWE-94: Improper Control of Generation of Code ('Code Injection') in the code generation phase of protobufjs.
When constructing JavaScript for toObject conversion, the generator treated schema-controlled default values for bytes fields as if they were safe to embed directly into source code.
Because these values were not restricted to plain strings or properly escaped, a crafted descriptor could supply a non-string value that, once interpolated into the generated function body, becomes executable JavaScript.
This reflects the broader bug class where structured configuration or metadata (here, Protobuf descriptors) are treated as trusted code fragments during generation, blurring the line between data and executable logic.
Potential impact
- Execution of attacker-controlled JavaScript within the process that runs the generated protobufjs conversion functions
- Compromise of the application using protobufjs, including access to in-process secrets and sensitive data
- Lateral movement within a service environment if compromised services hold credentials or tokens for other systems
- Data tampering or manipulation of serialized/deserialized messages
- Interference with logging, monitoring, or security checks executed in the same runtime
General mitigation guidance
Effective mitigation focuses on treating schemas and descriptors as untrusted data and constraining how they flow into code generation:
- Restrict who can define or modify Protobuf schemas; treat
.protofiles and descriptors as part of trusted source code, not user input. - Enforce strict validation of descriptors before passing them to protobufjs, especially for
bytesfield defaults, ensuring they conform to expected types (e.g., strings or binary-safe representations only). - Avoid runtime code generation from externally supplied schemas; prefer a build-time compilation step where schemas are reviewed and checked.
- Apply output encoding or safe templating patterns when generating code from metadata to ensure that schema fields are never interpolated into raw JavaScript expressions.
- Run services that process potentially untrusted schemas in isolated or least-privilege environments to limit the blast radius of any code execution in the generation pipeline.
How teams detect issues like this
Organizations can look for this bug pattern through a combination of static and process-focused detection:
- Static analysis patterns: Search codebases for dynamic protobufjs schema loading and runtime code generation, focusing on paths where descriptors may originate from configuration files, network input, or plugins.
- Code review signals: Flag any code that:
- Accepts arbitrary
.proto/descriptor input at runtime - Passes unvalidated descriptor objects into protobufjs compilation or generator APIs
- Emits or evaluates generated JavaScript strings derived from configuration or schema data
- Policy enforcement: Establish policies that disallow runtime compilation of schemas from unverified sources and require schema changes to go through code review.
- Automated tooling: Use SAST or custom linters to detect direct interpolation of descriptor fields into generated JavaScript or direct use of code-evaluation constructs around schema-driven strings.

