From 42e6416a3516ecf8a62d0338a4a91533e8f223c1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 22:54:44 +0000 Subject: [PATCH 1/5] fix(rules): case arms inside $(...), escaped operators before & The two Codex edge cases #1129 left as stated limits: - shell_mask: the word `case` at a command start pushes :case and `esac` pops it. While :case is on top, a `)` ends a pattern, not the enclosing `$(...)`, so `v=$(case yes in yes)# ...` is a comment and `$(case y in y) echo;; esac)#x` still closes the substitution. - WF033 later_command?/1: backslash-escaped bytes are literals, so in `echo \>& echo done` the `&` is a separator, not part of `>&`. Tests for both fail without the change. A guard test checks that `case` as an argument opens nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkGCWGEyedTtdEHAPT --- lib/hypatia/workflow.ex | 30 ++++++++++++++++++- lib/rules/workflow_audit.ex | 2 ++ .../always_reads_cancelled_needs_test.exs | 12 ++++++++ test/rules/ci_failure_triage_test.exs | 25 ++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/hypatia/workflow.ex b/lib/hypatia/workflow.ex index f4b06a2a..d12ded11 100644 --- a/lib/hypatia/workflow.ex +++ b/lib/hypatia/workflow.ex @@ -271,8 +271,12 @@ defmodule Hypatia.Workflow do # the word, so a `#` after it opens a comment; the `)` of `$(…)` does not, # so `$(name)#suffix` is one word (Codex and CodeRabbit on #1129). `parens` # is the stack of open `(`: :subst for `$(`, :sub for a bare `(`. A `)` - # with nothing open (a `case` pattern) ends the word. + # with nothing open ends the word. The word `case` pushes :case and `esac` + # pops it; while :case is on top, a `)` ends a pattern, not the enclosing + # `$(…)`, so it ends the word and pops nothing (Codex on #1129). @word_breaks [?\s, ?\t, ?;, ?&, ?|, ?(, ?<, ?>] + # What may follow the words `case` and `esac`. + @word_ends [?\s, ?\t, ?\n, ?;, ?&, ?|, ?)] defp mask(<<>>, _q, _ws?, _cmt?, _qs?, _parens, acc), do: acc |> Enum.reverse() |> IO.iodata_to_binary() @@ -297,6 +301,20 @@ defmodule Hypatia.Workflow do mask(r, q, false, false, qs?, ps, kept ++ acc) end + # `case` opens a `case` only where a command starts. + defp mask(<<"case", c, r::binary>>, nil, true, _, qs?, ps, acc) when c in @word_ends do + if command_start?(acc), + do: mask(<>, nil, false, false, qs?, [:case | ps], ["case" | acc]), + else: mask(<<"ase", c, r::binary>>, nil, false, false, qs?, ps, [?c | acc]) + end + + defp mask(<<"esac", c, r::binary>>, nil, true, _, qs?, [:case | ps], acc) + when c in @word_ends, + do: mask(<>, nil, false, false, qs?, ps, ["esac" | acc]) + + defp mask(<<"esac">>, nil, true, _, qs?, [:case | ps], acc), + do: mask(<<>>, nil, false, false, qs?, ps, ["esac" | acc]) + defp mask(<>, nil, _, _, qs?, ps, acc), do: mask(r, nil, true, false, qs?, [:subst | ps], [?(, ?$ | acc]) @@ -305,6 +323,7 @@ defmodule Hypatia.Workflow do defp mask(<>, nil, _, _, qs?, ps, acc) do case ps do + [:case | _] -> mask(r, nil, true, false, qs?, ps, [?) | acc]) [:subst | rest] -> mask(r, nil, false, false, qs?, rest, [?) | acc]) [_ | rest] -> mask(r, nil, true, false, qs?, rest, [?) | acc]) [] -> mask(r, nil, true, false, qs?, [], [?) | acc]) @@ -317,6 +336,15 @@ defmodule Hypatia.Workflow do defp mask(<>, q, _, _, qs?, ps, acc), do: mask(r, q, false, false, qs?, ps, [if(qs?, do: blank(c), else: c) | acc]) + # After only blanks since a newline, `;`, `&`, `|`, `(` or the start. + defp command_start?(acc) do + case Enum.drop_while(acc, &(&1 in [?\s, ?\t])) do + [] -> true + [c | _] when c in [?\n, ?;, ?&, ?|, ?(] -> true + _ -> false + end + end + defp blank(?\n), do: ?\n defp blank(_), do: ?\s end diff --git a/lib/rules/workflow_audit.ex b/lib/rules/workflow_audit.ex index ed9bbceb..edf0c332 100644 --- a/lib/rules/workflow_audit.ex +++ b/lib/rules/workflow_audit.ex @@ -5458,6 +5458,8 @@ defmodule Hypatia.Rules.WorkflowAudit do defp later_command?(line) do line + # A backslash-escaped byte is a literal, not an operator (Codex on #1129). + |> String.replace(~r/\\./s, " ") |> String.replace("&&", " ") # `&>`, `>&2` and `<&` are redirections, not a new command (Codex on #1110). |> String.split(~r/;|(?&])&(?![>&])/) diff --git a/test/rules/always_reads_cancelled_needs_test.exs b/test/rules/always_reads_cancelled_needs_test.exs index 96e7042f..16fe6904 100644 --- a/test/rules/always_reads_cancelled_needs_test.exs +++ b/test/rules/always_reads_cancelled_needs_test.exs @@ -344,6 +344,18 @@ defmodule Hypatia.Rules.AlwaysReadsCancelledNeedsTest do assert [%{rule: "WF033"}] = check(%{"f.yml" => wf}) end + # Codex on #1129, after merge. + test "an escaped > before a background & does not make a redirection" do + wf = + String.replace( + @verdict, + "[ \"$DEED_RESULT\" = success ] && [ \"$K9_RESULT\" = success ] || exit 1", + "test \"$DEED_RESULT\" = success && echo \\>& echo done" + ) + + assert check(%{"f.yml" => wf}) == [] + end + test "it is part of the full audit" do %{findings: fs} = WorkflowAudit.audit(["fork-governance.yml"], %{"fork-governance.yml" => @verdict}) diff --git a/test/rules/ci_failure_triage_test.exs b/test/rules/ci_failure_triage_test.exs index b6537c0b..073707d4 100644 --- a/test/rules/ci_failure_triage_test.exs +++ b/test/rules/ci_failure_triage_test.exs @@ -877,6 +877,31 @@ defmodule Hypatia.Rules.CiFailureTriageTest do assert T.check_hypatia_pin_drift(Map.put(template_set(@new), "ss.yml", wf)) == [] end + # Codex on #1129, after merge. + test "a case arm inside $(...) ends the word, so its # is a comment" do + wf = + "jobs:\n a:\n steps:\n - run: |\n" <> + " v=$(case yes in yes)# grid --hypatia-ref #{@old}\n" <> + " echo; esac)\n" + + assert T.check_hypatia_pin_drift(Map.put(template_set(@new), "ca.yml", wf)) == [] + end + + test "the substitution still closes after esac" do + wf = + "jobs:\n a:\n steps:\n - run: |\n" <> + " v=$(case y in y) echo;; esac)#x --hypatia-ref #{@old}\n" + + found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "ce.yml", wf)) + assert [%{file: "ce.yml"}] = Enum.filter(found, &(&1.file == "ce.yml")) + end + + test "the word case as an argument does not open a case" do + wf = "jobs:\n a:\n steps:\n - run: v=$(echo case)#x --hypatia-ref #{@old}\n" + found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "cw.yml", wf)) + assert [%{file: "cw.yml"}] = Enum.filter(found, &(&1.file == "cw.yml")) + end + test "a variable HYPATIA_SHA is not a pin" do set = Map.put(template_set(@new), "v.yml", "env:\n HYPATIA_SHA: ${{ inputs.sha }}\n") assert T.check_hypatia_pin_drift(set) == [] From ec9d3309d503378c8261ddd2cbd59e6b7beac316 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 23:00:46 +0000 Subject: [PATCH 2/5] fix(workflow): a case after then, do, else, ... starts a command Codex on #1143: in `v=$(if true; then case yes in yes)# ...` the byte before `case` belongs to `then`, so command_start?/1 refused it, the pattern's `)` closed the substitution and the comment stayed visible. A reserved word that a command follows (then, do, else, elif, if, while, until, !, {) now counts as a command start, provided it starts a command itself; `echo then case` still opens nothing. Test "a case after then or do opens a case too" fails without this change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkGCWGEyedTtdEHAPT --- lib/hypatia/workflow.ex | 23 +++++++++++++++++++++-- test/rules/ci_failure_triage_test.exs | 9 +++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/hypatia/workflow.ex b/lib/hypatia/workflow.ex index d12ded11..d24eeb45 100644 --- a/lib/hypatia/workflow.ex +++ b/lib/hypatia/workflow.ex @@ -336,15 +336,34 @@ defmodule Hypatia.Workflow do defp mask(<>, q, _, _, qs?, ps, acc), do: mask(r, q, false, false, qs?, ps, [if(qs?, do: blank(c), else: c) | acc]) - # After only blanks since a newline, `;`, `&`, `|`, `(` or the start. + # After only blanks since a newline, `;`, `&`, `|`, `(`, the start, or a + # reserved word that a command follows (`then`, `do`, `else`, ...; Codex + # on #1143). + @command_words ~w(then do else elif if while until ! {) + defp command_start?(acc) do case Enum.drop_while(acc, &(&1 in [?\s, ?\t])) do [] -> true [c | _] when c in [?\n, ?;, ?&, ?|, ?(] -> true - _ -> false + rest -> previous_word(rest) in @command_words and word_start_before?(rest) end end + # The word that ends the (reversed) accumulator, and whether a command + # could start before it. + defp previous_word(rest) do + rest + |> Enum.take_while(&(is_integer(&1) and &1 not in [?\s, ?\t, ?\n, ?;, ?&, ?|, ?(])) + |> Enum.reverse() + |> List.to_string() + end + + defp word_start_before?(rest) do + rest + |> Enum.drop_while(&(is_integer(&1) and &1 not in [?\s, ?\t, ?\n, ?;, ?&, ?|, ?(])) + |> command_start?() + end + defp blank(?\n), do: ?\n defp blank(_), do: ?\s end diff --git a/test/rules/ci_failure_triage_test.exs b/test/rules/ci_failure_triage_test.exs index 073707d4..74e77c3c 100644 --- a/test/rules/ci_failure_triage_test.exs +++ b/test/rules/ci_failure_triage_test.exs @@ -896,6 +896,15 @@ defmodule Hypatia.Rules.CiFailureTriageTest do assert [%{file: "ce.yml"}] = Enum.filter(found, &(&1.file == "ce.yml")) end + test "a case after then or do opens a case too" do + wf = + "jobs:\n a:\n steps:\n - run: |\n" <> + " v=$(if true; then case yes in yes)# grid --hypatia-ref #{@old}\n" <> + " echo; esac; fi)\n" + + assert T.check_hypatia_pin_drift(Map.put(template_set(@new), "cr.yml", wf)) == [] + end + test "the word case as an argument does not open a case" do wf = "jobs:\n a:\n steps:\n - run: v=$(echo case)#x --hypatia-ref #{@old}\n" found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "cw.yml", wf)) From 94c79682ecb5a1296c7beecd042bcd42657db794 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 23:01:43 +0000 Subject: [PATCH 3/5] fix(workflow): esac as a case subject does not close the case Codex on #1143: in `$(case esac in yes)# ...` the subject `esac` popped :case, so the pattern's `)` closed the substitution and the comment stayed visible. `case` now pushes :case_head; the word `in` turns it into :case, and only then does `esac` close it. A `)` under either state ends a pattern and pops nothing. Test "esac as the case subject does not close the case" fails without this change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkGCWGEyedTtdEHAPT --- lib/hypatia/workflow.ex | 14 ++++++++++---- test/rules/ci_failure_triage_test.exs | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/hypatia/workflow.ex b/lib/hypatia/workflow.ex index d24eeb45..905b7946 100644 --- a/lib/hypatia/workflow.ex +++ b/lib/hypatia/workflow.ex @@ -271,8 +271,8 @@ defmodule Hypatia.Workflow do # the word, so a `#` after it opens a comment; the `)` of `$(…)` does not, # so `$(name)#suffix` is one word (Codex and CodeRabbit on #1129). `parens` # is the stack of open `(`: :subst for `$(`, :sub for a bare `(`. A `)` - # with nothing open ends the word. The word `case` pushes :case and `esac` - # pops it; while :case is on top, a `)` ends a pattern, not the enclosing + # with nothing open ends the word. The word `case` pushes :case_head, `in` makes it + # :case and `esac` pops it; while :case is on top, a `)` ends a pattern, not the enclosing # `$(…)`, so it ends the word and pops nothing (Codex on #1129). @word_breaks [?\s, ?\t, ?;, ?&, ?|, ?(, ?<, ?>] # What may follow the words `case` and `esac`. @@ -304,10 +304,16 @@ defmodule Hypatia.Workflow do # `case` opens a `case` only where a command starts. defp mask(<<"case", c, r::binary>>, nil, true, _, qs?, ps, acc) when c in @word_ends do if command_start?(acc), - do: mask(<>, nil, false, false, qs?, [:case | ps], ["case" | acc]), + do: mask(<>, nil, false, false, qs?, [:case_head | ps], ["case" | acc]), else: mask(<<"ase", c, r::binary>>, nil, false, false, qs?, ps, [?c | acc]) end + # Between `case` and `in` (the subject) no reserved word counts; `in` turns + # the head into the body, where `esac` closes it (Codex on #1143). + defp mask(<<"in", c, r::binary>>, nil, true, _, qs?, [:case_head | ps], acc) + when c in @word_ends, + do: mask(<>, nil, false, false, qs?, [:case | ps], ["in" | acc]) + defp mask(<<"esac", c, r::binary>>, nil, true, _, qs?, [:case | ps], acc) when c in @word_ends, do: mask(<>, nil, false, false, qs?, ps, ["esac" | acc]) @@ -323,7 +329,7 @@ defmodule Hypatia.Workflow do defp mask(<>, nil, _, _, qs?, ps, acc) do case ps do - [:case | _] -> mask(r, nil, true, false, qs?, ps, [?) | acc]) + [top | _] when top in [:case, :case_head] -> mask(r, nil, true, false, qs?, ps, [?) | acc]) [:subst | rest] -> mask(r, nil, false, false, qs?, rest, [?) | acc]) [_ | rest] -> mask(r, nil, true, false, qs?, rest, [?) | acc]) [] -> mask(r, nil, true, false, qs?, [], [?) | acc]) diff --git a/test/rules/ci_failure_triage_test.exs b/test/rules/ci_failure_triage_test.exs index 74e77c3c..3598bde8 100644 --- a/test/rules/ci_failure_triage_test.exs +++ b/test/rules/ci_failure_triage_test.exs @@ -905,6 +905,15 @@ defmodule Hypatia.Rules.CiFailureTriageTest do assert T.check_hypatia_pin_drift(Map.put(template_set(@new), "cr.yml", wf)) == [] end + test "esac as the case subject does not close the case" do + wf = + "jobs:\n a:\n steps:\n - run: |\n" <> + " v=$(case esac in yes)# grid --hypatia-ref #{@old}\n" <> + " echo; esac)\n" + + assert T.check_hypatia_pin_drift(Map.put(template_set(@new), "cs2.yml", wf)) == [] + end + test "the word case as an argument does not open a case" do wf = "jobs:\n a:\n steps:\n - run: v=$(echo case)#x --hypatia-ref #{@old}\n" found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "cw.yml", wf)) From 68c010dd5fbeb2cc4ae38605cc8157f2f1b62146 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 23:08:01 +0000 Subject: [PATCH 4/5] fix(workflow): a redirection attached to esac still ends the word Codex on #1143: `esac>/dev/null)` was not recognised as `esac`, so the substitution's `)` was taken as another pattern end and the live `#suffix --hypatia-ref` was masked. `<` and `>` now end the words `case`, `in` and `esac`. Test "esac with a redirection attached still closes the case" fails without this change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkGCWGEyedTtdEHAPT --- lib/hypatia/workflow.ex | 2 +- test/rules/ci_failure_triage_test.exs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/hypatia/workflow.ex b/lib/hypatia/workflow.ex index 905b7946..565bd615 100644 --- a/lib/hypatia/workflow.ex +++ b/lib/hypatia/workflow.ex @@ -276,7 +276,7 @@ defmodule Hypatia.Workflow do # `$(…)`, so it ends the word and pops nothing (Codex on #1129). @word_breaks [?\s, ?\t, ?;, ?&, ?|, ?(, ?<, ?>] # What may follow the words `case` and `esac`. - @word_ends [?\s, ?\t, ?\n, ?;, ?&, ?|, ?)] + @word_ends [?\s, ?\t, ?\n, ?;, ?&, ?|, ?), ?<, ?>] defp mask(<<>>, _q, _ws?, _cmt?, _qs?, _parens, acc), do: acc |> Enum.reverse() |> IO.iodata_to_binary() diff --git a/test/rules/ci_failure_triage_test.exs b/test/rules/ci_failure_triage_test.exs index 3598bde8..0d938516 100644 --- a/test/rules/ci_failure_triage_test.exs +++ b/test/rules/ci_failure_triage_test.exs @@ -914,6 +914,15 @@ defmodule Hypatia.Rules.CiFailureTriageTest do assert T.check_hypatia_pin_drift(Map.put(template_set(@new), "cs2.yml", wf)) == [] end + test "esac with a redirection attached still closes the case" do + wf = + "jobs:\n a:\n steps:\n - run: |\n" <> + " v=$(case x in x) echo yes;; esac>/dev/null)#suffix --hypatia-ref #{@old}\n" + + found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "cr2.yml", wf)) + assert [%{file: "cr2.yml"}] = Enum.filter(found, &(&1.file == "cr2.yml")) + end + test "the word case as an argument does not open a case" do wf = "jobs:\n a:\n steps:\n - run: v=$(echo case)#x --hypatia-ref #{@old}\n" found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "cw.yml", wf)) From f010a27080d570a900b199415bf3a6b688f8e040 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 23:15:57 +0000 Subject: [PATCH 5/5] fix(workflow): case after time/coproc, in( and escaped separators Codex on #1143: - `time` and `coproc` join the reserved words a command follows. - `(` ends the words `case`, `in` and `esac`, so `in(x)` enters the body. - A separator escaped with a backslash (`\;`, `\` newline) is not a command start, so `echo \; case ...` opens nothing. Each has a test that fails without the change. Not done: `case` as a literal pattern alternative (`x|case)`) still opens a nested case; telling a pattern position from a command position needs a real parser. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZkGCWGEyedTtdEHAPT --- lib/hypatia/workflow.ex | 6 ++++-- test/rules/ci_failure_triage_test.exs | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/hypatia/workflow.ex b/lib/hypatia/workflow.ex index 565bd615..8befea3d 100644 --- a/lib/hypatia/workflow.ex +++ b/lib/hypatia/workflow.ex @@ -276,7 +276,7 @@ defmodule Hypatia.Workflow do # `$(…)`, so it ends the word and pops nothing (Codex on #1129). @word_breaks [?\s, ?\t, ?;, ?&, ?|, ?(, ?<, ?>] # What may follow the words `case` and `esac`. - @word_ends [?\s, ?\t, ?\n, ?;, ?&, ?|, ?), ?<, ?>] + @word_ends [?\s, ?\t, ?\n, ?;, ?&, ?|, ?(, ?), ?<, ?>] defp mask(<<>>, _q, _ws?, _cmt?, _qs?, _parens, acc), do: acc |> Enum.reverse() |> IO.iodata_to_binary() @@ -345,11 +345,13 @@ defmodule Hypatia.Workflow do # After only blanks since a newline, `;`, `&`, `|`, `(`, the start, or a # reserved word that a command follows (`then`, `do`, `else`, ...; Codex # on #1143). - @command_words ~w(then do else elif if while until ! {) + @command_words ~w(then do else elif if while until ! { time coproc) defp command_start?(acc) do case Enum.drop_while(acc, &(&1 in [?\s, ?\t])) do [] -> true + # A separator escaped with a backslash is a literal (Codex on #1143). + [c, ?\\ | _] when c in [?\n, ?;, ?&, ?|, ?(] -> false [c | _] when c in [?\n, ?;, ?&, ?|, ?(] -> true rest -> previous_word(rest) in @command_words and word_start_before?(rest) end diff --git a/test/rules/ci_failure_triage_test.exs b/test/rules/ci_failure_triage_test.exs index 0d938516..4911e76f 100644 --- a/test/rules/ci_failure_triage_test.exs +++ b/test/rules/ci_failure_triage_test.exs @@ -923,6 +923,32 @@ defmodule Hypatia.Rules.CiFailureTriageTest do assert [%{file: "cr2.yml"}] = Enum.filter(found, &(&1.file == "cr2.yml")) end + test "a case after time opens a case too" do + wf = + "jobs:\n a:\n steps:\n - run: |\n" <> + " v=$(time case x in x)# grid --hypatia-ref #{@old}\n" <> + " echo; esac)\n" + + assert T.check_hypatia_pin_drift(Map.put(template_set(@new), "ct.yml", wf)) == [] + end + + test "an escaped ; does not make case a command" do + wf = + "jobs:\n a:\n steps:\n - run: v=$(echo \\; case x in x)#suffix --hypatia-ref #{@old}\n" + + found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "ce2.yml", wf)) + assert [%{file: "ce2.yml"}] = Enum.filter(found, &(&1.file == "ce2.yml")) + end + + test "in( with no blank still enters the case body" do + wf = + "jobs:\n a:\n steps:\n - run: |\n" <> + " v=$(case x in(x) echo yes;; esac)#suffix --hypatia-ref #{@old}\n" + + found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "ci.yml", wf)) + assert [%{file: "ci.yml"}] = Enum.filter(found, &(&1.file == "ci.yml")) + end + test "the word case as an argument does not open a case" do wf = "jobs:\n a:\n steps:\n - run: v=$(echo case)#x --hypatia-ref #{@old}\n" found = T.check_hypatia_pin_drift(Map.put(template_set(@new), "cw.yml", wf))