From b0331ed1119b5037e885618b62277f8f77be8196 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Fri, 18 Oct 2024 11:48:00 -0400 Subject: [PATCH] Add safety around missing providerConfiguration --- .../PacketTunnelProvider.swift | 16 +++++++++++----- ios/NebulaNetworkExtension/Site.swift | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ios/NebulaNetworkExtension/PacketTunnelProvider.swift b/ios/NebulaNetworkExtension/PacketTunnelProvider.swift index 215cf02..336b71f 100644 --- a/ios/NebulaNetworkExtension/PacketTunnelProvider.swift +++ b/ios/NebulaNetworkExtension/PacketTunnelProvider.swift @@ -7,6 +7,7 @@ enum VPNStartError: Error { case noManagers case couldNotFindManager case noTunFileDescriptor + case noProviderConfig } class PacketTunnelProvider: NEPacketTunnelProvider { @@ -115,15 +116,20 @@ class PacketTunnelProvider: NEPacketTunnelProvider { // } private func findManager() async throws -> NETunnelProviderManager { - let targetProtoConfig = self.protocolConfiguration as? NETunnelProviderProtocol; - let targetID = targetProtoConfig?.providerConfiguration!["id"] as? String; + let targetProtoConfig = self.protocolConfiguration as? NETunnelProviderProtocol + guard let targetProviderConfig = targetProtoConfig?.providerConfiguration else { + throw VPNStartError.noProviderConfig + } + let targetID = targetProviderConfig["id"] as? String // Load vpn configs from system, and find the manager matching the one being started let managers = try await NETunnelProviderManager.loadAllFromPreferences() for manager in managers { - let mgrProtoConfig = manager.protocolConfiguration as? NETunnelProviderProtocol; - let id = mgrProtoConfig?.providerConfiguration!["id"] as? String; - + let mgrProtoConfig = manager.protocolConfiguration as? NETunnelProviderProtocol + guard let mgrProviderConfig = mgrProtoConfig?.providerConfiguration else { + throw VPNStartError.noProviderConfig + } + let id = mgrProviderConfig["id"] as? String if (id == targetID) { return manager } diff --git a/ios/NebulaNetworkExtension/Site.swift b/ios/NebulaNetworkExtension/Site.swift index ef1a3b1..8f034ec 100644 --- a/ios/NebulaNetworkExtension/Site.swift +++ b/ios/NebulaNetworkExtension/Site.swift @@ -489,6 +489,8 @@ struct IncomingSite: Codable { // Stuff our details in the protocol let proto = manager.protocolConfiguration as? NETunnelProviderProtocol ?? NETunnelProviderProtocol() proto.providerBundleIdentifier = "net.defined.mobileNebula.NebulaNetworkExtension"; + // WARN: If we stop setting providerConfiguration["id"] here, we'll need to use something else to match + // managers in PacketTunnelProvider.findManager proto.providerConfiguration = ["id": self.id] proto.serverAddress = "Nebula"