Skip to content

Commit

Permalink
Require full IPv4 address string to be treated as valid URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Jan 2, 2024
1 parent e9c344c commit 4dc0cf3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit 4dc0cf3

Please sign in to comment.