From 94c49229135456f30106b68f1893206af09c8aa7 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Tue, 8 Jan 2019 23:36:27 +0530 Subject: [PATCH] Parsing: Always error on unrecognized keys Signed-off-by: Roopesh Chander --- .../TunnelConfiguration+WgQuickConfig.swift | 22 +++++++++---------- .../TunnelEditViewController.swift | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift b/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift index 48c88b0..625c25d 100644 --- a/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift +++ b/WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift @@ -34,7 +34,7 @@ extension TunnelConfiguration { } //swiftlint:disable:next function_body_length cyclomatic_complexity - convenience init(fromWgQuickConfig wgQuickConfig: String, called name: String? = nil, ignoreUnrecognizedKeys: Bool = true) throws { + convenience init(fromWgQuickConfig wgQuickConfig: String, called name: String? = nil) throws { var interfaceConfiguration: InterfaceConfiguration? var peerConfigurations = [PeerConfiguration]() @@ -71,17 +71,15 @@ extension TunnelConfiguration { } else { attributes[key] = value } - if !ignoreUnrecognizedKeys { - let interfaceSectionKeys: Set = ["privatekey", "listenport", "address", "dns", "mtu"] - let peerSectionKeys: Set = ["publickey", "presharedkey", "allowedips", "endpoint", "persistentkeepalive"] - if parserState == .inInterfaceSection { - guard interfaceSectionKeys.contains(key) else { - throw ParseError.interfaceHasUnrecognizedKey(keyWithCase) - } - } else if parserState == .inPeerSection { - guard peerSectionKeys.contains(key) else { - throw ParseError.peerHasUnrecognizedKey(keyWithCase) - } + let interfaceSectionKeys: Set = ["privatekey", "listenport", "address", "dns", "mtu"] + let peerSectionKeys: Set = ["publickey", "presharedkey", "allowedips", "endpoint", "persistentkeepalive"] + if parserState == .inInterfaceSection { + guard interfaceSectionKeys.contains(key) else { + throw ParseError.interfaceHasUnrecognizedKey(keyWithCase) + } + } else if parserState == .inPeerSection { + guard peerSectionKeys.contains(key) else { + throw ParseError.peerHasUnrecognizedKey(keyWithCase) } } } else if lowercasedLine != "[interface]" && lowercasedLine != "[peer]" { diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift index 42cc681..8143787 100644 --- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift +++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift @@ -128,7 +128,7 @@ class TunnelEditViewController: NSViewController { return } do { - let tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value, ignoreUnrecognizedKeys: false) + let tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value) let onDemandSetting = ActivateOnDemandSetting.defaultSetting tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, activateOnDemandSetting: onDemandSetting) { [weak self] error in if let error = error {