From f815c77ca0f356ed8e005dc57ef53d64f62e7349 Mon Sep 17 00:00:00 2001 From: Spinnich Date: Mon, 3 Aug 2026 15:35:41 +0000 Subject: [PATCH] fix(v2): make hashes recoverable without HTTPS and show all firmware hashes The Clipboard API only exists in a secure context, so over plain HTTP the copy button on a hash chip always failed. Because the displayed value is abbreviated in JavaScript, the rest of the hash was never in the DOM either, leaving the full value unreachable on any HTTP install. Clicking a chip now reveals the full value as selectable text whenever a copy cannot land, so Ctrl+C works as a fallback. On HTTP the click no longer attempts the doomed copy, so the misleading error toast is gone. Behaviour over HTTPS is unchanged apart from revealing when a copy throws. The firmware tab only ever showed the MD5, as an unselectable chip with no copy affordance, while the stored CRC and SHA-1 were never displayed. All three now render as copyable chips, matching the ROM files tab. Fixes #4082 Co-Authored-By: Claude Opus 5 --- .../v2/components/Gallery/FirmwareTab.test.ts | 110 ++++++++++++++ .../src/v2/components/Gallery/FirmwareTab.vue | 36 ++--- .../src/v2/components/shared/HashChip.test.ts | 138 ++++++++++++++++++ .../src/v2/components/shared/HashChip.vue | 66 ++++++++- 4 files changed, 327 insertions(+), 23 deletions(-) create mode 100644 frontend/src/v2/components/Gallery/FirmwareTab.test.ts create mode 100644 frontend/src/v2/components/shared/HashChip.test.ts diff --git a/frontend/src/v2/components/Gallery/FirmwareTab.test.ts b/frontend/src/v2/components/Gallery/FirmwareTab.test.ts new file mode 100644 index 000000000..a095f6463 --- /dev/null +++ b/frontend/src/v2/components/Gallery/FirmwareTab.test.ts @@ -0,0 +1,110 @@ +import { shallowMount } from "@vue/test-utils"; +import { describe, expect, it, vi } from "vitest"; +import type { FirmwareSchema } from "@/__generated__"; +import type { Platform } from "@/stores/platforms"; +import HashChip from "@/v2/components/shared/HashChip.vue"; +import FirmwareTab from "./FirmwareTab.vue"; + +vi.mock("vue-i18n", () => ({ + useI18n: () => ({ t: (key: string, fallback?: string) => fallback ?? key }), +})); + +vi.mock("@/services/api/firmware", () => ({ + default: { + getFirmware: vi.fn(), + uploadFirmware: vi.fn(), + deleteFirmware: vi.fn(), + }, +})); + +vi.mock("@/stores/platforms", () => ({ + default: () => ({ update: vi.fn() }), +})); + +vi.mock("@/v2/stores/galleryRoms", () => ({ + default: () => ({ currentPlatform: null, setCurrentPlatform: vi.fn() }), +})); + +vi.mock("@/v2/composables/useCan", () => ({ + useCan: () => ({ value: true }), +})); + +vi.mock("@/v2/composables/useSnackbar", () => ({ + useSnackbar: () => ({ error: vi.fn(), success: vi.fn(), warning: vi.fn() }), +})); + +const CRC = "aabbccdd"; +const MD5 = "0123456789abcdef0123456789abcdef"; +const SHA1 = "0123456789abcdef0123456789abcdef01234567"; + +function firmware(overrides: Partial = {}): FirmwareSchema { + return { + id: 1, + file_name: "disksys.rom", + file_name_no_tags: "disksys", + file_name_no_ext: "disksys", + file_extension: "rom", + file_path: "fds", + file_size_bytes: 8192, + full_path: "fds/disksys.rom", + is_verified: true, + crc_hash: CRC, + md5_hash: MD5, + sha1_hash: SHA1, + missing_from_fs: false, + platform_id: 1, + created_at: "", + updated_at: "", + ...overrides, + } as FirmwareSchema; +} + +function platform(firmwareList: FirmwareSchema[]): Platform { + return { + id: 1, + slug: "fds", + fs_slug: "fds", + name: "Family Computer Disk System", + display_name: "Family Computer Disk System", + rom_count: 0, + firmware_count: firmwareList.length, + firmware: firmwareList, + } as Platform; +} + +function mountTab(firmwareList: FirmwareSchema[]) { + return shallowMount(FirmwareTab, { + props: { platform: platform(firmwareList) }, + global: { + // The whole tab is wrapped in a dropzone whose default slot holds + // the list, and a stubbed component does not render its slots. + stubs: { RDropzone: { template: "
" } }, + }, + }); +} + +function chipEntries(wrapper: ReturnType) { + return wrapper + .findAllComponents(HashChip) + .map((c) => [c.props("label"), c.props("value")]); +} + +describe("FirmwareTab", () => { + // #4082: the MD5 used to render as a plain chip with no copy affordance + // and `user-select: none`, and CRC / SHA-1 were never shown at all. + it("renders a copyable chip for every stored hash", () => { + const wrapper = mountTab([firmware()]); + + expect(chipEntries(wrapper)).toEqual([ + ["SHA-1", SHA1], + ["MD5", MD5], + ["CRC", CRC], + ]); + }); + + it("skips hashes the firmware record does not carry", () => { + const wrapper = mountTab([firmware({ crc_hash: "", sha1_hash: "" })]); + + expect(chipEntries(wrapper)).toEqual([["MD5", MD5]]); + }); +}); diff --git a/frontend/src/v2/components/Gallery/FirmwareTab.vue b/frontend/src/v2/components/Gallery/FirmwareTab.vue index 382609f5d..2cc26bd1e 100644 --- a/frontend/src/v2/components/Gallery/FirmwareTab.vue +++ b/frontend/src/v2/components/Gallery/FirmwareTab.vue @@ -32,6 +32,7 @@ import type { Platform } from "@/stores/platforms"; import storePlatforms from "@/stores/platforms"; import { formatBytes } from "@/utils"; import DeleteFirmwareDialog from "@/v2/components/Gallery/DeleteFirmwareDialog.vue"; +import HashChip from "@/v2/components/shared/HashChip.vue"; import { useCan } from "@/v2/composables/useCan"; import { useSnackbar } from "@/v2/composables/useSnackbar"; import storeGalleryRoms from "@/v2/stores/galleryRoms"; @@ -385,15 +386,24 @@ async function performDelete( {{ formatBytes(f.file_size_bytes) }} - - {{ f.md5_hash }} - + + + ({ + copy: vi.fn(), + clipboard: { isSupported: true }, +})); + +vi.mock("vue-i18n", () => ({ + useI18n: () => ({ + t: (key: string, params?: Record) => + params ? `${key}:${JSON.stringify(params)}` : key, + }), +})); + +vi.mock("@/v2/composables/useClipboard", () => ({ + useClipboard: () => ({ isSupported: clipboard.isSupported, copy }), +})); + +const SHA1 = "0123456789abcdef0123456789abcdef01234567"; +const ABBREVIATED = "012345…234567"; + +function mountChip( + props: Partial<{ label: string; value: string | null }> = {}, +) { + return mount(HashChip, { props: { label: "SHA-1", value: SHA1, ...props } }); +} + +async function click(wrapper: ReturnType) { + await wrapper.find("button").trigger("click"); + await flushPromises(); +} + +beforeEach(() => { + copy.mockReset(); + copy.mockResolvedValue(true); + clipboard.isSupported = true; + document.body.innerHTML = ""; +}); + +describe("HashChip", () => { + it("abbreviates a long value", () => { + const wrapper = mountChip(); + + expect(wrapper.text()).toContain(ABBREVIATED); + expect(wrapper.text()).not.toContain(SHA1); + }); + + it("leaves a short value intact", () => { + const wrapper = mountChip({ label: "CRC", value: "aabbccdd" }); + + expect(wrapper.text()).toContain("aabbccdd"); + }); + + it("keeps the full value in the title so hover still reads it", () => { + const wrapper = mountChip(); + + expect(wrapper.find("button").attributes("title")).toContain(SHA1); + }); + + it("copies the full value, not the abbreviation", async () => { + const wrapper = mountChip(); + + await click(wrapper); + + expect(copy).toHaveBeenCalledWith(SHA1, expect.anything()); + }); + + it("stays abbreviated when the copy succeeds", async () => { + const wrapper = mountChip(); + + await click(wrapper); + + expect(wrapper.text()).toContain(ABBREVIATED); + expect(wrapper.text()).not.toContain(SHA1); + }); + + // The core of #4082: over plain HTTP the clipboard API does not exist, + // so a copy can never succeed. Revealing the value is the only way the + // user can get at it. + it("reveals the full value instead of copying when the clipboard is unavailable", async () => { + clipboard.isSupported = false; + const wrapper = mountChip(); + + await click(wrapper); + + expect(copy).not.toHaveBeenCalled(); + expect(wrapper.text()).toContain(SHA1); + }); + + it("marks a revealed chip so its value can be selected", async () => { + clipboard.isSupported = false; + const wrapper = mountChip(); + + await click(wrapper); + + expect(wrapper.find("button").classes()).toContain( + "r-v2-hash-chip--revealed", + ); + }); + + it("collapses the value again on a second click", async () => { + clipboard.isSupported = false; + const wrapper = mountChip(); + + await click(wrapper); + await click(wrapper); + + expect(wrapper.text()).toContain(ABBREVIATED); + expect(wrapper.text()).not.toContain(SHA1); + }); + + it("still collapses when the page selection is outside the chip", async () => { + clipboard.isSupported = false; + const wrapper = mountChip(); + const outside = document.createElement("p"); + outside.textContent = "selected elsewhere"; + document.body.appendChild(outside); + + await click(wrapper); + window.getSelection()?.selectAllChildren(outside); + await click(wrapper); + + expect(wrapper.text()).toContain(ABBREVIATED); + expect(wrapper.text()).not.toContain(SHA1); + }); + + it("reveals the full value when an attempted copy fails", async () => { + copy.mockResolvedValue(false); + const wrapper = mountChip(); + + await click(wrapper); + + expect(copy).toHaveBeenCalled(); + expect(wrapper.text()).toContain(SHA1); + }); +}); diff --git a/frontend/src/v2/components/shared/HashChip.vue b/frontend/src/v2/components/shared/HashChip.vue index d4eb42e03..436c3b8de 100644 --- a/frontend/src/v2/components/shared/HashChip.vue +++ b/frontend/src/v2/components/shared/HashChip.vue @@ -9,11 +9,17 @@ // * value abbreviation (first 6…last 6) when long; the full // untruncated string is what we copy. // +// The abbreviation is all that reaches the DOM, so when a copy cannot +// land the value would be unreachable. Clicking reveals the full string +// as selectable text instead: outside a secure context the clipboard +// API does not exist at all, so the click never attempts a copy, and a +// copy that fails for any other reason falls back to the same reveal. +// // `compact` switches to the `x-small` size — useful for in-row -// hash clusters where vertical breathing room is tight. The copy +// hash clusters where vertical breathing room is tight. The trailing // icon stays in both sizes so the click affordance is consistent. import { RTag } from "@v2/lib"; -import { computed } from "vue"; +import { computed, ref } from "vue"; import { useI18n } from "vue-i18n"; import { useClipboard } from "@/v2/composables/useClipboard"; @@ -31,6 +37,9 @@ const props = withDefaults( const { t } = useI18n(); const clipboard = useClipboard(); +const root = ref(null); +const revealed = ref(false); + const shortened = computed(() => { if (!props.value) return ""; if (props.value.length <= 14) return props.value; @@ -39,26 +48,56 @@ const shortened = computed(() => { )}`; }); +const displayed = computed(() => + revealed.value ? (props.value ?? "") : shortened.value, +); + +const appendIcon = computed(() => { + if (revealed.value) return "mdi-eye-off-outline"; + return clipboard.isSupported ? "mdi-content-copy" : "mdi-eye-outline"; +}); + +// Scoped to this chip so a selection made elsewhere on the page does not +// leave the collapse click dead. +function hasSelectionInside() { + const selection = window.getSelection(); + if (!selection?.toString() || !selection.anchorNode) return false; + return root.value?.contains(selection.anchorNode) ?? false; +} + async function copy() { if (!props.value) return; - await clipboard.copy(props.value, { + + // Collapsing on the click that ends a drag-select would snatch the + // value back the moment it was highlighted. + if (revealed.value && hasSelectionInside()) return; + + if (!clipboard.isSupported) { + revealed.value = !revealed.value; + return; + } + + const copied = await clipboard.copy(props.value, { successMessage: t("common.clipboard-copied", { label: props.label }), }); + if (!copied) revealed.value = true; }