-
-
Notifications
You must be signed in to change notification settings - Fork 646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure non-ambiguous args/env vars injection into PEXes #18861
Ensure non-ambiguous args/env vars injection into PEXes #18861
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love to see a few more test cases to ensure proper quoting is done along the way, such as values with spaces and quotes in them as well.
Ah, good catch. There's two issues:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍🏽
…8861) This fixes pantsbuild#18779 by ensuring that injecting arguments and environment variables into a pex doesn't risk them being interpreted by the pex CLI itself if they start with `-`, and ensuring they're quoted appropriately to match the `shlex.split` within pex (https://github.com/pantsbuild/pex/blob/ff220b9e41484450e0c78136f98d6899f2e2325c/pex/bin/pex.py#L433). The issue noted in pantsbuild#18779 was specifically ambiguity with `args`, but the same problem will apply to `env` and so that is fixed here too (although it's rather less common to have env vars starting with `-`). NB. `shlex.quote`ing alone won't handle the `-` ambiguity, `shlex.quote("--foo") == "--foo"`. For a target like `pex_binary(..., args=["--foo"], env={"--bar": "baz"})`: - Previously, this would invoke `pex ... --inject-args --foo --inject-env --bar=baz`, and argparse within the pex CLI will intepret `--foo` and `--bar=...` as independent parameters, rather than arguments to `--inject-args`/`--inject-env`. - After this change (as suggested by @jsirois), the invocation will be `pex ... --inject-args=--foo --inject-env=--bar=baz` and there's no risk of misinterpretation. For a target like `pex_binary(..., args=["spaces 'n' quotes"])`: - Previously this would invoke with (equivalently) an arg `--inject-args=spaces 'n' quotes`, which would `shlex.split` to three arguments in the PEX, the same as passing `args=["spaces", "n", "quotes"])` - Now, this will invoke with `--inject-args='spaces '"'"'n'"'"' quotes'`, which, is correctly `shlex.split` to the single value: `spaces 'n' quotes`.
#18861) (#18877) This fixes #18779 by ensuring that injecting arguments and environment variables into a pex doesn't risk them being interpreted by the pex CLI itself if they start with `-`, and ensuring they're quoted appropriately to match the `shlex.split` within pex (https://github.com/pantsbuild/pex/blob/ff220b9e41484450e0c78136f98d6899f2e2325c/pex/bin/pex.py#L433). The issue noted in #18779 was specifically ambiguity with `args`, but the same problem will apply to `env` and so that is fixed here too (although it's rather less common to have env vars starting with `-`). NB. `shlex.quote`ing alone won't handle the `-` ambiguity, `shlex.quote("--foo") == "--foo"`. For a target like `pex_binary(..., args=["--foo"], env={"--bar": "baz"})`: - Previously, this would invoke `pex ... --inject-args --foo --inject-env --bar=baz`, and argparse within the pex CLI will intepret `--foo` and `--bar=...` as independent parameters, rather than arguments to `--inject-args`/`--inject-env`. - After this change (as suggested by @jsirois), the invocation will be `pex ... --inject-args=--foo --inject-env=--bar=baz` and there's no risk of misinterpretation. For a target like `pex_binary(..., args=["spaces 'n' quotes"])`: - Previously this would invoke with (equivalently) an arg `--inject-args=spaces 'n' quotes`, which would `shlex.split` to three arguments in the PEX, the same as passing `args=["spaces", "n", "quotes"])` - Now, this will invoke with `--inject-args='spaces '"'"'n'"'"' quotes'`, which, is correctly `shlex.split` to the single value: `spaces 'n' quotes`.
This fixes #18779 by ensuring that injecting arguments and environment variables into a pex doesn't risk them being interpreted by the pex CLI itself if they start with
-
, and ensuring they're quoted appropriately to match theshlex.split
within pex (https://github.com/pantsbuild/pex/blob/ff220b9e41484450e0c78136f98d6899f2e2325c/pex/bin/pex.py#L433).The issue noted in #18779 was specifically ambiguity with
args
, but the same problem will apply toenv
and so that is fixed here too (although it's rather less common to have env vars starting with-
). NB.shlex.quote
ing alone won't handle the-
ambiguity,shlex.quote("--foo") == "--foo"
.For a target like
pex_binary(..., args=["--foo"], env={"--bar": "baz"})
:pex ... --inject-args --foo --inject-env --bar=baz
, and argparse within the pex CLI will intepret--foo
and--bar=...
as independent parameters, rather than arguments to--inject-args
/--inject-env
.pex ... --inject-args=--foo --inject-env=--bar=baz
and there's no risk of misinterpretation.For a target like
pex_binary(..., args=["spaces 'n' quotes"])
:--inject-args=spaces 'n' quotes
, which wouldshlex.split
to three arguments in the PEX, the same as passingargs=["spaces", "n", "quotes"])
--inject-args='spaces '"'"'n'"'"' quotes'
, which, is correctlyshlex.split
to the single value:spaces 'n' quotes
.