Skip to content

fix: validate url syntax before moving on to the next wizard step #79

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
Apr 11, 2025
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
2 changes: 1 addition & 1 deletion src/main/kotlin/com/coder/toolbox/views/AuthWizardPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AuthWizardPage(
) : CoderPage(context, context.i18n.ptrl("Authenticate to Coder")) {
private val shouldAutoLogin = MutableStateFlow(initialAutoLogin)

private val signInStep = SignInStep(context)
private val signInStep = SignInStep(context, this::notify)
private val tokenStep = TokenStep(context)
private val connectStep = ConnectStep(context, shouldAutoLogin, this::notify, this::displaySteps, onConnect)

Expand Down
23 changes: 21 additions & 2 deletions src/main/kotlin/com/coder/toolbox/views/SignInStep.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.coder.toolbox.views

import com.coder.toolbox.CoderToolboxContext
import com.coder.toolbox.util.toURL
import com.coder.toolbox.views.state.AuthWizardState
import com.jetbrains.toolbox.api.localization.LocalizableString
import com.jetbrains.toolbox.api.ui.components.LabelField
Expand All @@ -9,14 +10,16 @@ import com.jetbrains.toolbox.api.ui.components.TextField
import com.jetbrains.toolbox.api.ui.components.TextType
import com.jetbrains.toolbox.api.ui.components.ValidationErrorField
import kotlinx.coroutines.flow.update
import java.net.MalformedURLException

/**
* A page with a field for providing the Coder deployment URL.
*
* Populates with the provided URL, at which point the user can accept or
* enter their own.
*/
class SignInStep(private val context: CoderToolboxContext) : WizardStep {
class SignInStep(private val context: CoderToolboxContext, private val notify: (String, Throwable) -> Unit) :
WizardStep {
private val urlField = TextField(context.i18n.ptrl("Deployment URL"), "", TextType.General)
private val descriptionField = LabelField(context.i18n.pnotr(""))
private val errorField = ValidationErrorField(context.i18n.pnotr(""))
Expand Down Expand Up @@ -53,12 +56,28 @@ class SignInStep(private val context: CoderToolboxContext) : WizardStep {
} else {
url
}

try {
validateRawUrl(url)
} catch (e: MalformedURLException) {
notify("URL is invalid", e)
return false
}
context.secrets.lastDeploymentURL = url
AuthWizardState.goToNextStep()
return true
}

/**
* Throws [MalformedURLException] if the given string violates RFC-2396
*/
private fun validateRawUrl(url: String) {
try {
url.toURL()
} catch (e: Exception) {
throw MalformedURLException(e.message)
}
}

override fun onBack() {
// it's the first step. Can't go anywhere back from here
}
Expand Down
Loading