IDNSafetyHelper.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import Punycode
  22. @objc class IDNSafetyHelper: NSObject {
  23. @objc class func safeOpen(url: URL, viewController: UIViewController) {
  24. if (isLegalURL(url: url)) {
  25. if (url.host?.lowercased() == "threema.id") {
  26. URLHandler.handleThreemaDotIdUrl(url, hideAppChooser: false)
  27. } else {
  28. UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
  29. }
  30. } else {
  31. let body = String(format: NSLocalizedString("url_warning_body", comment: ""), url.host!.idnaDecoded!, url.host!)
  32. let alert = UIAlertController(title: NSLocalizedString("url_warning_title", comment: ""), message: body, preferredStyle: .alert)
  33. alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .default, handler: { action in
  34. UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
  35. }))
  36. alert.addAction(UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel, handler: nil))
  37. viewController.present(alert, animated: true)
  38. }
  39. }
  40. @objc class func isLegalURL(url: URL) -> Bool {
  41. let legalHostnamePattern = try! NSRegularExpression(pattern: "^(([\\x00-\\x7F]*)|([^\\x00-\\x7F]*))$")
  42. let strippedHost = url.host?.idnaDecoded?.replacingOccurrences(of: ".", with: "")
  43. if (strippedHost != nil) {
  44. return (legalHostnamePattern.numberOfMatches(in: strippedHost!, range: NSRange(location: 0, length: strippedHost!.utf16.count)) > 0)
  45. } else {
  46. return true
  47. }
  48. }
  49. }