oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package wire
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "strings"
8 "unicode"
9 "unicode/utf8"
10)
11
12const (
13 MaxDiffBlobBytes = 1 << 20
14 MaxDiffInputBytes = 8 << 20
15 MaxFileDiffBytes = 64 << 10
16 MaxFileDiffLines = 2000
17 MaxCandidateDiffBytes = 2 << 20
18 MaxDiffResponseBytes = 128 << 10
19 FrameChangeDiff = "change_diff"
20 DiffBinary = "binary"
21 DiffTooLarge = "too_large"
22 DiffWithheld = "withheld"
23 DiffUnavailable = "unavailable"
24 DiffExpired = "expired"
25)
26
27// FileDiff is display evidence only, never a merge or Review input.
28type FileDiff struct {
29 Path string `json:"path"`
30 Patch string `json:"patch,omitempty"`
31 Reason string `json:"reason,omitempty"`
32 Truncated bool `json:"truncated"`
33}
34
35// ChangeDiffUpload sends one complete bounded file, base64 encoded so even
36// maximally escaped text fits a durable frame. The receiver scans the whole
37// decoded patch before storing it; no partial unscanned chunks are persisted.
38type ChangeDiffUpload struct {
39 TaskID string `json:"task_id"`
40 StepID string `json:"step_id"`
41 Attempt int64 `json:"attempt"`
42 Base string `json:"base"`
43 Candidate string `json:"candidate_sha"`
44 Collection string `json:"collection"`
45 Index int `json:"index"`
46 Total int `json:"total"`
47 Path string `json:"path"`
48 Size int `json:"size"`
49 SHA256 string `json:"sha256"`
50 Data string `json:"data"`
51 Reason string `json:"reason,omitempty"`
52 Truncated bool `json:"truncated"`
53 // Publication (protocol 37) binds the upload to a primary's
54 // publication of a worker's candidate; "" is the step runner's own.
55 Publication string `json:"publication,omitempty"`
56}
57
58type ChangeDiff struct {
59 HiddenCharacters bool `json:"hidden_characters"`
60 TaskID string `json:"task_id"`
61 StepID string `json:"step_id"`
62 Attempt int64 `json:"attempt"`
63 Base string `json:"base"`
64 Candidate string `json:"candidate_sha"`
65 Status string `json:"status"`
66 OldPath string `json:"old_path,omitempty"`
67 FileDiff
68}
69
70func ValidFileDiff(d FileDiff) bool {
71 if !ValidChangePath(d.Path) || len(d.Patch) > MaxFileDiffBytes || !utf8.ValidString(d.Patch) || strings.ContainsRune(d.Patch, 0) || strings.Count(d.Patch, "\n") > MaxFileDiffLines {
72 return false
73 }
74 switch d.Reason {
75 case "":
76 return d.Patch == "" || strings.HasSuffix(d.Patch, "\n")
77 case DiffBinary, DiffTooLarge, DiffWithheld, DiffUnavailable, DiffExpired:
78 return d.Patch == "" && !d.Truncated
79 default:
80 return false
81 }
82}
83
84func DiffDigest(data []byte) string { sum := sha256.Sum256(data); return hex.EncodeToString(sum[:]) }
85
86// The collection hash binds ordering, paths, refusals and every patch byte.
87func DiffCollection(files []FileDiff) string { raw, _ := json.Marshal(files); return DiffDigest(raw) }
88
89// PatchHasHiddenCharacters flags deceptive scalars without changing patch bytes.
90// Cf and DICP follow Unicode DerivedCoreProperties; LF and TAB stay structural.
91func PatchHasHiddenCharacters(patch string) bool {
92 var previous rune
93 for _, r := range patch {
94 // Only these two selectors immediately following an allowed pictographic base are benign.
95 emojiSelector := (r == '\ufe0e' || r == '\ufe0f') && unicode.Is(emojiSelectorBases, previous)
96 previous = r
97 if emojiSelector {
98 continue
99 }
100 // DICP is Other_Default_Ignorable + Cf + Variation_Selector minus
101 // exceptions that are already Cf. We deliberately include every Cf.
102 // LF is structural; TAB is allowed whitespace. CR remains visible.
103 if unicode.Is(unicode.Cf, r) || unicode.Is(unicode.Other_Default_Ignorable_Code_Point, r) ||
104 unicode.Is(unicode.Variation_Selector, r) ||
105 (unicode.Is(unicode.Cc, r) && r != '\t' && r != '\n') || r == '\u2028' || r == '\u2029' {
106 return true
107 }
108 }
109 return false
110}
111