quote arguments in run_cli_command before shell execution#47425
Open
alhudz wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens local-endpoint CLI execution by correctly quoting command arguments before running them via shell=True, preventing shell interpretation of metacharacters/spaces.
Changes:
- Update
run_cli_commandto build a safely-quoted command string usingshlex.join(POSIX) /subprocess.list2cmdline(Windows). - Add unit tests validating quoting behavior and ensuring a plain URI remains unchanged.
- Document the fix in the package changelog.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| sdk/ml/azure-ai-ml/azure/ai/ml/_local_endpoints/utilities/commandline_utility.py | Quote args when collapsing to a single shell command string. |
| sdk/ml/azure-ai-ml/tests/local_endpoint/unittests/test_commandline_utility.py | New unit tests asserting safe quoting and expected command string formatting. |
| sdk/ml/azure-ai-ml/CHANGELOG.md | Changelog entry describing the quoting/security fix. |
Comment on lines
+31
to
+38
| # "shell=True" (used below) is needed so the platform "az"/"code" wrappers resolve, and it doesn't work with | ||
| # the vector argv form on macOS, so the arguments are collapsed into a single command string. Quote each | ||
| # argument while joining so a value containing spaces or shell metacharacters is passed through literally | ||
| # instead of being re-interpreted by the shell. | ||
| if os.name == "nt": | ||
| command_to_execute = subprocess.list2cmdline(cmd_arguments) | ||
| else: | ||
| command_to_execute = shlex.join(cmd_arguments) |
|
|
||
| import json | ||
| import os | ||
| import shlex |
Contributor
|
Thank you for your contribution @alhudz! We will review the pull request and get back to you soon. |
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
run_cli_commandjoins its argument vector with spaces and runs the result undersubprocesswithshell=True, so any argument carrying spaces or shell metacharacters is re-interpreted by the shell rather than passed through literally. Callers already hand it a proper argv list; the unsafe collapse happens inside the helper.Repro:
run_cli_command(["echo", "x$(touch pwned)"])runs the$(...)substitution. The VS Code debug path reaches this withAML_APP_ROOT(derived from a deploymentscoring_script/codepath) embedded in the--folder-uriargument, and ordinary paths containing a space break the same way.Cause:
" ".join(cmd_arguments)turns the argv list into a shell string with no quoting.Fix: quote each argument while joining inside the helper,
shlex.joinon POSIX andsubprocess.list2cmdlineon Windows. A plainvscode-remote://URI argument is left unchanged.All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines