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

Allow calculations in the address bar #2298

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
4 changes: 2 additions & 2 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9224,8 +9224,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 100.0.1;
branch = "dominik/ipv4-parsing";
kind = branch;
};
};
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"
"branch" : "dominik/ipv4-parsing",
"revision" : "4dc0cf36c7be0d156d628881f788341ad35b3998"
}
},
{
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
Loading