Skip to content

Commit f1b2fb2

Browse files
authored
Fix bounds checking error when parsing WWW-Authenticate field (#5103)
* add TestURLProtectionSpace.test_createWithInvalidAuth to test parsing an invalid www-authenticate field * add bounds checking to rangeOfTokenPrefix. Fixes Fatal error: Substring index is out of bounds
1 parent 301bafe commit f1b2fb2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Sources/FoundationNetworking/URLSession/HTTP/HTTPMessage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ private extension String.UnicodeScalarView.SubSequence {
433433
var rangeOfTokenPrefix: Range<Index>? {
434434
guard !isEmpty else { return nil }
435435
var end = startIndex
436-
while self[end].isValidMessageToken {
436+
while end != self.endIndex && self[end].isValidMessageToken {
437437
end = self.index(after: end)
438438
}
439439
guard end != startIndex else { return nil }

Tests/Foundation/TestURLProtectionSpace.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,22 @@ class TestURLProtectionSpace : XCTestCase {
203203
XCTAssertEqual(param8_2_2.name, "param")
204204
XCTAssertEqual(param8_2_2.value, "")
205205
}
206+
207+
func test_createWithInvalidAuth() throws {
208+
let headerFields1 = [
209+
"Server": "Microsoft-IIS/10.0",
210+
"request-id": "c71c2202-4013-4d64-9319-d40aba6bbe5c",
211+
"WWW-Authenticate": "fdsfds",
212+
"X-Powered-By": "ASP.NET",
213+
"X-FEServer": "AM6PR0502CA0062",
214+
"Date": "Sat, 04 Apr 2020 16:19:39 GMT",
215+
"Content-Length": "0",
216+
]
217+
let response1 = try XCTUnwrap(HTTPURLResponse(url: URL(string: "https://outlook.office365.com/Microsoft-Server-ActiveSync")!,
218+
statusCode: 401,
219+
httpVersion: "HTTP/1.1",
220+
headerFields: headerFields1))
221+
XCTAssertNil(URLProtectionSpace.create(with: response1))
222+
}
206223
#endif
207224
}

0 commit comments

Comments
 (0)