Skip to content

NSURL dataRepresentation inits should allow some invalid URLs the old parser accepted#1810

Merged
jrflat merged 2 commits intoswiftlang:mainfrom
jrflat:lenient-data-representation-inits
Apr 2, 2026
Merged

NSURL dataRepresentation inits should allow some invalid URLs the old parser accepted#1810
jrflat merged 2 commits intoswiftlang:mainfrom
jrflat:lenient-data-representation-inits

Conversation

@jrflat
Copy link
Copy Markdown
Contributor

@jrflat jrflat commented Mar 10, 2026

Fix Swift CFURL parsing to accept some invalid URL strings that the old parser accepted for data representation inits.

Motivation:

CFURLCreateWithBytes and CFURLCreateAbsoluteURLWithBytes generally accept any input and percent-encode it until it's valid. However, the new Swift code path for these functions rejected some invalid URL formats. This caused +[NSURL (absolute)URLWithDataRepresentation:] to return nil for data that previously succeeded, such as IP-literal hosts with invalid characters, unterminated [ brackets, and relative paths with : in the first segment.

Modifications:

  • Gate the strict IP-literal content, unterminated [, and colon-in-relative-path rejection on useModernParsing: true, so the CFURL byte encoding path is as lenient as the old parser.
  • When encoding an invalid IP-literal for CFURL, preserve the [ and ] bracket delimiters and only percent-encode the inner content.
  • Use span.extracting(_:) more often for safer index validation.

Result:

Data representation inits accept the same URL strings as the old parser.

Testing:

Added unit tests for data representation NSURLs with invalid IP-literals, unterminated brackets, spaces in hosts, and relative paths with colons.

@jrflat jrflat requested a review from a team as a code owner March 10, 2026 18:59
@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Mar 10, 2026

@swift-ci please test

@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Mar 10, 2026

@swift-ci please test Windows Platform

@jrflat jrflat force-pushed the lenient-data-representation-inits branch from 868a437 to fa99eef Compare March 27, 2026 22:27
@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Mar 27, 2026

@swift-ci please test

@jrflat
Copy link
Copy Markdown
Contributor Author

jrflat commented Mar 31, 2026

@swift-ci please test

@jrflat jrflat merged commit bdc15b6 into swiftlang:main Apr 2, 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.

2 participants