Skip to content

Commit 4191cf5

Browse files
luizhf42otavio
authored andcommitted
fix(ui): validate key uniqueness in PrivateKeyEdit
1 parent 7923b45 commit 4191cf5

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

ui/src/components/PrivateKeys/PrivateKeyEdit.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,24 @@ const edit = async () => {
285285
} catch (error) {
286286
if ((error as Error).name === "KeyParseError") {
287287
setPassphraseError("Passphrase is not correct.");
288-
} else {
289-
snackbar.showError("Failed to update private key.");
290-
handleError(error as Error);
288+
return;
289+
}
290+
291+
const errorMessage = (error as Error).message;
292+
switch (errorMessage) {
293+
case "both":
294+
setNameError("Name is already used");
295+
privateKeyDataError.value = "Private key data is already used";
296+
break;
297+
case "name":
298+
setNameError("Name is already used");
299+
break;
300+
case "private_key":
301+
privateKeyDataError.value = "Private key data is already used";
302+
break;
303+
default:
304+
snackbar.showError("Failed to update private key.");
305+
handleError(error as Error);
291306
}
292307
}
293308
};

ui/src/store/modules/private_keys.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,34 @@ const usePrivateKeysStore = defineStore("privateKey", () => {
99
privateKeys.value = JSON.parse(localStorage.getItem("privateKeys") || "[]");
1010
};
1111

12-
const addPrivateKey = (newKey: Omit<IPrivateKey, "id">) => {
13-
const existentIds: number[] = [];
12+
const validateKeyUniqueness = (name: string, data: string, currentKeyId?: number) => {
13+
// currentKeyId prevents validating against the key being edited
14+
const hasSameName = privateKeys.value.some((key) => key.id !== currentKeyId && key.name === name);
15+
const hasSameData = privateKeys.value.some((key) => key.id !== currentKeyId && key.data === data);
16+
17+
if (hasSameName && hasSameData) throw new Error("both");
18+
if (hasSameData) throw new Error("private_key");
19+
if (hasSameName) throw new Error("name");
20+
};
1421

15-
privateKeys.value.forEach((key: IPrivateKey) => {
16-
if (key.data === newKey.data && key.name === newKey.name) throw new Error("both");
17-
if (key.data === newKey.data) throw new Error("private_key");
18-
if (key.name === newKey.name) throw new Error("name");
19-
existentIds.push(key.id);
20-
});
22+
const addPrivateKey = (newKey: Omit<IPrivateKey, "id">) => {
23+
validateKeyUniqueness(newKey.name, newKey.data);
2124

25+
const existentIds = privateKeys.value.map((key) => key.id);
2226
const newKeyId = existentIds.length ? Math.max(...existentIds) + 1 : 1;
27+
2328
privateKeys.value.push({ ...newKey, id: newKeyId });
2429
localStorage.setItem("privateKeys", JSON.stringify(privateKeys.value));
2530
};
2631

2732
const editPrivateKey = (updatedKey: IPrivateKey) => {
2833
const index = privateKeys.value.findIndex((key) => key.id === updatedKey.id);
34+
if (index === -1) throw new Error("Key not found");
35+
2936
const existingKey = privateKeys.value[index];
3037

31-
if (existingKey && existingKey.data === updatedKey.data && existingKey.name === updatedKey.name) {
32-
throw new Error();
38+
if (existingKey.name !== updatedKey.name || existingKey.data !== updatedKey.data) {
39+
validateKeyUniqueness(updatedKey.name, updatedKey.data, updatedKey.id);
3340
}
3441

3542
privateKeys.value.splice(index, 1, updatedKey);

ui/tests/components/PrivateKeys/PrivateKeyEdit.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe("Private Key Edit", () => {
9595
});
9696

9797
it("updates the store, emits 'update', and shows a success message on valid submission", async () => {
98+
privateKeysStore.addPrivateKey(mockPrivateKey); // Ensure the key exists in the store
9899
const storeSpy = vi.spyOn(privateKeysStore, "editPrivateKey");
99100

100101
await wrapper.find('[data-test="privatekey-edit-btn"]').trigger("click");

ui/tests/store/modules/private_keys.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe("PrivateKey Store", () => {
3232
});
3333

3434
it("Edits a private key", () => {
35+
const initialPrivateKey = { data: "data1", name: "name1", hasPassphrase: false, fingerprint: "fp1" };
36+
privateKeysStore.addPrivateKey(initialPrivateKey);
3537
const privateKey = { id: 1, data: "data1-updated", name: "name1-updated", hasPassphrase: true, fingerprint: "fp1-updated" };
3638

3739
privateKeysStore.editPrivateKey(privateKey);

0 commit comments

Comments
 (0)