Skip to content

Security: Command injection via shell=True in Kaldi command runner#6408

Open
tuanaiseo wants to merge 1 commit intoespnet:masterfrom
tuanaiseo:contribai/fix/security/command-injection-via-shell-true-in-kald
Open

Security: Command injection via shell=True in Kaldi command runner#6408
tuanaiseo wants to merge 1 commit intoespnet:masterfrom
tuanaiseo:contribai/fix/security/command-injection-via-shell-true-in-kald

Conversation

@tuanaiseo
Copy link
Copy Markdown

Problem

The helper executes arbitrary command strings with subprocess.Popen(..., shell=True). If any part of command is derived from user-controlled input (directly or indirectly through recipe/config values), an attacker can inject additional shell metacharacters and execute unintended commands.

Severity: high
File: egs2/TEMPLATE/asr1/steps/data/data_dir_manipulation_lib.py

Solution

Avoid shell=True and pass commands as an argument list (subprocess.run([...], check=True)). If shell usage is unavoidable for pipelines, strictly validate/allowlist inputs and safely quote each interpolated token with shlex.quote before constructing the command.

Changes

  • egs2/TEMPLATE/asr1/steps/data/data_dir_manipulation_lib.py (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

The helper executes arbitrary command strings with `subprocess.Popen(..., shell=True)`. If any part of `command` is derived from user-controlled input (directly or indirectly through recipe/config values), an attacker can inject additional shell metacharacters and execute unintended commands.

Affected files: data_dir_manipulation_lib.py

Signed-off-by: tuanaiseo <221258316+tuanaiseo@users.noreply.github.com>
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. Bugfix labels Apr 4, 2026
@mergify mergify Bot added the ESPnet2 label Apr 4, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates RunKaldiCommand to use shlex.split and sets shell=False in subprocess.Popen to improve security, while also correcting a comparison operator for the return code. However, a significant regression was identified: the function is designed to execute shell pipelines, which will fail with shell=False. It is recommended to either revert to shell=True with proper quoting or refactor the logic to handle pipelines manually.

Comment on lines +8 to +10
if isinstance(command, str):
command = shlex.split(command)
p = subprocess.Popen(command, shell = False,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This change introduces a functional regression. The docstring for RunKaldiCommand (lines 5-6) explicitly states that it is designed to run command sequences connected by pipes, which requires a shell environment. By applying shlex.split and setting shell=False, shell-specific operators like |, >, and && will be treated as literal arguments rather than shell instructions, causing pipeline commands to fail. To address the security risk while maintaining functionality, consider keeping shell=True but requiring callers to use shlex.quote() on any user-provided variables before constructing the command string, or refactor the function to handle pipelines manually by chaining multiple subprocess.Popen instances.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.32%. Comparing base (6a3f809) to head (7f6ad43).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6408   +/-   ##
=======================================
  Coverage   70.32%   70.32%           
=======================================
  Files         787      787           
  Lines       73675    73675           
=======================================
  Hits        51812    51812           
  Misses      21863    21863           
Flag Coverage Δ
test_integration_espnet2 46.83% <ø> (ø)
test_python_espnet2 61.40% <ø> (ø)
test_python_espnet3 17.55% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugfix ESPnet2 size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants