import { test, expect } from "@playwright/test"; /** * Security-Header & Cookie-Flags (Definition of Done #8, Querschnittsstandard * 1, 9, 11). * * NICHT in der Sandbox ausführbar (kein Server) — deferred. Wird über * `npm run test:e2e` gegen einen laufenden Server ausgeführt. Der statische * Header-Satz ist zusätzlich offline durch src/lib/security/headers.test.ts * abgesichert. * * Verifikation entspricht: `curl -sI https:///login | grep -i x-frame-options`. */ test.use({ storageState: { cookies: [], origins: [] } }); test.describe("Security-Header auf /login", () => { test("setzt X-Frame-Options, nosniff, CSP frame-ancestors none, HSTS", async ({ request, }) => { const res = await request.get("/login"); const h = res.headers(); expect(h["x-frame-options"]).toBe("DENY"); expect(h["x-content-type-options"]).toBe("nosniff"); expect(h["content-security-policy"]).toContain("frame-ancestors 'none'"); expect(h["strict-transport-security"]).toMatch(/max-age=\d+/); }); }); test.describe("Session-Cookie-Flags", () => { test("nach Login: Session-Cookie ist httpOnly + sameSite", async ({ context, page, }) => { // Erwartet einen funktionierenden Credentials-Login (Seed). Deferred. await page.goto("/login"); await page.getByLabel(/E-Mail/i).fill("wehr-admin-a@example.test"); await page.getByLabel(/Passwort/i).fill("Test-Passwort-1234"); await page.getByRole("button", { name: /Anmelden/i }).click(); await page.waitForURL((url) => !url.pathname.startsWith("/login")); const cookies = await context.cookies(); const session = cookies.find((c) => /authjs|next-auth|__Secure-/.test(c.name)); expect(session, "Session-Cookie gesetzt").toBeTruthy(); expect(session?.httpOnly).toBe(true); expect(session?.sameSite).toMatch(/Lax|Strict/); // `secure` nur unter https (Querschnittsstandard 9). Lokal (http) false. const isHttps = (process.env.E2E_BASE_URL ?? "").startsWith("https://"); expect(session?.secure).toBe(isHttps); }); });