@@ -22,48 +22,50 @@ describe('validateOrigin', () => {
2222 } ) ;
2323
2424 it ( 'rejects requests with no Origin or Referer' , ( ) => {
25- const req = createRequest ( { host : 'example.com' } ) ;
25+ const req = createRequest ( ) ;
2626 expect ( ( ) => validateOrigin ( req ) ) . toThrow ( ) ;
2727 } ) ;
2828
2929 it ( 'allows requests with matching Origin (self-origin fallback)' , ( ) => {
3030 const req = createRequest ( {
3131 origin : 'https://example.com' ,
32- host : 'example.com' ,
3332 } ) ;
3433 expect ( ( ) => validateOrigin ( req ) ) . not . toThrow ( ) ;
3534 } ) ;
3635
3736 it ( 'rejects requests with mismatched Origin (self-origin fallback)' , ( ) => {
3837 const req = createRequest ( {
3938 origin : 'https://evil.com' ,
40- host : 'example.com' ,
4139 } ) ;
4240 expect ( ( ) => validateOrigin ( req ) ) . toThrow ( ) ;
4341 } ) ;
4442
4543 it ( 'allows requests with matching Referer when no Origin' , ( ) => {
4644 const req = createRequest ( {
4745 referer : 'https://example.com/page' ,
48- host : 'example.com' ,
4946 } ) ;
5047 expect ( ( ) => validateOrigin ( req ) ) . not . toThrow ( ) ;
5148 } ) ;
5249
5350 it ( 'rejects requests with mismatched Referer' , ( ) => {
5451 const req = createRequest ( {
5552 referer : 'https://evil.com/page' ,
56- host : 'example.com' ,
5753 } ) ;
5854 expect ( ( ) => validateOrigin ( req ) ) . toThrow ( ) ;
5955 } ) ;
6056
57+ it ( 'rejects requests with malformed Referer' , ( ) => {
58+ for ( const referer of [ 'not-a-url' , '/relative/path' ] ) {
59+ const req = createRequest ( { referer } ) ;
60+ expect ( ( ) => validateOrigin ( req ) ) . toThrow ( ) ;
61+ }
62+ } ) ;
63+
6164 describe ( 'ALLOWED_ORIGINS' , ( ) => {
6265 it ( 'allows origins in the allowlist' , ( ) => {
6366 mockEnv . ALLOWED_ORIGINS = 'https://app.example.com, https://staging.example.com' ;
6467 const req = createRequest ( {
6568 origin : 'https://app.example.com' ,
66- host : 'example.com' ,
6769 } ) ;
6870 expect ( ( ) => validateOrigin ( req ) ) . not . toThrow ( ) ;
6971 } ) ;
@@ -72,25 +74,8 @@ describe('validateOrigin', () => {
7274 mockEnv . ALLOWED_ORIGINS = 'https://app.example.com' ;
7375 const req = createRequest ( {
7476 origin : 'https://evil.com' ,
75- host : 'example.com' ,
7677 } ) ;
7778 expect ( ( ) => validateOrigin ( req ) ) . toThrow ( ) ;
7879 } ) ;
7980 } ) ;
80-
81- it ( 'uses x-forwarded-proto for self-origin comparison' , ( ) => {
82- const req = createRequest ( {
83- origin : 'http://example.com' ,
84- host : 'example.com' ,
85- 'x-forwarded-proto' : 'http' ,
86- } ) ;
87- expect ( ( ) => validateOrigin ( req ) ) . not . toThrow ( ) ;
88- } ) ;
89-
90- it ( 'rejects when host header is missing and no ALLOWED_ORIGINS' , ( ) => {
91- const req = createRequest ( {
92- origin : 'https://example.com' ,
93- } ) ;
94- expect ( ( ) => validateOrigin ( req ) ) . toThrow ( ) ;
95- } ) ;
9681} ) ;
0 commit comments