| 1 | import Foundation |
| 2 | import Darwin |
| 3 | |
| 4 | /// The web's url.Parse check, including the places where Go deliberately |
| 5 | /// leaves percent escapes alone (opaque URLs and the query string). |
| 6 | enum MarkdownLinks { |
| 7 | static let schemes: Set<String> = ["http", "https", "mailto"] |
| 8 | static func allows(_ target: String) -> Bool { |
| 9 | guard !target.prefix(while: { $0 != "#" }).unicodeScalars.contains(where: { $0.value < 32 || $0.value == 127 }) else { return false } |
| 10 | guard let colon = target.firstIndex(of: ":"), schemes.contains(target[..<colon].lowercased()) else { |
| 11 | return MarkdownDraft.matches(#"^/(tasks/tk[a-z0-9]{1,32}|projects/p_[a-z0-9]{1,40})$"#, target) |
| 12 | } |
| 13 | let scheme = target[..<colon].lowercased() |
| 14 | let parts = target[target.index(after: colon)...].split(separator: "#", maxSplits: 1, omittingEmptySubsequences: false) |
| 15 | if parts.count == 2, badEscape(String(parts[1])) { return false } |
| 16 | let rest = String(parts[0].split(separator: "?", maxSplits: 1, omittingEmptySubsequences: false)[0]) |
| 17 | // An opaque URI does not have a decoded path in Go. |
| 18 | guard rest.hasPrefix("/") else { return true } |
| 19 | guard !badEscape(rest) else { return false } |
| 20 | guard rest.hasPrefix("//") else { return true } |
| 21 | let authority = rest.dropFirst(2).prefix(while: { $0 != "/" }) |
| 22 | let host = String(authority.split(separator: "@", omittingEmptySubsequences: false).last ?? "") |
| 23 | if let at = authority.lastIndex(of: "@") { |
| 24 | let user = authority[..<at] |
| 25 | guard user.utf8.allSatisfy({ asciiAlphaNumeric($0) || "-._:~!$&'()*+,;=%@".utf8.contains($0) }), !badEscape(String(user)) else { return false } |
| 26 | } |
| 27 | if let bracket = host.lastIndex(of: "["), bracket != host.startIndex { return false } |
| 28 | if host.hasPrefix("[") { |
| 29 | guard let end = host.lastIndex(of: "]") else { return false } |
| 30 | guard validPort(String(host[host.index(after: end)...])) else { return false } |
| 31 | let literal = String(host[host.index(after: host.startIndex)..<end]) |
| 32 | let address = String(literal.components(separatedBy: "%25")[0]) |
| 33 | if let separator = literal.range(of: "%25") { |
| 34 | guard validZone(String(literal[separator.upperBound...])) else { return false } |
| 35 | } |
| 36 | var parsed = in6_addr() |
| 37 | guard address.withCString({ inet_pton(AF_INET6, $0, &parsed) }) == 1 else { return false } |
| 38 | } else { |
| 39 | if let colon = (scheme == "mailto" ? host.lastIndex(of: ":") : host.firstIndex(of: ":")), !validPort(String(host[colon...])) { return false } |
| 40 | guard host.utf8.allSatisfy({ $0 >= 128 || asciiAlphaNumeric($0) || "!$&'()*+,-.:;=[]_%~<>\"".utf8.contains($0) }) else { return false } |
| 41 | // Go permits encoded non-ASCII host bytes and %25, not encoded |
| 42 | // ASCII host characters such as %41 or %2f. |
| 43 | let regex = try! NSRegularExpression(pattern: "%([0-9A-Fa-f]{2})") |
| 44 | for match in regex.matches(in: host, range: NSRange(host.startIndex..., in: host)) { |
| 45 | guard let range = Range(match.range(at: 1), in: host), let byte = UInt8(host[range], radix: 16) else { return false } |
| 46 | if byte < 128 && byte != 37 { return false } |
| 47 | } |
| 48 | } |
| 49 | return true |
| 50 | } |
| 51 | private static func validZone(_ value: String) -> Bool { |
| 52 | let bytes = Array(value.utf8) |
| 53 | guard !bytes.isEmpty else { return false } |
| 54 | var i = 0 |
| 55 | while i < bytes.count { |
| 56 | let byte = bytes[i] |
| 57 | if byte == 37 { |
| 58 | guard i + 2 < bytes.count, let decoded = UInt8(String(decoding: bytes[(i+1)...(i+2)], as: UTF8.self), radix: 16), decoded == 37 || decoded == 32 || hostByte(decoded) else { return false } |
| 59 | i += 3 |
| 60 | } else { |
| 61 | guard hostByte(byte) else { return false } |
| 62 | i += 1 |
| 63 | } |
| 64 | } |
| 65 | return true |
| 66 | } |
| 67 | private static func hostByte(_ b: UInt8) -> Bool { b >= 128 || asciiAlphaNumeric(b) || "!$&'()*+,-.:;=[]_~<>\"".utf8.contains(b) } |
| 68 | private static func asciiAlphaNumeric(_ b: UInt8) -> Bool { (65...90).contains(b) || (97...122).contains(b) || (48...57).contains(b) } |
| 69 | private static func validPort(_ port: String) -> Bool { port.isEmpty || port.first == ":" && port.utf8.dropFirst().allSatisfy { (48...57).contains($0) } } |
| 70 | private static func badEscape(_ value: String) -> Bool { MarkdownDraft.matches(#"%(?![0-9A-Fa-f]{2})"#, value) } |
| 71 | } |
| 72 | |