Skip to content

[6.2] Prevent accessing unicode scalar element with incorrect index. (#1214) #1219

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
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
26 changes: 16 additions & 10 deletions Sources/SwiftDocC/Utility/ValidatedURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,29 @@ private extension StringProtocol {
func addingPercentEncodingIfNeeded(withAllowedCharacters allowedCharacters: CharacterSet) -> String? {
var needsPercentEncoding: Bool {
for (index, character) in unicodeScalars.indexed() where !allowedCharacters.contains(character) {
// Check if the character "%" represents a percent encoded URL.
// Any other disallowed character is an indication that this substring needs percent encoding.
if character == "%" {
// % isn't allowed in a URL fragment but it is also the escape character for percent encoding.
let firstFollowingIndex = unicodeScalars.index(after: index)
let secondFollowingIndex = unicodeScalars.index(after: firstFollowingIndex)

guard secondFollowingIndex < unicodeScalars.endIndex else {
guard self.distance(from: index, to: self.endIndex) >= 2 else {
// There's not two characters after the "%". This "%" can't represent a percent encoded character.
return true
}
// If either of the two following characters aren't hex digits, the "%" doesn't represent a
return !Character(unicodeScalars[firstFollowingIndex]).isHexDigit
|| !Character(unicodeScalars[secondFollowingIndex]).isHexDigit
let firstFollowingIndex = self.index(after: index)
let secondFollowingIndex = self.index(after: firstFollowingIndex)

} else {
// Any other disallowed character is an indication that this substring needs percent encoding.
return true
// Check if the next two characthers represent a percent encoded
// URL.
// If either of the two following characters aren't hex digits,
// the "%" doesn't represent a percent encoded character.
if Character(unicodeScalars[firstFollowingIndex]).isHexDigit,
Character(unicodeScalars[secondFollowingIndex]).isHexDigit
{
// Later characters in the string might require percentage encoding.
continue
}
}
return true
}
return false
}
Expand Down
48 changes: 32 additions & 16 deletions Tests/SwiftDocCTests/Utility/ValidatedURLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ class ValidatedURLTests: XCTestCase {
}

func testQueryIsPartOfPathForAuthoredLinks() throws {

func validate(linkText: String, expectedPath: String, expectedFragment: String? = nil,file: StaticString = #filePath, line: UInt = #line) throws {
let validated = try XCTUnwrap(ValidatedURL(parsingAuthoredLink: linkText), "Failed to parse \(linkText.singleQuoted) as authored link")
XCTAssertNil(validated.components.queryItems, "Authored documentation links don't include query items", file: file, line: line)
XCTAssertEqual(validated.components.path, expectedPath, file: file, line: line)
XCTAssertEqual(validated.components.fragment, expectedFragment, file: file, line: line)
}

// Test return type disambiguation
for linkText in [
"SymbolName/memberName()->Int?",
Expand All @@ -91,14 +99,8 @@ class ValidatedURLTests: XCTestCase {
? "/SymbolName/memberName()->Int?"
: "SymbolName/memberName()->Int?"

let validated = try XCTUnwrap(ValidatedURL(parsingAuthoredLink: linkText), "Failed to parse \(linkText.singleQuoted) as authored link")
XCTAssertNil(validated.components.queryItems, "Authored documentation links don't include query items")
XCTAssertEqual(validated.components.path, expectedPath)

let validatedWithHeading = try XCTUnwrap(ValidatedURL(parsingAuthoredLink: linkText + "#Heading-Name"), "Failed to parse '\(linkText)#Heading-Name' as authored link")
XCTAssertNil(validatedWithHeading.components.queryItems, "Authored documentation links don't include query items")
XCTAssertEqual(validatedWithHeading.components.path, expectedPath)
XCTAssertEqual(validatedWithHeading.components.fragment, "Heading-Name")
try validate(linkText: linkText, expectedPath: expectedPath)
try validate(linkText: linkText + "#Heading-Name", expectedPath: expectedPath, expectedFragment: "Heading-Name")
}

// Test parameter type disambiguation
Expand All @@ -111,15 +113,29 @@ class ValidatedURLTests: XCTestCase {
? "/SymbolName/memberName(with:and:)-(Int?,_)"
: "SymbolName/memberName(with:and:)-(Int?,_)"

let validated = try XCTUnwrap(ValidatedURL(parsingAuthoredLink: linkText), "Failed to parse \(linkText.singleQuoted) as authored link")
XCTAssertNil(validated.components.queryItems, "Authored documentation links don't include query items")
XCTAssertEqual(validated.components.path, expectedPath)

let validatedWithHeading = try XCTUnwrap(ValidatedURL(parsingAuthoredLink: linkText + "#Heading-Name"), "Failed to parse '\(linkText)#Heading-Name' as authored link")
XCTAssertNil(validatedWithHeading.components.queryItems, "Authored documentation links don't include query items")
XCTAssertEqual(validatedWithHeading.components.path, expectedPath)
XCTAssertEqual(validatedWithHeading.components.fragment, "Heading-Name")
try validate(linkText: linkText, expectedPath: expectedPath)
try validate(linkText: linkText + "#Heading-Name", expectedPath: expectedPath, expectedFragment: "Heading-Name")
}

// Test parameter with percent encoding
var linkText = "doc://com.example.test/docc=Whats%20New&version=DocC&Title=[Update]"
var expectedPath = "/docc=Whats%20New&version=DocC&Title=[Update]"
try validate(linkText: linkText, expectedPath: expectedPath)

// Test parameter with percent encoding at the end of the URL
linkText = "doc://com.example.test/docc=Whats%20New&version=DocC&Title=[Update]%20"
expectedPath = "/docc=Whats%20New&version=DocC&Title=[Update]%20"
try validate(linkText: linkText, expectedPath: expectedPath)

// Test parameter without percent encoding
linkText = "doc://com.example.test/docc=WhatsNew&version=DocC&Title=[Update]"
expectedPath = "/docc=WhatsNew&version=DocC&Title=[Update]"
try validate(linkText: linkText, expectedPath: expectedPath)

// Test parameter with special characters
linkText = "doc://com.example.test/テスト"
expectedPath = "/テスト"
try validate(linkText: linkText, expectedPath: expectedPath)
}

func testEscapedFragment() throws {
Expand Down