mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 21:02:21 +01:00
fix(frontend): encode the download path segment exactly once
`getDownloadPath` encoded a selected file name but left `fs_name` raw, then `getDownloadLink` ran `encodeURI` over the whole result. Three things went wrong: - a selected file name came back double-encoded, so the browser saved `Game%20(USA).zip` instead of `Game (USA).zip` - `encodeURI` also hit the query string, so `file_ids=29%2C30` reached the backend as one value and `int()` raised on it: copying a link for a multi-file selection produced a 500 - a raw `#` in `fs_name` started the fragment, truncating the path before the request left the browser Encode the segment once in `getDownloadPath` for both branches and let `getDownloadLink` only prepend the origin. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { SimpleRom } from "@/stores/roms";
|
||||
import { getDownloadPath } from "./downloadPath";
|
||||
import { getDownloadLink, getDownloadPath } from "./downloadPath";
|
||||
import { makeRom } from "./rom.fixtures";
|
||||
|
||||
describe("getDownloadPath", () => {
|
||||
@@ -14,7 +14,7 @@ describe("getDownloadPath", () => {
|
||||
] as SimpleRom["files"],
|
||||
});
|
||||
expect(getDownloadPath({ rom })).toBe(
|
||||
"/api/roms/14/content/Maniac Mansion (1989).adf",
|
||||
"/api/roms/14/content/Maniac%20Mansion%20(1989).adf",
|
||||
);
|
||||
});
|
||||
|
||||
@@ -83,3 +83,50 @@ describe("getDownloadPath", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("download URL encoding", () => {
|
||||
const rom = makeRom({
|
||||
id: 24,
|
||||
fs_name: "Game & (USA)",
|
||||
has_nested_single_file: false,
|
||||
files: [
|
||||
{ id: 29, file_name: "Game & (USA) Disk1.adf" },
|
||||
{ id: 30, file_name: "Disk2.adf" },
|
||||
] as SimpleRom["files"],
|
||||
});
|
||||
|
||||
it("encodes a name that would otherwise truncate the URL", () => {
|
||||
// A bare `#` starts the fragment, so an unencoded fs_name loses
|
||||
// everything after it before the request is even sent.
|
||||
const hashRom = makeRom({ id: 7, fs_name: "Game #1 (100%)" });
|
||||
const url = new URL(getDownloadLink({ rom: hashRom }));
|
||||
expect(url.hash).toBe("");
|
||||
expect(decodeURIComponent(url.pathname)).toBe(
|
||||
"/api/roms/7/content/Game #1 (100%)",
|
||||
);
|
||||
});
|
||||
|
||||
it("does not double-encode the selected file name", () => {
|
||||
const url = new URL(getDownloadLink({ rom, fileIDs: [29] }));
|
||||
expect(url.pathname).not.toContain("%25");
|
||||
expect(decodeURIComponent(url.pathname)).toBe(
|
||||
"/api/roms/24/content/Game & (USA) Disk1.adf",
|
||||
);
|
||||
});
|
||||
|
||||
it("leaves file_ids parseable for a multi-file zip link", () => {
|
||||
// The backend splits this on "," and int()s each part, so a re-encoded
|
||||
// comma reaches it as one unparseable value.
|
||||
const url = new URL(getDownloadLink({ rom, fileIDs: [29, 30] }));
|
||||
expect(url.searchParams.get("file_ids")).toBe("29,30");
|
||||
expect(decodeURIComponent(url.pathname)).toBe(
|
||||
"/api/roms/24/content/Game & (USA)",
|
||||
);
|
||||
});
|
||||
|
||||
it("prefixes the path with the origin and nothing else", () => {
|
||||
expect(getDownloadLink({ rom })).toBe(
|
||||
`${window.location.origin}${getDownloadPath({ rom })}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -25,9 +25,12 @@ export function getDownloadPath({
|
||||
? rom.files[0]
|
||||
: undefined;
|
||||
const contentFile = selectedFile ?? nestedFile;
|
||||
const contentName = contentFile
|
||||
? encodeURIComponent(contentFile.file_name)
|
||||
: rom.fs_name;
|
||||
// One path segment, so it is encoded here and never again: callers that
|
||||
// prepend an origin must not re-encode. A bare `#` or `%` in a name would
|
||||
// otherwise truncate the URL or survive as a literal in the zip's name.
|
||||
const contentName = encodeURIComponent(
|
||||
contentFile ? contentFile.file_name : rom.fs_name,
|
||||
);
|
||||
|
||||
return `/api/roms/${rom.id}/content/${contentName}${
|
||||
queryString ? `?${queryString}` : ""
|
||||
@@ -41,7 +44,5 @@ export function getDownloadLink({
|
||||
rom: SimpleRom;
|
||||
fileIDs?: number[];
|
||||
}) {
|
||||
return `${window.location.origin}${encodeURI(
|
||||
getDownloadPath({ rom, fileIDs }),
|
||||
)}`;
|
||||
return `${window.location.origin}${getDownloadPath({ rom, fileIDs })}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user