Skip to content
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

[core] fix incorrect conversion of valid url #23331

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/expo-modules-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### 🐛 Bug fixes

- Fix the `View cannot be cast to ViewGroup` exception on Android. ([#23264](https://github.com/expo/expo/pull/23264) by [@lukmccall](https://github.com/lukmccall))
- [iOS] Fix conversion to `URL` type that failed despite receiving a string that contained a valid URL. ([#23331](https://github.com/expo/expo/pull/23331) by [@alanhughes](https://github.com/alanjhughes))

### 💡 Others

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ extension URL: Convertible {
throw Conversions.ConvertingException<URL>(value)
}

// Try to construct the URL object from the string as it came in.
if let url = URL(string: value) {
// If it has no scheme, we assume it was the file path which needs to be recreated to be recognized as the file url.
return url.scheme != nil ? url : URL(fileURLWithPath: value)
// First we try to create a URL without extra encoding, as it came.
if let url = convertToUrl(string: value) {
alanjhughes marked this conversation as resolved.
Show resolved Hide resolved
return url
}

// File path doesn't need to be percent-encoded.
Expand All @@ -29,8 +28,8 @@ extension URL: Convertible {
}

// If we get here, the string is not the file url and may require percent-encoding characters that are not URL-safe according to RFC 3986.
if let encodedValue = percentEncodeUrlString(value), let url = URL(string: encodedValue) {
return url.scheme != nil ? url : URL(fileURLWithPath: value)
if let encodedValue = percentEncodeUrlString(value), let url = convertToUrl(string: encodedValue) {
return url
}

// If it still fails to create the URL object, the string possibly contains characters that must be explicitly percent-encoded beforehand.
Expand Down
10 changes: 10 additions & 0 deletions packages/expo-modules-core/ios/Swift/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ internal func isFileUrlPath(_ path: String) -> Bool {
}
return URL(string: encodedPath)?.scheme == nil
}

internal func convertToUrl(string value: String) -> URL? {
// URLComponents parses and constructs URLs according to RFC 3986.
// For some unusual urls URL(string:) will fail incorrectly
guard let url = URLComponents(string: value)?.url ?? URL(string: value) else {
return nil
}
// If it has no scheme, we assume it was the file path which needs to be recreated to be recognized as the file url.
return url.scheme != nil ? url : URL(fileURLWithPath: value)
}
7 changes: 0 additions & 7 deletions packages/expo-modules-core/ios/Tests/ConvertiblesSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ class ConvertiblesSpec: ExpoSpec {
}
}

it("throws when url contains percent alone") {
// The percent character alone must be percent-encoded to `%25` beforehand, otherwise it should throw an exception.
let urlString = "https://expo.dev/?param=%"

expect({ try URL.convert(from: urlString, appContext: appContext) }).to(throwError(errorType: UrlContainsInvalidCharactersException.self))
}

it("converts from url containing the anchor") {
// The hash is not allowed in the query (requires percent-encoding),
// but we want it to be recognized as the beginning of the fragment,
Expand Down