swift-foundation icon indicating copy to clipboard operation
swift-foundation copied to clipboard

Double encoding of query component

Open lesha1201 opened this issue 10 months ago • 1 comments

I've noticed that when parsing some URL with reserved characters in query component, it does percent-encoding of a whole query instead of only invalid characters which is different behavior from Swift 5:

Image Playground

The problem is that there are a lot of URL that don't encode all reserved characters (e.g. [) by RFC3986 and the new behavior might break those URLs when there are already some percent-encoded characters.

I think the old behavior is a more safe approach because it's closer to the parsing of such URL on the web and preserves already encoded parts so it doesn't result in double encoding.

For the context, I'm not a Swift developer and don't know what's the right behavior in this case. Originally, I've encounted that issue when were sharing URL and opening it on iOS. When the URL is https://9953r5-3000.csb.app/?name=[тест and you copy it from the browser, it actually copies https://9953r5-3000.csb.app/?name=[%D1%82%D0%B5%D1%81%D1%82 (notice that [ symbol isn't encoded). And opening it in many iOS apps actually opens https://9953r5-3000.csb.app/?name=%5D%25D1%2582%25D0%25B5%25D1%2581%25D1%2582. Though https://9953r5-3000.csb.app/?name=[%D1%82%D0%B5%D1%81%D1%82 opens fine on all platfroms (including WebKit browsers) but I guess some iOS apps uses Swift URL to parse the URL first and then opens it in a browser.

lesha1201 avatar Feb 25 '25 10:02 lesha1201

https://developer.apple.com/documentation/foundation/url/init(string:encodinginvalidcharacters:)

adozenlines avatar Sep 21 '25 13:09 adozenlines