Skip to content

Commit

Permalink
Fix validation of localhost URLs
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
sindresorhus committed Aug 22, 2021
1 parent 44d8d85 commit 18c7d6b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
6 changes: 1 addition & 5 deletions Plash/AddWebsiteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ struct AddWebsiteView: View {
)

private var isURLValid: Bool {
let urlString2 = website.wrappedValue.url.absoluteString
return
(urlString.contains(".") || urlString.hasPrefix("file://"))
&& URL.isValid(string: urlString)
&& (urlString2.contains(".") || urlString2.hasPrefix("file://"))
URL.isValid(string: urlString)
&& website.wrappedValue.url.isValid
}

Expand Down
32 changes: 31 additions & 1 deletion Plash/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3259,6 +3259,8 @@ extension URL {
/**
Create a URL from a human string, gracefully.
By default, it only accepts `localhost` as a TLD-less URL.
```
URL(humanString: "sindresorhus.com")?.absoluteString
//=> "https://sindresorhus.com"
Expand All @@ -3267,7 +3269,35 @@ extension URL {
init?(humanString: String) {
let string = humanString.trimmed

guard !string.isEmpty else {
guard
!string.isEmpty,
!string.hasPrefix("."),
!string.hasSuffix("."),
string != "https://",
string != "http://",
string != "file://"
else {
return nil
}

let isValid = string.contains(".")
|| string.hasPrefix("localhost")
|| string.hasPrefix("file://")

guard
!string.hasPrefix("https://"),
!string.hasPrefix("http://"),
!string.hasPrefix("file://")
else {
guard isValid else {
return nil
}

self.init(string: string)
return
}

guard isValid else {
return nil
}

Expand Down

0 comments on commit 18c7d6b

Please sign in to comment.