import Foundation public struct ThinChangedFile: Codable, Sendable, Equatable, Identifiable { public var path: String public var oldPath: String? = nil public var status: String public var additions: Int public var deletions: Int public var binary: Bool public var id: String { path } } public struct ThinChangeFiles: Codable, Sendable, Equatable { public var base: String public var sha: String public var files: Int public var additions: Int public var deletions: Int public var binary: Int public var entries: [ThinChangedFile] public var truncated: Bool var isValid: Bool { guard (base.isEmpty || Self.commit(base)), Self.commit(sha), files >= 0, additions >= 0, deletions >= 0, binary >= 0, binary <= files, entries.count <= 200, entries.count <= files, truncated == (entries.count < files), Set(entries.map(\.path)).count == entries.count else { return false } var added = 0, removed = 0, binaries = 0 for file in entries { guard Self.path(file.path), ["added", "modified", "deleted", "renamed"].contains(file.status), file.additions >= 0, file.deletions >= 0, file.additions <= additions - added, file.deletions <= deletions - removed else { return false } if file.status == "renamed" { guard let old = file.oldPath, Self.path(old), old != file.path else { return false } } else if file.oldPath != nil { return false } added += file.additions; removed += file.deletions if file.binary { guard file.additions == 0, file.deletions == 0 else { return false } binaries += 1 } } guard binaries <= binary, truncated || (added == additions && removed == deletions && binaries == binary), let size = wireSize, size <= 96 * 1024 else { return false } return true } private var wireSize: Int? { let encoder = JSONEncoder() encoder.keyEncodingStrategy = .convertToSnakeCase encoder.outputFormatting = [.withoutEscapingSlashes] guard let data = try? encoder.encode(self), var json = String(data: data, encoding: .utf8) else { return nil } // Match encoding/json's HTML and separator escaping, including old_path. for (character, escape) in [("<", #"\u003c"#), (">", #"\u003e"#), ("&", #"\u0026"#), ("\u{2028}", #"\u2028"#), ("\u{2029}", #"\u2029"#)] { json = json.replacingOccurrences(of: character, with: escape) } return json.utf8.count } static func commit(_ value: String) -> Bool { value.utf8.count == 40 && value.utf8.allSatisfy { (48...57).contains($0) || (97...102).contains($0) } } static func path(_ value: String) -> Bool { !value.isEmpty && value.utf8.count <= 4096 && !value.contains("\\") && !value.unicodeScalars.contains { $0.properties.generalCategory == .control || $0.value == 0x061c || $0.value == 0x200e || $0.value == 0x200f || (0x202a...0x202e).contains($0.value) || (0x2066...0x2069).contains($0.value) } && value.split(separator: "/", omittingEmptySubsequences: false).allSatisfy { !$0.isEmpty && $0 != "." && $0 != ".." } } } // Validate the optional presentation field on entry, including decoded payloads // and programmatic assignment. A malformed optional field cannot hide the task. @propertyWrapper public struct ValidatedChangeFiles: Codable, Sendable, Equatable { private var value: ThinChangeFiles? public var wrappedValue: ThinChangeFiles? { get { value } set { value = newValue.flatMap { $0.isValid ? $0 : nil } } } public init(wrappedValue: ThinChangeFiles?) { value = wrappedValue.flatMap { $0.isValid ? $0 : nil } } public init(from decoder: Decoder) throws { self.init(wrappedValue: try? ThinChangeFiles(from: decoder)) } public func encode(to encoder: Encoder) throws { try value.encode(to: encoder) } } extension KeyedDecodingContainer { func decode(_ type: ValidatedChangeFiles.Type, forKey key: Key) throws -> ValidatedChangeFiles { try decodeIfPresent(type, forKey: key) ?? ValidatedChangeFiles(wrappedValue: nil) } } extension KeyedEncodingContainer { mutating func encode(_ value: ValidatedChangeFiles, forKey key: Key) throws { if let facts = value.wrappedValue { try encode(facts, forKey: key) } } }