Skip to content

Commit

Permalink
Allow calculations in the address bar (#2298)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ayoy authored Jan 3, 2024
1 parent 25ceec5 commit 1c418db
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ excluded:
- vendor
- LocalPackages/*/Package.swift
- PacketTunnelProvider/ProxyServer
- .ruby-lsp
18 changes: 14 additions & 4 deletions Core/URLExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import Foundation
import JavaScriptCore
import BrowserServicesKit
import Network

extension URL {

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9225,7 +9225,7 @@
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 100.0.1;
version = 100.0.2;
};
};
C14882EB27F211A000D59F0C /* XCRemoteSwiftPackageReference "SwiftSoup" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
20 changes: 19 additions & 1 deletion DuckDuckGoTests/AppURLsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/DuckUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SyncUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/Waitlist/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit 1c418db

Please sign in to comment.