| 1 | package main |
| 2 | |
| 3 | // The content-addressing scheme the subject of an attestation is named by. |
| 4 | // |
| 5 | // valley-tree-v1 is SHA-256 over a canonical manifest of a tree's entries. |
| 6 | // The manifest is |
| 7 | // |
| 8 | // "valley-tree-v1\n" |
| 9 | // for each entry, sorted by path bytes ascending: |
| 10 | // "<mode> <sha256 of content, hex> <byte length of path> <path>\n" |
| 11 | // |
| 12 | // The path's byte length is written out, so the trailing newline is a |
| 13 | // delimiter no path can forge. Nothing outside the tree enters the |
| 14 | // manifest: no commit, no author, no time, no store path, no directory |
| 15 | // entry (a directory is implied by the paths under it, and an empty one is |
| 16 | // not a thing a tree can hold). Mode is git's: 100644, 100755, or 120000 |
| 17 | // for a symlink, whose content is its target. |
| 18 | // |
| 19 | // valley-inputs-v1 is SHA-256 over the same shape of manifest for a list |
| 20 | // of opaque names — a pure check's input paths: |
| 21 | // |
| 22 | // "valley-inputs-v1\n" |
| 23 | // for each name, sorted and deduplicated: |
| 24 | // "<byte length of name> <name>\n" |
| 25 | |
| 26 | import ( |
| 27 | "bytes" |
| 28 | "crypto/sha256" |
| 29 | "encoding/hex" |
| 30 | "fmt" |
| 31 | "io/fs" |
| 32 | "os" |
| 33 | "path/filepath" |
| 34 | "sort" |
| 35 | "strconv" |
| 36 | ) |
| 37 | |
| 38 | const ( |
| 39 | treeScheme = "valley-tree-v1" |
| 40 | inputsScheme = "valley-inputs-v1" |
| 41 | |
| 42 | modeFile = "100644" |
| 43 | modeExec = "100755" |
| 44 | modeSymlink = "120000" |
| 45 | ) |
| 46 | |
| 47 | // entry is one member of a tree: its git mode, its path relative to the |
| 48 | // tree root, and the SHA-256 of its content. |
| 49 | type entry struct { |
| 50 | mode string |
| 51 | path string |
| 52 | sum [32]byte |
| 53 | } |
| 54 | |
| 55 | // treeManifest renders the canonical bytes digested by valley-tree-v1. |
| 56 | // Exposed separately from treeDigest so a test can read what is hashed. |
| 57 | func treeManifest(entries []entry) []byte { |
| 58 | sorted := append([]entry(nil), entries...) |
| 59 | sort.Slice(sorted, func(i, j int) bool { return sorted[i].path < sorted[j].path }) |
| 60 | |
| 61 | var b bytes.Buffer |
| 62 | b.WriteString(treeScheme + "\n") |
| 63 | for _, e := range sorted { |
| 64 | b.WriteString(e.mode) |
| 65 | b.WriteByte(' ') |
| 66 | b.WriteString(hex.EncodeToString(e.sum[:])) |
| 67 | b.WriteByte(' ') |
| 68 | b.WriteString(strconv.Itoa(len(e.path))) |
| 69 | b.WriteByte(' ') |
| 70 | b.WriteString(e.path) |
| 71 | b.WriteByte('\n') |
| 72 | } |
| 73 | return b.Bytes() |
| 74 | } |
| 75 | |
| 76 | func treeDigest(entries []entry) string { |
| 77 | return hex.EncodeToString(sha256sum(treeManifest(entries))) |
| 78 | } |
| 79 | |
| 80 | func inputsManifest(names []string) []byte { |
| 81 | sorted := append([]string(nil), names...) |
| 82 | sort.Strings(sorted) |
| 83 | |
| 84 | var b bytes.Buffer |
| 85 | b.WriteString(inputsScheme + "\n") |
| 86 | var last string |
| 87 | for i, n := range sorted { |
| 88 | if i > 0 && n == last { |
| 89 | continue |
| 90 | } |
| 91 | last = n |
| 92 | b.WriteString(strconv.Itoa(len(n))) |
| 93 | b.WriteByte(' ') |
| 94 | b.WriteString(n) |
| 95 | b.WriteByte('\n') |
| 96 | } |
| 97 | return b.Bytes() |
| 98 | } |
| 99 | |
| 100 | func inputsDigest(names []string) string { |
| 101 | return hex.EncodeToString(sha256sum(inputsManifest(names))) |
| 102 | } |
| 103 | |
| 104 | func sha256sum(b []byte) []byte { |
| 105 | sum := sha256.Sum256(b) |
| 106 | return sum[:] |
| 107 | } |
| 108 | |
| 109 | // gitTreeEntries reads the entries of a revision's tree. The revision's |
| 110 | // tree, never the working tree: the subject of an attestation must be a |
| 111 | // function of committed content alone. |
| 112 | func gitTreeEntries(repo, rev string) ([]entry, error) { |
| 113 | out, err := git(repo, "ls-tree", "-r", "-z", "--full-tree", rev) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | var entries []entry |
| 118 | var oids []string |
| 119 | for _, rec := range bytes.Split([]byte(out), []byte{0}) { |
| 120 | if len(rec) == 0 { |
| 121 | continue |
| 122 | } |
| 123 | // "<mode> <type> <oid>\t<path>" |
| 124 | tab := bytes.IndexByte(rec, '\t') |
| 125 | if tab < 0 { |
| 126 | return nil, fmt.Errorf("ls-tree: no path in %q", rec) |
| 127 | } |
| 128 | fields := bytes.Fields(rec[:tab]) |
| 129 | if len(fields) != 3 { |
| 130 | return nil, fmt.Errorf("ls-tree: malformed record %q", rec[:tab]) |
| 131 | } |
| 132 | mode, path := string(fields[0]), string(rec[tab+1:]) |
| 133 | switch mode { |
| 134 | case modeFile, modeExec, modeSymlink: |
| 135 | case "160000": |
| 136 | return nil, fmt.Errorf("%s is a submodule; valley-tree-v1 does not name trees that reach outside themselves", path) |
| 137 | default: |
| 138 | return nil, fmt.Errorf("%s has unsupported mode %s", path, mode) |
| 139 | } |
| 140 | entries = append(entries, entry{mode: mode, path: path}) |
| 141 | oids = append(oids, string(fields[2])) |
| 142 | } |
| 143 | sums, err := gitBlobSums(repo, oids) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | for i := range entries { |
| 148 | entries[i].sum = sums[i] |
| 149 | } |
| 150 | return entries, nil |
| 151 | } |
| 152 | |
| 153 | // fsEntries reads the entries of a directory on disk — a check's build |
| 154 | // output. A root that is not a directory is one entry at the empty path, |
| 155 | // which is what a check that writes a single file to $out produces. |
| 156 | func fsEntries(root string) ([]entry, error) { |
| 157 | info, err := os.Lstat(root) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | if !info.IsDir() { |
| 162 | e, err := fsEntry(root, "", info) |
| 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | return []entry{e}, nil |
| 167 | } |
| 168 | var entries []entry |
| 169 | err = filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error { |
| 170 | if err != nil || d.IsDir() { |
| 171 | return err |
| 172 | } |
| 173 | rel, err := filepath.Rel(root, p) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | info, err := d.Info() |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | e, err := fsEntry(p, filepath.ToSlash(rel), info) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | entries = append(entries, e) |
| 186 | return nil |
| 187 | }) |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | return entries, nil |
| 192 | } |
| 193 | |
| 194 | func fsEntry(p, rel string, info fs.FileInfo) (entry, error) { |
| 195 | switch { |
| 196 | case info.Mode()&fs.ModeSymlink != 0: |
| 197 | target, err := os.Readlink(p) |
| 198 | if err != nil { |
| 199 | return entry{}, err |
| 200 | } |
| 201 | return entry{mode: modeSymlink, path: rel, sum: sha256.Sum256([]byte(target))}, nil |
| 202 | case info.Mode().IsRegular(): |
| 203 | content, err := os.ReadFile(p) |
| 204 | if err != nil { |
| 205 | return entry{}, err |
| 206 | } |
| 207 | mode := modeFile |
| 208 | if info.Mode().Perm()&0o111 != 0 { |
| 209 | mode = modeExec |
| 210 | } |
| 211 | return entry{mode: mode, path: rel, sum: sha256.Sum256(content)}, nil |
| 212 | default: |
| 213 | return entry{}, fmt.Errorf("%s is neither a regular file nor a symlink", p) |
| 214 | } |
| 215 | } |