| 1 | package approval |
| 2 | |
| 3 | // The authority stamp: the first line of each approval signers file the |
| 4 | // identity compiler writes, saying which registry the keys below it were |
| 5 | // compiled from and for which day. |
| 6 | // |
| 7 | // # valley-approval-authority-v1 ref=refs/heads/main commit=5f0c… day=2026-11-16 |
| 8 | // |
| 9 | // ref is the full ref name the registry was read at, commit the commit |
| 10 | // that ref held when the compiler resolved it, and day the UTC day expiry |
| 11 | // was judged against. Every key in the file is read from that one commit |
| 12 | // and judged on that one day, so the stamp says exactly what the file is a |
| 13 | // rendering of. A file with no commit to name says ref=none commit=none. |
| 14 | // |
| 15 | // The line is a comment to the note format, so every reader of verifier |
| 16 | // keys reads a stamped file as it reads the keys alone |
| 17 | // (note.ParseVerifierKeys). |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "strings" |
| 25 | "time" |
| 26 | |
| 27 | "the-valley/change" |
| 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | // AuthorityStamp opens the stamp line, version included. |
| 32 | AuthorityStamp = "# valley-approval-authority-v1 " |
| 33 | |
| 34 | // authorityFamily opens every version's stamp, so a later version's |
| 35 | // line is told from a comment and refused as unsupported. |
| 36 | authorityFamily = "# valley-approval-authority-" |
| 37 | |
| 38 | // MaxStamp is the most bytes a stamp line holds, its newline |
| 39 | // included. A reader of the stamp reads no more of a file than this, |
| 40 | // however large the file or its first line. |
| 41 | MaxStamp = 512 |
| 42 | |
| 43 | // maxEmptyReads is how many reads in a row may give nothing before a |
| 44 | // reader of the stamp gives up, as bufio's readers do. |
| 45 | maxEmptyReads = 100 |
| 46 | |
| 47 | // noProvenance stands for the ref and the commit of a file compiled |
| 48 | // from no commit. |
| 49 | noProvenance = "none" |
| 50 | |
| 51 | dayLayout = "2006-01-02" |
| 52 | ) |
| 53 | |
| 54 | // Authority is what a stamp says. |
| 55 | type Authority struct { |
| 56 | // Ref is the full ref name the registry was read at, and Commit the |
| 57 | // full object ID of the commit it held. Both are "" for a file with |
| 58 | // no commit provenance. |
| 59 | Ref, Commit string |
| 60 | |
| 61 | // Day is the UTC day the compilation judged expiry on, at midnight. |
| 62 | Day time.Time |
| 63 | } |
| 64 | |
| 65 | // Stamp writes the stamp line, closing newline included. It refuses an |
| 66 | // Authority ReadAuthority would not read back. |
| 67 | func (a Authority) Stamp() (string, error) { |
| 68 | ref, commit := a.Ref, a.Commit |
| 69 | if ref == "" && commit == "" { |
| 70 | ref, commit = noProvenance, noProvenance |
| 71 | } |
| 72 | line := fmt.Sprintf("%sref=%s commit=%s day=%s\n", AuthorityStamp, ref, commit, a.Day.UTC().Format(dayLayout)) |
| 73 | if len(line) > MaxStamp { |
| 74 | return "", fmt.Errorf("the authority stamp would be %d bytes, more than the %d a stamp holds", len(line), MaxStamp) |
| 75 | } |
| 76 | got, err := parseStamp(strings.TrimSuffix(line, "\n")) |
| 77 | if err != nil { |
| 78 | return "", err |
| 79 | } |
| 80 | if !got.Day.Equal(a.Day) { |
| 81 | return "", fmt.Errorf("the day %s is not a UTC midnight", a.Day.Format(time.RFC3339)) |
| 82 | } |
| 83 | return line, nil |
| 84 | } |
| 85 | |
| 86 | // ReadAuthority reads the stamp of an approval signers file from r: its |
| 87 | // first line, closed by a newline. It reads one byte at a time and stops |
| 88 | // at the newline, so r holds the rest of the file afterwards, unread. It |
| 89 | // reads at most MaxStamp bytes, refusing a first line longer than that |
| 90 | // without reading on. A file with no stamp, a later version's stamp, and a |
| 91 | // stamp that does not read are refused. The stamp is the first line, and |
| 92 | // a later line opening like one is a comment to every reader, this one |
| 93 | // included. |
| 94 | func ReadAuthority(r io.Reader) (Authority, error) { |
| 95 | head := make([]byte, 0, MaxStamp) |
| 96 | one := make([]byte, 1) |
| 97 | for empty := 0; len(head) < MaxStamp; { |
| 98 | n, err := r.Read(one) |
| 99 | head = append(head, one[:n]...) |
| 100 | if n == 0 && err == nil { |
| 101 | // A reader that keeps giving nothing is given up on, as the io |
| 102 | // readers that wrap another give up on one. |
| 103 | if empty++; empty == maxEmptyReads { |
| 104 | return Authority{}, io.ErrNoProgress |
| 105 | } |
| 106 | continue |
| 107 | } |
| 108 | empty = 0 |
| 109 | if n == 1 && one[0] == '\n' { |
| 110 | break |
| 111 | } else if errors.Is(err, io.EOF) { |
| 112 | break |
| 113 | } else if err != nil { |
| 114 | return Authority{}, err |
| 115 | } |
| 116 | } |
| 117 | first, _, closed := bytes.Cut(head, []byte("\n")) |
| 118 | line := string(first) |
| 119 | switch { |
| 120 | case !strings.HasPrefix(line, authorityFamily): |
| 121 | return Authority{}, fmt.Errorf("the file does not open with an authority stamp") |
| 122 | case !strings.HasPrefix(line, AuthorityStamp): |
| 123 | return Authority{}, fmt.Errorf("the authority stamp %q is not a version this program reads", line) |
| 124 | case !closed && len(head) == MaxStamp: |
| 125 | return Authority{}, fmt.Errorf("the file's first line is longer than the %d bytes an authority stamp holds", MaxStamp) |
| 126 | case !closed: |
| 127 | return Authority{}, fmt.Errorf("the authority stamp is not closed by a newline") |
| 128 | } |
| 129 | return parseStamp(line) |
| 130 | } |
| 131 | |
| 132 | // parseStamp reads one stamp line, newline removed: the prefix, then |
| 133 | // ref=, commit= and day=, in that order, one space apart. |
| 134 | func parseStamp(line string) (Authority, error) { |
| 135 | fields := strings.Split(strings.TrimPrefix(line, AuthorityStamp), " ") |
| 136 | var values [3]string |
| 137 | for i, key := range []string{"ref=", "commit=", "day="} { |
| 138 | if len(fields) != 3 || !strings.HasPrefix(fields[i], key) { |
| 139 | return Authority{}, fmt.Errorf("the authority stamp %q is not ref=, commit= and day=, one space apart", line) |
| 140 | } |
| 141 | values[i] = strings.TrimPrefix(fields[i], key) |
| 142 | } |
| 143 | ref, commit, day := values[0], values[1], values[2] |
| 144 | |
| 145 | var a Authority |
| 146 | switch { |
| 147 | case ref == noProvenance && commit == noProvenance: |
| 148 | case ref == noProvenance || commit == noProvenance: |
| 149 | return Authority{}, fmt.Errorf("the authority stamp names a ref or a commit without the other") |
| 150 | case !fullRef(ref): |
| 151 | return Authority{}, fmt.Errorf("the authority stamp's ref %q is not a full ref name", ref) |
| 152 | case !isHex(commit, 40) && !isHex(commit, 64): |
| 153 | return Authority{}, fmt.Errorf("the authority stamp's commit %q is not a full lowercase object ID", commit) |
| 154 | default: |
| 155 | a.Ref, a.Commit = ref, commit |
| 156 | } |
| 157 | parsed, err := time.Parse(dayLayout, day) |
| 158 | if err != nil || parsed.Format(dayLayout) != day { |
| 159 | return Authority{}, fmt.Errorf("the authority stamp's day %q is not a calendar day", day) |
| 160 | } |
| 161 | a.Day = parsed |
| 162 | return a, nil |
| 163 | } |
| 164 | |
| 165 | // fullRef reports whether ref is a full ref name git accepts: refs/, then |
| 166 | // a name the branch rule accepts (change.ValidBranch). |
| 167 | func fullRef(ref string) bool { |
| 168 | rest, ok := strings.CutPrefix(ref, "refs/") |
| 169 | return ok && change.ValidBranch(rest) |
| 170 | } |
| 171 | |
| 172 | // StripAuthority is file without its stamp line: the first line, when it |
| 173 | // opens with AuthorityStamp and ends within MaxStamp bytes, up to and |
| 174 | // including its newline. Every other byte is kept. A file that does not |
| 175 | // open with the stamp, or whose first line is longer than a stamp, is |
| 176 | // returned as it is, and so no more than MaxStamp bytes are looked at. |
| 177 | func StripAuthority(file []byte) []byte { |
| 178 | if !bytes.HasPrefix(file, []byte(AuthorityStamp)) { |
| 179 | return file |
| 180 | } |
| 181 | head := file[:min(len(file), MaxStamp)] |
| 182 | end := bytes.IndexByte(head, '\n') |
| 183 | switch { |
| 184 | case end >= 0: |
| 185 | return file[end+1:] |
| 186 | case len(file) <= MaxStamp: |
| 187 | return nil |
| 188 | } |
| 189 | return file |
| 190 | } |
| 191 | |
| 192 | // Fresh says whether the file the stamp opens is a rendering of the |
| 193 | // registry as it is now: current is the commit the stamp's Ref holds now, |
| 194 | // in the repository the registry is read from, and now is the time. |
| 195 | // It is fresh when its commit is current and its day is now's UTC day. A |
| 196 | // file from another day is stale whatever its commit, because expiry was |
| 197 | // judged on that day. A file with no commit provenance is never fresh. |
| 198 | func (a Authority) Fresh(current string, now time.Time) error { |
| 199 | today := now.UTC().Format(dayLayout) |
| 200 | switch { |
| 201 | case a.Commit == "": |
| 202 | return fmt.Errorf("the approval keys name no registry commit") |
| 203 | case current != a.Commit: |
| 204 | return fmt.Errorf("the approval keys were compiled from %s at %s, which holds %s now", a.Ref, a.Commit, current) |
| 205 | case a.Day.Format(dayLayout) != today: |
| 206 | return fmt.Errorf("the approval keys were compiled for %s, and today is %s", a.Day.Format(dayLayout), today) |
| 207 | } |
| 208 | return nil |
| 209 | } |