Skip to content

Duplicate records when use URLProtocol to modify authorization header and ssl pinning challenge #154

@HoaPham98

Description

@HoaPham98

Hi, I have a problems with atlantis when run it in the app that use URLProtocol. Because the app is blackbox so I can't show the code, but I can reproduce it with my guess code

import SwiftUI
import Foundation

final class MyProtocol: URLProtocol {
    override class func canInit(with request: URLRequest) -> Bool {
        if request.allHTTPHeaderFields?["Custom-Header"] == "Test value" {
            return false
        }
        return true
    }
    
    override class func canonicalRequest(for request: URLRequest) -> URLRequest {
        var request = request
        request.addValue("Test value", forHTTPHeaderField: "Custom-Header")
        return request
    }
    
    override func startLoading() {
        URLSession.shared.dataTask(with: request) { data, response, error in
            if let error {
                self.client?.urlProtocol(self, didFailWithError: error)
            } else if let response {
                self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
                if let data {
                    self.client?.urlProtocol(self, didLoad: data)
                }
            }
            self.client?.urlProtocolDidFinishLoading(self)
        }.resume()
    }
    
    override func stopLoading() {
        
    }
}

struct ContentView: View {
    
    private let session: URLSession
    
    init() {
        URLProtocol.registerClass(MyProtocol.self)
        let config = URLSessionConfiguration.default
        config.protocolClasses = [MyProtocol.self]
        session = URLSession(configuration: config)
    }
    
    var body: some View {
        VStack {
            Button("Httpbin") {
                Task {
                    do {
                        let (_, response) = try await session.data(from: URL(string:"https://httpbin.org/get")!)
                        print(response)
                    } catch {
                        print(error)
                    }
                }
            }
        }
        .padding()
    }
}```

When I run this code, and tap to Httpbin, Atlantis will capture 2 records, one has "Custom-Header", one not

Metadata

Metadata

Assignees

Labels

DoneThis Ticket is donebugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions