A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.
perf(media): revalidate uploaded images with ETags (#7116)
Glare108b498fb395
diff --git a/apps/nextjs/src/app/api/user-medias/[id]/route.ts b/apps/nextjs/src/app/api/user-medias/[id]/route.tsindex e3401c976..2b0b4ffa5 100644--- a/apps/nextjs/src/app/api/user-medias/[id]/route.ts+++ b/apps/nextjs/src/app/api/user-medias/[id]/route.ts@@ -1,12 +1,54 @@+import { createHash } from "node:crypto";+import { notFound } from "next/navigation";import { NextResponse } from "next/server";import type { NextRequest } from "next/server";-import DOMPurify from "isomorphic-dompurify";+import { sanitize } from "isomorphic-dompurify";import { db, eq } from "@homarr/db";import { medias } from "@homarr/db/schema";-export async function GET(_req: NextRequest, props: { params: Promise<{ id: string }> }) {+const hasMatchingValidator = (ifNoneMatch: string | null, etag: string) => {+ if (!ifNoneMatch) return false;++ const value = ifNoneMatch.trim();+ if (value === "*") return true;++ let index = 0;+ let matches = false;+ while (index < value.length) {+ while (value[index] === " " || value[index] === "\t") index++;++ const isWeak = value.startsWith("W/", index);+ if (isWeak) index += 2;+ if (value[index] !== '"') return false;++ const start = index++;+ while (index < value.length && value[index] !== '"') {+ const characterCode = value.charCodeAt(index);+ if (+ characterCode !== 0x21 &&+ !(characterCode >= 0x23 && characterCode <= 0x7e) &&+ !(characterCode >= 0x80 && characterCode <= 0xff)+ ) {+ return false;+ }+ index++;+ }+ if (index === value.length) return false;++ const candidate = value.slice(start, ++index);+ if (candidate === etag) matches = true;++ while (value[index] === " " || value[index] === "\t") index++;+ if (index === value.length) return matches;+ if (value[index++] !== "," || index === value.length) return false;+ }++ return false;+};++export async function GET(request: NextRequest, props: { params: Promise<{ id: string }> }) {const params = await props.params;const image = await db.query.medias.findFirst({where: eq(medias.id, params.id),@@ -25,18 +67,26 @@ export async function GET(_req: NextRequest, props: { params: Promise<{ id: stri// Sanitize SVG content to prevent XSS attacksif (image.contentType === "image/svg+xml" || image.contentType === "image/svg") {const svgText = new TextDecoder().decode(content);- const sanitized = DOMPurify.sanitize(svgText, {+ const sanitized = sanitize(svgText, {USE_PROFILES: { svg: true, svgFilters: true },});content = new TextEncoder().encode(sanitized);}const headers = new Headers();+ const etag = `"${createHash("sha256").update(content).digest("base64url")}"`;+ headers.set("Cache-Control", "private, no-cache");+ headers.set("ETag", etag);headers.set("Content-Type", image.contentType);headers.set("Content-Length", content.length.toString());headers.set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox");headers.set("X-Content-Type-Options", "nosniff");+ if (hasMatchingValidator(request.headers.get("if-none-match"), etag)) {+ headers.delete("Content-Length");+ return new NextResponse(null, { status: 304, headers });+ }+return new NextResponse(content, {status: 200,headers,