// Package copydesk validates Copy's bounded inputs and results. It grants no authority. package copydesk import ( "bytes" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "regexp" "sort" "strings" ) const ( PassTokens = 32768 TaskTokens = 98304 MaxStrings = 200 MaxText = 4000 ) type Delegation struct { Owner string `json:"owner"` Revision int64 `json:"revision"` } type Literal struct { Key string `json:"key"` Path string `json:"path"` Start int `json:"start"` End int `json:"end"` Old string `json:"old"` Text string `json:"text"` Context string `json:"context"` } type Extraction struct { Version int `json:"version"` Strings []Literal `json:"strings"` } type Wording struct { Key string `json:"key"` Text string `json:"text"` Action string `json:"action"` Flag string `json:"flag,omitempty"` } type Review struct { Version int `json:"version"` Strings []Wording `json:"strings"` } type Record struct { Flag string Key, Hash, Candidate, Owner string Revision int64 } type Feedback struct { SourceID string `json:"source_id"` Actor string `json:"-"` Key string `json:"key"` Before string `json:"before"` After string `json:"after"` Reason string `json:"reason,omitempty"` Origin string `json:"-"` } func Hash(text string) string { h := sha256.Sum256([]byte(text)); return hex.EncodeToString(h[:]) } func ShouldRun(d Delegation, hasStrings bool) bool { return d.Owner != "" && d.Revision > 0 && hasStrings } func Accepted(s Literal, candidate string, d Delegation, records []Record) bool { if !ShouldRun(d, true) { return false } for _, r := range records { if r.Key == s.Key && r.Hash == Hash(s.Text) && r.Candidate == candidate && r.Owner == d.Owner && r.Revision == d.Revision { return true } } return false } func CanLearn(d Delegation, f Feedback) bool { 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 } 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_]*\}`) func ValidateReview(changed []Literal, r Review) ([]Wording, error) { if r.Version != 1 || len(changed) > MaxStrings || len(r.Strings) != len(changed) { return nil, errors.New("copy: incomplete review") } keys := map[string]Literal{} for _, s := range changed { if s.Key == "" { return nil, errors.New("copy: empty key") } if _, ok := keys[s.Key]; ok { return nil, errors.New("copy: duplicate extraction") } keys[s.Key] = s } for _, w := range r.Strings { s, ok := keys[w.Key] if !ok { return nil, errors.New("copy: unreviewed key") } delete(keys, w.Key) if w.Action == "question" && strings.TrimSpace(w.Flag) == "" { return nil, errors.New("copy: missing meaning question") } if w.Action != "accept" && w.Action != "question" || len(w.Text) > MaxText || len(w.Flag) > 1000 || strings.ContainsRune(w.Text, 0) { return nil, errors.New("copy: invalid wording") } if strings.Count(s.Text, "%") != strings.Count(w.Text, "%") { return nil, errors.New("copy: changed percent placeholders") } if strings.Join(placeholders.FindAllString(s.Text, -1), "\x00") != strings.Join(placeholders.FindAllString(w.Text, -1), "\x00") { return nil, errors.New("copy: changed placeholders") } } return r.Strings, nil } func ValidateScope(before, after []byte, locations []Literal, r Review) error { words, err := ValidateReview(locations, r) if err != nil { return err } byKey := map[string]Wording{} for _, w := range words { byKey[w.Key] = w } loc := append([]Literal(nil), locations...) sort.Slice(loc, func(i, j int) bool { return loc[i].Start < loc[j].Start }) var expected bytes.Buffer pos := 0 for _, s := range loc { if s.Start < pos || s.End < s.Start || s.End > len(before) || string(before[s.Start:s.End]) != s.Text { return errors.New("copy: stale location") } w := byKey[s.Key] if s.Start < 1 || s.End >= len(before) { return errors.New("copy: missing literal delimiters") } delimiter := before[s.Start-1] if before[s.End] != delimiter || !strings.ContainsRune("\"'`", rune(delimiter)) { return errors.New("copy: stale literal delimiters") } if strings.ContainsRune(w.Text, rune(delimiter)) || strings.ContainsAny(w.Text, "\\\n\r") || strings.Contains(w.Text, "${") { return errors.New("copy: unsafe literal contents") } if w.Action != "accept" { return errors.New("copy: meaning needs answer") } expected.Write(before[pos:s.Start]) expected.WriteString(w.Text) pos = s.End } expected.Write(before[pos:]) if !bytes.Equal(expected.Bytes(), after) { return errors.New("copy: edit outside extracted literals") } return nil } type Input struct { Version int `json:"version"` Units []string `json:"units"` Log string `json:"log"` Truncated bool `json:"truncated"` } type Failure struct { Unit string `json:"unit"` Test string `json:"test"` Code string `json:"code"` Subject string `json:"subject"` Category string `json:"category"` } type Classification struct { Version int `json:"version"` Complete bool `json:"complete"` Failures []Failure `json:"failures"` } type Signature struct { Version int `json:"version"` Complete bool `json:"complete"` Adapter string `json:"adapter"` Hash string `json:"hash"` Failures []Failure `json:"failures"` } func Classify(in Input, c Classification, adapter string) Signature { 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} covered := map[string]bool{} units := map[string]bool{} for _, u := range in.Units { units[u] = true } for _, f := range c.Failures { if !units[f.Unit] || f.Code == "" || f.Test == "" { s.Complete = false continue } if f.Category != "copy" && f.Category != "bookkeeping" && f.Category != "other" { f.Category = "other" s.Complete = false } covered[f.Unit] = true s.Failures = append(s.Failures, f) } for _, u := range in.Units { if !covered[u] { s.Complete = false s.Failures = append(s.Failures, Failure{Unit: u, Category: "other"}) } } sort.Slice(s.Failures, func(i, j int) bool { a, _ := json.Marshal(s.Failures[i]) b, _ := json.Marshal(s.Failures[j]) return string(a) < string(b) }) raw, _ := json.Marshal(s.Failures) s.Hash = Hash(string(raw)) return s } func SameFailure(a, b Signature) bool { 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 } func (s Signature) CopyOnly() bool { if !s.Complete || s.Adapter == "" || len(s.Failures) == 0 { return false } for _, f := range s.Failures { if f.Category != "copy" && f.Category != "bookkeeping" { return false } } return true } type Reference struct { Path string `json:"path"` Lines []int `json:"lines"` Reason string `json:"reason"` } func ReviewedWords(ex []Literal, candidate string, d Delegation, records []Record) (Review, bool) { r := Review{Version: 1, Strings: []Wording{}} for _, s := range ex { if !Accepted(s, candidate, d, records) { return Review{}, false } flag := "" for _, record := range records { if Accepted(s, candidate, d, []Record{record}) { flag = record.Flag break } } r.Strings = append(r.Strings, Wording{Key: s.Key, Text: s.Text, Action: "accept", Flag: flag}) } return r, true }