fix: shell-quote the working directory in the distributed launch script - #4189
Merged
zcbenz merged 1 commit intoAug 12, 2026
Merged
Conversation
zcbenz
approved these changes
Aug 12, 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.
What
RemoteProcess.make_launch_scriptinpython/mlx/_distributed_utils/launch.pybuilds a bash script that is run on each node (locally viaPopen(..., shell=True, executable="/bin/bash")and remotely overssh). The working directory (--cwd, falling back toos.getcwd()) is interpolated into that script using Python'srepr():repr()is not a shell-quoting function. When the string contains a single quote,repr()switches to a double-quoted form, and bash performs command substitution / variable expansion inside double quotes. Every other value interpolated into this same script (env values, file contents, the command itself, the host) is correctly escaped withshlex.quote; only the working directory usesrepr.Why it matters
A working directory such as:
renders as
cd "/tmp/a'$(touch /tmp/pwned)'b", and bash executes the$(...)substitution. This turns a value that is only supposed to name a directory into arbitrary shell execution, on every node the job is launched on. It also breaks legitimately-named directories that contain a'or$, so it is a correctness bug as well as an injection vector when the launch command (and its--cwd) is assembled programmatically or from a job/orchestration layer.Reproduction:
Fix
Quote the directory with
shlex.quote(already imported and already used for every other interpolation in this function) instead ofrepr. Also corrects the adjacent>2typo to>&2so the error message goes to stderr. No API change.Test
Before the change the same test with
repr(d)creates/tmp/pwned.