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 <noreply@anthropic.com>
This commit is contained in:
Spinnich
2026-08-03 15:35:41 +00:00
parent e7c2362527
commit f815c77ca0
4 changed files with 327 additions and 23 deletions

View File

@@ -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> = {}): 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: "<div><slot /></div>" } },
},
});
}
function chipEntries(wrapper: ReturnType<typeof mountTab>) {
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]]);
});
});

View File

@@ -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(
<RChip size="x-small" variant="translucent">
{{ formatBytes(f.file_size_bytes) }}
</RChip>
<RChip
size="x-small"
variant="translucent"
color="info"
class="r-v2-fw__row-hash"
:title="`MD5: ${f.md5_hash}`"
>
{{ f.md5_hash }}
</RChip>
<HashChip
v-if="f.sha1_hash"
label="SHA-1"
:value="f.sha1_hash"
compact
/>
<HashChip
v-if="f.md5_hash"
label="MD5"
:value="f.md5_hash"
compact
/>
<HashChip
v-if="f.crc_hash"
label="CRC"
:value="f.crc_hash"
compact
/>
<RChip
v-if="f.is_verified"
size="x-small"
@@ -615,14 +625,6 @@ async function performDelete(
flex-wrap: wrap;
gap: 4px;
}
.r-v2-fw__row-hash {
max-width: 220px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-family: var(--r-font-family-mono, monospace);
}
.r-v2-fw__row-actions {
display: inline-flex;
align-items: center;

View File

@@ -0,0 +1,138 @@
import { flushPromises, mount } from "@vue/test-utils";
import { beforeEach, describe, expect, it, vi } from "vitest";
import HashChip from "./HashChip.vue";
const { copy, clipboard } = vi.hoisted(() => ({
copy: vi.fn(),
clipboard: { isSupported: true },
}));
vi.mock("vue-i18n", () => ({
useI18n: () => ({
t: (key: string, params?: Record<string, unknown>) =>
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<typeof mountChip>) {
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);
});
});

View File

@@ -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<HTMLButtonElement | null>(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;
}
</script>
<template>
<button
v-if="value"
ref="root"
type="button"
class="r-v2-hash-chip"
:title="`${label}: ${value} (click to copy)`"
:class="{ 'r-v2-hash-chip--revealed': revealed }"
:title="`${label}: ${value}`"
@click="copy"
>
<RTag
:label="label"
:text="shortened"
append-icon="mdi-content-copy"
:text="displayed"
:append-icon="appendIcon"
:size="compact ? 'x-small' : 'small'"
mono
/>
@@ -88,4 +127,19 @@ async function copy() {
.r-v2-hash-chip:active {
transform: scale(0.98);
}
/* A revealed value exists to be selected by hand, so it opts back into
text selection and wraps instead of widening the row past its
container. */
.r-v2-hash-chip--revealed {
cursor: text;
user-select: text;
max-width: 100%;
}
.r-v2-hash-chip--revealed:active {
transform: none;
}
.r-v2-hash-chip--revealed :deep(.r-tag) {
white-space: normal;
}
</style>