From 4ab4c7993429df431c0a9248204051a8259403c0 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 3 Nov 2018 15:45:29 +0530 Subject: [PATCH] Ensure that all tunnel names are trimmed of whitespaces --- WireGuard/WireGuard/UI/TunnelViewModel.swift | 2 +- WireGuard/WireGuard/UI/iOS/QRScanViewController.swift | 2 +- .../WireGuard/UI/iOS/TunnelsListTableViewController.swift | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift index a9ee6f8..8a9cba4 100644 --- a/WireGuard/WireGuard/UI/TunnelViewModel.swift +++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift @@ -97,7 +97,7 @@ class TunnelViewModel { func save() -> SaveResult { fieldsWithError.removeAll() - guard let name = scratchpad[.name], (!name.isEmpty) else { + guard let name = scratchpad[.name]?.trimmingCharacters(in: .whitespacesAndNewlines), (!name.isEmpty) else { fieldsWithError.insert(.name) return .error("Interface name is required") } diff --git a/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift b/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift index 9dd298a..929b6b0 100644 --- a/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/QRScanViewController.swift @@ -116,7 +116,7 @@ class QRScanViewController: UIViewController { alert.addTextField(configurationHandler: nil) alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)) alert.addAction(UIAlertAction(title: NSLocalizedString("Save", comment: ""), style: .default, handler: { [weak self] _ in - let title = alert.textFields?[0].text ?? "" + let title = alert.textFields?[0].text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" if (title.isEmpty) { return } tunnelConfiguration.interface.name = title if let s = self { diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift index e42b964..df509a6 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift @@ -179,7 +179,7 @@ class TunnelsListTableViewController: UIViewController { func importFromFile(url: URL) { // Import configurations from a .conf or a .zip file if (url.pathExtension == "conf") { - let fileBaseName = url.deletingPathExtension().lastPathComponent + let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) if let fileContents = try? String(contentsOf: url), let tunnelConfiguration = try? WgQuickConfigFileParser.parse(fileContents, name: fileBaseName) { tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (tunnel, error) in @@ -206,7 +206,8 @@ class TunnelsListTableViewController: UIViewController { } for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() { - if let trimmedName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent, !trimmedName.isEmpty { + let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent + if let trimmedName = fileBaseName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty { unarchivedFiles[i].fileName = trimmedName } else { unarchivedFiles.remove(at: i)