@@ -362,20 +362,23 @@ describe('CORS by Middleware', () => {
362362 expect ( res2 . headers . get ( 'Access-Control-Allow-Methods' ) ) . toBe ( 'GET,HEAD' )
363363 } )
364364
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 ( ) => {
366366 const res = await app . request ( 'http://localhost/api10/abc' , {
367367 headers : {
368368 Origin : 'http://example.com' ,
369369 } ,
370370 } )
371371
372372 expect ( res . status ) . toBe ( 200 )
373- expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( 'http://example.com' )
373+ // Must not reflect the request origin; the wildcard + credentials combination
374+ // is rejected by the browser (fail closed). A wildcard response does not vary
375+ // by Origin, so no Vary header is set.
376+ expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( '*' )
374377 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 ( )
376379 } )
377380
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 ( ) => {
379382 const req = new Request ( 'http://localhost/api10/abc' , {
380383 method : 'OPTIONS' ,
381384 headers : {
@@ -385,40 +388,71 @@ describe('CORS by Middleware', () => {
385388 const res = await app . request ( req )
386389
387390 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 ( '* ' )
389392 expect ( res . headers . get ( 'Access-Control-Allow-Credentials' ) ) . toBe ( 'true' )
390- expect ( res . headers . get ( 'Vary' ) ?. split ( / \s * , \s * / ) ) . toEqual ( expect . arrayContaining ( [ 'Origin' ] ) )
391393 } )
392394
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 ( ) => {
394396 const res = await app . request ( 'http://localhost/api10/abc' )
395397
396398 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 ( '*' )
398400 expect ( res . headers . get ( 'Access-Control-Allow-Credentials' ) ) . toBe ( 'true' )
399401 } )
400402
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 ( ) => {
402404 const res1 = await app . request ( 'http://localhost/api10/abc' , {
403405 headers : { Origin : 'http://example.com' } ,
404406 } )
405407 const res2 = await app . request ( 'http://localhost/api10/abc' , {
406408 headers : { Origin : 'http://other.com' } ,
407409 } )
408410
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 ( '* ' )
411413 } )
412414
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 ( ) => {
414416 const res = await app . request ( 'http://localhost/api10/abc' , {
415417 headers : { Origin : 'null' } ,
416418 } )
417419
418- expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( 'null ' )
420+ expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( '* ' )
419421 expect ( res . headers . get ( 'Access-Control-Allow-Credentials' ) ) . toBe ( 'true' )
420422 } )
421423
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-origin
430+ // 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+
422456 it ( 'Options without origin fall back to wildcard default' , async ( ) => {
423457 const subApp = new Hono ( )
424458 subApp . use ( '/api/*' , cors ( { allowMethods : [ 'GET' , 'POST' ] } ) )
0 commit comments