|
| 1 | +// Copyright 2024 The Brave Authors. All rights reserved. |
| 2 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import UIKit |
| 8 | + |
| 9 | +class URLBarHelper { |
| 10 | + |
| 11 | + static let shared = URLBarHelper() |
| 12 | + |
| 13 | + func shouldShowSearchSuggestions(using lastReplacement: String) async -> Bool { |
| 14 | + // Check if last entry to url textfield needs to be checked as suspicious. |
| 15 | + // The reason of checking count is bigger than 1 is the single character |
| 16 | + // entries will always be safe and only way to achieve multi character entry is |
| 17 | + // using paste board. |
| 18 | + // This check also allow us to handle paste permission case |
| 19 | + guard lastReplacement.count > 1 else { |
| 20 | + return true |
| 21 | + } |
| 22 | + |
| 23 | + // Check if paste board has any text to guarantee the case |
| 24 | + guard UIPasteboard.general.hasStrings || UIPasteboard.general.hasURLs else { |
| 25 | + return true |
| 26 | + } |
| 27 | + |
| 28 | + // Perform check on pasted text |
| 29 | + if let pasteboardContents = UIPasteboard.general.string { |
| 30 | + let isSuspicious = await isSuspiciousQuery(pasteboardContents) |
| 31 | + return !isSuspicious |
| 32 | + } |
| 33 | + |
| 34 | + return true |
| 35 | + } |
| 36 | + |
| 37 | + /// Whether the desired text should allow search suggestions to appear when it is copied |
| 38 | + /// - Parameter query: Search query copied |
| 39 | + /// - Returns: the result if it is suspicious |
| 40 | + func isSuspiciousQuery(_ query: String) async -> Bool { |
| 41 | + // Remove the msg if the query is too long |
| 42 | + if query.count > 50 { |
| 43 | + return true |
| 44 | + } |
| 45 | + |
| 46 | + // Remove the msg if the query contains more than 7 words |
| 47 | + if query.components(separatedBy: " ").count > 7 { |
| 48 | + return true |
| 49 | + } |
| 50 | + |
| 51 | + // Remove the msg if the query contains a number longer than 7 digits |
| 52 | + if let _ = checkForLongNumber(query, 7) { |
| 53 | + return true |
| 54 | + } |
| 55 | + |
| 56 | + // Remove if email (exact), even if not totally well formed |
| 57 | + if checkForEmail(query) { |
| 58 | + return true |
| 59 | + } |
| 60 | + |
| 61 | + // Remove if query looks like an http pass |
| 62 | + if query.range(of: "[^:]+:[^@]+@", options: .regularExpression) != nil { |
| 63 | + return true |
| 64 | + } |
| 65 | + |
| 66 | + for word in query.components(separatedBy: " ") { |
| 67 | + if word.range(of: "[^:]+:[^@]+@", options: .regularExpression) != nil { |
| 68 | + return true |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if query.count > 12 { |
| 73 | + let literalsPattern = "[^A-Za-z0-9]" |
| 74 | + |
| 75 | + guard |
| 76 | + let literalsRegex = try? NSRegularExpression( |
| 77 | + pattern: literalsPattern, |
| 78 | + options: .caseInsensitive |
| 79 | + ) |
| 80 | + else { |
| 81 | + return true |
| 82 | + } |
| 83 | + |
| 84 | + let range = NSRange(location: 0, length: query.utf16.count) |
| 85 | + |
| 86 | + let cquery = literalsRegex.stringByReplacingMatches( |
| 87 | + in: query, |
| 88 | + options: [], |
| 89 | + range: range, |
| 90 | + withTemplate: "" |
| 91 | + ) |
| 92 | + |
| 93 | + if cquery.count > 12 { |
| 94 | + let pp = isHashProb(cquery) |
| 95 | + // we are a bit more strict here because the query |
| 96 | + // can have parts well formed |
| 97 | + if pp < URLBarHelperConstants.probHashThreshold * 1.5 { |
| 98 | + return true |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return false |
| 104 | + } |
| 105 | + |
| 106 | + private func checkForLongNumber(_ str: String, _ maxNumberLength: Int) -> String? { |
| 107 | + let controlString = str.replacingOccurrences( |
| 108 | + of: "[^A-Za-z0-9]", |
| 109 | + with: "", |
| 110 | + options: .regularExpression |
| 111 | + ) |
| 112 | + |
| 113 | + var location = 0 |
| 114 | + var maxLocation = 0 |
| 115 | + var maxLocationPosition: String.Index? = nil |
| 116 | + |
| 117 | + for i in controlString.indices { |
| 118 | + if controlString[i] >= "0" && controlString[i] <= "9" { |
| 119 | + location += 1 |
| 120 | + } else { |
| 121 | + if location > maxLocation { |
| 122 | + maxLocation = location |
| 123 | + maxLocationPosition = i |
| 124 | + } |
| 125 | + |
| 126 | + location = 0 |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if location > maxLocation { |
| 131 | + maxLocation = location |
| 132 | + maxLocationPosition = controlString.endIndex |
| 133 | + } |
| 134 | + |
| 135 | + if let maxLocationPosition = maxLocationPosition, maxLocation > maxNumberLength { |
| 136 | + let start = controlString.index(maxLocationPosition, offsetBy: -maxLocation) |
| 137 | + let end = maxLocationPosition |
| 138 | + |
| 139 | + return String(controlString[start..<end]) |
| 140 | + } else { |
| 141 | + return nil |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private func checkForEmail(_ str: String) -> Bool { |
| 146 | + let emailPattern = "[a-z0-9\\-_@]+(@|%40|%(25)+40)[a-z0-9\\-_]+\\.[a-z0-9\\-_]" |
| 147 | + |
| 148 | + guard |
| 149 | + let emailRegex = try? NSRegularExpression(pattern: emailPattern, options: .caseInsensitive) |
| 150 | + else { |
| 151 | + return false |
| 152 | + } |
| 153 | + |
| 154 | + let range = NSRange(location: 0, length: str.utf16.count) |
| 155 | + return emailRegex.firstMatch(in: str, options: [], range: range) != nil |
| 156 | + } |
| 157 | + |
| 158 | + private func isHashProb(_ str: String) -> Double { |
| 159 | + var logProb = 0.0 |
| 160 | + var transC = 0 |
| 161 | + let filteredStr = str.replacingOccurrences( |
| 162 | + of: "[^A-Za-z0-9]", |
| 163 | + with: "", |
| 164 | + options: .regularExpression |
| 165 | + ) |
| 166 | + |
| 167 | + let characters = Array(filteredStr) |
| 168 | + for i in 0..<(characters.count - 1) { |
| 169 | + if let pos1 = URLBarHelperConstants.probHashChars[characters[i]], |
| 170 | + let pos2 = URLBarHelperConstants.probHashChars[characters[i + 1]] |
| 171 | + { |
| 172 | + logProb += URLBarHelperConstants.probHashLogM[pos1][pos2] |
| 173 | + transC += 1 |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if transC > 0 { |
| 178 | + return exp(logProb / Double(transC)) |
| 179 | + } else { |
| 180 | + return exp(logProb) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments