Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate VPN errors before re-throwing them #1054

Merged
merged 8 commits into from
Nov 3, 2024
56 changes: 56 additions & 0 deletions Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,9 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
throw TunnelError.startingTunnelWithoutAuthToken
}
} catch {
// Check that the error is valid and able to be re-thrown to the OS before shutting the tunnel down
let error = validated(error: error)
samsymons marked this conversation as resolved.
Show resolved Hide resolved

if startupOptions.startupMethod == .automaticOnDemand {
// If the VPN was started by on-demand without the basic prerequisites for
// it to work we skip firing pixels. This should only be possible if the
Expand Down Expand Up @@ -723,6 +726,9 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {

providerEvents.fire(.tunnelStartAttempt(.success))
} catch {
// Check that the error is valid and able to be re-thrown to the OS before shutting the tunnel down
let error = validated(error: error)
samsymons marked this conversation as resolved.
Show resolved Hide resolved

if startupOptions.startupMethod == .automaticOnDemand {
// We add a delay when the VPN is started by
// on-demand and there's an error, to avoid frenetic ON/OFF
Expand Down Expand Up @@ -1815,6 +1821,56 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
snoozeTimingStore.reset()
}

// MARK: - Error Validation

enum InvalidDiagnosticError: Error, CustomNSError {
case errorWithInvalidUnderlyingError(Error)

var errorCode: Int {
switch self {
case .errorWithInvalidUnderlyingError(let error):
return (error as NSError).code
}
}

var localizedDescription: String {
switch self {
case .errorWithInvalidUnderlyingError(let error):
return "Error '\(type(of: error))', message: \(error.localizedDescription)"
}
}

var errorUserInfo: [String: Any] {
switch self {
case .errorWithInvalidUnderlyingError(let error):
let newError = NSError(domain: (error as NSError).domain, code: (error as NSError).code)
return [NSUnderlyingErrorKey: newError]
Comment on lines +1854 to +1855
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to know what error violated the underlying error validation, but without actually including its underlying errors - knowing its domain and code should be enough to debug further.

}
}
}

/// Validates that an error object is correctly structured; i.e., only uses an `NSError` instances for its underlying error, etc.
private func validated(error: Error) -> Error {
if containsValidUnderlyingError(error) {
return error
} else {
return InvalidDiagnosticError.errorWithInvalidUnderlyingError(error)
}
}

private func containsValidUnderlyingError(_ error: Error) -> Bool {
let nsError = error as NSError

if let underlyingError = nsError.userInfo[NSUnderlyingErrorKey] as? Error {
return containsValidUnderlyingError(underlyingError)
} else if nsError.userInfo[NSUnderlyingErrorKey] != nil {
// If `NSUnderlyingErrorKey` exists but is not an `Error`, return false
return false
}

return true
}

}

extension WireGuardAdapterError: LocalizedError, CustomDebugStringConvertible {
Expand Down
Loading