Prune unused validation helpers (#701)

This commit is contained in:
jack
2025-10-01 13:29:09 +02:00
committed by GitHub
parent 3183703a63
commit ea72212f66
2 changed files with 10 additions and 68 deletions
-4
View File
@@ -14,8 +14,4 @@ enum PacketIdUtil {
let digest = hasher.finalize() let digest = hasher.finalize()
return Data(digest.prefix(16)) return Data(digest.prefix(16))
} }
static func computeIdHex(_ packet: BitchatPacket) -> String {
return computeId(packet).hexEncodedString()
}
} }
+10 -64
View File
@@ -11,7 +11,6 @@ struct InputValidator {
// BinaryProtocol caps payload length at UInt16.max (65_535). Leave headroom // BinaryProtocol caps payload length at UInt16.max (65_535). Leave headroom
// for headers/padding by limiting user content to 60_000 bytes. // for headers/padding by limiting user content to 60_000 bytes.
static let maxMessageLength = 60_000 static let maxMessageLength = 60_000
static let maxReasonLength = 200
static let maxPeerIDLength = 64 static let maxPeerIDLength = 64
static let hexPeerIDLength = 16 // 8 bytes = 16 hex chars static let hexPeerIDLength = 16 // 8 bytes = 16 hex chars
} }
@@ -37,25 +36,20 @@ struct InputValidator {
// MARK: - String Content Validation // MARK: - String Content Validation
/// Validates and sanitizes user-provided strings (nicknames, messages) /// Validates and sanitizes user-provided strings used in UI
static func validateUserString(_ string: String, maxLength: Int, allowNewlines: Bool = false) -> String? { static func validateUserString(_ string: String, maxLength: Int) -> String? {
// Check empty // Check empty
guard !string.isEmpty else { return nil } guard !string.isEmpty else { return nil }
// Trim whitespace // Trim whitespace
let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines) let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return nil } guard !trimmed.isEmpty else { return nil }
// Check length // Check length
guard trimmed.count <= maxLength else { return nil } guard trimmed.count <= maxLength else { return nil }
// Remove control characters except allowed ones // Remove control characters
var allowedControlChars = CharacterSet() let controlChars = CharacterSet.controlCharacters
if allowNewlines {
allowedControlChars.insert(charactersIn: "\n\r")
}
let controlChars = CharacterSet.controlCharacters.subtracting(allowedControlChars)
let cleaned = trimmed.components(separatedBy: controlChars).joined() let cleaned = trimmed.components(separatedBy: controlChars).joined()
// Ensure valid UTF-8 (should already be, but double-check) // Ensure valid UTF-8 (should already be, but double-check)
@@ -70,29 +64,14 @@ struct InputValidator {
/// Validates nickname /// Validates nickname
static func validateNickname(_ nickname: String) -> String? { static func validateNickname(_ nickname: String) -> String? {
return validateUserString(nickname, maxLength: Limits.maxNicknameLength, allowNewlines: false) return validateUserString(nickname, maxLength: Limits.maxNicknameLength)
}
/// Validates message content
static func validateMessageContent(_ content: String) -> String? {
return validateUserString(content, maxLength: Limits.maxMessageLength, allowNewlines: true)
}
/// Validates error/reason strings
static func validateReasonString(_ reason: String) -> String? {
return validateUserString(reason, maxLength: Limits.maxReasonLength, allowNewlines: false)
} }
// MARK: - Protocol Field Validation // MARK: - Protocol Field Validation
// Note: Message type validation is performed closer to decoding using // Note: Message type validation is performed closer to decoding using
// MessageType/NoisePayloadType enums; keeping validator free of stale lists. // MessageType/NoisePayloadType enums; keeping validator free of stale lists.
/// Validates hop count is reasonable
static func validateHopCount(_ hopCount: UInt8) -> Bool {
return hopCount <= 10 // Prevent excessive forwarding
}
/// Validates timestamp is reasonable (not too far in past or future) /// Validates timestamp is reasonable (not too far in past or future)
static func validateTimestamp(_ timestamp: Date) -> Bool { static func validateTimestamp(_ timestamp: Date) -> Bool {
let now = Date() let now = Date()
@@ -100,38 +79,5 @@ struct InputValidator {
let oneHourFromNow = now.addingTimeInterval(3600) let oneHourFromNow = now.addingTimeInterval(3600)
return timestamp >= oneHourAgo && timestamp <= oneHourFromNow return timestamp >= oneHourAgo && timestamp <= oneHourFromNow
} }
/// Validates data size for different contexts
static func validateDataSize(_ data: Data, maxSize: Int) -> Bool {
return data.count > 0 && data.count <= maxSize
}
// MARK: - Binary Data Validation
/// Validates UUID format
static func validateUUID(_ uuid: String) -> Bool {
// Remove dashes and validate hex
let cleaned = uuid.replacingOccurrences(of: "-", with: "")
return cleaned.count == 32 && cleaned.allSatisfy { $0.isHexDigit }
}
/// Validates public key data
static func validatePublicKey(_ keyData: Data) -> Bool {
// Curve25519 public keys are 32 bytes
return keyData.count == 32
}
/// Validates signature data
static func validateSignature(_ signature: Data) -> Bool {
// Ed25519 signatures are 64 bytes
return signature.count == 64
}
}
// MARK: - Character Extensions
private extension Character {
var isHexDigit: Bool {
return "0123456789abcdefABCDEF".contains(self)
}
} }