fix(network): 禁止公网查询持久化缓存及会话数据
This commit is contained in:
@@ -52,7 +52,9 @@ private actor Deliveries {
|
||||
|
||||
@main struct PublicIPAddressChecks {
|
||||
static func main() async throws {
|
||||
let config = URLSessionConfiguration.ephemeral
|
||||
let config = PublicIPAddressService.sessionConfiguration()
|
||||
precondition(config.urlCache == nil && config.httpCookieStorage == nil && config.urlCredentialStorage == nil)
|
||||
precondition(!config.httpShouldSetCookies && config.requestCachePolicy == .reloadIgnoringLocalCacheData)
|
||||
config.protocolClasses = [IPResponse.self]
|
||||
let session = URLSession(configuration: config)
|
||||
defer { session.invalidateAndCancel() }
|
||||
|
||||
@@ -5,15 +5,23 @@ import Darwin
|
||||
/// This opt-in integration check contacts the same HTTPS endpoint as the app.
|
||||
@main struct SandboxNetworkChecks {
|
||||
static func main() async {
|
||||
var request = URLRequest(url: URL(string: "https://api.ipify.org")!)
|
||||
request.timeoutInterval = 5
|
||||
let before = persistentNetworkFiles()
|
||||
let service = PublicIPAddressService()
|
||||
do {
|
||||
let (_, response) = try await URLSession.shared.data(for: request)
|
||||
guard (response as? HTTPURLResponse)?.statusCode == 200 else {
|
||||
print("FAIL: public-address endpoint did not return HTTP 200")
|
||||
guard await service.address(route: "sandbox-check") != nil else {
|
||||
print("FAIL: production public-address lookup did not return a valid address")
|
||||
exit(1)
|
||||
}
|
||||
print("PASS: signed sandbox public-address HTTPS request returned HTTP 200")
|
||||
// Repeat both the memory-cache path and a fresh route without enabling HTTP storage.
|
||||
guard await service.address(route: "sandbox-check") != nil,
|
||||
await service.address(route: "sandbox-new-route") != nil else { exit(1) }
|
||||
try await Task.sleep(for: .seconds(1))
|
||||
let after = persistentNetworkFiles()
|
||||
guard after == before else {
|
||||
print("FAIL: public-address lookup created or changed persistent network files")
|
||||
exit(1)
|
||||
}
|
||||
print("PASS: signed sandbox production HTTPS, repeat/route lookup and no persistent network files")
|
||||
} catch {
|
||||
// Keep the public address and potentially identifying error URLs out of logs.
|
||||
let error = error as NSError
|
||||
@@ -21,4 +29,19 @@ import Darwin
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
private static func persistentNetworkFiles() -> [String: String] {
|
||||
let library = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Library")
|
||||
var files: [String: String] = [:]
|
||||
for directory in ["Caches", "HTTPStorages", "Cookies", "WebKit"] {
|
||||
let root = library.appendingPathComponent(directory)
|
||||
guard let entries = FileManager.default.enumerator(at: root, includingPropertiesForKeys: [.isRegularFileKey, .fileSizeKey, .contentModificationDateKey],
|
||||
options: []) else { continue }
|
||||
for case let url as URL in entries {
|
||||
guard let value = try? url.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey, .contentModificationDateKey]), value.isRegularFile == true else { continue }
|
||||
files[url.path] = "\(value.fileSize ?? 0)|\(value.contentModificationDate?.timeIntervalSince1970 ?? 0)"
|
||||
}
|
||||
}
|
||||
return files
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user