From 135a6f30bfc0c6350e8b16b146120457208abebe Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:25:09 -0500 Subject: [PATCH] Refuse a new VM whose name differs from another only in case save without a previousName only validated the name. On Windows kali.json is the existing Kali.json, so the new VM silently replaced the other one's settings and disk path. The wizard and clone dialog already check this, now the backend does too, like rename does. The mock backend refuses it the same way. --- src/main/store.ts | 10 ++++++++++ src/renderer/src/mockApi.ts | 1 + tests/store.test.ts | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/main/store.ts b/src/main/store.ts index 9b15102..f6fe751 100644 --- a/src/main/store.ts +++ b/src/main/store.ts @@ -62,6 +62,12 @@ export function listConfigs(paths: DataPaths): VmConfig[] { .sort((a, b) => a.name.localeCompare(b.name)) } +/** a config file for this name is there already, ignoring case like Windows does */ +function taken(paths: DataPaths, name: string): boolean { + const file = `${name}.json`.toLowerCase() + return existsSync(paths.vms) && readdirSync(paths.vms).some((f) => f.toLowerCase() === file) +} + export function saveConfig(paths: DataPaths, cfg: VmConfig, previousName?: string): VmConfig { const err = validateVmName(cfg.name) if (err) throw new Error(err) @@ -75,6 +81,10 @@ export function saveConfig(paths: DataPaths, cfg: VmConfig, previousName?: strin if (renamed && !caseOnly && existsSync(target)) { throw new Error(`A VM called "${clean.name}" already exists`) } + // a new VM. On Windows kali.json is Kali.json, writing it would replace that VM's settings + if (!previousName && taken(paths, clean.name)) { + throw new Error(`A VM called "${clean.name}" already exists`) + } // write to a temp file first so a crash never leaves a half-written config const tmp = `${target}.tmp` writeFileSync(tmp, JSON.stringify(clean, null, 2), 'utf8') diff --git a/src/renderer/src/mockApi.ts b/src/renderer/src/mockApi.ts index d850fce..626e5d8 100644 --- a/src/renderer/src/mockApi.ts +++ b/src/renderer/src/mockApi.ts @@ -65,6 +65,7 @@ export function installMockApi(): void { list: async () => [...vms], newConfig: async (name) => defaultConfig(name?.trim() || `New VM ${vms.length + 1}`, disks), save: async (cfg, prev) => { + if (!prev && vms.some((v) => v.name.toLowerCase() === cfg.name.toLowerCase())) throw new Error(`A VM called "${cfg.name}" already exists`) if (prev && prev !== cfg.name && profiles[prev]) { profiles[cfg.name] = profiles[prev] delete profiles[prev] diff --git a/tests/store.test.ts b/tests/store.test.ts index 1da2393..ba4eab7 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -114,6 +114,17 @@ describe('saveConfig renames and deleteConfig', () => { expect(readdirSync(paths.vms).sort()).toEqual(['Kali.json', 'Parrot.json']) }) + it('refuses a new VM whose name is taken, ignoring case', () => { + const paths = setup() + saveConfig(paths, { ...defaultConfig('Kali', paths.disks), ramMB: 4096 }) + expect(() => saveConfig(paths, { ...defaultConfig('kali', paths.disks), ramMB: 1024 })).toThrow(/A VM called "kali" already exists/) + expect(() => saveConfig(paths, defaultConfig('Kali', paths.disks))).toThrow(/already exists/) + expect(listConfigs(paths).map((c) => [c.name, c.ramMB])).toEqual([['Kali', 4096]]) + // saving the VM itself (previousName = its own name) still works + saveConfig(paths, { ...defaultConfig('Kali', paths.disks), ramMB: 2048 }, 'Kali') + expect(listConfigs(paths)[0].ramMB).toBe(2048) + }) + it('renames to the same name in different case without losing the config', () => { const paths = setup() saveConfig(paths, { ...defaultConfig('kali', paths.disks), ramMB: 2048 })