Summary
dataclasses-avroschema can generate Python enum code using schema-controlled symbol values without safely escaping them. If an attacker can influence those symbols and generated code is later imported, arbitrary Python code may execute during module loading.
dataclasses-avroschema can generate Python enum code using schema-controlled symbol values without safely escaping them. If an attacker can influence those symbols and generated code is later imported, arbitrary Python code may execute during module loading.
Affected context
The vulnerability occurs when dataclasses-avroschema generator classes are invoked directly to render Python code from Avro schemas containing attacker-influenced enum symbols. The risk is present when generated modules are written to a location that the application or build process imports or executes.
The vulnerability occurs when dataclasses-avroschema generator classes are invoked directly to render Python code from Avro schemas containing attacker-influenced enum symbols. The risk is present when generated modules are written to a location that the application or build process imports or executes.
Exploit preconditions
- Attack surface: Avro-to-Python code generation, specifically enum rendering - Required access: Ability to provide or alter an Avro schema or enum symbol consumed by the generator - Trigger point: Import or execution of the generated Python module - Why exploitation is possible: Schema-controlled symbols are inserted into a Python string literal without adequate escaping, allowing generated source structure to be altered
- Attack surface: Avro-to-Python code generation, specifically enum rendering - Required access: Ability to provide or alter an Avro schema or enum symbol consumed by the generator - Trigger point: Import or execution of the generated Python module - Why exploitation is possible: Schema-controlled symbols are inserted into a Python string literal without adequate escaping, allowing generated source structure to be altered
Why this happens
The generator constructs Python enum assignments by concatenating schema-controlled symbol values between double quotes. A symbol containing source-breaking characters can terminate the intended string literal and introduce additional Python syntax into the generated enum class body. This behavior maps to CWE-94, Improper Control of Generation of Code, because untrusted input influences executable source code without reliable encoding or context-aware escaping.
The generator constructs Python enum assignments by concatenating schema-controlled symbol values between double quotes. A symbol containing source-breaking characters can terminate the intended string literal and introduce additional Python syntax into the generated enum class body. This behavior maps to CWE-94, Improper Control of Generation of Code, because untrusted input influences executable source code without reliable encoding or context-aware escaping.
Potential impact
- Arbitrary Python code may execute when a generated module is imported
- Build, development, or application environments could be compromised
- Credentials and other process-accessible data could be exposed
- Generated artifacts could be modified to support persistence or supply-chain abuse
- Arbitrary Python code may execute when a generated module is imported
- Build, development, or application environments could be compromised
- Credentials and other process-accessible data could be exposed
- Generated artifacts could be modified to support persistence or supply-chain abuse
General mitigation guidance
Mitigation generally focuses on treating schema values as data throughout code generation. Generators should apply context-aware escaping or use structured code-generation APIs that do not concatenate untrusted values into source text. Strict schema validation and allowlists for enum symbols can reduce input trust, while separating generated artifacts from sensitive execution environments and applying least-privilege permissions can limit impact. Generated code should be reviewed or validated before it is imported, and secure-by-default generator behavior should reject or safely encode unsupported symbols.
Mitigation generally focuses on treating schema values as data throughout code generation. Generators should apply context-aware escaping or use structured code-generation APIs that do not concatenate untrusted values into source text. Strict schema validation and allowlists for enum symbols can reduce input trust, while separating generated artifacts from sensitive execution environments and applying least-privilege permissions can limit impact. Generated code should be reviewed or validated before it is imported, and secure-by-default generator behavior should reject or safely encode unsupported symbols.
How teams detect issues like this
Detection can include static analysis rules for string concatenation or formatting of schema-controlled values into Python source, along with code review checks for generated literals that lack context-aware escaping. Repository policies can require schema validation, prohibit direct execution of unreviewed generated artifacts, and test enum symbols containing quotes or other syntax-sensitive characters. Automated security review tools may help identify these code-generation patterns.
Detection can include static analysis rules for string concatenation or formatting of schema-controlled values into Python source, along with code review checks for generated literals that lack context-aware escaping. Repository policies can require schema validation, prohibit direct execution of unreviewed generated artifacts, and test enum symbols containing quotes or other syntax-sensitive characters. Automated security review tools may help identify these code-generation patterns.

