| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | // gitProgram is the git this program runs. The package sets it at build |
| 15 | // time to an absolute store path, so nothing in a caller's PATH decides |
| 16 | // which git reads the repository. |
| 17 | var gitProgram = "git" |
| 18 | |
| 19 | // repository is what a decision needs to know about the repository a push |
| 20 | // is landing in. |
| 21 | type repository interface { |
| 22 | // symbolic reports whether ref is a symbolic ref there now. |
| 23 | symbolic(ref string) (bool, error) |
| 24 | // notes returns every blob under the tree id names, keyed by path, |
| 25 | // or an error saying why id is not such a tree. |
| 26 | notes(id string) (map[string][]byte, error) |
| 27 | } |
| 28 | |
| 29 | // The bounds on what an attestation ref may hold. A statement is a few |
| 30 | // hundred bytes and a ref holds one note per check, so these are far above |
| 31 | // anything attest writes, and they keep a hostile push from making the |
| 32 | // hook read without limit. |
| 33 | const ( |
| 34 | maxNotes = 256 |
| 35 | maxNoteSize = 64 << 10 |
| 36 | ) |
| 37 | |
| 38 | // gitRepository reads the repository the hook runs in. receive-pack runs |
| 39 | // the hook inside the repository, with the pushed objects in a quarantine |
| 40 | // directory its environment names, so the objects a push brings are |
| 41 | // readable here before any ref points at them. |
| 42 | type gitRepository struct{} |
| 43 | |
| 44 | func (gitRepository) symbolic(ref string) (bool, error) { |
| 45 | _, err := run(nil, "symbolic-ref", "--quiet", ref) |
| 46 | var exit *exec.ExitError |
| 47 | switch { |
| 48 | case err == nil: |
| 49 | return true, nil |
| 50 | case errors.As(err, &exit) && exit.ExitCode() == 1: |
| 51 | // Not a symbolic ref, or no ref at all. |
| 52 | return false, nil |
| 53 | default: |
| 54 | return false, err |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func (gitRepository) notes(id string) (map[string][]byte, error) { |
| 59 | kind, err := run(nil, "cat-file", "-t", id) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | if kind := strings.TrimSpace(string(kind)); kind != "tree" { |
| 64 | return nil, fmt.Errorf("it points at a %s, and an attestation ref points at a tree of notes", kind) |
| 65 | } |
| 66 | listing, err := run(nil, "ls-tree", "-r", "-z", "--full-tree", id) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | entries := strings.Split(strings.TrimSuffix(string(listing), "\x00"), "\x00") |
| 71 | if len(entries) == 1 && entries[0] == "" { |
| 72 | return nil, errors.New("it points at an empty tree, and an attestation holds at least one note") |
| 73 | } |
| 74 | if len(entries) > maxNotes { |
| 75 | return nil, fmt.Errorf("its tree holds %d entries, more than the %d an attestation may", len(entries), maxNotes) |
| 76 | } |
| 77 | var paths, oids []string |
| 78 | for _, entry := range entries { |
| 79 | meta, path, ok := strings.Cut(entry, "\t") |
| 80 | fields := strings.Fields(meta) |
| 81 | if !ok || len(fields) != 3 || fields[0] != "100644" || fields[1] != "blob" { |
| 82 | return nil, fmt.Errorf("its tree holds %q, and an attestation holds only notes, as plain files", entry) |
| 83 | } |
| 84 | paths = append(paths, path) |
| 85 | oids = append(oids, fields[2]) |
| 86 | } |
| 87 | |
| 88 | // Every size is checked before any note is read, and then one |
| 89 | // cat-file serves them all. |
| 90 | ids := []byte(strings.Join(oids, "\n") + "\n") |
| 91 | sizes, err := run(ids, "cat-file", "--batch-check=%(objectsize)") |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | for i, line := range strings.Fields(string(sizes)) { |
| 96 | if size, err := strconv.Atoi(line); err != nil || size > maxNoteSize { |
| 97 | return nil, fmt.Errorf("%s holds %s bytes, more than the %d a note may", paths[i], line, maxNoteSize) |
| 98 | } |
| 99 | } |
| 100 | out, err := run(ids, "cat-file", "--batch") |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | found := map[string][]byte{} |
| 105 | for i := range oids { |
| 106 | header, rest, ok := bytes.Cut(out, []byte("\n")) |
| 107 | fields := strings.Fields(string(header)) |
| 108 | if !ok || len(fields) != 3 || fields[1] != "blob" { |
| 109 | return nil, fmt.Errorf("reading %s: git answered %q", paths[i], header) |
| 110 | } |
| 111 | size, err := strconv.Atoi(fields[2]) |
| 112 | if err != nil || size+1 > len(rest) { |
| 113 | return nil, fmt.Errorf("reading %s: git answered %q", paths[i], header) |
| 114 | } |
| 115 | found[paths[i]] = rest[:size] |
| 116 | out = rest[size+1:] |
| 117 | } |
| 118 | return found, nil |
| 119 | } |
| 120 | |
| 121 | // run runs git in the repository and returns its stdout. |
| 122 | // |
| 123 | // Replacement refs are off: a replacement could make the hook read some |
| 124 | // other object than the one pushed. The rest of git's environment is the |
| 125 | // one receive-pack gave the hook, which the client has no way to set, less |
| 126 | // any GIT_ variable but the four that say where this repository and the |
| 127 | // pushed objects are. System and user configuration are not read. |
| 128 | func run(stdin []byte, args ...string) ([]byte, error) { |
| 129 | cmd := exec.Command(gitProgram, append([]string{"--no-replace-objects"}, args...)...) |
| 130 | cmd.Env = gitEnvironment(os.Environ()) |
| 131 | if stdin != nil { |
| 132 | cmd.Stdin = bytes.NewReader(stdin) |
| 133 | } |
| 134 | var stderr bytes.Buffer |
| 135 | cmd.Stderr = &stderr |
| 136 | out, err := cmd.Output() |
| 137 | if err != nil { |
| 138 | if msg := strings.TrimSpace(stderr.String()); msg != "" { |
| 139 | return nil, fmt.Errorf("git %s: %w: %s", args[0], err, msg) |
| 140 | } |
| 141 | return nil, fmt.Errorf("git %s: %w", args[0], err) |
| 142 | } |
| 143 | return out, nil |
| 144 | } |
| 145 | |
| 146 | // kept are the GIT_ variables receive-pack sets for a hook and that the |
| 147 | // hook's own reads need: the repository, and the quarantine the pushed |
| 148 | // objects sit in until the push is accepted. |
| 149 | var kept = map[string]bool{ |
| 150 | "GIT_DIR": true, |
| 151 | "GIT_OBJECT_DIRECTORY": true, |
| 152 | "GIT_ALTERNATE_OBJECT_DIRECTORIES": true, |
| 153 | "GIT_QUARANTINE_PATH": true, |
| 154 | } |
| 155 | |
| 156 | func gitEnvironment(inherited []string) []string { |
| 157 | var env []string |
| 158 | for _, kv := range inherited { |
| 159 | name, _, _ := strings.Cut(kv, "=") |
| 160 | if strings.HasPrefix(name, "GIT_") && !kept[name] { |
| 161 | continue |
| 162 | } |
| 163 | env = append(env, kv) |
| 164 | } |
| 165 | return append(env, |
| 166 | "GIT_CONFIG_NOSYSTEM=1", |
| 167 | "GIT_CONFIG_SYSTEM=/dev/null", |
| 168 | "GIT_CONFIG_GLOBAL=/dev/null", |
| 169 | "GIT_NO_REPLACE_OBJECTS=1", |
| 170 | "GIT_TERMINAL_PROMPT=0", |
| 171 | "LC_ALL=C", |
| 172 | ) |
| 173 | } |
| 174 | |
| 175 | func sortedKeys[V any](m map[string]V) []string { |
| 176 | keys := make([]string, 0, len(m)) |
| 177 | for k := range m { |
| 178 | keys = append(keys, k) |
| 179 | } |
| 180 | sort.Strings(keys) |
| 181 | return keys |
| 182 | } |