Skip to content

Commit

Permalink
Check AddressCache as well when overriding hostname for SSL validation
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet committed Oct 14, 2024
1 parent 12ccbe3 commit 2cdf896
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 9 deletions.
6 changes: 4 additions & 2 deletions ios/MullvadREST/ApiHandlers/RESTURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
//

import Foundation
import Network

extension REST {
public static func makeURLSession() -> URLSession {
public static func makeURLSession(addressCache: AddressCache) -> URLSession {
let certificatePath = Bundle(for: SSLPinningURLSessionDelegate.self)
.path(forResource: "le_root_cert", ofType: "cer")!
let data = FileManager.default.contents(atPath: certificatePath)!
let secCertificate = SecCertificateCreateWithData(nil, data as CFData)!

let sessionDelegate = SSLPinningURLSessionDelegate(
sslHostname: defaultAPIHostname,
trustedRootCertificates: [secCertificate]
trustedRootCertificates: [secCertificate],
addressCache: addressCache
)

let sessionConfiguration = URLSessionConfiguration.ephemeral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import Security
final class SSLPinningURLSessionDelegate: NSObject, URLSessionDelegate {
private let sslHostname: String
private let trustedRootCertificates: [SecCertificate]
private let addressCache: REST.AddressCache

private let logger = Logger(label: "SSLPinningURLSessionDelegate")

init(sslHostname: String, trustedRootCertificates: [SecCertificate]) {
init(sslHostname: String, trustedRootCertificates: [SecCertificate], addressCache: REST.AddressCache) {
self.sslHostname = sslHostname
self.trustedRootCertificates = trustedRootCertificates
self.addressCache = addressCache
}

// MARK: - URLSessionDelegate
Expand All @@ -40,6 +42,7 @@ final class SSLPinningURLSessionDelegate: NSObject, URLSessionDelegate {
"\(IPv4Address.loopback)",
"\(IPv6Address.loopback)",
"\(REST.defaultAPIEndpoint.ip)",
"\(addressCache.getCurrentEndpoint().ip)",
]
if overridenHostnames.contains(hostName) {
hostName = sslHostname
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ProxyConfigurationTransportProvider {
}

public func makeTransport(with configuration: PersistentProxyConfiguration) throws -> RESTTransport {
let urlSession = REST.makeURLSession()
let urlSession = REST.makeURLSession(addressCache: addressCache)
switch configuration {
case .direct:
return URLSessionTransport(urlSession: urlSession)
Expand Down
2 changes: 1 addition & 1 deletion ios/MullvadVPN/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
accountsProxy: accountsProxy,
transactionLog: .default
)
let urlSessionTransport = URLSessionTransport(urlSession: REST.makeURLSession())
let urlSessionTransport = URLSessionTransport(urlSession: REST.makeURLSession(addressCache: addressCache))
let shadowsocksCache = ShadowsocksConfigurationCache(cacheDirectory: containerURL)
let shadowsocksRelaySelector = ShadowsocksRelaySelector(
relayCache: ipOverrideWrapper
Expand Down
2 changes: 1 addition & 1 deletion ios/MullvadVPN/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate, SettingsMigrationUIHand
accountsProxy: appDelegate.accountsProxy,
outgoingConnectionService: OutgoingConnectionService(
outgoingConnectionProxy: OutgoingConnectionProxy(
urlSession: REST.makeURLSession(),
urlSession: REST.makeURLSession(addressCache: appDelegate.addressCache),
hostname: ApplicationConfiguration.hostName
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Marco Nikic on 2023-10-02.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadREST
@testable import MullvadREST

@testable import MullvadMockData
@testable import MullvadSettings
Expand All @@ -24,6 +24,7 @@ class TunnelManagerTests: XCTestCase {
var accessTokenManager: AccessTokenManagerStub!
var devicesProxy: DevicesProxyStub!
var apiProxy: APIProxyStub!
var addressCache: REST.AddressCache!

var transportProvider: TransportProvider!

Expand All @@ -42,9 +43,13 @@ class TunnelManagerTests: XCTestCase {
accessTokenManager = AccessTokenManagerStub()
devicesProxy = DevicesProxyStub(deviceResult: .success(Device.mock(publicKey: PrivateKey().publicKey)))
apiProxy = APIProxyStub()
addressCache = REST.AddressCache(
canWriteToCache: false,
fileCache: MockFileCache(initialState: .fileNotFound)
)

transportProvider = TransportProvider(
urlSessionTransport: URLSessionTransport(urlSession: REST.makeURLSession()),
urlSessionTransport: URLSessionTransport(urlSession: REST.makeURLSession(addressCache: addressCache)),
addressCache: REST.AddressCache(
canWriteToCache: true,
cacheDirectory: FileManager.default.temporaryDirectory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
ipOverrideWrapper: IPOverrideWrapper,
addressCache: REST.AddressCache
) -> TransportProvider {
let urlSession = REST.makeURLSession()
let urlSession = REST.makeURLSession(addressCache: addressCache)
let urlSessionTransport = URLSessionTransport(urlSession: urlSession)
let shadowsocksCache = ShadowsocksConfigurationCache(cacheDirectory: appContainerURL)

Expand Down

0 comments on commit 2cdf896

Please sign in to comment.