Monday, August 14, 2017

NSURLSessionDownloadTask switching from http to https

Leave a Comment

I have an AFHTTPSessionManager created NSURLSessionDownloadTask being used to download a video within an app that sometimes when initialized with an http:80 url will convert it to https:443. This is happening before any connection attempt is being made (I added a custom HTTP protocol class via NSURLSessionConfiguration to the session in order to log when the connection is being made).

By the time the request makes it to the

-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response  

delegate method of my CustomHTTPProtocolDelegate class, the request has been changed to https.

App Transport Security is disabled (NSAllowsArbitraryLoads=true) and this behavior seems to be associated with a particular http-only server (other http-only server have no issue, and the connection is made as http on port 80).

Any idea of what could be going on? Anything else I could do to debug?

Here is how the download task is being created (including the debug custom protocol class):

    NSURLRequest *request = [NSURLRequest requestWithURL:url];     NSURLSessionConfiguration* config = [NSURLSessionConfiguration ephemeralSessionConfiguration];     config.protocolClasses = @[[CustomHTTPProtocol class]];     AFHTTPSessionManager *session = [[AFHTTPSessionManager manager] initWithSessionConfiguration:config];      self.downloadTask = [session downloadTaskWithRequest:request  progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response){ 

...

[UPDATE] This issue is causing us a number of headaches, so to help facilitate troubleshooting, I created a small test project to help narrow in on the problem. My simple project does two things, loads a url into a UIWebView and downloads a file using NSURLSessionDownloadTask. The urls for these actions follow this pattern: WebView URL: https://console.company.com/home.html Download URL: http://data.company.com/file.txt And those hostnames resolve to different IPs (different servers).

If I download the file before navigating the webview, then everything is fine, but if the webview loads its URL first, then the download URL will be switched to HTTPS automatically and the initial request for data will fail. One thought we had was that once iOS opens a TLS tunnel for the HTTPS connection that the webview is creating, that it tries to use that same tunnel for all subsequent *.company.com connections. Or at the very least, it assumes all *.company.com connections must also be TLS.

1 Answers

Answers 1

Figured it out. Both servers were sending a HSTS header for all subdomains. Because the networking layer under NSURLSession observes this header, the calls to the HTTP server were being upgraded to HTTPS prior to leaving the client.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment