import CryptoKit import Foundation struct NamePolicy: Sendable { let salt: String let always: Set let contextual: Set private let saltBytes: Data init(_ data: Data) throws { let object = try DevJSON.decode(data).requireObject() guard Set(object.keys) == ["version", "salt", "always", "contextual"] else { throw DevError("Invalid hash policy fields") } guard object["version"]?.number == 1 else { throw DevError("Unsupported hash policy version") } guard let salt = object["salt"]?.string, try NameRegex.matches("[0-9a-f]{32}", salt) else { throw DevError("Invalid hash policy salt") } self.salt = salt var saltBytes = Data() let bytes = Array(salt.utf8) for offset in stride(from: 0, to: bytes.count, by: 2) { let high = bytes[offset] <= 57 ? bytes[offset] - 48 : bytes[offset] - 87 let low = bytes[offset + 1] <= 57 ? bytes[offset + 1] - 48 : bytes[offset + 1] - 87 saltBytes.append(high * 16 + low) } self.saltBytes = saltBytes func hashes(_ key: String) throws -> [String] { guard let values = object[key]?.array else { throw DevError("Invalid hash policy entries") } return try values.map { guard let hash = $0.string, try NameRegex.matches("[0-9a-f]{64}", hash) else { throw DevError("Invalid hash policy entries") } return hash } } let always = try hashes("always") let contextual = try hashes("contextual") let all = always + contextual guard !all.isEmpty, Set(all).count == all.count else { throw DevError("Hash policy must be nonempty with no duplicate entries") } self.always = Set(always) self.contextual = Set(contextual) } func digest(_ value: String) -> String { var data = saltBytes data.append(0) data.append(contentsOf: value.utf8) return DevHex.string(SHA256.hash(data: data)) } } enum NameRegex { static func matches(_ pattern: String, _ value: String) throws -> Bool { let expression = try NSRegularExpression(pattern: "\\A(?:" + pattern + ")\\z") return expression.firstMatch(in: value, range: NSRange(value.startIndex..., in: value)) != nil } static func contains(_ pattern: String, _ value: String) throws -> Bool { try NSRegularExpression(pattern: pattern).firstMatch(in: value, range: NSRange(value.startIndex..., in: value)) != nil } } struct NameAllowanceKey: Hashable { let scope: String let source: String? let line: String let token: String } struct NameAllowances { let entries: [NameAllowanceKey: String] init(_ data: Data) throws { let document = try DevJSON.decode(data).requireObject() guard Set(document.keys) == ["version", "entries"], document["version"]?.number == 1 else { throw DevError("Invalid allowlist version or fields") } guard let values = document["entries"]?.array else { throw DevError("Invalid allowlist entries") } var entries: [NameAllowanceKey: String] = [:] for value in values { guard let object = value.object else { throw DevError("Invalid allowlist entry fields") } var fields: Set = ["scope", "line_hash", "token_hash", "reason"] if object["scope"]?.string != "artifact" || object["source_hash"] != nil { fields.insert("source_hash") } guard Set(object.keys) == fields else { throw DevError("Invalid allowlist entry fields") } var strings: [String: String] = [:] for (key, value) in object { guard let text = value.string, !text.isEmpty else { throw DevError("Allowlist fields must be nonempty strings") } strings[key] = text } guard let scope = strings["scope"], ["content", "path", "commit", "artifact"].contains(scope), let line = strings["line_hash"], let token = strings["token_hash"], let reason = strings["reason"], try NameRegex.matches("[0-9a-f]{64}", line), try NameRegex.matches("[0-9a-f]{64}", token) else { throw DevError("Invalid allowlist scope or hash") } if let source = strings["source_hash"], try !NameRegex.matches("[0-9a-f]{64}", source) { throw DevError("Invalid allowlist scope or hash") } let key = NameAllowanceKey(scope: scope, source: strings["source_hash"], line: line, token: token) guard entries.updateValue(reason, forKey: key) == nil else { throw DevError("Duplicate allowlist entry") } } self.entries = entries } } struct NameHit: Codable { let scope: String let source: String let sourceHash: String let line: Int let column: Int let lineHash: String let tokenHash: String let reason: String? enum CodingKeys: String, CodingKey { case scope, source, line, column, reason case sourceHash = "source_hash", lineHash = "line_hash", tokenHash = "token_hash" } } struct NameWord { let start: Int let end: Int let value: String let folded: String } final class NameScanner { let policy: NamePolicy let allowances: NameAllowances var hits: [NameHit] = [] var allowed: [NameHit] = [] var artifacts = 0 private var cache: [String: String] = [:] init(policy: NamePolicy, allowances: NameAllowances) { self.policy = policy self.allowances = allowances } func hash(_ value: String) -> String { if let value = cache[value] { return value } let digest = policy.digest(value) if cache.count >= 65_536 { cache.removeAll(keepingCapacity: true) } cache[value] = digest return digest } static func words(_ scalars: [Unicode.Scalar]) -> [NameWord] { func isWord(_ scalar: Unicode.Scalar) -> Bool { let value = scalar.value if value < 128 { return (48...57).contains(value) || (65...90).contains(value) || (97...122).contains(value) } switch scalar.properties.generalCategory { case .uppercaseLetter, .lowercaseLetter, .titlecaseLetter, .modifierLetter, .otherLetter, .decimalNumber, .letterNumber, .otherNumber: return true default: return false } } var words: [NameWord] = [] var index = 0 while index < scalars.count { guard isWord(scalars[index]) else { index += 1; continue } let start = index repeat { index += 1 } while index < scalars.count && isWord(scalars[index]) let value = String(String.UnicodeScalarView(scalars[start.. Int { var low = 0 var high = starts.count while low < high { let middle = (low + high) / 2 if starts[middle] <= position { low = middle + 1 } else { high = middle } } return low } func slice(_ range: Range) -> String { String(String.UnicodeScalarView(scalars[range])) } let sourceHash = hash("source\0" + source) for (index, word) in words.enumerated() { for width in 1...2 { let endIndex = index + width - 1 guard endIndex < words.count else { continue } let final = words[endIndex] if width == 2 { let separator = scalars[word.end.. Bool { if isPath || (capitalization && words[first...last].contains(where: { word in guard let scalar = word.value.unicodeScalars.first else { return false } return CharacterSet.uppercaseLetters.contains(scalar) })) { return true } let before = words[max(0, first - 2)..(){}[],;".unicodeScalars while left > 0 && !CharacterSet.whitespacesAndNewlines.contains(text[left - 1]) && !stops.contains(text[left - 1]) { left -= 1 } while right < text.count && !CharacterSet.whitespacesAndNewlines.contains(text[right]) && !stops.contains(text[right]) { right += 1 } let component = String(String.UnicodeScalarView(text[left.. 0 && "\"'`".unicodeScalars.contains(text[left - 1]) return try (quoted && (try NameRegex.matches("[\\w-]+(?:\\.[\\w-]+){2,}", component))) || (try NameRegex.matches("(?i:(?:com|org|net|io|dev|app|edu)\\.[\\w.-]+)", component)) || (try NameRegex.matches("(?i:[\\w-]+\\.(?:app|com|org|net|io|dev|swift|md|png|json|toml))", component)) } func binary(_ scope: String, source: String, path: URL, checkout: String? = nil) throws { var text = String( decoding: try DevProcess.checked("/usr/bin/strings", ["-n", "4", "--", path.path]), as: UTF8.self) if let checkout { text = text.replacingOccurrences(of: checkout, with: ".") } for (index, line) in text.components(separatedBy: "\n").enumerated() { try scan(scope, source: source, text: line, lineOffset: index, binary: true) } if scope == "artifact" { artifacts += 1 } } }