From a8cfe94853e6ea85dc6d83a8818e6b8a361f35ca Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Wed, 18 Nov 2026 20:01:49 +0100 Subject: [PATCH] Harden the setup wizard after the branch review Written-by: Gemini 4 Hydrogen (Antigravity) Written-by: Claude Fable 5.2 --- .../src/main/i18n/strings_de.properties | 2 + .../src/main/i18n/strings_en.properties | 2 + .../view/ApplicationRuntimeLauncher.java | 86 +++++++++++++------ .../view/compose/wizard/SetupWizardContent.kt | 8 +- .../view/compose/wizard/SetupWizardState.kt | 27 +++++- .../view/compose/wizard/SetupWizardWindow.kt | 6 +- ...icationRuntimeLauncherSetupWizardTest.java | 38 ++++++++ .../wizard/SetupWizardContentScreenTest.kt | 12 +++ .../compose/wizard/SetupWizardLauncherTest.kt | 17 +++- .../compose/wizard/SetupWizardStateTest.kt | 63 ++++++++++++++ .../compose/wizard/SetupWizardWriteTest.kt | 2 +- .../kst4contest/model/ChatPreferences.java | 4 + .../model/ChatPreferencesReadResultTest.java | 12 +++ 13 files changed, 248 insertions(+), 31 deletions(-) diff --git a/app-desktop/src/main/i18n/strings_de.properties b/app-desktop/src/main/i18n/strings_de.properties index afd9e6ee..a53f705d 100644 --- a/app-desktop/src/main/i18n/strings_de.properties +++ b/app-desktop/src/main/i18n/strings_de.properties @@ -517,9 +517,11 @@ wizard.source.stationKeep=Station unverändert lassen (Locator {0}) wizard.personal.title=Persönliches wizard.personal.lead=Gehört zum Operator. wizard.personal.name=Anzeigename im Chat +wizard.personal.nameInvalid=Der Name darf kein Zeichen | enthalten. wizard.login.title=Anmeldung bei ON4KST wizard.login.lead=Die Zugangsdaten deines ON4KST-Kontos. wizard.login.callsignInvalid=Das sieht nicht nach einem Rufzeichen aus. +wizard.login.passwordInvalid=Das Passwort darf nicht mit einem Leerzeichen beginnen oder enden und kein Zeichen | enthalten. wizard.site.title=Standort und Bänder wizard.site.lead=Gehört zur Station, nicht zum Operator. wizard.site.locatorInvalid=Sechsstelliger Maidenhead-Locator, zum Beispiel JO50AA. diff --git a/app-desktop/src/main/i18n/strings_en.properties b/app-desktop/src/main/i18n/strings_en.properties index c84f6c7b..3fcf55c3 100644 --- a/app-desktop/src/main/i18n/strings_en.properties +++ b/app-desktop/src/main/i18n/strings_en.properties @@ -526,9 +526,11 @@ wizard.source.stationKeep=Leave the station unchanged (locator {0}) wizard.personal.title=Personal wizard.personal.lead=Belongs to the operator. wizard.personal.name=Name shown in the chat +wizard.personal.nameInvalid=The name must not contain a | character. wizard.login.title=ON4KST login wizard.login.lead=The credentials of your ON4KST account. wizard.login.callsignInvalid=This does not look like a callsign. +wizard.login.passwordInvalid=The password must not begin or end with a blank and must not contain a | character. wizard.site.title=Location and bands wizard.site.lead=Belongs to the station, not to the operator. wizard.site.locatorInvalid=Six-character Maidenhead locator, for example JO50AA. diff --git a/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java b/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java index 1721d779..11b904cf 100644 --- a/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java +++ b/app-desktop/src/main/java/kst4contest/view/ApplicationRuntimeLauncher.java @@ -32,6 +32,13 @@ public final class ApplicationRuntimeLauncher { private static final java.util.concurrent.atomic.AtomicBoolean SETUP_WIZARD_REQUESTED = new java.util.concurrent.atomic.AtomicBoolean(false); + /** + * Guards {@link #switchProfile(OperatorProfile)} against re-entry while window teardown + * inside {@link Kst4ContestApplication#shutdownRuntime()} is still pumping events. + */ + private static final java.util.concurrent.atomic.AtomicBoolean SWITCH_IN_PROGRESS = + new java.util.concurrent.atomic.AtomicBoolean(false); + private ApplicationRuntimeLauncher() { // Utility class. } @@ -83,6 +90,20 @@ public final class ApplicationRuntimeLauncher { return SETUP_WIZARD_REQUESTED.getAndSet(false); } + /** + * Marks a profile switch as in progress if none is currently running. + * + * @return true if this call obtained the mark + */ + static boolean beginSwitch() { + return SWITCH_IN_PROGRESS.compareAndSet(false, true); + } + + /** Clears the in-progress mark of a profile switch. */ + static void endSwitch() { + SWITCH_IN_PROGRESS.set(false); + } + /** * Rebuilds the runtime of the active profile with the setup wizard in front of it. * @@ -100,7 +121,16 @@ public final class ApplicationRuntimeLauncher { } requestSetupWizard(); - return switchProfile(active.getProfile()); + try { + boolean switched = switchProfile(active.getProfile()); + if (!switched) { + consumeSetupWizardRequest(); + } + return switched; + } catch (RuntimeException e) { + consumeSetupWizardRequest(); + throw e; + } } /** @@ -111,33 +141,41 @@ public final class ApplicationRuntimeLauncher { */ public static boolean switchProfile(final OperatorProfile targetProfile) { - if (targetProfile == null) { + if (!beginSwitch()) { return false; } - new OperatorProfileStore().recordLastUsed(targetProfile.getProfileId()); - - if (currentRuntime != null) { - currentRuntime.shutdownRuntime(); - } - - ActiveOperatorProfile.set(OperatorProfilePaths.resolve(targetProfile)); - - Kst4ContestApplication nextRuntime = new Kst4ContestApplication(); - try { - nextRuntime.startRuntime(); - } catch (Exception e) { - // The previous runtime is already gone, so there is nothing left to return to. - LOGGER.log(Level.SEVERE, "Could not start the selected operator profile", e); - Kst4ContestApplication.alertWindowEvent( - "The operator profile could not be started: " + e.getMessage() - + "\n\nKST4Contest has to be closed."); - System.exit(1); - return false; - } + if (targetProfile == null) { + return false; + } - setCurrent(nextRuntime); - return true; + new OperatorProfileStore().recordLastUsed(targetProfile.getProfileId()); + + if (currentRuntime != null) { + currentRuntime.shutdownRuntime(); + } + + ActiveOperatorProfile.set(OperatorProfilePaths.resolve(targetProfile)); + + Kst4ContestApplication nextRuntime = new Kst4ContestApplication(); + + try { + nextRuntime.startRuntime(); + } catch (Exception e) { + // The previous runtime is already gone, so there is nothing left to return to. + LOGGER.log(Level.SEVERE, "Could not start the selected operator profile", e); + Kst4ContestApplication.alertWindowEvent( + "The operator profile could not be started: " + e.getMessage() + + "\n\nKST4Contest has to be closed."); + System.exit(1); + return false; + } + + setCurrent(nextRuntime); + return true; + } finally { + endSwitch(); + } } } diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt index 2d585de4..885a8b06 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt @@ -44,7 +44,7 @@ private fun categoryName(number: Int): String = ChatCategory(number).getChatCate * The wizard's pages and its button row. * * All decisions are in [state]; this draws them. The texts are read from [languageStore] in - * the composition, so the language picker on the first page switches the wizard itself. + * the composition, so the language picker on the personal page switches the wizard itself. * * @param saveFailed whether the last Save could not write the file * @param onLater the operator does not want to set up now; nothing is written @@ -183,6 +183,7 @@ private fun PersonalPage(state: SetupWizardState, strings: Strings, languageStor onSelect = { languageStore.use(it) }, ) Form.text(strings.wizardPersonalName, state.name) { state.name = it } + if (state.nameRefused) Refusal(strings.wizardPersonalNameInvalid) } @Composable @@ -192,6 +193,7 @@ private fun LoginPage(state: SetupWizardState, strings: Strings) { Form.text(strings.stationCallsign, state.callsign) { state.callsign = it } if (state.callsignRefused) Refusal(strings.wizardLoginCallsignInvalid) Form.password(strings.stationPassword, state.password) { state.password = it } + if (state.passwordRefused) Refusal(strings.wizardLoginPasswordInvalid) } @Composable @@ -249,6 +251,10 @@ private fun SummaryPage(state: SetupWizardState, strings: Strings, saveFailed: B if (state.leavesIntegrationsUntouched) Text(strings.wizardSummaryIntegrationsUnchanged) Form.section(strings.wizardSummaryPersonal) { + SummaryRow( + strings.guiLanguage, + if (state.language == SYSTEM_LANGUAGE) strings.guiLanguageSystem else state.language.uppercase(), + ) SummaryRow(strings.wizardPersonalName, state.name.ifBlank { strings.wizardSummaryNone }) Form.button(strings.wizardChange) { state.edit(WizardPage.PERSONAL) } } diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt index e13d7e24..0557db29 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt @@ -204,10 +204,10 @@ class SetupWizardState(prefs: ChatPreferences) { } private val personalComplete: Boolean - get() = name.isNotBlank() + get() = isStorableName(name) private val loginComplete: Boolean - get() = isPlausibleCallsign(callsign) && password.isNotEmpty() + get() = isPlausibleCallsign(callsign) && isStorablePassword(password) private val stationComplete: Boolean get() = isLocator(locator) && bands.isNotEmpty() && category in CATEGORY_NUMBERS && @@ -217,10 +217,18 @@ class SetupWizardState(prefs: ChatPreferences) { val mandatoryComplete: Boolean get() = personalComplete && loginComplete && stationComplete + /** A chat name that was typed and cannot be sent. An empty or blank field is not an error yet. */ + val nameRefused: Boolean + get() = name.isNotBlank() && !isStorableName(name) + /** A callsign that was typed and does not look like one. An empty field is not an error yet. */ val callsignRefused: Boolean get() = callsign.isNotBlank() && !isPlausibleCallsign(callsign) + /** A password that was typed and cannot be stored or sent. An empty field is not an error yet. */ + val passwordRefused: Boolean + get() = password.isNotEmpty() && !isStorablePassword(password) + /** A locator that was typed and is not one. An empty field is not an error yet. */ val locatorRefused: Boolean get() = locator.isNotBlank() && !isLocator(locator) @@ -342,6 +350,21 @@ class SetupWizardState(prefs: ChatPreferences) { /** Whether a text is a six-character Maidenhead locator, in either case. */ fun isLocator(text: String): Boolean = LOCATOR.matches(text.trim()) + /** + * Whether a password can be stored in preferences.xml and sent in an ON4KST login frame: + * not blank, free of surrounding whitespace (which the XML reader trims), and free of + * frame delimiters. + */ + fun isStorablePassword(text: String): Boolean = + text.isNotBlank() && text == text.trim() && '|' !in text && '\r' !in text && '\n' !in text + + /** + * Whether a chat display name can be sent in an ON4KST frame: not blank and free of + * frame delimiters. + */ + fun isStorableName(text: String): Boolean = + text.isNotBlank() && '|' !in text && '\r' !in text && '\n' !in text + /** * The chat category the lowest band belongs to: 1 for 50/70 MHz, 2 for 144/432 MHz, * 3 above. Null without a band. A suggestion only -- the operator confirms it. diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt index edffa68a..c6297620 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt @@ -145,9 +145,11 @@ object SetupWizardLauncher { /* * A file that cannot be read is not a profile to set up: the wizard would start from * compiled-in defaults and Save would write them over whatever the file holds. The - * runtime deals with such a file as it always did. + * runtime deals with such a file as it always did. A throwing read counts as failed too. */ - if (!prefs.readPreferencesFromXmlFile()) return SetupWizardOutcome.NOT_SHOWN + if (!runCatching { prefs.readPreferencesFromXmlFile() }.getOrDefault(false)) { + return SetupWizardOutcome.NOT_SHOWN + } if (!forced && !LoginConfiguration.isUnconfigured(prefs)) return SetupWizardOutcome.NOT_SHOWN diff --git a/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java b/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java index bf45efeb..1aa2b58e 100644 --- a/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java +++ b/app-desktop/src/test/java/kst4contest/view/ApplicationRuntimeLauncherSetupWizardTest.java @@ -1,5 +1,8 @@ package kst4contest.view; +import kst4contest.controller.ActiveOperatorProfile; +import kst4contest.model.OperatorProfile; +import kst4contest.model.OperatorProfileSelection; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -20,4 +23,39 @@ class ApplicationRuntimeLauncherSetupWizardTest { assertTrue(ApplicationRuntimeLauncher.consumeSetupWizardRequest()); assertFalse(ApplicationRuntimeLauncher.consumeSetupWizardRequest(), "the request must not stick"); } + + @Test + void switchGuardAllowsOnlyOneSwitchAtATime() { + try { + assertTrue(ApplicationRuntimeLauncher.beginSwitch()); + assertFalse(ApplicationRuntimeLauncher.beginSwitch(), + "a second switch must be refused while one is in progress"); + } finally { + ApplicationRuntimeLauncher.endSwitch(); + } + + try { + assertTrue(ApplicationRuntimeLauncher.beginSwitch(), + "ending the first switch must release the guard"); + } finally { + ApplicationRuntimeLauncher.endSwitch(); + } + } + + @Test + void rerunSetupWizardWhileSwitchInProgressReturnsFalseAndClearsRequest() { + OperatorProfileSelection previous = ActiveOperatorProfile.get(); + OperatorProfile profile = new OperatorProfile("wizard-guard-test", "Wizard Guard Test", false, false); + ActiveOperatorProfile.set(new OperatorProfileSelection(profile, "preferences.xml", "worked.db", false)); + assertTrue(ApplicationRuntimeLauncher.beginSwitch()); + try { + assertFalse(ApplicationRuntimeLauncher.rerunSetupWizard()); + assertFalse(ApplicationRuntimeLauncher.consumeSetupWizardRequest(), + "a refused rerun must not leave a wizard request behind"); + } finally { + ApplicationRuntimeLauncher.endSwitch(); + ApplicationRuntimeLauncher.consumeSetupWizardRequest(); + ActiveOperatorProfile.set(previous); + } + } } diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt index 79e0ad4c..04d786ec 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt @@ -112,4 +112,16 @@ class SetupWizardContentScreenTest { onNodeWithText("The settings could not be written. Nothing was changed.").assertExists() } + + @Test + fun theSummaryShowsTheChosenLanguage() = runComposeUiTest { + val state = SetupWizardState(configuredProfile().apply { setGuiOptions_language("de") }) + state.chooseLoginSource(WizardSource.KEEP) + state.chooseStationSource(WizardSource.KEEP) + state.next() + state.next() + show(state) + + onNodeWithText("DE").assertExists() + } } diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardLauncherTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardLauncherTest.kt index 6b76d42b..c1fba21f 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardLauncherTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardLauncherTest.kt @@ -12,7 +12,7 @@ import java.nio.file.Path import java.nio.file.StandardCopyOption /** When the wizard opens at all, decided without a window. */ -class SetupWizardLauncherTest(@TempDir private val directory: Path) { +class SetupWizardLauncherTest(@param:TempDir private val directory: Path) { private fun templateFile(): Path { val file = directory.resolve("preferences.xml") @@ -81,6 +81,21 @@ class SetupWizardLauncherTest(@TempDir private val directory: Path) { assertArrayEquals(before, Files.readAllBytes(garbage), "the unreadable file was touched") } + @Test + fun aFileTheReaderThrowsOnNeverOpensTheWizard() { + val file = templateFile() + val original = Files.readString(file) + assertTrue(original.contains("2")) + Files.writeString(file, original.replace("2", "2.0")) + val before = Files.readAllBytes(file) + + assertEquals( + SetupWizardOutcome.NOT_SHOWN, + SetupWizardLauncher.run(ChatPreferences(file), forced = true, neverShown), + ) + assertArrayEquals(before, Files.readAllBytes(file), "the unreadable file was touched") + } + @Test fun aFailedSaveKeepsTheWizardOpen() { val source = ChatPreferences(templateFile()).apply { assertTrue(readPreferencesFromXmlFile()) } diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardStateTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardStateTest.kt index fa8780f3..465e94ba 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardStateTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardStateTest.kt @@ -351,4 +351,67 @@ class SetupWizardStateTest { assertTrue(state.mandatoryComplete) assertFalse(state.pages.contains(WizardPage.SITE)) } + + @Test + fun storablePasswordsAndNamesRejectDelimitersAndOuterWhitespace() { + listOf("secret", "se cret").forEach { + assertTrue(SetupWizardState.isStorablePassword(it), "'$it' must be accepted") + } + listOf("", " ", " secret", "secret ", "se|cret", "se\ncret", "se\rcret").forEach { + assertFalse(SetupWizardState.isStorablePassword(it), "'$it' must be refused") + } + + listOf("Philipp", "Philipp W").forEach { + assertTrue(SetupWizardState.isStorableName(it), "'$it' must be accepted") + } + listOf("", " ", "Phil|ipp", "Phil\nipp", "Phil\ripp").forEach { + assertFalse(SetupWizardState.isStorableName(it), "'$it' must be refused") + } + } + + @Test + fun aRefusedPasswordOrNameIsReportedAndBlocksCompletion() { + val state = SetupWizardState(freshInstallation()) + assertFalse(state.nameRefused) + state.name = " " + assertFalse(state.nameRefused) + assertFalse(state.canGoNext) + state.name = "Phil|ipp" + assertTrue(state.nameRefused) + assertFalse(state.canGoNext) + + state.fillFirstInstallation() + state.back() + assertEquals(WizardPage.LOGIN, state.page) + assertFalse(state.passwordRefused) + + listOf(" secret", "secret ", " ", "se|cret", "se\ncret").forEach { refused -> + state.password = refused + assertTrue(state.passwordRefused, "'$refused' must set passwordRefused") + assertFalse(state.canGoNext, "'$refused' must block Next on login") + assertFalse(state.mandatoryComplete, "'$refused' must block mandatoryComplete") + } + + state.password = "" + assertFalse(state.passwordRefused) + assertFalse(state.canGoNext) + + state.password = "se cret" + assertFalse(state.passwordRefused) + assertTrue(state.canGoNext) + assertTrue(state.mandatoryComplete) + } + + @Test + fun keepingALoginWithAStorablePasswordSkipsTheLoginPage() { + val state = SetupWizardState(configuredProfile()) + state.chooseLoginSource(WizardSource.KEEP) + state.chooseStationSource(WizardSource.KEEP) + state.next() + + assertFalse(state.pages.contains(WizardPage.LOGIN)) + assertEquals(WizardPage.PERSONAL, state.page) + state.next() + assertEquals(WizardPage.SUMMARY, state.page) + } } diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardWriteTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardWriteTest.kt index 8d0ca4a2..63bf424e 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardWriteTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardWriteTest.kt @@ -16,7 +16,7 @@ import java.nio.file.StandardCopyOption * What the wizard does to a real preferences file: nothing until Save, exactly the answers * afterwards, and every setting it never asked about left as it was. */ -class SetupWizardWriteTest(@TempDir private val directory: Path) { +class SetupWizardWriteTest(@param:TempDir private val directory: Path) { /** A copy of the bundled template, read the way the runtime reads a profile. */ private fun templateFile(): Path { diff --git a/core/src/main/java/kst4contest/model/ChatPreferences.java b/core/src/main/java/kst4contest/model/ChatPreferences.java index 4bd4ba12..d754467b 100644 --- a/core/src/main/java/kst4contest/model/ChatPreferences.java +++ b/core/src/main/java/kst4contest/model/ChatPreferences.java @@ -2619,6 +2619,10 @@ public class ChatPreferences { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(xmlConfigFile); + Element rootElement = doc.getDocumentElement(); + if (rootElement == null || !"praktiKST".equals(rootElement.getTagName())) { + readable = false; + } NodeList list; // --- Schema version (optional) --- diff --git a/core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java b/core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java index 6f1e484b..59c2d16f 100644 --- a/core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java +++ b/core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java @@ -45,6 +45,18 @@ class ChatPreferencesReadResultTest { assertFalse(Files.exists(file), "reading a missing file must not create it"); } + @Test + void aWellFormedXmlWithForeignRootIsReportedAsUnreadable() throws IOException { + Path file = temporaryDirectory.resolve("foreign.xml"); + byte[] original = "DL0TEST" + .getBytes(StandardCharsets.UTF_8); + Files.write(file, original); + + ChatPreferences preferences = new ChatPreferences(file); + assertFalse(preferences.readPreferencesFromXmlFile(), "a foreign XML root must be reported as unreadable"); + assertArrayEquals(original, Files.readAllBytes(file), "the foreign XML file must be left untouched"); + } + @Test void theBundledTemplateIsReadable() throws IOException { Path templateCopy = temporaryDirectory.resolve("template.xml");