feat: Bild-Upload fuer Inhalte, Produkte und Ernten
Docker Image bauen und veroeffentlichen / build-and-push (push) Successful in 2m34s
Docker Image bauen und veroeffentlichen / build-and-push (push) Successful in 2m34s
Echter Datei-Upload statt CSS-Platzhaltergrafiken, gespeichert unter UPLOAD_DIR (Laufzeit-Verzeichnis, weiss nichts von seiner tatsaechlichen Speicher-Anbindung — in Produktion ein von der Betreiberin selbst per Kubernetes eingebundenes Volume, z.B. SMB; lokal ein simples ./uploads). - lib/uploads.ts: Kernlogik. Bildformat wird ausschliesslich aus den tatsaechlichen Bilddaten erkannt (Magic-Bytes via sharp), nie aus Client-Dateiname/MIME-Type. Zufaellige, nicht erratbare Dateinamen (Path-Traversal-/Overwrite-Schutz). Re-Encoding validiert Pixel-Daten vollstaendig und entfernt eingebettete Metadaten (EXIF/ICC/XMP) als Nebeneffekt. Content-Type beim Ausliefern kommt ausschliesslich aus einer festen Endung->MIME-Zuordnung, nie aus Dateiinhalt/-namen. - app/api/admin/upload/route.ts: Admin-only Upload-Endpunkt (CSRF- + Auth- + Rate-Limit-Pruefung, max. 8 MB, JPEG/PNG/WebP/AVIF). - app/uploads/[...path]/route.ts: liefert hochgeladene Bilder aus (public/ wird zur Build-Zeit fest ins Image gebacken, kann also keine zur Laufzeit hinzugefuegten Dateien bedienen) — mit Path-Traversal-Schutz und nosniff-Header. - content-registry.ts: neuer "image"-Feldtyp fuer Hero-/Startseite- Grafiken, admin/inhalte hat dafuer ein Upload-Widget. - Produkt-/Ernten-Admin-Formulare: Mehrfach-Bild-Upload fuer Product.images/Harvest.images (beide Felder existierten im Schema, hatten aber bislang keine Admin-Oberflaeche). Oeffentliche Seiten (Shop, Ernten, Chargen-QR-Seite) zeigen jetzt echte Fotos statt Platzhalter, sobald vorhanden. - Kubernetes: nur ein einfacher volumeMount in 05-app-deployment.yaml (Platzhalter-emptyDir, klar als CHANGE-ME markiert) — die eigentliche Volume-Quelle (SMB o.ae.) bindet die Betreiberin selbst ein, wie gewuenscht keine SMB/CSI-spezifischen Manifeste hinzugefuegt. Verifiziert: tsc --noEmit clean, 139/139 Tests gruen (21 neu), pnpm build clean. Upload-/Serving-Pipeline live gegen einen laufenden Server getestet (Auth-/CSRF-Gates, Path-Traversal-Ablehnung, Format-Ablehnung) — ein vollstaendiger authentifizierter Upload end-to-end (braucht echten Admin-Login inkl. 2FA) wurde nicht live durchgespielt, nur per Code-Review verifiziert. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
1 parent
3de9f2906e
commit
54919d82ed
35 files changed
+1014
-44
No files matched your search
@@ -0,0 +1,122 @@
|
||||
import { describe, test, expect } from "vitest";
|
||||
import path from "node:path";
|
||||
import {
|
||||
UPLOAD_DIR,
|
||||
extensionForFormat,
|
||||
contentTypeForExtension,
|
||||
generateUploadFilename,
|
||||
isValidUploadPath,
|
||||
resolveUploadPath,
|
||||
} from "./uploads";
|
||||
|
||||
describe("extensionForFormat", () => {
|
||||
test("maps sharp's jpeg format to the jpg extension", () => {
|
||||
expect(extensionForFormat("jpeg")).toBe("jpg");
|
||||
});
|
||||
|
||||
test("passes png/webp/avif through unchanged", () => {
|
||||
expect(extensionForFormat("png")).toBe("png");
|
||||
expect(extensionForFormat("webp")).toBe("webp");
|
||||
expect(extensionForFormat("avif")).toBe("avif");
|
||||
});
|
||||
|
||||
test("rejects an unsupported format (e.g. gif/svg/tiff)", () => {
|
||||
expect(extensionForFormat("gif")).toBeNull();
|
||||
expect(extensionForFormat("svg")).toBeNull();
|
||||
expect(extensionForFormat("tiff")).toBeNull();
|
||||
});
|
||||
|
||||
test("rejects an undefined format", () => {
|
||||
expect(extensionForFormat(undefined)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("contentTypeForExtension", () => {
|
||||
test("returns the correct MIME type for each allowed extension", () => {
|
||||
expect(contentTypeForExtension("jpg")).toBe("image/jpeg");
|
||||
expect(contentTypeForExtension("png")).toBe("image/png");
|
||||
expect(contentTypeForExtension("webp")).toBe("image/webp");
|
||||
expect(contentTypeForExtension("avif")).toBe("image/avif");
|
||||
});
|
||||
|
||||
test("returns null for an extension outside the fixed allow-list", () => {
|
||||
// Verhindert, dass eine Datei je als text/html o.ae. ausgeliefert wird —
|
||||
// siehe app/uploads/[...path]/route.ts.
|
||||
expect(contentTypeForExtension("html")).toBeNull();
|
||||
expect(contentTypeForExtension("svg")).toBeNull();
|
||||
expect(contentTypeForExtension("")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("generateUploadFilename", () => {
|
||||
test("produces a 32-char lowercase hex name with the given extension", () => {
|
||||
const name = generateUploadFilename("webp");
|
||||
expect(name).toMatch(/^[a-f0-9]{32}\.webp$/);
|
||||
});
|
||||
|
||||
test("never reuses the same name across calls (collision-resistant)", () => {
|
||||
const names = new Set(Array.from({ length: 50 }, () => generateUploadFilename("jpg")));
|
||||
expect(names.size).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isValidUploadPath", () => {
|
||||
test("accepts a path in exactly the shape the upload route produces", () => {
|
||||
expect(isValidUploadPath(`/uploads/${generateUploadFilename("png")}`)).toBe(true);
|
||||
});
|
||||
|
||||
test("rejects a path traversal attempt", () => {
|
||||
expect(isValidUploadPath("/uploads/../../etc/passwd")).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects an absolute external URL (not an upload-endpoint path)", () => {
|
||||
expect(isValidUploadPath("https://attacker.example/x.png")).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects a javascript: pseudo-protocol value", () => {
|
||||
expect(isValidUploadPath("javascript:alert(1)")).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects a client-chosen filename instead of the generated random one", () => {
|
||||
expect(isValidUploadPath("/uploads/my-photo.png")).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects an extension outside the allow-list", () => {
|
||||
expect(isValidUploadPath("/uploads/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.svg")).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects an empty string", () => {
|
||||
expect(isValidUploadPath("")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveUploadPath", () => {
|
||||
test("resolves a plain filename to a path inside UPLOAD_DIR", () => {
|
||||
const resolved = resolveUploadPath(["abc123.png"]);
|
||||
expect(resolved).toBe(path.join(UPLOAD_DIR, "abc123.png"));
|
||||
});
|
||||
|
||||
test("resolves nested-looking but safe segments inside UPLOAD_DIR", () => {
|
||||
const resolved = resolveUploadPath(["sub", "abc123.png"]);
|
||||
expect(resolved).toBe(path.join(UPLOAD_DIR, "sub", "abc123.png"));
|
||||
});
|
||||
|
||||
test("rejects a '..' segment (path traversal attempt)", () => {
|
||||
expect(resolveUploadPath(["..", "..", "etc", "passwd"])).toBeNull();
|
||||
});
|
||||
|
||||
test("rejects a segment that is a single '.'", () => {
|
||||
expect(resolveUploadPath([".", "abc123.png"])).toBeNull();
|
||||
});
|
||||
|
||||
test("rejects an empty segment", () => {
|
||||
expect(resolveUploadPath([""])).toBeNull();
|
||||
});
|
||||
|
||||
test("rejects an encoded traversal baked into one segment", () => {
|
||||
// Next.js liefert Catch-all-Segmente bereits prozent-dekodiert an — ein
|
||||
// Segment kann trotzdem woertlich ".." enthalten, wenn jemand die Route
|
||||
// direkt (nicht ueber generierte Links) aufruft.
|
||||
expect(resolveUploadPath(["a", "..", "..", "b"])).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user