T09 · Insecure Skill Coding Practices
- Location
confmt.py:43- Finding
Unsafe .env Serialization Enables Downstream Shell Injection
- Content
View full analysis
Vulnerability Details
File Location:
confmt.py:43-47and duplicate implementation inscripts/confmt.py:43-47
Vulnerability Type: Improper escaping of attacker-controlled.envkeys and values
Risk Level: MediumVulnerable Code
python def to_env(d): flat = flatten_dict(d) lines = [] for k, v in sorted(flat.items()): key = k.upper().replace(".", "_").replace("[", "_").replace("]", "") lines.append(f'{key}="{v}"') return "\n".join(lines)Technical Analysis
The
to_envfunction directly interpolates configuration keys and values into.envoutput. It neither validates generated variable names nor escapes characters with special meaning to shell-compatible parsers, including double quotes, backslashes, dollar signs, backticks, carriage returns, and newlines.For example, an attacker-controlled JSON input containing:
json { "SAFE": "$(malicious-command)" }is converted into:
sh SAFE="$(malicious-command)"The converter does not execute this command itself. However, if a user or automation subsequently loads the generated file using a shell command such as
source output.env, shell command substitution evaluates the injected expression.A value containing quotes and newline characters could also terminate the intended assignment and inject additional shell statements. Unsafe keys may similarly produce malformed or semantically unexpected assignments.
Attack Path
-
An attacker supplies or modifies a JSON, TOML, or other supported configuration file.
-
A user converts the untrusted configuration to
.envformat:sh python3 scripts/confmt.py format attacker.json -o env > output.env -
The converter writes attacker-controlled shell metacharacters without safe escaping.
-
A user, deployment script, or CI/CD process treats the output as shell-compatible environment syntax:
sh source output.env -
The shell evaluates injected command s ...[truncated 718 chars]
-
- Remediation
View remediation
Remediation Suggestions
- Validate every generated environment variable name against a strict pattern such as
^[A-Za-z_][A-Za-z0-9_]*$; reject invalid names rather than attempting ambiguous normalization. - Use a clearly specified dotenv serialization routine that safely handles quotes, backslashes, dollar signs, carriage returns, and newlines.
- Avoid claiming that generated output is safe to load as shell source code. Explicitly document that users must parse it with a non-executing dotenv parser.
- Consider rejecting control characters and multiline values unless the chosen dotenv grammar supports them unambiguously.
- Apply the same correction to both
confmt.pyandscripts/confmt.py, or consolidate them into one implementation to prevent security fixes from diverging. - Add regression tests for command substitutions, backticks, embedded double and single quotes, backslashes, CR/LF characters, multiline values, and malformed keys.
- Validate every generated environment variable name against a strict pattern such as
