Summary
dataclasses-avroschema can copy attacker-controlled Avro record documentation into generated Python class docstrings without escaping special characters. A crafted schema may therefore inject Python statements that execute when the generated module is imported, particularly when untrusted schemas are processed or distributed.
dataclasses-avroschema can copy attacker-controlled Avro record documentation into generated Python class docstrings without escaping special characters. A crafted schema may therefore inject Python statements that execute when the generated module is imported, particularly when untrusted schemas are processed or distributed.
Affected context
The vulnerability occurs in deployments that use dataclasses-avroschema to generate Python code from Avro schemas and then import or execute that generated code. It is relevant when schema documentation fields can be influenced by an untrusted user, external repository, build artifact, or other source outside the application's security boundary.
The vulnerability occurs in deployments that use dataclasses-avroschema to generate Python code from Avro schemas and then import or execute that generated code. It is relevant when schema documentation fields can be influenced by an untrusted user, external repository, build artifact, or other source outside the application's security boundary.
Exploit preconditions
- Attack surface: Avro schema processing and Python source-code generation
- Required access: Ability to supply or modify an Avro schema record's
docvalue - Trigger point: Import or execution of the generated Python module
- Why exploitation is possible: Documentation text is inserted into a generated triple-quoted Python string without safely escaping quotes, backslashes, and unsafe control characters
- Attack surface: Avro schema processing and Python source-code generation
- Required access: Ability to supply or modify an Avro schema record's
docvalue - Trigger point: Import or execution of the generated Python module
- Why exploitation is possible: Documentation text is inserted into a generated triple-quoted Python string without safely escaping quotes, backslashes, and unsafe control characters
Why this happens
The vulnerable code treats schema documentation as inert text while rendering it directly into Python source code. Because delimiter characters and escape sequences are not encoded for the target language, crafted documentation can terminate the generated docstring and introduce additional statements into the class body.
This behavior maps to CWE-94: Improper Control of Generation of Code, where externally influenced data is incorporated into generated source without adequate contextual escaping or structural separation.
The vulnerable code treats schema documentation as inert text while rendering it directly into Python source code. Because delimiter characters and escape sequences are not encoded for the target language, crafted documentation can terminate the generated docstring and introduce additional statements into the class body.
This behavior maps to CWE-94: Improper Control of Generation of Code, where externally influenced data is incorporated into generated source without adequate contextual escaping or structural separation.
Potential impact
- Execution of attacker-selected Python code during module import
- Compromise of the application or build environment
- Exposure or modification of accessible secrets and data
- Supply-chain contamination through malicious generated artifacts
- Persistence or lateral movement within the affected runtime environment
- Execution of attacker-selected Python code during module import
- Compromise of the application or build environment
- Exposure or modification of accessible secrets and data
- Supply-chain contamination through malicious generated artifacts
- Persistence or lateral movement within the affected runtime environment
General mitigation guidance
Mitigation generally focuses on separating data from generated source code and applying context-appropriate encoding before rendering.
Common defensive patterns include:
- Escaping quotes, backslashes, and unsafe control characters for Python string contexts
- Using structured code-generation APIs or templates that preserve data boundaries
- Treating external Avro schemas as untrusted input
- Reviewing generated artifacts before import or distribution
- Restricting the privileges and filesystem access of code-generation and import processes
- Avoiding automatic execution of generated code from untrusted sources
Mitigation generally focuses on separating data from generated source code and applying context-appropriate encoding before rendering.
Common defensive patterns include:
- Escaping quotes, backslashes, and unsafe control characters for Python string contexts
- Using structured code-generation APIs or templates that preserve data boundaries
- Treating external Avro schemas as untrusted input
- Reviewing generated artifacts before import or distribution
- Restricting the privileges and filesystem access of code-generation and import processes
- Avoiding automatic execution of generated code from untrusted sources
How teams detect issues like this
Organizations can identify this class of issue through:
- Static analysis for direct interpolation of schema fields into Python source
- Code review checks for generated string literals built from external metadata
- Tests using quotes, backslashes, control characters, and delimiter sequences in schema documentation
- Build policies that inspect or prohibit unreviewed generated source artifacts
- Runtime monitoring for unexpected child processes, filesystem changes, or network activity during module import
Automated security and code-quality review tools may help identify unsafe code-generation patterns, but generated output should also be inspected because source-level checks may miss template-specific escaping failures.
Organizations can identify this class of issue through:
- Static analysis for direct interpolation of schema fields into Python source
- Code review checks for generated string literals built from external metadata
- Tests using quotes, backslashes, control characters, and delimiter sequences in schema documentation
- Build policies that inspect or prohibit unreviewed generated source artifacts
- Runtime monitoring for unexpected child processes, filesystem changes, or network activity during module import
Automated security and code-quality review tools may help identify unsafe code-generation patterns, but generated output should also be inspected because source-level checks may miss template-specific escaping failures.

