test: guard run_ansible_in_environment arg word-splitting#2499
Merged
Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The regression test currently relies on
command.endswith(...), which is a bit brittle; consider asserting the full exact command string or parsing the relevant flags from the command to ensure future unrelated changes to prefix/suffix formatting don’t accidentally bypass this guard. - The explanatory comment in
test_run_ansible_list_multitoken_element_word_split_not_quotedis quite long; consider extracting the rationale into a brief reference (e.g., link to an issue or dev note) and keeping only the key invariants this test is enforcing so the test remains easy to scan.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The regression test currently relies on `command.endswith(...)`, which is a bit brittle; consider asserting the full exact command string or parsing the relevant flags from the command to ensure future unrelated changes to prefix/suffix formatting don’t accidentally bypass this guard.
- The explanatory comment in `test_run_ansible_list_multitoken_element_word_split_not_quoted` is quite long; consider extracting the rationale into a brief reference (e.g., link to an issue or dev note) and keeping only the key invariants this test is enforcing so the test remains easy to scan.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
run_ansible_in_environment joins a list of arguments with plain spaces
(" ".join(arguments)) and runs the result through
subprocess.Popen(..., shell=True), so /bin/sh word-splits each element
into separate argv. Several callers depend on this: they pack multiple
shell words into a single list element and rely on that split, e.g.
commands/set.py and commands/noset.py pass ["-e status=True",
"-l <host>"], and commands/validate.py / commands/apply.py prepend
"-e kolla_action=...". The run-<environment>.sh scripts forward args
via "$@", so the outer-shell split is load-bearing.
A well-meaning "fix" that shlex.quote()s each list element (to make a
value with shell metacharacters safe, such as a tempest_include_regex
of "(A|B)") would glue "-e status=True" into one token and silently
break -e/-l parsing for those callers. The existing
test_run_ansible_list_arguments_joined uses only single-word elements,
on which shlex.quote is a no-op, so it does not catch this.
Add a guard that pins the multi-token element behaviour: a
space-containing element must stay word-split and must not be
per-element quoted. It fails under a naive per-element quote and
records the safe alternatives (split each element on whitespace before
quoting, or normalise the callers to emit already-split tokens).
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Roger Luethi <luethi@osism.tech>
ideaship
force-pushed
the
test-guard-arg-word-split
branch
from
July 21, 2026 14:18
5896519 to
a2d13cc
Compare
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
Adds a unit-test regression guard for the argument-joining behaviour of
run_ansible_in_environmentinosism/tasks/__init__.py. Test only — nobehaviour change.
Why
run_ansible_in_environmentjoins a list of arguments with plain spaces(
" ".join(arguments)) and runs the result throughsubprocess.Popen(..., shell=True), so/bin/shword-splits each element intoseparate argv. Several callers deliberately rely on this — they pack multiple
shell words into a single list element:
commands/set.py,commands/noset.py:["-e status=True", f"-l {host}"]commands/validate.py:"-e kolla_action=config_validate"commands/apply.py:[f"-e kolla_action={action}"] + argumentsThe
run-<environment>.shscripts forward args via"$@"(no re-split), so theouter-shell split is load-bearing.
This makes the join fragile in a non-obvious way: a value containing a shell
metacharacter (e.g.
tempest_include_regex=(A|B)) breaks at/bin/sh(
Syntax error: "(" unexpected). The tempting one-line "fix" —" ".join(shlex.quote(a) for a in arguments)— would regress the callersabove by gluing
-e status=Trueinto a single token, and the existingtest_run_ansible_list_arguments_joinedwould not catch it (it uses onlysingle-word elements, on which
shlex.quoteis a no-op).What the guard does
Pins the multi-token behaviour: a space-containing element must stay word-split
and must not be per-element quoted. It fails under a naive per-element
shlex.quote, and its comment records the safe alternatives (split each elementon whitespace first then quote, or normalise the callers to emit already-split
tokens) so a future metacharacter-safety fix is done without breaking the
split-dependent callers.
🤖 Generated with Claude Code