Skip to content

[SR-10281] URLSession crash while handling basic authentication #2061

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

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Foundation/URLSession/URLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,11 @@ extension _ProtocolClient : URLProtocolClient {
sender: `protocol` as! _HTTPURLProtocol)
task.previousFailureCount += 1
urlProtocol(`protocol`, didReceive: authenticationChallenge)
return
} else {
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: NSURLErrorUserAuthenticationRequired, userInfo: nil))
urlProtocol(`protocol`, didFailWithError: urlError)
}
return
}
switch session.behaviour(for: task) {
case .taskDelegate(let delegate):
Expand Down
10 changes: 9 additions & 1 deletion TestFoundation/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,13 @@ class _HTTPServer {
try self.socket.writeRawData(responseData)
}


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

struct _HTTPRequest {
Expand Down Expand Up @@ -453,6 +459,8 @@ public class TestURLSessionServer {
} else if req.uri.hasPrefix("/auth") {
httpServer.willReadAgain = true
try httpServer.respondWithAuthResponse(uri: req.uri, firstRead: true)
} else if req.uri.hasPrefix("/unauthorized") {
try httpServer.respondWithUnauthorizedHeader()
} else {
try httpServer.respond(with: process(request: req), startDelay: self.startDelay, sendDelay: self.sendDelay, bodyChunks: self.bodyChunks)
}
Expand Down
15 changes: 15 additions & 0 deletions TestFoundation/TestURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TestURLSession : LoopbackServerTest {
("test_basicAuthRequest", test_basicAuthRequest),
("test_redirectionWithSetCookies", test_redirectionWithSetCookies),
("test_postWithEmptyBody", test_postWithEmptyBody),
("test_basicAuthWithUnauthorizedHeader", test_basicAuthWithUnauthorizedHeader),
]
}

Expand Down Expand Up @@ -758,6 +759,20 @@ class TestURLSession : LoopbackServerTest {
task.resume()
waitForExpectations(timeout: 30)
}

func test_basicAuthWithUnauthorizedHeader() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/unauthorized"
let url = URL(string: urlString)!
let expect = expectation(description: "GET \(urlString): with a completion handler")
var expectedResult = "unknown"
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: url) { _, _, error in
defer { expect.fulfill() }
XCTAssertNotNil(error)
}
task.resume()
waitForExpectations(timeout: 12, handler: nil)
}
}

class SharedDelegate: NSObject {
Expand Down