| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // Canonical form and the tree digest are the two pieces of genuinely |
| 12 | // tricky logic here: they decide which bytes get signed and what a subject |
| 13 | // is, and both must agree byte for byte with any other implementation. |
| 14 | // Everything else in this program is exercised end to end by the flake |
| 15 | // checks against the real binary. |
| 16 | |
| 17 | func TestCanonicalJSONSortsAndTightens(t *testing.T) { |
| 18 | got, err := canonicalJSON(map[string]any{ |
| 19 | "b": "two", |
| 20 | "a": map[string]any{"z": "last", "y": []any{"1", "2"}}, |
| 21 | }) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | want := `{"a":{"y":["1","2"],"z":"last"},"b":"two"}` |
| 26 | if string(got) != want { |
| 27 | t.Errorf("canonicalJSON =\n %s\nwant\n %s", got, want) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestCanonicalJSONEscaping(t *testing.T) { |
| 32 | // Go's encoding/json escapes <, > and & by default, and escapes U+2028 |
| 33 | // and U+2029 for JavaScript's benefit whatever the HTML setting. RFC |
| 34 | // 8785 escapes none of them, and a signer that did would not match one |
| 35 | // that did not. The whole escape table is pinned by the conformance |
| 36 | // vectors; these are the characters an encoder is most likely to |
| 37 | // disagree about. |
| 38 | got, err := canonicalJSON(map[string]any{"k": "a<b>c&d \"q\" \\ \b\f\n ü\u2028\u2029"}) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | want := `{"k":"a<b>c&d \"q\" \\ \b\f\n ü` + "\u2028\u2029" + `"}` |
| 43 | if string(got) != want { |
| 44 | t.Errorf("canonicalJSON =\n %s\nwant\n %s", got, want) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestCanonicalJSONRejectsUnorderableKeys(t *testing.T) { |
| 49 | // Member order is by UTF-8 bytes here and by UTF-16 code units in RFC |
| 50 | // 8785. They agree over ASCII, so a key above it is where two |
| 51 | // implementations would sign different bytes over one statement. |
| 52 | // schema/attestation.cue keeps such a key out; this refuses one that |
| 53 | // arrives anyway rather than sorting it. |
| 54 | if _, err := canonicalJSON(map[string]any{"café": "x"}); err == nil { |
| 55 | t.Fatal("canonicalJSON accepted a non-ASCII key") |
| 56 | } |
| 57 | if _, err := canonicalJSON(map[string]any{"a-b": "x", "": "y", "~": "z"}); err != nil { |
| 58 | t.Fatalf("canonicalJSON refused printable ASCII keys: %v", err) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestCanonicalJSONRejectsInvalidUTF8(t *testing.T) { |
| 63 | // A string that is not valid UTF-8 has no canonical form, and |
| 64 | // encoding/json would substitute U+FFFD for it — a silent change to the |
| 65 | // bytes a signature covers. |
| 66 | if _, err := canonicalJSON(map[string]any{"k": "\xff"}); err == nil { |
| 67 | t.Fatal("canonicalJSON accepted invalid utf-8") |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestCanonicalJSONRejectsNumbers(t *testing.T) { |
| 72 | // A statement carries no numbers, so the canonical form of one has |
| 73 | // never been decided. Refusing beats guessing. |
| 74 | if _, err := canonicalize(map[string]any{"n": 1.5}); err == nil { |
| 75 | t.Fatal("canonicalize accepted a number") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestTreeManifestIsUnambiguous(t *testing.T) { |
| 80 | // A path holding the manifest's own separators must not be able to |
| 81 | // forge an entry boundary: the byte length is what delimits it. |
| 82 | entries := []entry{ |
| 83 | {mode: modeFile, path: "b\n100644 x 1 c", sum: [32]byte{}}, |
| 84 | {mode: modeExec, path: "a", sum: [32]byte{0xff}}, |
| 85 | } |
| 86 | want := "valley-tree-v1\n" + |
| 87 | "100755 ff00000000000000000000000000000000000000000000000000000000000000 1 a\n" + |
| 88 | "100644 0000000000000000000000000000000000000000000000000000000000000000 14 b\n100644 x 1 c\n" |
| 89 | if got := string(treeManifest(entries)); got != want { |
| 90 | t.Errorf("treeManifest =\n%q\nwant\n%q", got, want) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestTreeDigestIsPinned(t *testing.T) { |
| 95 | // The digest of a fixed tree is a wire format: changing it invalidates |
| 96 | // every attestation ever made. This value may only change with the |
| 97 | // scheme's version. |
| 98 | entries := []entry{{mode: modeFile, path: "a.txt", sum: sha256of("hi\n")}} |
| 99 | const want = "74781d4ba499b9896e97f2fdd38815d5723cb7b12e448ef0ce5788a1e72c2220" |
| 100 | if got := treeDigest(entries); got != want { |
| 101 | t.Errorf("treeDigest = %s, want %s", got, want) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestInputsManifestSortsAndDeduplicates(t *testing.T) { |
| 106 | want := "valley-inputs-v1\n1 a\n1 b\n" |
| 107 | if got := string(inputsManifest([]string{"b", "a", "b"})); got != want { |
| 108 | t.Errorf("inputsManifest = %q, want %q", got, want) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // The digest of a committed tree must equal the digest of that tree |
| 113 | // written to disk — the two enumerators are the same scheme or the check |
| 114 | // output digest and the subject digest mean different things. |
| 115 | func TestGitTreeAndFilesystemAgree(t *testing.T) { |
| 116 | repo := t.TempDir() |
| 117 | run := func(args ...string) string { |
| 118 | cmd := exec.Command("git", args...) |
| 119 | cmd.Dir = repo |
| 120 | cmd.Env = append(os.Environ(), |
| 121 | "GIT_CONFIG_NOSYSTEM=1", "HOME="+t.TempDir(), |
| 122 | "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@l", |
| 123 | "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@l") |
| 124 | out, err := cmd.CombinedOutput() |
| 125 | if err != nil { |
| 126 | t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, out) |
| 127 | } |
| 128 | return strings.TrimSpace(string(out)) |
| 129 | } |
| 130 | write := func(name, content string, mode os.FileMode) { |
| 131 | p := filepath.Join(repo, name) |
| 132 | if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | if err := os.WriteFile(p, []byte(content), mode); err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | run("init", "--quiet", "-b", "main") |
| 141 | write("plain.txt", "hello\n", 0o644) |
| 142 | write("nested/dir/script.sh", "#!/bin/sh\n", 0o755) |
| 143 | if err := os.Symlink("plain.txt", filepath.Join(repo, "link")); err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | run("add", "-A") |
| 147 | run("commit", "--quiet", "-m", "seed") |
| 148 | |
| 149 | entries, err := gitTreeEntries(repo, "HEAD") |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | fromGit := treeDigest(entries) |
| 154 | |
| 155 | export := t.TempDir() |
| 156 | if err := exportTree(repo, "HEAD", export); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | fsEnts, err := fsEntries(export) |
| 160 | if err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | if fromFS := treeDigest(fsEnts); fromFS != fromGit { |
| 164 | t.Errorf("git tree digest %s != filesystem digest %s\n git: %s\n fs: %s", |
| 165 | fromGit, fromFS, treeManifest(entries), treeManifest(fsEnts)) |
| 166 | } |
| 167 | |
| 168 | // The exec bit and the symlink are the two entries a naive walk gets |
| 169 | // wrong, so pin that they are actually present. |
| 170 | modes := map[string]string{} |
| 171 | for _, e := range entries { |
| 172 | modes[e.path] = e.mode |
| 173 | } |
| 174 | for path, want := range map[string]string{ |
| 175 | "plain.txt": modeFile, |
| 176 | "nested/dir/script.sh": modeExec, |
| 177 | "link": modeSymlink, |
| 178 | } { |
| 179 | if modes[path] != want { |
| 180 | t.Errorf("%s has mode %q, want %q", path, modes[path], want) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func sha256of(s string) [32]byte { |
| 186 | var out [32]byte |
| 187 | copy(out[:], sha256sum([]byte(s))) |
| 188 | return out |
| 189 | } |