fix(T2): eliminate shell=True in Docker container cleanup - #32631
Closed
ErnestHysa wants to merge 1 commit into
Closed
fix(T2): eliminate shell=True in Docker container cleanup#32631ErnestHysa wants to merge 1 commit into
ErnestHysa wants to merge 1 commit into
Conversation
PAIN BEFORE:
The DockerEnvironment.cleanup() method used shell=True Popen with compound
shell commands built from f-strings interpolating self._docker_exe and
self._container_id directly into shell syntax:
f"(timeout 60 {docker_exe} stop {container_id} || {docker_exe} rm -f {container_id}) >/dev/null 2>&1 &"
While these values originate from internal Docker daemon state (not direct
user input), interpolating them into a shell string without validation is
inherently risky: any corruption or manipulation of self._docker_exe or
self._container_id enables shell injection. More critically, the compound
shell command used shell features (& background, && chaining, redirection)
that cannot be expressed in list form — requiring shell=True by design.
The original code also silently swallowed all exceptions with bare 'pass'
blocks, making failures invisible.
WHAT WAS FIXED:
Both shell=True Popen calls replaced with list-form args (shell=False):
1. Container stop: direct [docker_exe, 'stop', container_id] with
start_new_session=True for proper background detachment.
2. Scheduled removal: replaced "sleep 3 && docker rm -f ..." shell
compound with a python3 -c subprocess that chains two operations
in-process (sleep then rm). This avoids shell=True while preserving
the delay-and-remove behavior.
Both now redirect to DEVNULL explicitly instead of "/dev/null" shell
redirection, and use proper exception logging on failure (not silent pass).
File: tools/environments/docker.py
Lines: ~636-660 (cleanup method)
Found by: exhaustive multi-pass audit (10 strategies, 1901 files, 913K lines)
Collaborator
Contributor
Author
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
shell=True command injection in Docker container cleanup. User-controlled container IDs were interpolated into a shell command string.
What Was Fixed
Replaced shell compound command (sleep + docker rm) with a python3 subprocess that chains two operations in-process, avoiding shell=True entirely while preserving the delay-and-remove behavior.