Skip to content
Myr

fix(cors): reflect request origin when credentials is true with wildcard (#4813)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(cors): reflect request origin when credentials is true with wildcard Per the Fetch spec, Access-Control-Allow-Origin must not be '*' when credentials mode is 'include'. Browsers reject these responses. When origin is '*' and credentials is true, reflect the actual request origin instead of the wildcard. Also set Vary: Origin in this case since the response now varies by request origin. Fixes #4811 * test(cors): add tests for credentials with wildcard origin Cover the case where origin: '*' and credentials: true should reflect the request Origin header instead of sending *. * add more tests ---------

authored by Charlie Tonneslan · committed by GitHub · parent 50e2611 · Browse files · 2 files+76 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+76 added−2 removed

  • tests:+69 added−0 removed
  • other:+7 added−2 removed

2 files+76 added−2 removed

modified:src/middleware/cors/index.test.ts+69 added
82 unmodified lines
83 ),84 })85 )8687 app.use(88 '/api10/*',89 cors({90 origin: '*',91 credentials: true,92 })93 )9495 app.get('/api/abc', (c) => {96 return c.json({ success: true })97 })98
32 unmodified lines
131 app.get('/api7/abc', () => {132 return new Response(JSON.stringify({ success: true }))133 })134135 app.get('/api10/abc', (c) => {136 return c.json({ success: true })137 })138139 it('GET default', async () => {140 const res = await app.request('http://localhost/api/abc')141142 expect(res.status).toBe(200)
217 unmodified lines
360 const res2 = await app.request(req2)361 expect(res2.headers.get('Access-Control-Allow-Origin')).toBe('*')362 expect(res2.headers.get('Access-Control-Allow-Methods')).toBe('GET,HEAD')363 })364365 it('Reflect request origin when credentials is true with wildcard origin', async () => {366 const res = await app.request('http://localhost/api10/abc', {367 headers: {368 Origin: 'http://example.com',369 },370 })371372 expect(res.status).toBe(200)373 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')374 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')375 expect(res.headers.get('Vary')).toBe('Origin')376 })377378 it('Preflight: reflect request origin when credentials is true with wildcard origin', async () => {379 const req = new Request('http://localhost/api10/abc', {380 method: 'OPTIONS',381 headers: {382 Origin: 'http://example.com',383 },384 })385 const res = await app.request(req)386387 expect(res.status).toBe(204)388 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')389 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')390 expect(res.headers.get('Vary')?.split(/\s*,\s*/)).toEqual(expect.arrayContaining(['Origin']))391 })392393 it('Should not set Allow-Origin when no Origin header with credentials and wildcard', async () => {394 const res = await app.request('http://localhost/api10/abc')395396 expect(res.status).toBe(200)397 expect(res.headers.has('Access-Control-Allow-Origin')).toBeFalsy()398 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')399 })400401 it('Reflect different origins correctly for credentials with wildcard', async () => {402 const res1 = await app.request('http://localhost/api10/abc', {403 headers: { Origin: 'http://example.com' },404 })405 const res2 = await app.request('http://localhost/api10/abc', {406 headers: { Origin: 'http://other.com' },407 })408409 expect(res1.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')410 expect(res2.headers.get('Access-Control-Allow-Origin')).toBe('http://other.com')411 })412413 it('Handle Origin: null with credentials and wildcard', async () => {414 const res = await app.request('http://localhost/api10/abc', {415 headers: { Origin: 'null' },416 })417418 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('null')419 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')420 })421})
modified:src/middleware/cors/index.ts+7 added-2 removed