Skip to content

Commit

Permalink
fix: validate domain name in Endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Ricky Saechao <[email protected]>
  • Loading branch information
RickyLB committed Aug 26, 2024
1 parent 5c21298 commit ec51ad1
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/Hedera/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ extension Endpoint: TryProtobufCodable {
port = 50211
}

guard isValidDomainName(proto.domainName) else {
throw HError(kind: .basicParse, description: "Invalid domain name format")
}

self.init(ipAddress: ipAddress, port: port, domainName: proto.domainName)
}

Expand All @@ -83,3 +87,10 @@ extension Endpoint: TryProtobufCodable {
}
}
}

private func isValidDomainName(_ domainName: String) -> Bool {
let pattern = "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$"
let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let range = NSRange(location: 0, length: domainName.utf16.count)
return regex?.firstMatch(in: domainName, options: [], range: range) != nil
}

0 comments on commit ec51ad1

Please sign in to comment.