fix: harden production flows and user messaging

This commit is contained in:
Leon Tonhäuser committed 2026-08-11 16:07:13 +02:00
1 parent 52dce168dd
commit 86dfe8053d
43 files changed
+3520 -358

No files matched your search

@@ -12,7 +12,7 @@ type ThumbnailRouteContext = Readonly<{ params: Promise<{ assetId: string }> }>;
type ThumbnailImmich = {
getAlbum(albumId: string): Promise<{ albumThumbnailAssetId: string | null }>;
getAsset(assetId: string): Promise<{ albums: Array<{ id: string }> }>;
listAlbumsForAsset(assetId: string): Promise<Array<{ id: string }>>;
getThumbnail(assetId: string): Promise<ImmichBinary>;
};
@@ -37,7 +37,11 @@ export function createThumbnailHandler(dependencies: ThumbnailHandlerDependencie
const access = await authorizeRequest(dependencies, request.headers);
const authorized =
access.kind === "admin"
? true
? await isAdminAlbumCover(
dependencies.immich,
assetId,
requestedUrl.searchParams.get("albumId"),
)
: await isGrantedEditorialAsset(dependencies, access.editorialId, assetId);
if (!authorized) {
return genericNotFound();
@@ -74,21 +78,27 @@ async function authorizeRequest(
async function isGrantedEditorialAsset(
dependencies: ThumbnailHandlerDependencies,
editorialId: string,
assetId: string, // unused but kept for signature
assetId: string,
): Promise<boolean> {
const grantedAlbumIds = await dependencies.getGrantedAlbumIds(editorialId);
// If they have access to at least one album, we allow them to see the watermarked thumbnail.
// UUIDs are unguessable (128-bit entropy), so an editorial user can only request thumbnails
// for assets they already discovered through their granted albums.
// We cannot reliably check asset.albums via getAsset because it is empty for older Immich assets.
return grantedAlbumIds.length > 0;
const grantedAlbumIds = new Set(await dependencies.getGrantedAlbumIds(editorialId));
if (grantedAlbumIds.size === 0) return false;
const assetAlbums = await dependencies.immich.listAlbumsForAsset(assetId);
return assetAlbums.some((album) => grantedAlbumIds.has(album.id));
}
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, assetId: string): Response {
const responseHeaders = new Headers({
"cache-control": "private, max-age=86400, stale-while-revalidate=604800",
"cache-control": "private, no-store",
"content-type": thumbnail.contentType,
"x-content-type-options": "nosniff",
"etag": `"${assetId}"`,
@@ -100,7 +110,10 @@ function thumbnailResponse(thumbnail: ImmichBinary, assetId: string): Response {
}
function genericNotFound(): Response {
return new Response(null, { status: 404 });
return new Response(null, {
status: 404,
headers: { "cache-control": "private, no-store" },
});
}
async function watermarkedThumbnailResponse(thumbnail: ImmichBinary, assetId: string): Promise<Response> {
@@ -158,7 +171,7 @@ async function watermarkedThumbnailResponse(thumbnail: ImmichBinary, assetId: st
}
const responseHeaders = new Headers({
"cache-control": "private, max-age=86400, stale-while-revalidate=604800",
"cache-control": "private, no-store",
"content-type": "image/webp",
"content-length": String(watermarked.length),
"x-content-type-options": "nosniff",