From b184ab7d6fd327760309afc1cda2ab093587ab70 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Sat, 24 Aug 2024 11:58:10 +0200 Subject: [PATCH] fix bug when url does not have scheme --- .../VerifyContextFactory.swift | 16 ++++++++++++++-- .../VerifyTests/VerifyContextFactoryTests.swift | 10 ++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Sources/WalletConnectVerify/VerifyContextFactory.swift b/Sources/WalletConnectVerify/VerifyContextFactory.swift index 629973e38..6c280f2ef 100644 --- a/Sources/WalletConnectVerify/VerifyContextFactory.swift +++ b/Sources/WalletConnectVerify/VerifyContextFactory.swift @@ -1,7 +1,15 @@ - import Foundation class VerifyContextFactory { + + private func ensureUrlHasScheme(_ urlString: String) -> String { + if urlString.hasPrefix("http://") || urlString.hasPrefix("https://") { + return urlString + } else { + return "https://" + urlString + } + } + public func createVerifyContext(origin: String?, domain: String, isScam: Bool?, isVerified: Bool?) -> VerifyContext { guard isScam != true else { @@ -19,7 +27,11 @@ class VerifyContextFactory { ) } - if let origin, let originUrl = URL(string: origin), let domainUrl = URL(string: domain) { + // Ensure both origin and domain have a scheme + let originWithScheme = origin.map { ensureUrlHasScheme($0) } + let domainWithScheme = ensureUrlHasScheme(domain) + + if let originWithScheme, let originUrl = URL(string: originWithScheme), let domainUrl = URL(string: domainWithScheme) { return VerifyContext( origin: origin, validation: (originUrl.host == domainUrl.host) ? .valid : .invalid diff --git a/Tests/VerifyTests/VerifyContextFactoryTests.swift b/Tests/VerifyTests/VerifyContextFactoryTests.swift index 0d43c2aed..026bfba67 100644 --- a/Tests/VerifyTests/VerifyContextFactoryTests.swift +++ b/Tests/VerifyTests/VerifyContextFactoryTests.swift @@ -49,6 +49,16 @@ class VerifyContextFactoryTests: XCTestCase { XCTAssertEqual(contextWithFalseVerification.validation, .scam) } + func testValidOriginAndDomainWithoutScheme() { + let context = factory.createVerifyContext(origin: "https://dev.lab.web3modal.com", domain: "dev.lab.web3modal.com", isScam: false, isVerified: nil) + XCTAssertEqual(context.validation, .valid) + } + + func testInvalidOriginAndDomainWithoutScheme() { + let context = factory.createVerifyContext(origin: "https://dev.lab.web3modal.com", domain: "different.com", isScam: false, isVerified: nil) + XCTAssertEqual(context.validation, .invalid) + } + // tests for createVerifyContextForLinkMode func testValidUniversalLink() {