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
57 changes: 56 additions & 1 deletion lib/hypatia/workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(<<c, r::binary>>, nil, false, false, qs?, [:case_head | ps], ["case" | acc]),
Comment thread
hyperpolymath marked this conversation as resolved.
Resolved
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(<<c, r::binary>>, 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(<<c, r::binary>>, nil, false, false, qs?, ps, ["esac" | acc])
Comment thread
hyperpolymath marked this conversation as resolved.
Resolved

defp mask(<<"esac">>, nil, true, _, qs?, [:case | ps], acc),
do: mask(<<>>, nil, false, false, qs?, ps, ["esac" | acc])

defp mask(<<?$, ?(, r::binary>>, nil, _, _, qs?, ps, acc),
do: mask(r, nil, true, false, qs?, [:subst | ps], [?(, ?$ | acc])

Expand All @@ -305,6 +329,7 @@ defmodule Hypatia.Workflow do

defp mask(<<?), r::binary>>, nil, _, _, qs?, ps, acc) do
case ps do
[top | _] when top in [:case, :case_head] -> mask(r, nil, true, false, qs?, ps, [?) | acc])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recognize a nested case at the start of a case arm

When an outer arm starts a nested case on the same line, as in case a in a) case b in ..., this pattern-closing ) sets whitespace state but leaves no indication that the next token is at a command start, so command_start?/1 rejects the nested case. Its esac then pops the outer case state, and a later outer pattern ) can incorrectly close the surrounding $(...), leaving a following commented pin visible to check_hypatia_pin_drift; preserve the case-arm command-start context after this delimiter.

Useful? React with 👍 / 👎.

[: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])
Expand All @@ -317,6 +342,36 @@ defmodule Hypatia.Workflow do
defp mask(<<c, r::binary>>, 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
Comment thread
hyperpolymath marked this conversation as resolved.
Resolved
Comment thread
hyperpolymath marked this conversation as resolved.
Resolved
rest -> previous_word(rest) in @command_words and word_start_before?(rest)
Comment thread
hyperpolymath marked this conversation as resolved.
Resolved
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
2 changes: 2 additions & 0 deletions lib/rules/workflow_audit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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/;|(?<![<>&])&(?![>&])/)
Expand Down
12 changes: 12 additions & 0 deletions test/rules/always_reads_cancelled_needs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
78 changes: 78 additions & 0 deletions test/rules/ci_failure_triage_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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) == []
Expand Down
Loading