| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // pushTo runs preReceive over the lines as git would hand them to the |
| 13 | // hook, as who, against repo, under a policy that protects nothing. |
| 14 | func pushTo(t *testing.T, repo repository, who string, lines []string) (int, string) { |
| 15 | t.Helper() |
| 16 | dir := t.TempDir() |
| 17 | policyFile := filepath.Join(dir, "policy.json") |
| 18 | if err := os.WriteFile(policyFile, []byte(`{"refs": [], "writers": [], "grants": {}}`), 0o644); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | var stderr bytes.Buffer |
| 22 | code := preReceive([]string{"--project", "project", "--policy", policyFile, "--principal", who}, |
| 23 | strings.NewReader(strings.Join(lines, "\n")+"\n"), &stderr, repo) |
| 24 | return code, stderr.String() |
| 25 | } |
| 26 | |
| 27 | // A push names as many refs as its sender likes. The hook keeps at most |
| 28 | // maxUpdates of them, so it asks git about at most that many. |
| 29 | func TestAPushIsBoundInTheRefsItUpdates(t *testing.T) { |
| 30 | r := newRecords(t) |
| 31 | h := r.source("one") |
| 32 | topics := func(n int) []string { |
| 33 | var lines []string |
| 34 | for i := 0; i < n; i++ { |
| 35 | lines = append(lines, fmt.Sprintf("%s %s refs/heads/topic/%04d", zeroID, h, i)) |
| 36 | } |
| 37 | return lines |
| 38 | } |
| 39 | |
| 40 | // One past the bound is refused before any ref is looked up: this |
| 41 | // repository fails the test if one is. |
| 42 | code, says := pushTo(t, unreadable{t}, "alice", topics(maxUpdates+1)) |
| 43 | if code == 0 || !strings.Contains(says, "it updates more than 1000 refs, the most one push may") { |
| 44 | t.Errorf("1001 updates: exit %d, %q", code, says) |
| 45 | } |
| 46 | if code, says := pushTo(t, gitRepository{}, "alice", topics(maxUpdates)); code != 0 { |
| 47 | t.Errorf("1000 updates: exit %d, %q", code, says) |
| 48 | } |
| 49 | |
| 50 | // A symbolic ref among many is still found, in every form git reads |
| 51 | // as one: written by git symbolic-ref, naming a ref that does not exist, |
| 52 | // "ref:" followed by a tab or by nothing, and a filesystem symlink. |
| 53 | // Each would have a push write the ref it names. |
| 54 | r.git("", "update-ref", "refs/heads/real", h) |
| 55 | r.git("", "symbolic-ref", "refs/heads/alias", "refs/heads/real") |
| 56 | r.git("", "symbolic-ref", "refs/heads/dangling", "refs/heads/main") |
| 57 | heads := filepath.Join(r.dir, "refs", "heads") |
| 58 | for name, content := range map[string]string{ |
| 59 | "tabbed": "ref:\trefs/heads/real\n", |
| 60 | "tight": "ref:refs/heads/real\n", |
| 61 | } { |
| 62 | if err := os.WriteFile(filepath.Join(heads, name), []byte(content), 0o644); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | } |
| 66 | if err := os.Symlink("refs/heads/real", filepath.Join(heads, "linked")); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | for _, alias := range []string{"alias", "dangling", "tabbed", "tight", "linked"} { |
| 70 | ref := "refs/heads/" + alias |
| 71 | lines := append(topics(maxUpdates-1), fmt.Sprintf("%s %s %s", zeroID, h, ref)) |
| 72 | code, says := pushTo(t, gitRepository{}, "alice", lines) |
| 73 | want := "valley: alice may not write " + ref + " — it is a symbolic ref in this repository, and a push to it would write the ref it points at instead" |
| 74 | if code == 0 || !strings.Contains(says, want) { |
| 75 | t.Errorf("%s among %d updates: exit %d, %q", ref, len(lines), code, says) |
| 76 | } |
| 77 | if strings.Count(says, "\n") != 1 { |
| 78 | t.Errorf("%s among %d updates: refused more than the symbolic ref: %q", ref, len(lines), says) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // The push policy is read with every key spelled exactly. Each policy |
| 84 | // below makes alice a writer of tags only under a key that does not spell |
| 85 | // its field exactly, or under a key written twice, and encoding/json alone |
| 86 | // would read her in: it matches a key to a field without regard to case, |
| 87 | // under Unicode's case folding, and merges two objects read into one map. |
| 88 | // Each refuses the whole push, naming the key. The same grant spelled |
| 89 | // exactly lets her push the tag. |
| 90 | func TestAPolicyKeyNotSpelledExactlyRefusesThePush(t *testing.T) { |
| 91 | r := newRecords(t) |
| 92 | h := r.source("one") |
| 93 | push := func(policy string) (int, string) { |
| 94 | t.Helper() |
| 95 | file := filepath.Join(t.TempDir(), "policy.json") |
| 96 | if err := os.WriteFile(file, []byte(policy), 0o644); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | var stderr bytes.Buffer |
| 100 | code := preReceive([]string{"--project", "project", "--policy", file, "--principal", "alice"}, |
| 101 | strings.NewReader(zeroID+" "+h+" refs/tags/v1\n"), &stderr, gitRepository{}) |
| 102 | return code, stderr.String() |
| 103 | } |
| 104 | const release = `{"refs": ["refs/tags/*"], "writers": ["alice"]}` |
| 105 | if code, says := push(`{"grants": {"release": ` + release + `}, "refs": [], "writers": []}`); code != 0 { |
| 106 | t.Fatalf("alice under a grant spelled exactly: exit %d, %q", code, says) |
| 107 | } |
| 108 | // The keys of each policy are in the order Nix writes them, sorted |
| 109 | // bytewise, as the host module renders the policy file. |
| 110 | for _, c := range []struct{ name, policy, says string }{ |
| 111 | {"a grant's writers spelled with a long s, after its own writers", |
| 112 | `{"grants": {"release": {"refs": ["refs/tags/*"], "writers": ["operator"], "writerſ": ["alice"]}}, "refs": [], "writers": []}`, |
| 113 | `grants["release"] holds the key "writerſ", which is its field "writers" spelled another way`}, |
| 114 | {"Grants beside an empty grants", |
| 115 | `{"Grants": {"release": ` + release + `}, "grants": {}, "refs": [], "writers": []}`, |
| 116 | `the document holds the key "Grants", which is its field "grants" spelled another way`}, |
| 117 | {"a grant written twice", |
| 118 | `{"grants": {"release": {"refs": ["refs/tags/*"], "writers": ["operator"]}, "release": ` + release + `}, "refs": [], "writers": []}`, |
| 119 | `grants holds the key "release" twice`}, |
| 120 | } { |
| 121 | code, says := push(c.policy) |
| 122 | if code == 0 || !strings.Contains(says, "its policy could not be applied") || !strings.Contains(says, c.says) { |
| 123 | t.Errorf("%s: exit %d, %q", c.name, code, says) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // git looks a missing ref up under other names: refs/, refs/tags/, |
| 129 | // refs/heads/ or refs/remotes/ followed by the name it was given. A ref |
| 130 | // named that way could stand in for a ref the valley reads, so no push |
| 131 | // may create or update one, even under a policy that makes the pusher a |
| 132 | // writer of every ref and opens every ref to her through a grant. Each |
| 133 | // may still be deleted. Names that hold refs anywhere else are ordinary. |
| 134 | func TestARefNamedLikeAnotherIsNeitherCreatedNorUpdated(t *testing.T) { |
| 135 | r := newRecords(t) |
| 136 | h, h2 := r.source("one"), r.source("two") |
| 137 | file := filepath.Join(t.TempDir(), "policy.json") |
| 138 | policy := `{"grants": {"every": {"refs": ["refs/*"], "writers": ["alice"]}}, "refs": ["refs/*"], "writers": ["alice"]}` |
| 139 | if err := os.WriteFile(file, []byte(policy), 0o644); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | push := func(old, new, ref string) (int, string) { |
| 143 | t.Helper() |
| 144 | var stderr bytes.Buffer |
| 145 | code := preReceive([]string{"--project", "project", "--policy", file, "--principal", "alice"}, |
| 146 | strings.NewReader(old+" "+new+" "+ref+"\n"), &stderr, gitRepository{}) |
| 147 | return code, stderr.String() |
| 148 | } |
| 149 | |
| 150 | for _, c := range []struct{ ref, prefix string }{ |
| 151 | {"refs/heads/refs/the-valley/knots", "refs/heads/"}, |
| 152 | {"refs/heads/refs/heads/main", "refs/heads/"}, |
| 153 | {"refs/tags/refs/heads/main", "refs/tags/"}, |
| 154 | {"refs/tags/refs/the-valley/integration-requests/main/c-1", "refs/tags/"}, |
| 155 | {"refs/refs/the-valley/signers", "refs/"}, |
| 156 | {"refs/remotes/refs/heads/registry", "refs/remotes/"}, |
| 157 | {"refs/remotes/refs/heads/main/HEAD", "refs/remotes/"}, |
| 158 | } { |
| 159 | for _, w := range []struct{ verb, old, new string }{ |
| 160 | {"create", zeroID, h}, |
| 161 | {"update", h, h2}, |
| 162 | } { |
| 163 | code, says := push(w.old, w.new, c.ref) |
| 164 | want := fmt.Sprintf("valley: alice may not %s %s — no push may create or update a ref named %s followed by a full ref name: when %s is missing", |
| 165 | w.verb, c.ref, c.prefix, strings.TrimPrefix(c.ref, c.prefix)) |
| 166 | if code == 0 || !strings.Contains(says, want) { |
| 167 | t.Errorf("%s %s: exit %d, %q", w.verb, c.ref, code, says) |
| 168 | } |
| 169 | } |
| 170 | if code, says := push(h, zeroID, c.ref); code != 0 { |
| 171 | t.Errorf("delete %s: exit %d, %q", c.ref, code, says) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | for _, ref := range []string{ |
| 176 | "refs/heads/fix-refs", |
| 177 | "refs/heads/a/refs/b", |
| 178 | "refs/heads/refs", |
| 179 | "refs/heads/refsx/y", |
| 180 | "refs/tags/a/refs/heads/main", |
| 181 | "refs/tags/refs", |
| 182 | "refs/remotes/origin/main", |
| 183 | "refs/meta/refs/heads/main", |
| 184 | } { |
| 185 | if code, says := push(zeroID, h, ref); code != 0 { |
| 186 | t.Errorf("create %s: exit %d, %q", ref, code, says) |
| 187 | } |
| 188 | if code, says := push(h, h2, ref); code != 0 { |
| 189 | t.Errorf("update %s: exit %d, %q", ref, code, says) |
| 190 | } |
| 191 | } |
| 192 | } |