| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // The patterns are documented as matching the full refname with `*` |
| 10 | // crossing path separators, and the hook they replace matched them as |
| 11 | // shell case patterns. A matcher is wrong exactly at the stars, so the |
| 12 | // stars are what is pinned. |
| 13 | func TestAPatternMatchesTheWholeRefnameAndItsStarsCrossSeparators(t *testing.T) { |
| 14 | for _, c := range []struct { |
| 15 | pattern, ref string |
| 16 | want bool |
| 17 | }{ |
| 18 | {"refs/heads/main", "refs/heads/main", true}, |
| 19 | {"refs/heads/main", "refs/heads/main2", false}, |
| 20 | {"refs/heads/main", "refs/heads/mai", false}, |
| 21 | {"refs/heads/release/*", "refs/heads/release/1.0", true}, |
| 22 | {"refs/heads/release/*", "refs/heads/release/1/hotfix", true}, |
| 23 | {"refs/heads/release/*", "refs/heads/release/", true}, |
| 24 | {"refs/heads/release/*", "refs/heads/releases/1", false}, |
| 25 | {"refs/tags/release/*/v*", "refs/tags/release/cosmo/v1", true}, |
| 26 | {"refs/tags/release/*/v*", "refs/tags/release/cosmo/nightly", false}, |
| 27 | {"refs/*/main", "refs/heads/x/main", true}, |
| 28 | {"refs/*/main", "refs/heads/x/main/y", false}, |
| 29 | {"refs/heads/*a*b", "refs/heads/aab", true}, |
| 30 | {"refs/heads/*a*b", "refs/heads/aaba", false}, |
| 31 | } { |
| 32 | if got := match(c.pattern, c.ref); got != c.want { |
| 33 | t.Errorf("match(%q, %q) = %v, want %v", c.pattern, c.ref, got, c.want) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // fakeRepository holds the symbolic refs of a repository and no objects. |
| 39 | type fakeRepository struct{ symrefs map[string]bool } |
| 40 | |
| 41 | func (f fakeRepository) symbolic(ref string) (bool, error) { return f.symrefs[ref], nil } |
| 42 | func (f fakeRepository) notes(string) (map[string][]byte, error) { |
| 43 | return nil, errors.New("holds no objects") |
| 44 | } |
| 45 | |
| 46 | // Every rule that applies to a ref has to allow the write, and none of |
| 47 | // them can be satisfied by another. Each case here is a declaration that |
| 48 | // tries to stand in for a rule it is not: a writer list covering a |
| 49 | // namespace whose rule is a grant or a prohibition, a grant covering one |
| 50 | // whose rule is fixed, and a ref name standing for another ref. |
| 51 | func TestNoRuleStandsInForAnother(t *testing.T) { |
| 52 | const zero = "0000000000000000000000000000000000000000" |
| 53 | const some = "1111111111111111111111111111111111111111" |
| 54 | p := policy{ |
| 55 | project: "everything", |
| 56 | push: pushPolicy{ |
| 57 | // Every ref the project has is protected, and the writer is |
| 58 | // also opened every ref by a grant. |
| 59 | Refs: []string{"refs/*"}, |
| 60 | Writers: []string{"writer"}, |
| 61 | Grants: map[string]refGrant{ |
| 62 | "everything": {Refs: []string{"refs/*"}, Writers: []string{"writer", "opened"}}, |
| 63 | }, |
| 64 | }, |
| 65 | grants: grants{"requester": {"request": true}}, |
| 66 | repo: fakeRepository{symrefs: map[string]bool{"refs/heads/alias": true}}, |
| 67 | } |
| 68 | for _, c := range []struct { |
| 69 | who, ref string |
| 70 | allowed bool |
| 71 | says string |
| 72 | }{ |
| 73 | // Prohibited outright, to the writer the grant and the protection |
| 74 | // both name. |
| 75 | {"writer", "refs/replace/" + some, false, "refs/replace/ is closed to every push"}, |
| 76 | {"writer", "refs/notes/commits", false, "refs/notes/ is closed to every push"}, |
| 77 | {"writer", "refs/the-valley/integration-outcomes/main/c", false, "written only by the machinery"}, |
| 78 | // A symbolic ref is refused whoever writes it. |
| 79 | {"writer", "refs/heads/alias", false, "symbolic ref"}, |
| 80 | // The request grant is needed whatever else is declared, and |
| 81 | // protection is needed on top of it. |
| 82 | {"writer", "refs/the-valley/integration-requests/main/c", false, "request grant"}, |
| 83 | {"requester", "refs/the-valley/integration-requests/main/c", false, "a protected ref"}, |
| 84 | // A tag takes a grant, and protection on top: being opened the |
| 85 | // ref does not make a principal its writer. |
| 86 | {"writer", "refs/tags/v1", true, ""}, |
| 87 | {"opened", "refs/tags/v1", false, "a protected ref"}, |
| 88 | {"requester", "refs/tags/v1", false, "opens only through a named grant"}, |
| 89 | // An attestation named for a digest alone is refused before |
| 90 | // anything is read: it would block every ref beneath it. |
| 91 | {"writer", "refs/the-valley/attestations/" + strings.Repeat("a", 64), false, "<digest>/<key hash>"}, |
| 92 | } { |
| 93 | got := p.decide(c.who, update{old: zero, new: some, ref: c.ref}) |
| 94 | if (got == "") != c.allowed || !strings.Contains(got, c.says) { |
| 95 | t.Errorf("%s writing %s: got %q, want allowed=%v saying %q", c.who, c.ref, got, c.allowed, c.says) |
| 96 | } |
| 97 | } |
| 98 | } |