fix: avoid shell injection in training helper#28776
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR mitigates command-injection risk in the ORT training helper script by removing shell-based command construction/execution and replacing it with safe argument-list subprocess invocation, plus adding regression tests.
Changes:
- Replace
os.system()shell execution intrain.pywithsubprocess.run([...], check=True)behind amain(argv=None)entry point. - Add pytest coverage to ensure importing the script does not execute a command and that
subprocess.runis called with an argument list.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pr_description.md | Documents the security motivation, implementation summary, and test command for the change. |
| orttraining/tools/scripts/train.py | Switches from shell-string execution to subprocess.run with an argv list via a main() entry point. |
| orttraining/orttraining/test/python/test_train_script.py | Adds regression tests for import-time behavior and argv-list subprocess invocation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tianleiwu
marked this pull request as ready for review
June 3, 2026 23:03
skottmckay
approved these changes
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fix the command-injection risk in the training helper by removing shell-based execution and validating the safe subprocess path with regression tests.
Summary of Changes
Security fix: replace shell execution with argument-list execution
orttraining/tools/scripts/train.pyos.system(...)withsubprocess.run([...], check=True)and add a smallmain(argv=None)entry point for safe argument forwarding.orttraining/orttraining/test/python/test_train_script.pyMotivation
The previous implementation built a single shell string from
sys.argv[1:]and executed it withos.system(). That allowed shell metacharacters in user-supplied arguments to be interpreted by the shell. The updated path passes the command and arguments directly tosubprocess.run, which avoids shell interpretation and preserves the original argument boundaries.Testing
pytest orttraining/orttraining/test/python/test_train_script.py -qChecklist