import { describe, it, expect } from "vitest"; /** * CORS Configuration Tests * * These tests verify that CORS is configured correctly for cookie-based authentication. * * CRITICAL REQUIREMENTS: * - credentials: true (allows cookies to be sent) * - origin: must be specific origins, NOT wildcard (security requirement with credentials) * - Access-Control-Allow-Credentials: true header * - Access-Control-Allow-Origin: specific origin (not *) */ describe("CORS Configuration", () => { describe("Configuration requirements", () => { it("should document required CORS settings for cookie-based auth", () => { // This test documents the requirements const requiredSettings = { origin: ["http://localhost:3000", "https://app.mosaicstack.dev"], credentials: true, allowedHeaders: ["Content-Type", "Authorization", "Cookie"], exposedHeaders: ["Set-Cookie"], methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], }; expect(requiredSettings.credentials).toBe(true); expect(requiredSettings.origin).not.toContain("*"); expect(requiredSettings.allowedHeaders).toContain("Cookie"); }); it("should NOT use wildcard origin with credentials (security violation)", () => { // Wildcard origin with credentials is a security violation // This test ensures we never use that combination const validConfig1 = { origin: "*", credentials: false }; const validConfig2 = { origin: "http://localhost:3000", credentials: true }; const invalidConfig = { origin: "*", credentials: true }; // Valid configs expect(validConfig1.origin === "*" && !validConfig1.credentials).toBe(true); expect(validConfig2.origin !== "*" && validConfig2.credentials).toBe(true); // Invalid config check - this combination should NOT be allowed const isInvalidCombination = invalidConfig.origin === "*" && invalidConfig.credentials; expect(isInvalidCombination).toBe(true); // This IS an invalid combination // We will prevent this in our CORS config }); }); describe("Origin validation", () => { it("should define allowed origins list", () => { const allowedOrigins = [ process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000", "http://localhost:3001", // API origin (dev) "https://app.mosaicstack.dev", // Production web "https://api.mosaicstack.dev", // Production API ]; expect(allowedOrigins).toHaveLength(4); expect(allowedOrigins).toContain("http://localhost:3000"); expect(allowedOrigins).toContain("https://app.mosaicstack.dev"); }); it("should match exact origins, not partial matches", () => { const origin = "http://localhost:3000"; const maliciousOrigin = "http://localhost:3000.evil.com"; expect(origin).toBe("http://localhost:3000"); expect(maliciousOrigin).not.toBe(origin); }); it("should support dynamic origin from environment variable", () => { const defaultOrigin = "http://localhost:3000"; const envOrigin = process.env.NEXT_PUBLIC_APP_URL ?? defaultOrigin; expect(envOrigin).toBeDefined(); expect(typeof envOrigin).toBe("string"); }); }); });