This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] [test] Quote env variables that are set with a shell "export" in ssh.py
ClosedPublic

Authored by mstorsjo on Mar 24 2021, 12:48 AM.

Details

Summary

This safeguards against cases if some of the env vars contain chars
that are problematic for shells.

Diff Detail

Event Timeline

mstorsjo created this revision.Mar 24 2021, 12:48 AM
mstorsjo requested review of this revision.Mar 24 2021, 12:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2021, 12:48 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
curdeius accepted this revision as: curdeius.Mar 24 2021, 1:59 AM
curdeius added a subscriber: curdeius.

LGTM.

Quuxplusone added inline comments.
libcxx/utils/ssh.py
116

pipes.quote is gone and/or undocumented in Python 3; the Internet tells me to use shlex.quote instead.

I'm skeptical of "shell escaping" cleanups because it's essentially impossible to do right, and so any given commit is likely to make matters more complicated without making them any more correct. I'd like the commit message to contain a concrete example of something that didn't work before, that does work now.
(I'm guessing this has to do with a PATH containing names-with-spaces; but I want the commit message to tell me explicitly.)

mstorsjo added inline comments.Mar 24 2021, 12:29 PM
libcxx/utils/ssh.py
116

Yes, but the shebang only specifies python, so depending on distribution, this still can be executed by python2, deprecated or not - and shlex.quote is python3 only. Don't remember what the overall progress of python 2->3 transition/forcing scripts to python3 in llvm, but I didn't want to get sidetracked with that right now.

I can clarify the message regarding what chars are quoted/escaped. Spaces is an obvious one, but the one I tripped on actually is semicolons. (The semicolons aren't right on their own either - they come from trying to set a windows PATH while crosstesting from linux via ssh. No path from the build host make sense on the remote system anyway, regardless of how they're separated, but the cross testing setup works well for statically linked setups anyway.) Tl;dr, unneeded env vars can contain semicolons, this makes sure ssh.py doesn't trip up on them, even if they aren't entirely correct.

curdeius added inline comments.Mar 24 2021, 1:38 PM
libcxx/utils/ssh.py
116

How about doing this?

try:
    from shlex import quote as cmd_quote
except ImportError:
    from pipes import quote as cmd_quote
mstorsjo added inline comments.Mar 24 2021, 1:41 PM
libcxx/utils/ssh.py
116

Sure, I can do that

mstorsjo updated this revision to Diff 333120.Mar 24 2021, 2:20 PM

Updated to prefer shlex.quote if available.

Amended the commit message to include this:

This safeguards against cases if some of the env vars contain chars
that are problematic for shells. (In cases of cross testing for
windows, the PATH variable can end up specified with semicolon
separators - even if specifying a PATH overall doesn't even make sense
when cross testing, but this makes ssh.py not break on such a variable.)

LGTM, as long as the commit message contains that example! Sounds like an almost-complete example of what I'm looking for is
libcxx/utils/ssh.py --env "X=Y;Z" something

libcxx/utils/ssh.py
28

Let's leave a comment here like # for Python 2 compatibility
I switched from "Python 2 chauvinist" to "Python 3 chauvinist" a year or so ago — IIRC just after Python 2 was officially end-of-lifed in January 2020. So I'm happy not to cater to anyone still stuck on Python 2, but I don't mind (as long as we leave the greppable comment for later cleanup).

This revision was not accepted when it landed; it landed in state Needs Review.Mar 25 2021, 12:54 AM
This revision was automatically updated to reflect the committed changes.