Skip to content
Myr

Merge commit from fork

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

authored by Yusuke Wada · committed by GitHub · parent fa5f9bf · Browse files · 2 files+49 added−20 removed
This commit

State

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

Diff stats

2 files+49 added−20 removed

  • tests:+47 added−13 removed
  • other:+2 added−7 removed

2 files+49 added−20 removed

modified:src/middleware/cors/index.test.ts+47 added-13 removed
360 unmodified lines
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 () => {365 it('Emits the wildcard, not the reflected 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 // Must not reflect the request origin; the wildcard + credentials combination374 // is rejected by the browser (fail closed). A wildcard response does not vary375 // by Origin, so no Vary header is set.373 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')376 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')377 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')375 expect(res.headers.get('Vary')).toBe('Origin')378 expect(res.headers.get('Vary')).toBeNull()379 })380378 it('Preflight: reflect request origin when credentials is true with wildcard origin', async () => {381 it('Preflight emits the wildcard, not the reflected origin, when credentials is true with wildcard origin', async () => {382 const req = new Request('http://localhost/api10/abc', {383 method: 'OPTIONS',384 headers: {385 Origin: 'http://example.com',
1 unmodified line
387 })388 const res = await app.request(req)389390 expect(res.status).toBe(204)388 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')391 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')392 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')390 expect(res.headers.get('Vary')?.split(/\s*,\s*/)).toEqual(expect.arrayContaining(['Origin']))393 })394393 it('Should not set Allow-Origin when no Origin header with credentials and wildcard', async () => {395 it('Emits the wildcard with no Origin header when credentials is true with wildcard origin', async () => {396 const res = await app.request('http://localhost/api10/abc')397398 expect(res.status).toBe(200)397 expect(res.headers.has('Access-Control-Allow-Origin')).toBeFalsy()399 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')400 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')401 })402401 it('Reflect different origins correctly for credentials with wildcard', async () => {403 it('Emits the wildcard for every origin (never reflects) when credentials is true with wildcard origin', async () => {404 const res1 = await app.request('http://localhost/api10/abc', {405 headers: { Origin: 'http://example.com' },406 })407 const res2 = await app.request('http://localhost/api10/abc', {408 headers: { Origin: 'http://other.com' },409 })410409 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 expect(res1.headers.get('Access-Control-Allow-Origin')).toBe('*')412 expect(res2.headers.get('Access-Control-Allow-Origin')).toBe('*')413 })414413 it('Handle Origin: null with credentials and wildcard', async () => {415 it('Origin: null gets the wildcard, not a reflected null origin, with credentials', async () => {416 const res = await app.request('http://localhost/api10/abc', {417 headers: { Origin: 'null' },418 })419418 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('null')420 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')421 expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true')422 })423424 it('Should not reflect an arbitrary Origin when credentials is true with default origin', async () => {425 const app = new Hono()426 app.use('*', cors({ credentials: true }))427 app.get('/api/me', (c) => c.json({ secret: 'private user data' }))428429 // A wildcard/default origin must not be turned into a reflect-any-origin430 // credentialed policy; the browser must keep rejecting it (fail closed).431 for (const origin of ['https://attacker.example', 'http://evil.test', 'null']) {432 const res = await app.request('http://localhost/api/me', { headers: { Origin: origin } })433 expect(res.headers.get('Access-Control-Allow-Origin')).not.toBe(origin)434 }435 })436437 it('Preflight should not approve an arbitrary Origin when credentials is true with default origin', async () => {438 const app = new Hono()439 app.use('*', cors({ credentials: true }))440 app.delete('/api/account', (c) => c.json({ deleted: true }))441442 const res = await app.request(443 new Request('http://localhost/api/account', {444 method: 'OPTIONS',445 headers: {446 Origin: 'https://attacker.example',447 'Access-Control-Request-Method': 'DELETE',448 'Access-Control-Request-Headers': 'X-CSRF',449 },450 })451 )452453 expect(res.headers.get('Access-Control-Allow-Origin')).not.toBe('https://attacker.example')454 })455456 it('Options without origin fall back to wildcard default', async () => {457 const subApp = new Hono()458 subApp.use('/api/*', cors({ allowMethods: ['GET', 'POST'] }))459 subApp.get('/api/abc', (c) => c.json({ ok: true }))
14 unmodified lines
modified:src/middleware/cors/index.ts+2 added-7 removed