Skip to content

Allow square brackets in Swift CFURL path, query, and fragment#1802

Merged
jrflat merged 2 commits intoswiftlang:mainfrom
jrflat:cfurl-brackets
Mar 10, 2026
Merged

Allow square brackets in Swift CFURL path, query, and fragment#1802
jrflat merged 2 commits intoswiftlang:mainfrom
jrflat:cfurl-brackets

Conversation

@jrflat
Copy link
Copy Markdown
Contributor

@jrflat jrflat commented Mar 9, 2026

Allow "[" and "]" to exist unencoded in the path, query, and fragment of CFURL.

Motivation:

The old CFURL implementation allowed "[" and "]" anywhere in the URL string due to a quirk of how IPv6 literals were implemented (despite both RFC 3986 and the old RFC 1808 only allowing those characters in the host component). We should match this behavior for the path, query, and fragment components for compatibility with both the old implementation and the WHATWG URL spec as a bonus.

Modifications:

  • Allow "[" and "]" unencoded in the path, query, and fragment components
  • Update the addPercentEscapes function used during string encoding to use the newer, faster URLComponentAllowedSet implementation, which I forgot to do earlier. The older URLComponentAllowedMask bitmasks used by URLComponents and the CFURL/NSURL file path initializers still behave the same as before and as expected for those APIs. We can investigate allowing "[" and "]" for those separately.

Result:

CFURL again allows "[" and "]" in path, query, and fragment components and does not return NULL.

Testing:

CFURL/NSURL unit test with the Swift implementation enabled.

@jrflat jrflat requested a review from a team as a code owner March 9, 2026 18:37
Copy link
Copy Markdown
Contributor

@chloe-yeo chloe-yeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Mar 9, 2026

@swift-ci please test

Copy link
Copy Markdown
Contributor

@itingliu itingliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this codepath is available everywhere, not just behind #if FOUNDATION_FRAMEWORK? If so would it be possible to add a test for this change even though there's no public Swift URL API that exercises this path?

@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Mar 9, 2026

It looks like this codepath is available everywhere, not just behind #if FOUNDATION_FRAMEWORK? If so would it be possible to add a test for this change even though there's no public Swift URL API that exercises this path?

Sure the internal function should be testable, I can add a test

@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Mar 10, 2026

@swift-ci please test

@jrflat jrflat merged commit fb16a93 into swiftlang:main Mar 10, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants