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

Require full IPv4 address string to be treated as valid URL #616

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions Sources/Common/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//

import Foundation
import Network

extension URL {

Expand Down Expand Up @@ -193,6 +194,12 @@ extension URL {
// could be a local domain but user needs to use the protocol to specify that
return nil
}
if IPv4Address(String(hostname)) != nil {
// Require 4 octets specified explicitly for an IPv4 address (avoid 1.4 -> 1.0.0.4 expansion)
guard hostname.split(separator: ".").count == 4 else {
return nil
}
}
} else {
return nil
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/CommonTests/Extensions/URLExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ final class URLExtensionTests: XCTestCase {
XCTAssertNil("localdomain".url)
}

func testThatIPv4AddressMustContainFourOctets() {
XCTAssertNil("1.4".url)
XCTAssertNil("1.4/3.4".url)
XCTAssertNil("1.0.4".url)
XCTAssertNil("127.0.1".url)

XCTAssertEqual("127.0.0.1".url?.absoluteString, "http://127.0.0.1")
XCTAssertEqual("1.0.0.4/3.4".url?.absoluteString, "http://1.0.0.4/3.4")
}

func testWhenNakedIsCalled_ThenURLWithNoSchemeWWWPrefixAndLastSlashIsReturned() {
let url = URL(string: "http://duckduckgo.com")!
let duplicate = URL(string: "https://www.duckduckgo.com/")!
Expand Down
Loading