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
Provenance
- Commit
f0b094d- Tree
b375516- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
fa5f9bf
2 files changed+49 added−20 removed
modified:src/middleware/cors/index.test.ts+47 added-13 removed
360 unmodified lines361 expect(res2.headers.get('Access-Control-Allow-Origin')).toBe('*')362 expect(res2.headers.get('Access-Control-Allow-Methods')).toBe('GET,HEAD')363 })364 365- 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 })371 372 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 })380 378- 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 line387 })388 const res = await app.request(req)389 390 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 })394 393- 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')397 398 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 })402 401- 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 })410 409- 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 })414 413- 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 })419 418- 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 })423 424+ 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' }))428+429+ // 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+ })436+437+ 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 }))441+442+ 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+ )452+453+ expect(res.headers.get('Access-Control-Allow-Origin')).not.toBe('https://attacker.example')454+ })455+456 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 linesmodified:src/middleware/cors/index.ts+2 added-7 removed
70 unmodified lines7172737475767778797576777832 unmodified lines11111211311412011511611711811929 unmodified lines14915015115215815315415515615770 unmodified lines
const findAllowOrigin = ((optsOrigin) => { if (typeof optsOrigin === 'string') { if (optsOrigin === '*') { // When credentials is true, the spec forbids Access-Control-Allow-Origin: *. // Reflect the request origin instead so browsers accept the response. if (opts.credentials) { return (origin: string) => origin || null } return () => optsOrigin } else { return (origin: string) => (optsOrigin === origin ? origin : null) }32 unmodified lines set('Access-Control-Expose-Headers', opts.exposeHeaders.join(',')) }
if (c.req.method === 'OPTIONS') { if (opts.origin !== '*' || opts.credentials) { if (opts.origin !== '*') { set('Vary', 'Origin') }
if (opts.maxAge != null) {29 unmodified lines await next()
// Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard). // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin if (opts.origin !== '*' || opts.credentials) { if (opts.origin !== '*') { c.header('Vary', 'Origin', { append: true }) } }}