From ed690d3323f4257850420912d16fc10a1d1d51ee Mon Sep 17 00:00:00 2001 From: Albert Hsieh Date: Tue, 17 Nov 2026 09:39:31 +0000 Subject: [PATCH 1/2] fix(ci): quote smoke asserts heredoc and deepen fast-gate checkout Use a quoted heredoc in smoke-linux common_asserts() so the CI host cannot expand backticks or command substitution inside assertion comments. Splice only allowlisted dpkg/rpm list commands. Set source-gates checkout fetch-depth to 0 so fork merge-base resolution does not fall back to base TIP. Regression tests pin both behaviors. Signed-off-by: Albert Hsieh --- .github/workflows/fast-gate.yml | 7 ++ scripts/smoke-linux-packages.sh | 44 +++++---- test/test_check_comment_history.py | 7 ++ ...est_smoke_linux_packages_common_asserts.py | 91 +++++++++++++++++++ 4 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 test/test_smoke_linux_packages_common_asserts.py diff --git a/.github/workflows/fast-gate.yml b/.github/workflows/fast-gate.yml index a44ecb364720..088157b2a644 100644 --- a/.github/workflows/fast-gate.yml +++ b/.github/workflows/fast-gate.yml @@ -93,6 +93,13 @@ jobs: # origin` against the same remote, through the shared diff-base resolver # script. - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + # Full history so resolve_base() can merge-base(COMMENT_HISTORY_BASE_REF, + # HEAD). With the default depth-1 checkout the resolver still fetches + # the base SHA, but it arrives as an isolated tip and merge-base falls + # back to measuring against the base TIP instead of the divergence point. + fetch-depth: 0 - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: "3.12" diff --git a/scripts/smoke-linux-packages.sh b/scripts/smoke-linux-packages.sh index b411ea6dd546..bbfc5014af81 100755 --- a/scripts/smoke-linux-packages.sh +++ b/scripts/smoke-linux-packages.sh @@ -56,8 +56,8 @@ RPM_NAME="$(resolve_one rpm)" ABS_DIST="$(cd "$DIST_DIR" && pwd)" # Shared assertions, run inside the container after the package manager has -# installed the package. `$1` is the command that lists the package's own files -# (`dpkg -L ` / `rpm -ql `). +# installed the package. The list-files command is the only caller-controlled +# fragment (dpkg -L "$pkg" / rpm -ql "$pkg"); everything else is fixed. # # EVERY identity is DERIVED from that list rather than written here, because the # nightly channel deliberately ships different ones so it can sit beside stable: @@ -68,12 +68,21 @@ ABS_DIST="$(cd "$DIST_DIR" && pwd)" # that would silently skip nightly's entire Linux publication. What is asserted # is the INVARIANT, not one channel's names. common_asserts() { - cat <&2 + return 1 + ;; + esac + local body + body=$(cat <<'ASSERTS' + files=$(__LIST_CMD__) + entry=$(printf '%s\n' "$files" | grep -E '^/usr/share/applications/[^/]+[.]desktop$' | head -1) + stamp=$(printf '%s\n' "$files" | grep -E '/kiro_crew/_build_info[.]py$' | head -1) + test -n "$entry" && test -f "$entry" + test -n "$stamp" # The identity comes from the desktop entry's FILENAME, not from parsing its # Exec value. linux.syncDesktopName ties four things to one name -- the entry # filename, executableName, Electron's app_id and StartupWMClass -- so the @@ -81,23 +90,26 @@ common_asserts() { # # Exec is deliberately NOT the source: electron-builder quotes that path when # it contains a character outside [/0-9A-Za-z._-], which the nightly channel's - # `/opt/KiroCrew Nightly/...` does. Parsing it would work on stable and return + # /opt/KiroCrew Nightly/... does. Parsing it would work on stable and return # empty on nightly -- the third time this gate assumed stable's shape. - exe=\$(basename "\$entry" .desktop) - launcher=/usr/bin/\$exe - test -x "\$launcher" + exe=$(basename "$entry" .desktop) + launcher=/usr/bin/$exe + test -x "$launcher" # Window association: StartupWMClass must equal Electron's app_id, which # electron-builder derives from desktopName -- the same value the launcher # name follows. A mismatch leaves the running window unassociated with its # launcher icon, silently, at runtime. - grep -q "^StartupWMClass=\${exe}\$" "\$entry" + grep -q "^StartupWMClass=${exe}$" "$entry" # Installed under the fixed /opt prefix, which is what makes the AppArmor # profile, the PATH launcher and in-place updates durable. - grep -qE "^Exec=\\"?/opt/.*/\${exe}" "\$entry" + grep -qE "^Exec=\"?/opt/.*/${exe}" "$entry" # The bundled CLI that fixed prefix makes reachable -- what an AppImage - # cannot offer, and what makes \`kirocrew service install\` usable. - printf '%s\\n' "\$files" | grep -qE '/backend-dist/kirocrew-backend/bin/kirocrew\$' + # cannot offer, and what makes `kirocrew service install` usable. + printf '%s\n' "$files" | grep -qE '/backend-dist/kirocrew-backend/bin/kirocrew$' ASSERTS +) + body="${body//__LIST_CMD__/$list_cmd}" + printf '%s' "$body" } echo "▶ Installing '${DEB_NAME}' on Ubuntu 24.04 (the t64 release)…" diff --git a/test/test_check_comment_history.py b/test/test_check_comment_history.py index c136575fb3f2..263a789092d8 100644 --- a/test/test_check_comment_history.py +++ b/test/test_check_comment_history.py @@ -46,6 +46,13 @@ def test_the_gate_runs_the_self_test_first(self) -> None: # silently disables a rule fails in CI instead of shipping green. assert "--test" in str(_gate_step().get("run", "")) + def test_source_gates_checkout_fetches_full_history_for_merge_base(self) -> None: + job = yaml.safe_load(FAST_GATE.read_text(encoding="utf-8"))["jobs"]["source-gates"] + checkout = next( + step for step in job["steps"] if "actions/checkout" in str(step.get("uses", "")) + ) + assert (checkout.get("with") or {}).get("fetch-depth") == 0 + def test_the_gate_is_diff_scoped_via_the_base_ref_env(self) -> None: # Without the env the script only reports. A step that forgot to export # it would print counts and pass every PR. diff --git a/test/test_smoke_linux_packages_common_asserts.py b/test/test_smoke_linux_packages_common_asserts.py new file mode 100644 index 000000000000..c11617eae2f2 --- /dev/null +++ b/test/test_smoke_linux_packages_common_asserts.py @@ -0,0 +1,91 @@ +r"""``common_asserts`` must not execute host commands while building container script. + +The docker wrapper expands ``$(common_asserts …)`` on the HOST. An unquoted +heredoc there treats backticks and ``$()`` as shell syntax on the host before +``docker run`` starts — not inside the container. +""" + +from __future__ import annotations + +import subprocess +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "scripts" / "smoke-linux-packages.sh" + +_ALLOWED_LIST_CMDS = ('dpkg -L "$pkg"', 'rpm -ql "$pkg"') + + +def _common_asserts_definition() -> str: + text = SCRIPT.read_text(encoding="utf-8") + start = text.index("common_asserts() {") + depth = 0 + for i in range(start, len(text)): + ch = text[i] + if ch == "{": + depth += 1 + elif ch == "}": + depth -= 1 + if depth == 0: + return text[start : i + 1] + raise AssertionError("common_asserts() body not closed in smoke-linux-packages.sh") + + +def _bash(script: str) -> subprocess.CompletedProcess[str]: + return subprocess.run( + ["bash", "-c", script], + cwd=ROOT, + check=False, + capture_output=True, + text=True, + ) + + +def test_common_asserts_rejects_unknown_list_command() -> None: + proc = _bash(f"{_common_asserts_definition()}\ncommon_asserts 'id'") + assert proc.returncode != 0 + assert "unsupported list command" in (proc.stderr or "") + + +def test_common_asserts_quoted_heredoc_has_no_host_expansion() -> None: + for list_cmd in _ALLOWED_LIST_CMDS: + proc = _bash(f"{_common_asserts_definition()}\ncommon_asserts {list_cmd!r}") + assert proc.returncode == 0, proc.stderr + assert "No such file or directory" not in (proc.stderr or ""), proc.stderr + assert list_cmd in proc.stdout + assert 'exe=$(basename "$entry" .desktop)' in proc.stdout + + +def test_unquoted_heredoc_with_sentinel_runs_on_host(tmp_path: Path) -> None: + """Documents the failure mode the quoted heredoc removes.""" + marker = tmp_path / "host-expanded" + probe = f""" +common_asserts_old() {{ + cat < None: + proc = _bash(f"{_common_asserts_definition()}\n" f"common_asserts {_ALLOWED_LIST_CMDS[0]!r}") + assert "/opt/KiroCrew Nightly" in proc.stdout + assert proc.returncode == 0 + assert not (proc.stderr or "").strip() + + +def test_common_asserts_uses_quoted_heredoc_delimiter() -> None: + text = SCRIPT.read_text(encoding="utf-8") + start = text.index("common_asserts() {") + chunk = text[start:] + assert "<<'ASSERTS'" in chunk, "common_asserts must expand body with a quoted heredoc delimiter" + assert "cat < None: + subprocess.run(["bash", "-n", str(SCRIPT)], check=True) From c20074833255b1c87edc9db4901f5a73bc0394d8 Mon Sep 17 00:00:00 2001 From: Albert Hsieh Date: Fri, 20 Nov 2026 07:46:41 +0000 Subject: [PATCH 2/2] fix(ci): run smoke assert tests on real bash and keep gate credentials The smoke assert tests invoked PATH bash, which on a Windows runner is the WSL launcher: it prints a UTF-16 "no distributions" line and exits 1, so the new suite failed every Windows shard and the subprocess encoding ratchet. Run them with the shared POSIX shell (Git Bash there) and pin the child's encoding. source-gates still fetches origin for the diff base, so the checkout keeps its credential. The opt-out contradicted the retention comment and failed the checkout ratchet, which failed Coverage Gate closed. Signed-off-by: Albert Hsieh --- .github/workflows/fast-gate.yml | 1 - ...est_smoke_linux_packages_common_asserts.py | 81 +++++++++++++++---- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/.github/workflows/fast-gate.yml b/.github/workflows/fast-gate.yml index 088157b2a644..1f7e4177f099 100644 --- a/.github/workflows/fast-gate.yml +++ b/.github/workflows/fast-gate.yml @@ -94,7 +94,6 @@ jobs: # script. - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - persist-credentials: false # Full history so resolve_base() can merge-base(COMMENT_HISTORY_BASE_REF, # HEAD). With the default depth-1 checkout the resolver still fetches # the base SHA, but it arrives as an isolated tip and merge-base falls diff --git a/test/test_smoke_linux_packages_common_asserts.py b/test/test_smoke_linux_packages_common_asserts.py index c11617eae2f2..f4d15ee94451 100644 --- a/test/test_smoke_linux_packages_common_asserts.py +++ b/test/test_smoke_linux_packages_common_asserts.py @@ -7,15 +7,51 @@ from __future__ import annotations +import shutil import subprocess from pathlib import Path +import pytest + ROOT = Path(__file__).resolve().parents[1] SCRIPT = ROOT / "scripts" / "smoke-linux-packages.sh" _ALLOWED_LIST_CMDS = ('dpkg -L "$pkg"', 'rpm -ql "$pkg"') +def _bash_executable(posix_shell: str) -> str: + """Bash that can parse this script, never Windows' WSL launcher. + + On a Windows runner ``shutil.which("bash")`` resolves to + ``C:\\Windows\\System32\\bash.exe``. That launcher writes a UTF-16 + "Windows Subsystem for Linux has no installed distributions" line and + exits 1, so a PATH lookup fails every assertion before the script runs. + ``posix_test_shell`` is Git Bash on that host. A path whose file name is + already bash is the interpreter to run; looking it up again finds the + launcher. + """ + if Path(posix_shell).stem.lower() == "bash": + return posix_shell + resolved = shutil.which("bash") + assert resolved is not None, f"{posix_shell} is not bash and no bash is on PATH" + return resolved + + +def test_a_bash_path_is_not_looked_up_again() -> None: + """The Windows choice, checked without a Windows host. + + Git Bash's path is what the shared fixture returns there. Re-resolving + ``bash`` from PATH selects the WSL launcher instead. + """ + git_bash = "C:/Program Files/Git/bin/bash.exe" + assert _bash_executable(git_bash) == git_bash + + +@pytest.fixture +def bash_exe(posix_test_shell: str) -> str: + return _bash_executable(posix_test_shell) + + def _common_asserts_definition() -> str: text = SCRIPT.read_text(encoding="utf-8") start = text.index("common_asserts() {") @@ -31,49 +67,57 @@ def _common_asserts_definition() -> str: raise AssertionError("common_asserts() body not closed in smoke-linux-packages.sh") -def _bash(script: str) -> subprocess.CompletedProcess[str]: +def _bash(bash_exe: str, script: str) -> subprocess.CompletedProcess[str]: return subprocess.run( - ["bash", "-c", script], + [bash_exe, "-c", script], cwd=ROOT, check=False, capture_output=True, text=True, + encoding="utf-8", ) -def test_common_asserts_rejects_unknown_list_command() -> None: - proc = _bash(f"{_common_asserts_definition()}\ncommon_asserts 'id'") +def test_common_asserts_rejects_unknown_list_command(bash_exe: str) -> None: + proc = _bash(bash_exe, f"{_common_asserts_definition()}\ncommon_asserts 'id'") assert proc.returncode != 0 assert "unsupported list command" in (proc.stderr or "") -def test_common_asserts_quoted_heredoc_has_no_host_expansion() -> None: +def test_common_asserts_quoted_heredoc_has_no_host_expansion(bash_exe: str) -> None: for list_cmd in _ALLOWED_LIST_CMDS: - proc = _bash(f"{_common_asserts_definition()}\ncommon_asserts {list_cmd!r}") + proc = _bash(bash_exe, f"{_common_asserts_definition()}\ncommon_asserts {list_cmd!r}") assert proc.returncode == 0, proc.stderr assert "No such file or directory" not in (proc.stderr or ""), proc.stderr assert list_cmd in proc.stdout assert 'exe=$(basename "$entry" .desktop)' in proc.stdout -def test_unquoted_heredoc_with_sentinel_runs_on_host(tmp_path: Path) -> None: +def test_unquoted_heredoc_with_sentinel_runs_on_host(bash_exe: str, tmp_path: Path) -> None: """Documents the failure mode the quoted heredoc removes.""" marker = tmp_path / "host-expanded" + # The path is inside the script body, so Git Bash does not convert + # backslashes the way it converts an argv path. ``as_posix`` is a path + # both Git Bash and a POSIX bash can ``touch``. + marker_shell = marker.as_posix().replace("'", "'\\''") probe = f""" common_asserts_old() {{ cat < None: - proc = _bash(f"{_common_asserts_definition()}\n" f"common_asserts {_ALLOWED_LIST_CMDS[0]!r}") +def test_common_asserts_emits_nightly_path_in_comment_without_executing(bash_exe: str) -> None: + proc = _bash( + bash_exe, + f"{_common_asserts_definition()}\n" f"common_asserts {_ALLOWED_LIST_CMDS[0]!r}", + ) assert "/opt/KiroCrew Nightly" in proc.stdout assert proc.returncode == 0 assert not (proc.stderr or "").strip() @@ -87,5 +131,12 @@ def test_common_asserts_uses_quoted_heredoc_delimiter() -> None: assert "cat < None: - subprocess.run(["bash", "-n", str(SCRIPT)], check=True) +def test_smoke_linux_packages_shell_syntax(bash_exe: str) -> None: + proc = subprocess.run( + [bash_exe, "-n", str(SCRIPT)], + check=False, + capture_output=True, + text=True, + encoding="utf-8", + ) + assert proc.returncode == 0, proc.stderr