| 1 | // Package copydesk validates Copy's bounded inputs and results. It grants no authority. |
| 2 | package copydesk |
| 3 | |
| 4 | import ( |
| 5 | "bytes" |
| 6 | "crypto/sha256" |
| 7 | "encoding/hex" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "regexp" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | PassTokens = 32768 |
| 17 | TaskTokens = 98304 |
| 18 | MaxStrings = 200 |
| 19 | MaxText = 4000 |
| 20 | ) |
| 21 | |
| 22 | type Delegation struct { |
| 23 | Owner string `json:"owner"` |
| 24 | Revision int64 `json:"revision"` |
| 25 | } |
| 26 | type Literal struct { |
| 27 | Key string `json:"key"` |
| 28 | Path string `json:"path"` |
| 29 | Start int `json:"start"` |
| 30 | End int `json:"end"` |
| 31 | Old string `json:"old"` |
| 32 | Text string `json:"text"` |
| 33 | Context string `json:"context"` |
| 34 | } |
| 35 | type Extraction struct { |
| 36 | Version int `json:"version"` |
| 37 | Strings []Literal `json:"strings"` |
| 38 | } |
| 39 | type Wording struct { |
| 40 | Key string `json:"key"` |
| 41 | Text string `json:"text"` |
| 42 | Action string `json:"action"` |
| 43 | Flag string `json:"flag,omitempty"` |
| 44 | } |
| 45 | type Review struct { |
| 46 | Version int `json:"version"` |
| 47 | Strings []Wording `json:"strings"` |
| 48 | } |
| 49 | type Record struct { |
| 50 | Flag string |
| 51 | Key, Hash, Candidate, Owner string |
| 52 | Revision int64 |
| 53 | } |
| 54 | type Feedback struct { |
| 55 | SourceID string `json:"source_id"` |
| 56 | Actor string `json:"-"` |
| 57 | Key string `json:"key"` |
| 58 | Before string `json:"before"` |
| 59 | After string `json:"after"` |
| 60 | Reason string `json:"reason,omitempty"` |
| 61 | Origin string `json:"-"` |
| 62 | } |
| 63 | |
| 64 | func Hash(text string) string { h := sha256.Sum256([]byte(text)); return hex.EncodeToString(h[:]) } |
| 65 | func ShouldRun(d Delegation, hasStrings bool) bool { |
| 66 | return d.Owner != "" && d.Revision > 0 && hasStrings |
| 67 | } |
| 68 | func Accepted(s Literal, candidate string, d Delegation, records []Record) bool { |
| 69 | if !ShouldRun(d, true) { |
| 70 | return false |
| 71 | } |
| 72 | for _, r := range records { |
| 73 | if r.Key == s.Key && r.Hash == Hash(s.Text) && r.Candidate == candidate && r.Owner == d.Owner && r.Revision == d.Revision { |
| 74 | return true |
| 75 | } |
| 76 | } |
| 77 | return false |
| 78 | } |
| 79 | func CanLearn(d Delegation, f Feedback) bool { |
| 80 | return ShouldRun(d, true) && f.Actor == d.Owner && (f.Origin == "equipo" || f.Origin == "copy-review") && f.SourceID != "" && f.Before != f.After && f.Key != "" && len(f.Before)+len(f.After)+len(f.Reason) <= 400 |
| 81 | } |
| 82 | |
| 83 | var placeholders = regexp.MustCompile(`%\[[0-9]+\][+#0 -]*[0-9.]*[a-zA-Z]|%[+#0 -]*[0-9.]*[a-zA-Z]|\{\{[^{}]+\}\}|\$\{[^{}]+\}|\{[a-zA-Z_][a-zA-Z0-9_]*\}`) |
| 84 | |
| 85 | func ValidateReview(changed []Literal, r Review) ([]Wording, error) { |
| 86 | if r.Version != 1 || len(changed) > MaxStrings || len(r.Strings) != len(changed) { |
| 87 | return nil, errors.New("copy: incomplete review") |
| 88 | } |
| 89 | keys := map[string]Literal{} |
| 90 | for _, s := range changed { |
| 91 | if s.Key == "" { |
| 92 | return nil, errors.New("copy: empty key") |
| 93 | } |
| 94 | if _, ok := keys[s.Key]; ok { |
| 95 | return nil, errors.New("copy: duplicate extraction") |
| 96 | } |
| 97 | keys[s.Key] = s |
| 98 | } |
| 99 | for _, w := range r.Strings { |
| 100 | s, ok := keys[w.Key] |
| 101 | if !ok { |
| 102 | return nil, errors.New("copy: unreviewed key") |
| 103 | } |
| 104 | delete(keys, w.Key) |
| 105 | if w.Action == "question" && strings.TrimSpace(w.Flag) == "" { |
| 106 | return nil, errors.New("copy: missing meaning question") |
| 107 | } |
| 108 | if w.Action != "accept" && w.Action != "question" || len(w.Text) > MaxText || len(w.Flag) > 1000 || strings.ContainsRune(w.Text, 0) { |
| 109 | return nil, errors.New("copy: invalid wording") |
| 110 | } |
| 111 | if strings.Count(s.Text, "%") != strings.Count(w.Text, "%") { |
| 112 | return nil, errors.New("copy: changed percent placeholders") |
| 113 | } |
| 114 | if strings.Join(placeholders.FindAllString(s.Text, -1), "\x00") != strings.Join(placeholders.FindAllString(w.Text, -1), "\x00") { |
| 115 | return nil, errors.New("copy: changed placeholders") |
| 116 | } |
| 117 | } |
| 118 | return r.Strings, nil |
| 119 | } |
| 120 | func ValidateScope(before, after []byte, locations []Literal, r Review) error { |
| 121 | words, err := ValidateReview(locations, r) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | byKey := map[string]Wording{} |
| 126 | for _, w := range words { |
| 127 | byKey[w.Key] = w |
| 128 | } |
| 129 | loc := append([]Literal(nil), locations...) |
| 130 | sort.Slice(loc, func(i, j int) bool { return loc[i].Start < loc[j].Start }) |
| 131 | var expected bytes.Buffer |
| 132 | pos := 0 |
| 133 | for _, s := range loc { |
| 134 | if s.Start < pos || s.End < s.Start || s.End > len(before) || string(before[s.Start:s.End]) != s.Text { |
| 135 | return errors.New("copy: stale location") |
| 136 | } |
| 137 | w := byKey[s.Key] |
| 138 | if s.Start < 1 || s.End >= len(before) { |
| 139 | return errors.New("copy: missing literal delimiters") |
| 140 | } |
| 141 | delimiter := before[s.Start-1] |
| 142 | if before[s.End] != delimiter || !strings.ContainsRune("\"'`", rune(delimiter)) { |
| 143 | return errors.New("copy: stale literal delimiters") |
| 144 | } |
| 145 | if strings.ContainsRune(w.Text, rune(delimiter)) || strings.ContainsAny(w.Text, "\\\n\r") || strings.Contains(w.Text, "${") { |
| 146 | return errors.New("copy: unsafe literal contents") |
| 147 | } |
| 148 | if w.Action != "accept" { |
| 149 | return errors.New("copy: meaning needs answer") |
| 150 | } |
| 151 | expected.Write(before[pos:s.Start]) |
| 152 | expected.WriteString(w.Text) |
| 153 | pos = s.End |
| 154 | } |
| 155 | expected.Write(before[pos:]) |
| 156 | if !bytes.Equal(expected.Bytes(), after) { |
| 157 | return errors.New("copy: edit outside extracted literals") |
| 158 | } |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | type Input struct { |
| 163 | Version int `json:"version"` |
| 164 | Units []string `json:"units"` |
| 165 | Log string `json:"log"` |
| 166 | Truncated bool `json:"truncated"` |
| 167 | } |
| 168 | type Failure struct { |
| 169 | Unit string `json:"unit"` |
| 170 | Test string `json:"test"` |
| 171 | Code string `json:"code"` |
| 172 | Subject string `json:"subject"` |
| 173 | Category string `json:"category"` |
| 174 | } |
| 175 | type Classification struct { |
| 176 | Version int `json:"version"` |
| 177 | Complete bool `json:"complete"` |
| 178 | Failures []Failure `json:"failures"` |
| 179 | } |
| 180 | type Signature struct { |
| 181 | Version int `json:"version"` |
| 182 | Complete bool `json:"complete"` |
| 183 | Adapter string `json:"adapter"` |
| 184 | Hash string `json:"hash"` |
| 185 | Failures []Failure `json:"failures"` |
| 186 | } |
| 187 | |
| 188 | func Classify(in Input, c Classification, adapter string) Signature { |
| 189 | s := Signature{Version: 1, Adapter: adapter, Complete: in.Version == 1 && c.Version == 1 && c.Complete && !in.Truncated && adapter != "" && len(in.Units) > 0 && len(in.Units) < 20} |
| 190 | covered := map[string]bool{} |
| 191 | units := map[string]bool{} |
| 192 | for _, u := range in.Units { |
| 193 | units[u] = true |
| 194 | } |
| 195 | for _, f := range c.Failures { |
| 196 | if !units[f.Unit] || f.Code == "" || f.Test == "" { |
| 197 | s.Complete = false |
| 198 | continue |
| 199 | } |
| 200 | if f.Category != "copy" && f.Category != "bookkeeping" && f.Category != "other" { |
| 201 | f.Category = "other" |
| 202 | s.Complete = false |
| 203 | } |
| 204 | covered[f.Unit] = true |
| 205 | s.Failures = append(s.Failures, f) |
| 206 | } |
| 207 | for _, u := range in.Units { |
| 208 | if !covered[u] { |
| 209 | s.Complete = false |
| 210 | s.Failures = append(s.Failures, Failure{Unit: u, Category: "other"}) |
| 211 | } |
| 212 | } |
| 213 | sort.Slice(s.Failures, func(i, j int) bool { |
| 214 | a, _ := json.Marshal(s.Failures[i]) |
| 215 | b, _ := json.Marshal(s.Failures[j]) |
| 216 | return string(a) < string(b) |
| 217 | }) |
| 218 | raw, _ := json.Marshal(s.Failures) |
| 219 | s.Hash = Hash(string(raw)) |
| 220 | return s |
| 221 | } |
| 222 | func SameFailure(a, b Signature) bool { |
| 223 | return a.Version == 1 && b.Version == 1 && a.Complete && b.Complete && a.Adapter != "" && a.Adapter == b.Adapter && a.Hash != "" && a.Hash == b.Hash && len(a.Failures) > 0 && len(b.Failures) > 0 |
| 224 | } |
| 225 | func (s Signature) CopyOnly() bool { |
| 226 | if !s.Complete || s.Adapter == "" || len(s.Failures) == 0 { |
| 227 | return false |
| 228 | } |
| 229 | for _, f := range s.Failures { |
| 230 | if f.Category != "copy" && f.Category != "bookkeeping" { |
| 231 | return false |
| 232 | } |
| 233 | } |
| 234 | return true |
| 235 | } |
| 236 | |
| 237 | type Reference struct { |
| 238 | Path string `json:"path"` |
| 239 | Lines []int `json:"lines"` |
| 240 | Reason string `json:"reason"` |
| 241 | } |
| 242 | |
| 243 | func ReviewedWords(ex []Literal, candidate string, d Delegation, records []Record) (Review, bool) { |
| 244 | r := Review{Version: 1, Strings: []Wording{}} |
| 245 | for _, s := range ex { |
| 246 | if !Accepted(s, candidate, d, records) { |
| 247 | return Review{}, false |
| 248 | } |
| 249 | flag := "" |
| 250 | for _, record := range records { |
| 251 | if Accepted(s, candidate, d, []Record{record}) { |
| 252 | flag = record.Flag |
| 253 | break |
| 254 | } |
| 255 | } |
| 256 | r.Strings = append(r.Strings, Wording{Key: s.Key, Text: s.Text, Action: "accept", Flag: flag}) |
| 257 | } |
| 258 | return r, true |
| 259 | } |
| 260 | |