T09 · Insecure Skill Coding Practices
- Location
scripts/test_estimate.py:14- Finding
Shell Command Injection in Test Command Helper
- Content
View full analysis
Vulnerability Details
File Location:
scripts/test_estimate.py, lines 14–17
Vulnerability Type: OS command injection through unsafe shell invocation
Risk Level: MediumVulnerable Code
python def run_cmd(args): """运行估算命令""" cmd = f"python3 estimate.py {' '.join(str(a) for a in args)}" result = subprocess.run(cmd, shell=True, capture_output=True, text=True) return resultTechnical Analysis
The
run_cmdfunction converts argument values to strings, joins them into one command, and executes that command through a shell usingshell=True. The arguments are neither escaped nor validated before interpolation.Consequently, shell metacharacters in any argument—such as
;,&&,|, command substitutions, or redirections—are interpreted by the shell rather than passed literally toestimate.py. This creates an arbitrary command-execution primitive.The bundled test cases are currently hard-coded, so the default test run does not directly expose attacker-controlled input. Exploitation becomes possible if this helper is imported, reused, or modified to accept arguments from users, files, environment variables, CI parameters, or other untrusted sources.
Attack Path
-
An attacker gains influence over an argument passed to
run_cmd. -
The attacker supplies a value containing a shell control operator, for example:
python run_cmd(["100", "精装", "北京; id > /tmp/injection-proof"]) -
The function constructs a command equivalent to:
bash python3 estimate.py 100 精装 北京; id > /tmp/injection-proof -
Because
shell=Trueis enabled, the shell treats the semicolon as a command separator. -
The injected command executes with the same operating-system privileges as the Python test process.
Impact Assessment
Successful exploitation permits arbitrary local command execution under the account running the tests. Depending on that account’s permissions, an attacker could:
- Read or al ...[truncated 445 chars]
-
- Remediation
View remediation
Remediation Suggestions
Avoid constructing a shell command from concatenated arguments. Pass the executable and each argument as separate list elements, and keep shell processing disabled:
python def run_cmd(args): """Run the estimator without invoking a shell.""" cmd = ["python3", "estimate.py", *map(str, args)] return subprocess.run( cmd, shell=False, capture_output=True, text=True, check=False, )Additional hardening measures:
- Validate argument count and expected types before invocation.
- Restrict the grade argument to the supported allowlist.
- Validate the area as a finite numeric value within the documented range.
- Validate or normalize city input according to application requirements.
- Resolve
estimate.pyrelative to__file__rather than relying on process working-directory state. - Add a regression test containing shell metacharacters and verify that they are passed literally and do not create files or execute secondary commands.
- If future CI parameters or external test fixtures are supported, treat all such values as untrusted input.
