diff --git a/lib/hypatia/workflow.ex b/lib/hypatia/workflow.ex index f4b06a2a..8befea3d 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_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`. + @word_ends [?\s, ?\t, ?\n, ?;, ?&, ?|, ?(, ?), ?<, ?>] defp mask(<<>>, _q, _ws?, _cmt?, _qs?, _parens, acc), do: acc |> Enum.reverse() |> IO.iodata_to_binary() @@ -297,6 +301,26 @@ 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_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]) + + 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 +329,7 @@ defmodule Hypatia.Workflow do defp mask(<>, nil, _, _, qs?, ps, acc) do case ps do + [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]) @@ -317,6 +342,36 @@ 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, `;`, `&`, `|`, `(`, 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 ! { 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 + 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/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..4911e76f 100644 --- a/test/rules/ci_failure_triage_test.exs +++ b/test/rules/ci_failure_triage_test.exs @@ -877,6 +877,84 @@ 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 "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 "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 "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 "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)) + 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) == []