Authentication challenge delegate method won't be called if use system default proxy with credentials

Environments: MacOS 12.5.1 A squid proxy sever is set up on my local machine(127.0.0.1 with port 3128), and it requires username and password. In System Preferences->Network->Advanced, enable "Secure Web Proxy (HTTPS)", input the server 127.0.0.1 and port 3128, and do not enable "Proxy server requires password".

Open a website on Chrome, an dialog will pop up to ask user to input the credentials for proxy server 127.0.0.1.

I want to do the same but met this issue. As I understand, the delegate method - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler should be called after [mTask resume], but the actual behaviour is - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error was called, and the current session was terminated. Error message in console is "[] nw_http_connect_send_auth_request_block_invoke Proxy authentication failed with error -1002, cancelling connection".

BTW, I tried put proxy server and port into connectionProxyDictionary, then didReceiveChallenge can be called. AM I misuse the APIs? or this is a bug in MSURLSession?

Here is the codes for testing:

// proxyHandler.h

#import <Foundation/Foundation.h>

@interface proxyHandler : NSObject <NSURLSessionTaskDelegate>

- (void)submitRequest;

@end

//proxyHandler.m

#import "proxyHandler.h"

@interface proxyHandler()
{
    NSURLSession *mSession;
    NSURLSessionTask *mTask;
}

@end

@implementation proxyHandler

- (void)submitRequest
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; // connectionProxyDictionary is NULL, which means that tasks use the default system settings.
    mSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]];
    mTask = [mSession dataTaskWithRequest:request];
    [mTask resume];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
    NSLog(@"Task compelter with error domain : %s, error code: %ld\n", [error.domain UTF8String], error.code);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
    NSLog(@"Task challenge: %@, isProxy: %d", challenge.protectionSpace.authenticationMethod, [challenge.protectionSpace isProxy]);
}

@end
Answered by lesterhan in 745203022

This is a defect in NSURLSession API, confirmed by Apple.

Any advice is greatly appreciated.

Accepted Answer

This is a defect in NSURLSession API, confirmed by Apple.

Authentication challenge delegate method won't be called if use system default proxy with credentials
 
 
Q