Avoid shell in async command runner#1225
Open
NguyenCong2k wants to merge 2 commits into
Open
Conversation
For more information, see https://pre-commit.ci
There was a problem hiding this comment.
Pull request overview
This PR hardens openllm.common.async_run_command() by avoiding shell invocation when running async subprocesses, reducing the risk of shell metacharacters being interpreted and improving cleanup behavior.
Changes:
- Switch from
asyncio.create_subprocess_shell()toasyncio.create_subprocess_exec()and pass argv entries directly. - Avoid sending
SIGINTto the subprocess if it has already exited (viareturncodecheck). - Add a regression test to ensure shell metacharacters in arguments don’t trigger command chaining.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/openllm/common.py |
Uses create_subprocess_exec(*cmd) and adds a returncode guard before sending SIGINT during teardown. |
tests/test_common.py |
Adds regression coverage to ensure async_run_command() doesn’t interpret shell metacharacters as command separators. |
Comments suppressed due to low confidence (1)
src/openllm/common.py:440
- Even with the returncode check, there is still a race where the process can exit but returncode hasn't been updated yet; send_signal() can raise ProcessLookupError in that window. To avoid intermittent failures during context manager teardown, catch/ignore ProcessLookupError (or similar) around proc.send_signal(signal.SIGINT).
def md5(*strings: str) -> int:
m = hashlib.md5()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+3
to
+14
|
|
||
| from openllm.common import async_run_command | ||
|
|
||
|
|
||
| def test_async_run_command_does_not_invoke_shell(tmp_path): | ||
| marker = tmp_path / 'shell_injection_marker.txt' | ||
| if os.name == 'nt': | ||
| separator = '&' | ||
| payload = ['cmd', '/c', f'echo PWNED>{marker}'] | ||
| else: | ||
| separator = ';' | ||
| payload = ['sh', '-c', f'echo PWNED > {marker}'] |
| async def run() -> None: | ||
| cmd = ['python', '-c', 'pass', separator, *payload] | ||
| async with async_run_command(cmd, cwd=str(tmp_path), silent=True) as proc: | ||
| await proc.wait() |
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.
Summary
asyncio.create_subprocess_shell()withasyncio.create_subprocess_exec()inasync_run_command()SIGINTafter the subprocess has already exitedTest