fix(cors): avoid setting `Access-Control-Allow-Origin` if there is no matching origin (#3510)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yuki Tanaka · committed by GitHub · parent 3311664 · Browse files · 2 files·+39 added−6 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+39 added−6 removed
- tests:+33 added−4 removed
- other:+6 added−2 removed
Provenance
- Commit
cebf4e8- Tree
c4649d4- Author
Yuki Tanaka <uki00a@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
3311664
2 files changed+39 added−6 removed
modified:src/middleware/cors/index.test.ts+33 added-4 removed
38 unmodified lines394041424344454647484950515235 unmodified lines8889909186929394959697989913 unmodified lines1131141151161171181191201211221231241251261271281293 unmodified lines13313413513611913713813914014114214314414514614712714814915015115215315415515615715815916016116230 unmodified lines19319419519617219719819920020120220320420538 unmodified lines cors({ origin: 'http://example.com', }) ) app.use( '/api6/*', cors({ origin: 'http://example.com', }) )
app.get('/api/abc', (c) => { return c.json({ success: true }) })35 unmodified lines ]) })
it('Preflight with options', async () => { const req = new Request('https://localhost/api2/abc', { method: 'OPTIONS' }) const req = new Request('https://localhost/api2/abc', { method: 'OPTIONS', headers: { origin: 'http://example.com' }, }) const res = await app.request(req)
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com') expect(res.headers.get('Vary')?.split(/\s*,\s*/)).toEqual(expect.arrayContaining(['Origin']))13 unmodified lines expect(res.headers.get('Access-Control-Max-Age')).toBe('600') expect(res.headers.get('Access-Control-Allow-Credentials')).toBe('true') })
it('Disallow an unmatched origin', async () => { const req = new Request('https://localhost/api2/abc', { method: 'OPTIONS', headers: { origin: 'http://example.net' }, }) const res = await app.request(req) expect(res.headers.has('Access-Control-Allow-Origin')).toBeFalsy() })
it('Allow multiple origins', async () => { let req = new Request('http://localhost/api3/abc', { headers: { Origin: 'http://example.org',3 unmodified lines expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.org')
req = new Request('http://localhost/api3/abc') res = await app.request(req) expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com') expect( res.headers.has('Access-Control-Allow-Origin'), 'An unmatched origin should be disallowed' ).toBeFalsy()
req = new Request('http://localhost/api3/abc', { headers: { Referer: 'http://example.net/', }, }) res = await app.request(req) expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com') expect( res.headers.has('Access-Control-Allow-Origin'), 'An unmatched origin should be disallowed' ).toBeFalsy() })
it('Allow different Vary header value', async () => { const res = await app.request('http://localhost/api3/abc', { headers: { Vary: 'accept-encoding', Origin: 'http://example.com', }, })
expect(res.status).toBe(200)30 unmodified lines expect(res.headers.get('Vary')).toBeNull() })
it('Should not return duplicate header values', async () => { const res = await app.request('http://localhost/api6/abc') const res = await app.request('http://localhost/api6/abc', { headers: { origin: 'http://example.com', }, })
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com') })})modified:src/middleware/cors/index.ts+6 added-2 removed
66 unmodified lines67 }68 69 const findAllowOrigin = ((optsOrigin) => {70 if (typeof optsOrigin === 'string') {71+ if (optsOrigin === '*') {71- return () => optsOrigin72+ return () => optsOrigin73+ } else {74+ return (origin: string) => (optsOrigin === origin ? origin : null)75+ }76 } else if (typeof optsOrigin === 'function') {77 return optsOrigin78 } else {75- return (origin: string) => (optsOrigin.includes(origin) ? origin : optsOrigin[0])79+ return (origin: string) => (optsOrigin.includes(origin) ? origin : null)80 }81 })(opts.origin)82 83 return async function cors(c, next) {62 unmodified lines