| 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 | } |