From 1c418db907f129ecce6b1c5fb8db449f0f45b1e4 Mon Sep 17 00:00:00 2001 From: Dominik Kapusta Date: Wed, 3 Jan 2024 15:51:32 +0100 Subject: [PATCH] Allow calculations in the address bar (#2298) Task/Issue URL: https://app.asana.com/0/0/1206262563462715/f Description: Treat strings like "1.4" in address bar as search queries, not as IPs (1.0.0.4). --- .swiftlint.yml | 1 + Core/URLExtension.swift | 18 +++++++++++++---- DuckDuckGo.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 4 ++-- DuckDuckGoTests/AppURLsTests.swift | 20 ++++++++++++++++++- LocalPackages/DuckUI/Package.swift | 2 +- LocalPackages/SyncUI/Package.swift | 2 +- LocalPackages/Waitlist/Package.swift | 2 +- 8 files changed, 40 insertions(+), 11 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index ae3959c57f..5808536458 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -95,3 +95,4 @@ excluded: - vendor - LocalPackages/*/Package.swift - PacketTunnelProvider/ProxyServer + - .ruby-lsp diff --git a/Core/URLExtension.swift b/Core/URLExtension.swift index 75c2cf99fe..1d65b8c0b5 100644 --- a/Core/URLExtension.swift +++ b/Core/URLExtension.swift @@ -20,6 +20,7 @@ import Foundation import JavaScriptCore import BrowserServicesKit +import Network extension URL { @@ -48,10 +49,19 @@ extension URL { break case .none: // assume http by default - guard let urlWithScheme = URL(string: URLProtocol.http.scheme + text), - // only allow 2nd+ level domains or "localhost" without scheme - urlWithScheme.host?.contains(".") == true || urlWithScheme.host == .localhost - else { return nil } + guard let urlWithScheme = URL(string: URLProtocol.http.scheme + text), let host = urlWithScheme.host else { + return nil + } + // only allow 2nd+ level domains or "localhost" without scheme + guard host.contains(".") == true || host == .localhost else { + return nil + } + if IPv4Address(host) != nil { + // Require 4 octets specified explicitly for an IPv4 address (avoid 1.4 -> 1.0.0.4 expansion) + guard host.split(separator: ".").count == 4 else { + return nil + } + } url = urlWithScheme default: diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index b06c9d1ea3..25b8245add 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -9225,7 +9225,7 @@ repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit"; requirement = { kind = exactVersion; - version = 100.0.1; + version = 100.0.2; }; }; C14882EB27F211A000D59F0C /* XCRemoteSwiftPackageReference "SwiftSoup" */ = { diff --git a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 7abce468cd..963d9334c6 100644 --- a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/DuckDuckGo/BrowserServicesKit", "state" : { - "revision" : "e9c344c15d550112d02853c07efeaa154e911e2b", - "version" : "100.0.1" + "revision" : "39a5daf268f9fcb57d95e6193eca20f7ea222de6", + "version" : "100.0.2" } }, { diff --git a/DuckDuckGoTests/AppURLsTests.swift b/DuckDuckGoTests/AppURLsTests.swift index 0d2adfdbe1..280e855dcc 100644 --- a/DuckDuckGoTests/AppURLsTests.swift +++ b/DuckDuckGoTests/AppURLsTests.swift @@ -204,6 +204,24 @@ final class AppURLsTests: XCTestCase { XCTAssertEqual(url.getParameter(named: "q"), "query") } + func testSearchUrlCreatesSearchUrlWhenFloatingPointNumberIsPassed() { + let url = URL.makeSearchURL(query: "1.4") + XCTAssertEqual(url?.getParameter(named: "q"), "1.4") + } + + func testSearchUrlCreatesSearchUrlWhenFloatingPointNumbersDivisionIsPassed() { + let url = URL.makeSearchURL(query: "1.4/3.4") + XCTAssertEqual(url?.getParameter(named: "q"), "1.4/3.4") + + let url2 = URL.makeSearchURL(query: "4/3.4") + XCTAssertEqual(url2?.getParameter(named: "q"), "4/3.4") + } + + func testSearchUrlCreatesWebUrlWhenIPv4WithFourOctetsIsPassed() { + let url = URL.makeSearchURL(query: "1.0.0.4/3.4") + XCTAssertEqual(url?.absoluteString, "http://1.0.0.4/3.4") + } + func testExtiUrlCreatesUrlWithAtbParam() throws { let url = URL.makeExtiURL(atb: "x") XCTAssertEqual(url.getParameter(named: "atb"), "x") @@ -272,7 +290,7 @@ final class AppURLsTests: XCTestCase { let result = url.searchQuery XCTAssertNil(result) } - + func testExternalDependencyURLsNotChanged() { XCTAssertEqual(URL.surrogates.absoluteString, "https://staticcdn.duckduckgo.com/surrogates.txt") XCTAssertEqual(URL.privacyConfig.absoluteString, "https://staticcdn.duckduckgo.com/trackerblocking/config/v4/ios-config.json") diff --git a/LocalPackages/DuckUI/Package.swift b/LocalPackages/DuckUI/Package.swift index 349e53d7a9..80e0a08d0f 100644 --- a/LocalPackages/DuckUI/Package.swift +++ b/LocalPackages/DuckUI/Package.swift @@ -31,7 +31,7 @@ let package = Package( targets: ["DuckUI"]) ], dependencies: [ - .package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "100.0.1"), + .package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "100.0.2"), ], targets: [ .target( diff --git a/LocalPackages/SyncUI/Package.swift b/LocalPackages/SyncUI/Package.swift index 205ddbc3f9..09fdb7dc37 100644 --- a/LocalPackages/SyncUI/Package.swift +++ b/LocalPackages/SyncUI/Package.swift @@ -32,7 +32,7 @@ let package = Package( ], dependencies: [ .package(path: "../DuckUI"), - .package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "100.0.1"), + .package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "100.0.2"), .package(url: "https://github.com/duckduckgo/DesignResourcesKit", exact: "2.0.0") ], targets: [ diff --git a/LocalPackages/Waitlist/Package.swift b/LocalPackages/Waitlist/Package.swift index 1ba3524983..a666f90ef7 100644 --- a/LocalPackages/Waitlist/Package.swift +++ b/LocalPackages/Waitlist/Package.swift @@ -15,7 +15,7 @@ let package = Package( targets: ["Waitlist", "WaitlistMocks"]) ], dependencies: [ - .package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "100.0.1"), + .package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "100.0.2"), .package(url: "https://github.com/duckduckgo/DesignResourcesKit", exact: "2.0.0") ], targets: [