Skip to content

Commit

Permalink
Email parsing improved
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstrba committed Oct 15, 2024
1 parent e0c0c85 commit 756afd0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Sources/Common/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,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 @@ -223,6 +239,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

0 comments on commit 756afd0

Please sign in to comment.