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

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

Open
HoaPham98 opened this issue Nov 7, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@HoaPham98
Copy link

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
@NghiaTranUIT
Copy link
Member

@HoaPham98 thanks for the report. It looks like a bug.

May I ask: If you don't use URLProtocol, is the bug gone?

@NghiaTranUIT
Copy link
Member

I'm able to reproduce the bug, let me find a way to fix it 👍

@NghiaTranUIT NghiaTranUIT self-assigned this Nov 7, 2024
@NghiaTranUIT NghiaTranUIT added the bug Something isn't working label Nov 7, 2024
@HoaPham98
Copy link
Author

@HoaPham98 thanks for the report. It looks like a bug.

May I ask: If you don't use URLProtocol, is the bug gone?

Yes. If I don't use URLProtocol, the bus is gone. And I have an idea for this issue. Hope you like to check it: #155

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants