fix: harden editorial thumbnail authorization

This commit is contained in:
Leon Tonhäuser committed 2026-08-09 22:32:18 +02:00
1 parent 66f0599653
commit 846904f3db
11 files changed
+182 -36

No files matched your search

+16
View File
@@ -104,6 +104,22 @@ test("normalizes descriptions and bounds invalid pagination before sending an up
);
});
test("passes a bounded decimal page string from the SSR query through to Immich", async () => {
const { catalog, immich } = createCatalog();
await catalog.searchVisiblePhotos("editorial-1", { page: "2" });
expect(immich.searchPhotos).toHaveBeenLastCalledWith(expect.objectContaining({ page: 2 }));
await catalog.searchVisiblePhotos("editorial-1", { page: "not-a-page" });
expect(immich.searchPhotos).toHaveBeenLastCalledWith(expect.objectContaining({ page: 1 }));
await catalog.searchVisiblePhotos("editorial-1", { page: "-2" });
expect(immich.searchPhotos).toHaveBeenLastCalledWith(expect.objectContaining({ page: 1 }));
await catalog.searchVisiblePhotos("editorial-1", { page: "100000" });
expect(immich.searchPhotos).toHaveBeenLastCalledWith(expect.objectContaining({ page: 1_000 }));
});
test("only returns the current editorial's albums from the Immich album list", async () => {
const { catalog } = createCatalog({ grantedAlbumIds: ["allowed"] });
+15 -2
View File
@@ -93,10 +93,23 @@ function normalizeDescription(description: unknown): string | undefined {
}
function normalizePage(page: unknown): number {
if (typeof page !== "number" || !Number.isFinite(page) || !Number.isInteger(page)) {
const parsed = parsePage(page);
if (parsed === null) {
return 1;
}
return Math.min(MAX_PHOTO_PAGE, Math.max(1, page));
return Math.min(MAX_PHOTO_PAGE, Math.max(1, parsed));
}
function parsePage(page: unknown): number | null {
if (typeof page === "number") {
return Number.isFinite(page) && Number.isInteger(page) ? page : null;
}
if (typeof page !== "string" || !/^\d+$/.test(page)) {
return null;
}
const parsed = Number(page);
return Number.isSafeInteger(parsed) ? parsed : MAX_PHOTO_PAGE;
}
async function loadAlbums(
+23
View File
@@ -38,6 +38,29 @@ test("lists albums through the documented API with server-side authentication",
await expect(client.listAlbums()).resolves.toEqual([immichAlbum]);
});
test("lists only the albums containing an asset through the documented assetId query", async () => {
server.use(
http.get(`${baseUrl}/api/albums`, ({ request }) => {
const url = new URL(request.url);
expect(url.searchParams.get("assetId")).toBe("asset-1");
expect(request.headers.get("x-api-key")).toBe(apiKey);
return HttpResponse.json([immichAlbum]);
}),
);
await expect(client.listAlbumsForAsset("asset-1")).resolves.toEqual([immichAlbum]);
});
test("validates the narrow asset-album response before authorizing a thumbnail", async () => {
server.use(
http.get(`${baseUrl}/api/albums`, () => HttpResponse.json([{ id: "missing-album-fields" }])),
);
await expect(client.listAlbumsForAsset("asset-1")).rejects.toMatchObject({
code: "IMMICH_INVALID_RESPONSE",
});
});
test("gets album details from the documented album endpoint", async () => {
server.use(
http.get(`${baseUrl}/api/albums/album-1`, ({ request }) => {
+9
View File
@@ -308,6 +308,15 @@ export function createImmichClient({
listAlbums(signal) {
return requestJson<ImmichAlbumSummary[]>("/albums", { method: "GET" }, z.array(albumSummarySchema), signal);
},
listAlbumsForAsset(assetId, signal) {
const query = new URLSearchParams({ assetId });
return requestJson<ImmichAlbumSummary[]>(
`/albums?${query.toString()}`,
{ method: "GET" },
z.array(albumSummarySchema),
signal,
);
},
getAlbum(albumId, signal) {
return requestJson<ImmichAlbum>(
`/albums/${encodeURIComponent(albumId)}`,
+1
View File
@@ -36,6 +36,7 @@ export type ImmichBinary = {
export interface ImmichClient {
listAlbums(signal?: AbortSignal): Promise<ImmichAlbumSummary[]>;
listAlbumsForAsset(assetId: string, signal?: AbortSignal): Promise<ImmichAlbumSummary[]>;
getAlbum(albumId: string, signal?: AbortSignal): Promise<ImmichAlbum>;
searchPhotos(
input: { albumIds: string[]; description?: string; page: number; size: number; type?: "IMAGE" },