security: fix pre-auth bypasses and restore Immich 3.x compatibility (#378)
Seven security issues from an audit, plus album pages and the map coming up empty against Immich 3.x. Verified against a real Immich 3.0.3 instance in a production build. Security: - Admin session forgery via a hardcoded 'dev-fallback-secret' signing key that bypassed the production AUTH_SECRET guard - /api/map ignored album passwords and returned working asset tokens; rewriting ?size=thumbnail to ?size=original downloaded full-resolution originals from protected albums - Admin login had no rate limit, and the client IP came from a spoofable header, so rotating it bypassed every limit in the app - Raw Immich asset UUIDs were serialized into the RSC payload - Album tokens never expired server-side; a captured token replayed forever - Path traversal into the Immich API via an unvalidated album id - /admin ran with no enforced CSP (only Report-Only reached it) TRUSTED_PROXIES is replaced by TRUSTED_PROXY_HOPS. The old variable matched proxy IPs against the socket peer address, which self-hosted Next.js never exposes, so the check could never pass: unset, the limiter read a spoofable header; set, every request shared one bucket and a single client could rate-limit the whole site. Migrates automatically. Immich 3.x: GET /albums/:id no longer embeds assets. Assets now come from POST /search/metadata with withExif: true — without that flag EXIF is omitted and grid aspect ratios, the lightbox panel and map markers stay empty. Drops support for Immich below 3.0, which now logs a warning. Upgrade: requires Immich 3.0+; set TRUSTED_PROXY_HOPS (nginx = 1); visitors of protected areas and admin sessions must log in once more. 91 unit tests (25 new); the new admin-auth and rate-limit cases were confirmed to fail against the pre-fix code. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -14,10 +14,12 @@ import { checkRateLimit, getClientIp } from '@/lib/rate-limit';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
/** Map data is cached client-side, so a low limit is plenty. */
|
||||
const MAP_RPM = 120;
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
// Rate limit: 120 requests/minute per IP (map data is cached client-side; this guards against abuse)
|
||||
const ip = getClientIp(request);
|
||||
const rl = checkRateLimit(ip, 120);
|
||||
const rl = checkRateLimit(`map:${ip}`, MAP_RPM);
|
||||
if (!rl.success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Too many requests' },
|
||||
@@ -43,28 +45,50 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const locations = await getMapData();
|
||||
|
||||
// Filter locations and albums based on auth
|
||||
const authCache = new Map<string, boolean>();
|
||||
// Filter locations and albums based on auth.
|
||||
// An album is visible only if BOTH its subpage gate (if any) and its own
|
||||
// album-level password gate are satisfied. Standalone albums are NOT
|
||||
// implicitly public — they can carry their own password.
|
||||
const subpageAuthCache = new Map<string, boolean>();
|
||||
const albumAuthCache = new Map<string, boolean>();
|
||||
|
||||
const isSubpageAllowed = (slug: string): boolean => {
|
||||
const cached = subpageAuthCache.get(slug);
|
||||
if (cached !== undefined) return cached;
|
||||
const result = isAuthenticated(slug, getCookie);
|
||||
subpageAuthCache.set(slug, result);
|
||||
return result;
|
||||
};
|
||||
|
||||
const isAlbumAllowed = (albumId: string): boolean => {
|
||||
const cached = albumAuthCache.get(albumId);
|
||||
if (cached !== undefined) return cached;
|
||||
const result = isAuthenticated(albumId, getCookie, 'album');
|
||||
albumAuthCache.set(albumId, result);
|
||||
return result;
|
||||
};
|
||||
|
||||
const publicLocations = locations
|
||||
.map((loc) => {
|
||||
// Only keep albums the user is allowed to see
|
||||
const allowedAlbums = loc.albums.filter((a) => {
|
||||
if (!a.subpageSlug) return true; // Standalone albums are public
|
||||
if (authCache.has(a.subpageSlug)) return authCache.get(a.subpageSlug)!;
|
||||
const result = isAuthenticated(a.subpageSlug, getCookie);
|
||||
authCache.set(a.subpageSlug, result);
|
||||
return result;
|
||||
});
|
||||
const allowedAlbums = loc.albums.filter(
|
||||
(a) => (!a.subpageSlug || isSubpageAllowed(a.subpageSlug)) && isAlbumAllowed(a.id),
|
||||
);
|
||||
|
||||
if (allowedAlbums.length === 0) return null;
|
||||
|
||||
// Aggregate over visible albums only, so neither the marker position,
|
||||
// the photo count, nor the cover asset reveals a protected album.
|
||||
const photoCount = allowedAlbums.reduce((sum, a) => sum + a.photoCount, 0);
|
||||
const latSum = allowedAlbums.reduce((sum, a) => sum + a.latSum, 0);
|
||||
const lngSum = allowedAlbums.reduce((sum, a) => sum + a.lngSum, 0);
|
||||
|
||||
return {
|
||||
city: loc.city,
|
||||
country: loc.country,
|
||||
lat: loc.lat,
|
||||
lng: loc.lng,
|
||||
photoCount: loc.photoCount, // Note: This count might be slightly off if some photos are in hidden albums
|
||||
coverUrl: imageUrl(loc.coverAssetId, 'thumbnail'),
|
||||
lat: latSum / photoCount,
|
||||
lng: lngSum / photoCount,
|
||||
photoCount,
|
||||
coverUrl: imageUrl(allowedAlbums[0].coverAssetId, 'thumbnail'),
|
||||
albums: allowedAlbums.map((a) => ({
|
||||
name: a.name,
|
||||
url: a.subpageSlug ? `/${a.subpageSlug}/${a.slug}` : `/${a.slug}`,
|
||||
|
||||
Reference in New Issue
Block a user