Skip to content

fix: validate server URL has a host #57

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

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
35 changes: 30 additions & 5 deletions Coder Desktop/Coder Desktop/Views/LoginForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ struct LoginForm<S: Session>: View {
guard sessionToken != "" else {
return
}
guard let url = URL(string: baseAccessURL), url.scheme == "https" else {
loginError = .invalidURL
let url: URL
do {
url = try validateURL(baseAccessURL)
} catch {
loginError = error
return
}
loading = true
Expand Down Expand Up @@ -152,8 +155,10 @@ struct LoginForm<S: Session>: View {
guard baseAccessURL != "" else {
return
}
guard let url = URL(string: baseAccessURL), url.scheme == "https" else {
loginError = .invalidURL
do {
try validateURL(baseAccessURL)
} catch {
loginError = error
return
}
withAnimation {
Expand All @@ -170,12 +175,32 @@ struct LoginForm<S: Session>: View {
}
}

enum LoginError {
@discardableResult
func validateURL(_ url: String) throws(LoginError) -> URL {
guard let url = URL(string: url) else {
throw LoginError.invalidURL
}
guard url.scheme == "https" else {
throw LoginError.httpsRequired
}
guard url.host != nil else {
throw LoginError.noHost
}
return url
}

enum LoginError: Error {
case httpsRequired
case noHost
case invalidURL
case failedAuth(ClientError)

var description: String {
switch self {
case .httpsRequired:
"URL must use HTTPS"
case .noHost:
"URL must have a host"
case .invalidURL:
"Invalid URL"
case let .failedAuth(err):
Expand Down
Loading