package wire import ( "crypto/sha256" "encoding/hex" "encoding/json" "strings" "unicode" "unicode/utf8" ) const ( MaxDiffBlobBytes = 1 << 20 MaxDiffInputBytes = 8 << 20 MaxFileDiffBytes = 64 << 10 MaxFileDiffLines = 2000 MaxCandidateDiffBytes = 2 << 20 MaxDiffResponseBytes = 128 << 10 FrameChangeDiff = "change_diff" DiffBinary = "binary" DiffTooLarge = "too_large" DiffWithheld = "withheld" DiffUnavailable = "unavailable" DiffExpired = "expired" ) // FileDiff is display evidence only, never a merge or Review input. type FileDiff struct { Path string `json:"path"` Patch string `json:"patch,omitempty"` Reason string `json:"reason,omitempty"` Truncated bool `json:"truncated"` } // ChangeDiffUpload sends one complete bounded file, base64 encoded so even // maximally escaped text fits a durable frame. The receiver scans the whole // decoded patch before storing it; no partial unscanned chunks are persisted. type ChangeDiffUpload struct { TaskID string `json:"task_id"` StepID string `json:"step_id"` Attempt int64 `json:"attempt"` Base string `json:"base"` Candidate string `json:"candidate_sha"` Collection string `json:"collection"` Index int `json:"index"` Total int `json:"total"` Path string `json:"path"` Size int `json:"size"` SHA256 string `json:"sha256"` Data string `json:"data"` Reason string `json:"reason,omitempty"` Truncated bool `json:"truncated"` // Publication (protocol 37) binds the upload to a primary's // publication of a worker's candidate; "" is the step runner's own. Publication string `json:"publication,omitempty"` } type ChangeDiff struct { HiddenCharacters bool `json:"hidden_characters"` TaskID string `json:"task_id"` StepID string `json:"step_id"` Attempt int64 `json:"attempt"` Base string `json:"base"` Candidate string `json:"candidate_sha"` Status string `json:"status"` OldPath string `json:"old_path,omitempty"` FileDiff } func ValidFileDiff(d FileDiff) bool { if !ValidChangePath(d.Path) || len(d.Patch) > MaxFileDiffBytes || !utf8.ValidString(d.Patch) || strings.ContainsRune(d.Patch, 0) || strings.Count(d.Patch, "\n") > MaxFileDiffLines { return false } switch d.Reason { case "": return d.Patch == "" || strings.HasSuffix(d.Patch, "\n") case DiffBinary, DiffTooLarge, DiffWithheld, DiffUnavailable, DiffExpired: return d.Patch == "" && !d.Truncated default: return false } } func DiffDigest(data []byte) string { sum := sha256.Sum256(data); return hex.EncodeToString(sum[:]) } // The collection hash binds ordering, paths, refusals and every patch byte. func DiffCollection(files []FileDiff) string { raw, _ := json.Marshal(files); return DiffDigest(raw) } // PatchHasHiddenCharacters flags deceptive scalars without changing patch bytes. // Cf and DICP follow Unicode DerivedCoreProperties; LF and TAB stay structural. func PatchHasHiddenCharacters(patch string) bool { var previous rune for _, r := range patch { // Only these two selectors immediately following an allowed pictographic base are benign. emojiSelector := (r == '\ufe0e' || r == '\ufe0f') && unicode.Is(emojiSelectorBases, previous) previous = r if emojiSelector { continue } // DICP is Other_Default_Ignorable + Cf + Variation_Selector minus // exceptions that are already Cf. We deliberately include every Cf. // LF is structural; TAB is allowed whitespace. CR remains visible. if unicode.Is(unicode.Cf, r) || unicode.Is(unicode.Other_Default_Ignorable_Code_Point, r) || unicode.Is(unicode.Variation_Selector, r) || (unicode.Is(unicode.Cc, r) && r != '\t' && r != '\n') || r == '\u2028' || r == '\u2029' { return true } } return false }