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

Email parsing improved #1016

Merged
merged 2 commits into from
Oct 24, 2024
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
24 changes: 23 additions & 1 deletion Sources/Common/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ extension URL {
return true
}

// swiftlint:disable cyclomatic_complexity
/// URL and URLComponents can't cope with emojis and international characters so this routine does some manual processing while trying to
/// retain the input as much as possible.
public init?(trimmedAddressBarString: String) {
Expand Down Expand Up @@ -176,7 +177,23 @@ extension URL {
urlWithScheme.port != nil || urlWithScheme.user != nil {
// could be a local domain but user needs to use the protocol to specify that
// make exception for "localhost"
guard urlWithScheme.host?.contains(".") == true || urlWithScheme.host == .localhost else { return nil }
let hasDomain = urlWithScheme.host?.contains(".") == true
guard hasDomain || urlWithScheme.host == .localhost else { return nil }

let isInvalidUserInfo = {
let hasUser = urlWithScheme.user != nil
let hasPassword = urlWithScheme.password != nil
let hasPath = !urlWithScheme.path.isEmpty
let hasPort = urlWithScheme.port != nil
let hasFragment = urlWithScheme.fragment != nil

return hasUser && !hasPassword && !hasPath && !hasPort && !hasFragment
}()

if isInvalidUserInfo {
return nil
}

self = urlWithScheme
return

Expand Down Expand Up @@ -204,6 +221,7 @@ extension URL {

self.init(punycodeEncodedString: s)
}
// swiftlint:enable cyclomatic_complexity

private init?(punycodeEncodedString: String) {
var s = punycodeEncodedString
Expand All @@ -223,6 +241,10 @@ extension URL {

guard let (authData, urlPart, query) = Self.fixupAndSplitURLString(s) else { return nil }

if (authData?.contains(" ") == true) || urlPart.contains(" ") {
return nil
}

let componentsWithoutQuery = urlPart.split(separator: "/").map(String.init)
guard !componentsWithoutQuery.isEmpty else {
return nil
Expand Down
8 changes: 8 additions & 0 deletions Tests/CommonTests/Extensions/URLExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ final class URLExtensionTests: XCTestCase {
XCTAssertEqual(result.last, .init(name: "another_item", value: "test_2"))
}

func testWhenUserInfoDoesNotContaintPassword_ThenNavigateToSearch() {
XCTAssertNil(URL(trimmedAddressBarString: "[email protected]"))
XCTAssertNil(URL(trimmedAddressBarString: "user: @domain.com"))

XCTAssertEqual(URL(trimmedAddressBarString: "user:,,@domain.com")?.host, "domain.com")
XCTAssertEqual(URL(trimmedAddressBarString: "user:[email protected]")?.host, "domain.com")
}

}

extension String {
Expand Down
Loading