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

@@ -8,8 +8,8 @@ import type { ImmichBinary } from "@/modules/immich/types";
type ThumbnailRouteContext = Readonly<{ params: Promise<{ assetId: string }> }>;
type ThumbnailImmich = {
listAlbums(): Promise<Array<{ albumThumbnailAssetId: string | null }>>;
getAlbum(albumId: string): Promise<{ assets: Array<{ id: string; type: "IMAGE" }> }>;
listAlbumsForAsset(assetId: string): Promise<Array<{ id: string }>>;
getAlbum(albumId: string): Promise<{ albumThumbnailAssetId: string | null }>;
getThumbnail(assetId: string): Promise<ImmichBinary>;
};
@@ -25,6 +25,7 @@ type Access = { kind: "admin" } | { kind: "editorial"; editorialId: string };
export function createThumbnailHandler(dependencies: ThumbnailHandlerDependencies) {
return async function GET(request: Request, context: ThumbnailRouteContext): Promise<Response> {
try {
const requestedUrl = new URL(request.url);
const { assetId } = await context.params;
if (!isSafeAssetId(assetId)) {
return genericNotFound();
@@ -33,7 +34,11 @@ export function createThumbnailHandler(dependencies: ThumbnailHandlerDependencie
const access = await authorizeRequest(dependencies, request.headers);
const authorized =
access.kind === "admin"
? await isAdminAlbumCover(dependencies.immich, assetId)
? await isAdminAlbumCover(
dependencies.immich,
assetId,
requestedUrl.searchParams.get("albumId"),
)
: await isGrantedEditorialAsset(dependencies, access.editorialId, assetId);
if (!authorized) {
return genericNotFound();
@@ -74,17 +79,21 @@ async function isGrantedEditorialAsset(
return false;
}
const grantedAlbums = await Promise.all(
grantedAlbumIds.map((albumId) => dependencies.immich.getAlbum(albumId)),
);
return grantedAlbums.some((album) =>
album.assets.some((asset) => asset.id === assetId && asset.type === "IMAGE"),
);
const assetAlbums = await dependencies.immich.listAlbumsForAsset(assetId);
const granted = new Set(grantedAlbumIds);
return assetAlbums.some((album) => granted.has(album.id));
}
async function isAdminAlbumCover(immich: ThumbnailImmich, assetId: string): Promise<boolean> {
const albums = await immich.listAlbums();
return albums.some((album) => album.albumThumbnailAssetId === assetId);
async function isAdminAlbumCover(
immich: ThumbnailImmich,
assetId: string,
albumId: string | null,
): Promise<boolean> {
if (!albumId || !isSafeAssetId(albumId)) {
return false;
}
const album = await immich.getAlbum(albumId);
return album.albumThumbnailAssetId === assetId;
}
function thumbnailResponse(thumbnail: ImmichBinary): Response {