aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/server/middleware/cors.test.ts
blob: 6f413a508340ccf80e2ec4c3ed84bb299eb9158c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Hono } from "hono";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createCorsMiddleware } from "./cors.js";

describe("createCorsMiddleware", () => {
	const originalEnv = process.env.CORS_ORIGIN;

	afterEach(() => {
		if (originalEnv === undefined) {
			delete process.env.CORS_ORIGIN;
		} else {
			process.env.CORS_ORIGIN = originalEnv;
		}
	});

	describe("when CORS_ORIGIN is not set", () => {
		beforeEach(() => {
			delete process.env.CORS_ORIGIN;
		});

		it("does not add CORS headers", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				headers: { Origin: "https://attacker.com" },
			});

			expect(res.status).toBe(200);
			expect(res.headers.get("Access-Control-Allow-Origin")).toBeNull();
		});

		it("does not allow preflight requests", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				method: "OPTIONS",
				headers: {
					Origin: "https://attacker.com",
					"Access-Control-Request-Method": "POST",
				},
			});

			expect(res.headers.get("Access-Control-Allow-Origin")).toBeNull();
		});
	});

	describe("when CORS_ORIGIN is set to single origin", () => {
		beforeEach(() => {
			process.env.CORS_ORIGIN = "https://allowed.example.com";
		});

		it("allows requests from the configured origin", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				headers: { Origin: "https://allowed.example.com" },
			});

			expect(res.status).toBe(200);
			expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
				"https://allowed.example.com",
			);
		});

		it("does not allow requests from other origins", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				headers: { Origin: "https://attacker.com" },
			});

			expect(res.status).toBe(200);
			expect(res.headers.get("Access-Control-Allow-Origin")).toBeNull();
		});

		it("handles preflight requests correctly", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.post("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				method: "OPTIONS",
				headers: {
					Origin: "https://allowed.example.com",
					"Access-Control-Request-Method": "POST",
				},
			});

			expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
				"https://allowed.example.com",
			);
			expect(res.headers.get("Access-Control-Allow-Methods")).toContain("POST");
		});

		it("exposes rate limit headers", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				method: "OPTIONS",
				headers: {
					Origin: "https://allowed.example.com",
					"Access-Control-Request-Method": "GET",
				},
			});

			const exposeHeaders = res.headers.get("Access-Control-Expose-Headers");
			expect(exposeHeaders).toContain("RateLimit-Limit");
			expect(exposeHeaders).toContain("RateLimit-Remaining");
			expect(exposeHeaders).toContain("RateLimit-Reset");
		});
	});

	describe("when CORS_ORIGIN is set to multiple origins", () => {
		beforeEach(() => {
			process.env.CORS_ORIGIN =
				"https://app.example.com, https://admin.example.com";
		});

		it("allows requests from first configured origin", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				headers: { Origin: "https://app.example.com" },
			});

			expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
				"https://app.example.com",
			);
		});

		it("allows requests from second configured origin", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				headers: { Origin: "https://admin.example.com" },
			});

			expect(res.headers.get("Access-Control-Allow-Origin")).toBe(
				"https://admin.example.com",
			);
		});

		it("does not allow requests from unlisted origins", async () => {
			const app = new Hono();
			app.use("*", createCorsMiddleware());
			app.get("/test", (c) => c.json({ ok: true }));

			const res = await app.request("/test", {
				headers: { Origin: "https://other.example.com" },
			});

			expect(res.headers.get("Access-Control-Allow-Origin")).toBeNull();
		});
	});
});