|
| 1 | +# SPDX-FileCopyrightText: Sudo Apt Holdings LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +defmodule DcoRangeTest do |
| 4 | + @moduledoc """ |
| 5 | + Slice 128: the workflow's range check says "every commit" and checks "at least one". |
| 6 | +
|
| 7 | + `.github/workflows/gate.yml` carries a step named **DCO sign-off present on every commit** whose |
| 8 | + body concatenates every message in the range and greps for one `Signed-off-by:` anywhere: |
| 9 | +
|
| 10 | + git log --format=%B BASE..HEAD | grep -q '^Signed-off-by: ' |
| 11 | +
|
| 12 | + A range of five commits where one is signed passes it. `test/dco_test.exs` does check every |
| 13 | + non-merge commit, over the whole history, on every leg, so the practical exposure is small. The |
| 14 | + defect is that a step's name is a claim, and this one is false: a reader of the workflow, or of a |
| 15 | + donation review, is told something the file does not do. |
| 16 | +
|
| 17 | + This test is about the logic rather than about the workflow's YAML, so it runs the same shape |
| 18 | + against a range it builds itself. |
| 19 | + """ |
| 20 | + use ExUnit.Case, async: true |
| 21 | + |
| 22 | + @moduletag :tmp_dir |
| 23 | + |
| 24 | + # The shape the workflow uses today, extracted so it can be run against a known range. |
| 25 | + defp concatenated_grep(dir, range) do |
| 26 | + {out, _} = System.cmd("git", ["log", "--format=%B", range], cd: dir) |
| 27 | + if String.match?(out, ~r/^Signed-off-by: /m), do: :pass, else: :fail |
| 28 | + end |
| 29 | + |
| 30 | + # What the name promises: every commit in the range, individually. |
| 31 | + defp every_commit(dir, range) do |
| 32 | + {out, 0} = |
| 33 | + System.cmd( |
| 34 | + "git", |
| 35 | + ["log", "--no-merges", "--format=%H%x09%(trailers:key=Signed-off-by,valueonly)", range], |
| 36 | + cd: dir |
| 37 | + ) |
| 38 | + |
| 39 | + unsigned = |
| 40 | + out |
| 41 | + |> String.split("\n", trim: true) |
| 42 | + |> Enum.filter(fn line -> |
| 43 | + case String.split(line, "\t") do |
| 44 | + [_sha, trailer] -> String.trim(trailer) == "" |
| 45 | + [_sha] -> true |
| 46 | + _ -> false |
| 47 | + end |
| 48 | + end) |
| 49 | + |
| 50 | + if unsigned == [], do: :pass, else: :fail |
| 51 | + end |
| 52 | + |
| 53 | + defp git(dir, args), do: {_, 0} = System.cmd("git", args, cd: dir) |
| 54 | + |
| 55 | + defp repo_with_one_signed_one_not(dir) do |
| 56 | + git(dir, ["init", "-q"]) |
| 57 | + git(dir, ["config", "user.name", "Test"]) |
| 58 | + git(dir, ["config", "user.email", "test@example.com"]) |
| 59 | + # This machine signs commits globally; a scratch repository has no key and would fail at 128. |
| 60 | + git(dir, ["config", "commit.gpgsign", "false"]) |
| 61 | + |
| 62 | + git(dir, [ |
| 63 | + "commit", |
| 64 | + "-q", |
| 65 | + "--allow-empty", |
| 66 | + "-m", |
| 67 | + "base\n\nSigned-off-by: Test <test@example.com>" |
| 68 | + ]) |
| 69 | + |
| 70 | + base = String.trim(elem(System.cmd("git", ["rev-parse", "HEAD"], cd: dir), 0)) |
| 71 | + |
| 72 | + git(dir, [ |
| 73 | + "commit", |
| 74 | + "-q", |
| 75 | + "--allow-empty", |
| 76 | + "-m", |
| 77 | + "signed\n\nSigned-off-by: Test <test@example.com>" |
| 78 | + ]) |
| 79 | + |
| 80 | + git(dir, ["commit", "-q", "--allow-empty", "-m", "unsigned, no trailer at all"]) |
| 81 | + base |
| 82 | + end |
| 83 | + |
| 84 | + test "AC1: the step's current logic accepts a range containing an unsigned commit", %{ |
| 85 | + tmp_dir: dir |
| 86 | + } do |
| 87 | + base = repo_with_one_signed_one_not(dir) |
| 88 | + |
| 89 | + assert concatenated_grep(dir, "#{base}..HEAD") == :pass, |
| 90 | + "the concatenating grep rejected this range, so the premise of this slice is wrong" |
| 91 | + |
| 92 | + assert every_commit(dir, "#{base}..HEAD") == :fail, |
| 93 | + "a per-commit check should reject a range with an unsigned commit" |
| 94 | + end |
| 95 | + |
| 96 | + test "AC2: the workflow step now checks every commit, and both copies agree" do |
| 97 | + yaml = File.read!(".github/workflows/gate.yml") |
| 98 | + |
| 99 | + steps = |
| 100 | + yaml |
| 101 | + |> String.split("\n") |
| 102 | + |> Enum.chunk_every(12, 1, :discard) |
| 103 | + |> Enum.filter(fn chunk -> String.contains?(hd(chunk), "DCO sign-off") end) |
| 104 | + |
| 105 | + assert length(steps) == 2, |
| 106 | + "expected the DCO step in two legs, found #{length(steps)}" |
| 107 | + |
| 108 | + for chunk <- steps do |
| 109 | + body = Enum.join(chunk, "\n") |
| 110 | + |
| 111 | + refute body =~ "grep -q '^Signed-off-by: '", |
| 112 | + """ |
| 113 | + the step still concatenates the range and greps for one sign-off anywhere, which is \ |
| 114 | + not what its name says: |
| 115 | +
|
| 116 | + #{body} |
| 117 | + """ |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + describe "AC5: a pull request description is checked too" do |
| 122 | + test "the workflow carries the step, in both legs, and passes the body through env" do |
| 123 | + yaml = File.read!(".github/workflows/gate.yml") |
| 124 | + |
| 125 | + assert length( |
| 126 | + String.split(yaml, "No assistant attribution in the pull request description") |
| 127 | + ) - 1 == 2, |
| 128 | + "the PR-description check is not present in both legs" |
| 129 | + |
| 130 | + assert yaml =~ "PR_BODY: ${{ github.event.pull_request.body }}", |
| 131 | + "the body is not passed through env, which means it is interpolated into a shell " <> |
| 132 | + "command; a pull request description is attacker-controlled text" |
| 133 | + |
| 134 | + refute yaml =~ ~r/grep -qiE '[^']*'\s*<<<\s*"\$\{\{/, |
| 135 | + "the body is interpolated directly into the script" |
| 136 | + end |
| 137 | + |
| 138 | + test "the pattern it uses catches the shapes that were found in the wild" do |
| 139 | + # The seven descriptions on #99 to #105 carried the first of these. The others are the |
| 140 | + # trailers the commit hook already strips, included so one check knows every shape. |
| 141 | + pattern = ~r/Co-Authored-By: Claude|Claude-Session:|Generated with \[Claude/i |
| 142 | + |
| 143 | + for body <- [ |
| 144 | + "some text\n\n🤖 Generated with [Claude Code](https://claude.com/claude-code)\n", |
| 145 | + "Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>", |
| 146 | + "Claude-Session: abc123", |
| 147 | + "GENERATED WITH [CLAUDE CODE]" |
| 148 | + ] do |
| 149 | + assert Regex.match?(pattern, body), "the pattern misses: #{inspect(body)}" |
| 150 | + end |
| 151 | + |
| 152 | + for body <- [ |
| 153 | + "a normal description mentioning claude in prose", |
| 154 | + "Signed-off-by: Ayla Croft <aylacroft@proton.me>", |
| 155 | + "This slice was generated from a template" |
| 156 | + ] do |
| 157 | + refute Regex.match?(pattern, body), "the pattern over-matches: #{inspect(body)}" |
| 158 | + end |
| 159 | + end |
| 160 | + end |
| 161 | +end |
0 commit comments