|
| 1 | +import express from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import request from "supertest"; |
| 4 | +import { makeCorsOptions } from "./corsOptions"; |
| 5 | + |
| 6 | +describe("makeCorsOptions", () => { |
| 7 | + const createTestApp = ( |
| 8 | + isProduction: boolean, |
| 9 | + allowedOrigins: string[] = [] |
| 10 | + ) => { |
| 11 | + const app = express(); |
| 12 | + const corsOptions = makeCorsOptions(isProduction, allowedOrigins); |
| 13 | + |
| 14 | + app.use(cors(corsOptions)); |
| 15 | + |
| 16 | + // Simple test endpoint |
| 17 | + app.post("/test", (_req, res) => { |
| 18 | + res.json({ message: "success" }); |
| 19 | + }); |
| 20 | + |
| 21 | + return app; |
| 22 | + }; |
| 23 | + |
| 24 | + describe("Non-production mode", () => { |
| 25 | + let app: express.Express; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + app = createTestApp(false, ["https://trusted-site.com"]); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should allow localhost origins", async () => { |
| 32 | + const response = await request(app) |
| 33 | + .post("/test") |
| 34 | + .set("Origin", "http://localhost:3000") |
| 35 | + .send({ data: "test" }); |
| 36 | + |
| 37 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 38 | + "http://localhost:3000" |
| 39 | + ); |
| 40 | + expect(response.headers["access-control-allow-credentials"]).toBe("true"); |
| 41 | + expect(response.status).toBe(200); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should allow localhost with different ports", async () => { |
| 45 | + const response = await request(app) |
| 46 | + .post("/test") |
| 47 | + .set("Origin", "http://localhost:8080") |
| 48 | + .send({ data: "test" }); |
| 49 | + |
| 50 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 51 | + "http://localhost:8080" |
| 52 | + ); |
| 53 | + expect(response.headers["access-control-allow-credentials"]).toBe("true"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should allow 127.0.0.1 origins", async () => { |
| 57 | + const response = await request(app) |
| 58 | + .post("/test") |
| 59 | + .set("Origin", "http://127.0.0.1:3000") |
| 60 | + .send({ data: "test" }); |
| 61 | + |
| 62 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 63 | + "http://127.0.0.1:3000" |
| 64 | + ); |
| 65 | + expect(response.headers["access-control-allow-credentials"]).toBe("true"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should allow requests with no origin (no CORS headers)", async () => { |
| 69 | + const response = await request(app).post("/test").send({ data: "test" }); |
| 70 | + |
| 71 | + expect(response.status).toBe(200); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should allow origins from allowedOrigins array", async () => { |
| 75 | + const response = await request(app) |
| 76 | + .post("/test") |
| 77 | + .set("Origin", "https://trusted-site.com") |
| 78 | + .send({ data: "test" }); |
| 79 | + |
| 80 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 81 | + "https://trusted-site.com" |
| 82 | + ); |
| 83 | + expect(response.headers["access-control-allow-credentials"]).toBe("true"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should block non-localhost origins not in allowedOrigins", async () => { |
| 87 | + const response = await request(app) |
| 88 | + .options("/test") |
| 89 | + .set("Origin", "https://malicious-site.com") |
| 90 | + .set("Access-Control-Request-Method", "POST"); |
| 91 | + |
| 92 | + // CORS blocking is indicated by missing allow-origin header |
| 93 | + expect(response.headers["access-control-allow-origin"]).toBeUndefined(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("Production mode", () => { |
| 98 | + let app: express.Express; |
| 99 | + |
| 100 | + beforeEach(() => { |
| 101 | + app = createTestApp(true, [ |
| 102 | + "https://production-site.com", |
| 103 | + "https://another-prod-site.com", |
| 104 | + ]); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should block localhost origins in production", async () => { |
| 108 | + const response = await request(app) |
| 109 | + .options("/test") |
| 110 | + .set("Origin", "http://localhost:3000") |
| 111 | + .set("Access-Control-Request-Method", "POST"); |
| 112 | + |
| 113 | + // CORS blocking is indicated by missing allow-origin header |
| 114 | + expect(response.headers["access-control-allow-origin"]).toBeUndefined(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should block 127.0.0.1 origins in production", async () => { |
| 118 | + const response = await request(app) |
| 119 | + .options("/test") |
| 120 | + .set("Origin", "http://127.0.0.1:3000") |
| 121 | + .set("Access-Control-Request-Method", "POST"); |
| 122 | + |
| 123 | + // CORS blocking is indicated by missing allow-origin header |
| 124 | + expect(response.headers["access-control-allow-origin"]).toBeUndefined(); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should only allow origins from allowedOrigins array", async () => { |
| 128 | + const response = await request(app) |
| 129 | + .post("/test") |
| 130 | + .set("Origin", "https://production-site.com") |
| 131 | + .send({ data: "test" }); |
| 132 | + |
| 133 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 134 | + "https://production-site.com" |
| 135 | + ); |
| 136 | + expect(response.headers["access-control-allow-credentials"]).toBe("true"); |
| 137 | + expect(response.status).toBe(200); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should block unknown origins in production", async () => { |
| 141 | + const response = await request(app) |
| 142 | + .options("/test") |
| 143 | + .set("Origin", "https://unknown-site.com") |
| 144 | + .set("Access-Control-Request-Method", "POST"); |
| 145 | + |
| 146 | + // CORS blocking is indicated by missing allow-origin header |
| 147 | + expect(response.headers["access-control-allow-origin"]).toBeUndefined(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe("CORS preflight requests", () => { |
| 152 | + let app: express.Express; |
| 153 | + |
| 154 | + beforeEach(() => { |
| 155 | + app = createTestApp(false, ["https://trusted-site.com"]); |
| 156 | + }); |
| 157 | + |
| 158 | + it("should handle OPTIONS requests for localhost", async () => { |
| 159 | + const response = await request(app) |
| 160 | + .options("/test") |
| 161 | + .set("Origin", "http://localhost:3000") |
| 162 | + .set("Access-Control-Request-Method", "POST") |
| 163 | + .set("Access-Control-Request-Headers", "Content-Type"); |
| 164 | + |
| 165 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 166 | + "http://localhost:3000" |
| 167 | + ); |
| 168 | + expect(response.headers["access-control-allow-methods"]).toMatch(/POST/); |
| 169 | + expect(response.headers["access-control-allow-headers"]).toMatch( |
| 170 | + /Content-Type/ |
| 171 | + ); |
| 172 | + expect(response.headers["access-control-allow-credentials"]).toBe("true"); |
| 173 | + expect(response.status).toBe(204); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should handle complex preflight with multiple headers", async () => { |
| 177 | + const response = await request(app) |
| 178 | + .options("/test") |
| 179 | + .set("Origin", "http://localhost:8080") |
| 180 | + .set("Access-Control-Request-Method", "POST") |
| 181 | + .set( |
| 182 | + "Access-Control-Request-Headers", |
| 183 | + "Content-Type, Authorization, X-Custom-Header" |
| 184 | + ); |
| 185 | + |
| 186 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 187 | + "http://localhost:8080" |
| 188 | + ); |
| 189 | + expect(response.headers["access-control-allow-methods"]).toMatch(/POST/); |
| 190 | + expect(response.headers["access-control-allow-headers"]).toMatch( |
| 191 | + /Content-Type/ |
| 192 | + ); |
| 193 | + expect(response.headers["access-control-allow-credentials"]).toBe("true"); |
| 194 | + expect(response.status).toBe(204); |
| 195 | + }); |
| 196 | + |
| 197 | + it("should include Vary header for Origin", async () => { |
| 198 | + const response = await request(app) |
| 199 | + .post("/test") |
| 200 | + .set("Origin", "http://localhost:3000") |
| 201 | + .send({ data: "test" }); |
| 202 | + |
| 203 | + expect(response.headers["vary"]).toMatch(/Origin/i); |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + describe("Edge cases", () => { |
| 208 | + it("should handle empty allowedOrigins array", async () => { |
| 209 | + const app = createTestApp(false, []); |
| 210 | + |
| 211 | + const response = await request(app) |
| 212 | + .post("/test") |
| 213 | + .set("Origin", "http://localhost:3000") |
| 214 | + .send({ data: "test" }); |
| 215 | + |
| 216 | + expect(response.headers["access-control-allow-origin"]).toBe( |
| 217 | + "http://localhost:3000" |
| 218 | + ); |
| 219 | + expect(response.status).toBe(200); |
| 220 | + }); |
| 221 | + |
| 222 | + it("should handle multiple allowedOrigins in production", async () => { |
| 223 | + const app = createTestApp(true, [ |
| 224 | + "https://site1.com", |
| 225 | + "https://site2.com", |
| 226 | + "https://site3.com", |
| 227 | + ]); |
| 228 | + |
| 229 | + const response1 = await request(app) |
| 230 | + .post("/test") |
| 231 | + .set("Origin", "https://site2.com") |
| 232 | + .send({ data: "test" }); |
| 233 | + |
| 234 | + expect(response1.headers["access-control-allow-origin"]).toBe( |
| 235 | + "https://site2.com" |
| 236 | + ); |
| 237 | + expect(response1.status).toBe(200); |
| 238 | + }); |
| 239 | + }); |
| 240 | +}); |
0 commit comments