|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest" |
| 2 | +import { detectOrigin } from "../src/lib/utils/detect-origin.js" |
| 3 | + |
| 4 | +describe("detectOrigin", () => { |
| 5 | + const originalEnv = process.env |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + // Reset process.env before each test |
| 9 | + process.env = { ...originalEnv } |
| 10 | + delete process.env.AUTH_URL |
| 11 | + delete process.env.AUTH_TRUST_HOST |
| 12 | + delete process.env.VERCEL |
| 13 | + delete process.env.CF_PAGES |
| 14 | + }) |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + process.env = originalEnv |
| 18 | + }) |
| 19 | + |
| 20 | + describe("AUTH_URL has highest priority", () => { |
| 21 | + it("should return AUTH_URL when set, ignoring forwarded headers", () => { |
| 22 | + process.env.AUTH_URL = "https://from-env.com" |
| 23 | + const result = detectOrigin("forwarded.com", "http") |
| 24 | + expect(result).toBe("https://from-env.com") |
| 25 | + }) |
| 26 | + |
| 27 | + it("should return AUTH_URL even when AUTH_TRUST_HOST is set", () => { |
| 28 | + process.env.AUTH_URL = "https://from-env.com" |
| 29 | + process.env.AUTH_TRUST_HOST = "true" |
| 30 | + const result = detectOrigin("forwarded.com", "https") |
| 31 | + expect(result).toBe("https://from-env.com") |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe("Trusted environments (AUTH_TRUST_HOST, VERCEL, CF_PAGES)", () => { |
| 36 | + it("should use forwarded headers when AUTH_TRUST_HOST is set", () => { |
| 37 | + process.env.AUTH_TRUST_HOST = "true" |
| 38 | + const result = detectOrigin("example.com", "https") |
| 39 | + expect(result).toBe("https://example.com") |
| 40 | + }) |
| 41 | + |
| 42 | + it("should use forwarded headers when VERCEL is set", () => { |
| 43 | + process.env.VERCEL = "1" |
| 44 | + const result = detectOrigin("vercel-app.com", "https") |
| 45 | + expect(result).toBe("https://vercel-app.com") |
| 46 | + }) |
| 47 | + |
| 48 | + it("should use forwarded headers when CF_PAGES is set", () => { |
| 49 | + process.env.CF_PAGES = "1" |
| 50 | + const result = detectOrigin("cloudflare.com", "https") |
| 51 | + expect(result).toBe("https://cloudflare.com") |
| 52 | + }) |
| 53 | + |
| 54 | + it("should default to https when protocol is not 'http'", () => { |
| 55 | + process.env.AUTH_TRUST_HOST = "true" |
| 56 | + const result = detectOrigin("example.com", undefined) |
| 57 | + expect(result).toBe("https://example.com") |
| 58 | + }) |
| 59 | + |
| 60 | + it("should use http when protocol is explicitly 'http'", () => { |
| 61 | + process.env.AUTH_TRUST_HOST = "true" |
| 62 | + const result = detectOrigin("example.com", "http") |
| 63 | + expect(result).toBe("http://example.com") |
| 64 | + }) |
| 65 | + |
| 66 | + it("should return undefined when no forwardedHost is provided in trusted environment", () => { |
| 67 | + process.env.AUTH_TRUST_HOST = "true" |
| 68 | + const result = detectOrigin(undefined, "https") |
| 69 | + expect(result).toBeUndefined() |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe("Untrusted environment (no environment variables)", () => { |
| 74 | + it("should return undefined when no trust indicators are set", () => { |
| 75 | + const result = detectOrigin("example.com", "https") |
| 76 | + expect(result).toBeUndefined() |
| 77 | + }) |
| 78 | + |
| 79 | + it("should return undefined even with forwarded headers", () => { |
| 80 | + const result = detectOrigin("malicious.com", "https") |
| 81 | + expect(result).toBeUndefined() |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe("Edge cases", () => { |
| 86 | + it("should return undefined for empty forwardedHost", () => { |
| 87 | + process.env.AUTH_TRUST_HOST = "true" |
| 88 | + const result = detectOrigin("", "https") |
| 89 | + // Empty string should be treated as invalid |
| 90 | + expect(result).toBeUndefined() |
| 91 | + }) |
| 92 | + |
| 93 | + it("should handle forwardedHost with port", () => { |
| 94 | + process.env.VERCEL = "1" |
| 95 | + const result = detectOrigin("example.com:3000", "http") |
| 96 | + expect(result).toBe("http://example.com:3000") |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments