Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/brigade/work_cmd/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def _receipt_git_snapshot(target: Path) -> dict[str, Any] | None:
return {"head": head, "branch": branch, "dirty_files": len(status.stdout.splitlines())}


def _high_risk_command_message(executable: str) -> str:
"""Rejection message for a shell/remote executable used as a verify command.

Mirrors the shell-metacharacter branch by naming the remedy: verify runs the
command directly with no shell, so a shell interpreter is never a valid
executable. ``--argv-json`` is deliberately not suggested here because that
path applies the same high-risk block, so the fix is a resolvable non-shell
executable, e.g. a chmod +x script invoked by its path.
"""
return (
f"high-risk verification command: {executable} "
"(verify runs with no shell; use a resolvable executable, e.g. a "
"chmod +x script run by its path like ./scripts/check.sh)"
)


def _verify_parse_command(command: str, target: Path) -> tuple[list[str] | None, dict[str, str], str | None]:
try:
parts = shlex.split(command)
Expand All @@ -57,7 +73,7 @@ def _verify_parse_command(command: str, target: Path) -> tuple[list[str] | None,
return None, env, "command contains only environment assignments"
executable = Path(argv[0]).name
if executable in constants.SCANNER_HIGH_RISK_COMMANDS:
return None, env, f"high-risk verification command: {executable}"
return None, env, _high_risk_command_message(executable)
if any(constants.SCANNER_SHELL_META_RE.search(part) for part in argv):
return (
None,
Expand Down Expand Up @@ -89,7 +105,7 @@ def _verify_parse_argv(argv: list[str], target: Path) -> tuple[list[str] | None,
return None, {}, "empty command"
executable = Path(argv[0]).name
if executable in constants.SCANNER_HIGH_RISK_COMMANDS:
return None, {}, f"high-risk verification command: {executable}"
return None, {}, _high_risk_command_message(executable)
if "/" in argv[0]:
executable_path = Path(argv[0]).expanduser()
if not executable_path.is_absolute():
Expand Down
26 changes: 26 additions & 0 deletions tests/test_work_cmd_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,32 @@ def test_work_verify_run_command_still_rejects_shell_metacharacters(tmp_path, ca
assert "--argv-json" in receipt["commands"][0]["stderr_summary"]


@pytest.mark.parametrize(
("option", "value"),
[
("--command", "bash ./check.sh"),
("--argv-json", json.dumps(["bash", "-c", "true"])),
],
)
def test_work_verify_run_rejects_shell_interpreter_with_remedy(tmp_path, capsys, option, value):
# A shell interpreter is never a valid verify executable (verify runs argv
# directly with shell=False). Both the --command and --argv-json paths reject
# it, and the message must name the remedy - mirroring the metacharacter
# branch - so a caller is not left at a dead end. It must NOT point at
# --argv-json, which applies the same block.
_init_git_repo(tmp_path)

rc = cli.main(["work", "verify", "run", "--target", str(tmp_path), option, value, "--json"])
receipt = json.loads(capsys.readouterr().out)
summary = receipt["commands"][0]["stderr_summary"]
assert rc != 0
assert receipt["status"] == "rejected"
assert receipt["commands"][0]["status"] == "rejected"
assert "high-risk verification command: bash" in summary
assert "resolvable executable" in summary
assert "--argv-json" not in summary


def test_work_verify_run_command_and_argv_json_are_mutually_exclusive(tmp_path, capsys):
with pytest.raises(SystemExit) as exc:
cli.main(
Expand Down
Loading