Skip to content

Commit 3967095

Browse files
authored
fix: validate url syntax before moving on to the next wizard step (#79)
1 parent f4710e2 commit 3967095

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Diff for: src/main/kotlin/com/coder/toolbox/views/AuthWizardPage.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AuthWizardPage(
2020
) : CoderPage(context, context.i18n.ptrl("Authenticate to Coder")) {
2121
private val shouldAutoLogin = MutableStateFlow(initialAutoLogin)
2222

23-
private val signInStep = SignInStep(context)
23+
private val signInStep = SignInStep(context, this::notify)
2424
private val tokenStep = TokenStep(context)
2525
private val connectStep = ConnectStep(context, shouldAutoLogin, this::notify, this::displaySteps, onConnect)
2626

Diff for: src/main/kotlin/com/coder/toolbox/views/SignInStep.kt

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.coder.toolbox.views
22

33
import com.coder.toolbox.CoderToolboxContext
4+
import com.coder.toolbox.util.toURL
45
import com.coder.toolbox.views.state.AuthWizardState
56
import com.jetbrains.toolbox.api.localization.LocalizableString
67
import com.jetbrains.toolbox.api.ui.components.LabelField
@@ -9,14 +10,16 @@ import com.jetbrains.toolbox.api.ui.components.TextField
910
import com.jetbrains.toolbox.api.ui.components.TextType
1011
import com.jetbrains.toolbox.api.ui.components.ValidationErrorField
1112
import kotlinx.coroutines.flow.update
13+
import java.net.MalformedURLException
1214

1315
/**
1416
* A page with a field for providing the Coder deployment URL.
1517
*
1618
* Populates with the provided URL, at which point the user can accept or
1719
* enter their own.
1820
*/
19-
class SignInStep(private val context: CoderToolboxContext) : WizardStep {
21+
class SignInStep(private val context: CoderToolboxContext, private val notify: (String, Throwable) -> Unit) :
22+
WizardStep {
2023
private val urlField = TextField(context.i18n.ptrl("Deployment URL"), "", TextType.General)
2124
private val descriptionField = LabelField(context.i18n.pnotr(""))
2225
private val errorField = ValidationErrorField(context.i18n.pnotr(""))
@@ -53,12 +56,28 @@ class SignInStep(private val context: CoderToolboxContext) : WizardStep {
5356
} else {
5457
url
5558
}
56-
59+
try {
60+
validateRawUrl(url)
61+
} catch (e: MalformedURLException) {
62+
notify("URL is invalid", e)
63+
return false
64+
}
5765
context.secrets.lastDeploymentURL = url
5866
AuthWizardState.goToNextStep()
5967
return true
6068
}
6169

70+
/**
71+
* Throws [MalformedURLException] if the given string violates RFC-2396
72+
*/
73+
private fun validateRawUrl(url: String) {
74+
try {
75+
url.toURL()
76+
} catch (e: Exception) {
77+
throw MalformedURLException(e.message)
78+
}
79+
}
80+
6281
override fun onBack() {
6382
// it's the first step. Can't go anywhere back from here
6483
}

0 commit comments

Comments
 (0)