Skip to content

Commit e3410e3

Browse files
committed
[SR-10281] URLSession crash while handling basic authentication
1 parent 7741f0d commit e3410e3

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Foundation/URLSession/URLSessionTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,11 @@ extension _ProtocolClient : URLProtocolClient {
574574
sender: `protocol` as! _HTTPURLProtocol)
575575
task.previousFailureCount += 1
576576
urlProtocol(`protocol`, didReceive: authenticationChallenge)
577-
return
578577
} else {
579578
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: NSURLErrorUserAuthenticationRequired, userInfo: nil))
580579
urlProtocol(`protocol`, didFailWithError: urlError)
581580
}
581+
return
582582
}
583583
switch session.behaviour(for: task) {
584584
case .taskDelegate(let delegate):

TestFoundation/HTTPServer.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,13 @@ class _HTTPServer {
335335
try self.socket.writeRawData(responseData)
336336
}
337337

338-
338+
func respondWithUnauthorizedHeader() throws{
339+
let responseData = ("HTTP/1.1 401 UNAUTHORIZED \r\n" +
340+
"Content-Length: 0\r\n" +
341+
"Connection: keep-Alive\r\n" +
342+
"\r\n").data(using: .utf8)!
343+
try self.socket.writeRawData(responseData)
344+
}
339345
}
340346

341347
struct _HTTPRequest {
@@ -453,6 +459,8 @@ public class TestURLSessionServer {
453459
} else if req.uri.hasPrefix("/auth") {
454460
httpServer.willReadAgain = true
455461
try httpServer.respondWithAuthResponse(uri: req.uri, firstRead: true)
462+
} else if req.uri.hasPrefix("/unauthorized") {
463+
try httpServer.respondWithUnauthorizedHeader()
456464
} else {
457465
try httpServer.respond(with: process(request: req), startDelay: self.startDelay, sendDelay: self.sendDelay, bodyChunks: self.bodyChunks)
458466
}

TestFoundation/TestURLSession.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TestURLSession : LoopbackServerTest {
4747
("test_basicAuthRequest", test_basicAuthRequest),
4848
("test_redirectionWithSetCookies", test_redirectionWithSetCookies),
4949
("test_postWithEmptyBody", test_postWithEmptyBody),
50+
("test_basicAuthWithUnauthorizedHeader", test_basicAuthWithUnauthorizedHeader),
5051
]
5152
}
5253

@@ -758,6 +759,20 @@ class TestURLSession : LoopbackServerTest {
758759
task.resume()
759760
waitForExpectations(timeout: 30)
760761
}
762+
763+
func test_basicAuthWithUnauthorizedHeader() {
764+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/unauthorized"
765+
let url = URL(string: urlString)!
766+
let expect = expectation(description: "GET \(urlString): with a completion handler")
767+
var expectedResult = "unknown"
768+
let session = URLSession(configuration: URLSessionConfiguration.default)
769+
let task = session.dataTask(with: url) { _, _, error in
770+
defer { expect.fulfill() }
771+
XCTAssertNotNil(error)
772+
}
773+
task.resume()
774+
waitForExpectations(timeout: 12, handler: nil)
775+
}
761776
}
762777

763778
class SharedDelegate: NSObject {

0 commit comments

Comments
 (0)