protocolbuffers / protocolbuffers/protobuf
Security hardening: --python_out emits unvalidated filename-derived import statements (CWE-94)
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 72k
- Forks
- 16.3k
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 140
Description
Summary
The protoc Python code generator (--python_out) derives module names and
import aliases directly from .proto filenames and writes them verbatim into
generated *_pb2.py files as Python import statements, without validating
that the result is a legal Python identifier.
Affected File
src/google/protobuf/compiler/python/helpers.cc (ModuleName())
src/google/protobuf/compiler/python/generator.cc (PrintImports())
Current Behavior
ModuleName() performs only -→_ and /→. substitution on the filename
before appending _pb2. Characters with syntactic meaning in Python
(;, #, quotes, parentheses) are not rejected or escaped.
PrintImports() interpolates the unvalidated module name directly into a
Python import statement written to generated source.
Impact
A .proto filename containing ; or # characters causes the generated
_pb2.py to contain injected Python statements that execute on import.
Example: a file named os;payload#.proto causes generated code like:
import os;payload#_pb2 as os;payload#__pb2
Related Issues
This is the same bug class as:
- #26569 — Ruby/PHP/C# codegen option injection
- #26833 / #26940 — Java java_package/class name injection
This report covers the --python_out sink, which is distinct and not
covered by the above fixes.
Suggested Fix
After substitution in ModuleName(), validate the result matches
[A-Za-z0-9_.]+ and reject filenames that produce invalid identifiers.
Apply the same guard to the .pyi stub generation path in
pyi_generator.cc.
Note: This was reported privately to Google VRP and they suggested opening
a public GitHub issue for tracking.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with ModuleName() in src/google/protobuf/compiler/python/helpers.cc and follow how PrintImports() in generator.cc writes the derived name into generated Python. Review the .pyi stub generation path in pyi_generator.cc as well. Done means invalid filename-derived names are rejected or safely handled in both generated-source paths so they cannot inject Python statements.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- compilers, security
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 62/100