@@ -41,9 +41,23 @@ describe("throwIfPotentialCSRFAttack", () => {
4141 } ,
4242 } ) ;
4343 expect ( ( ) => throwIfPotentialCSRFAttack ( request , undefined ) ) . toThrow (
44- "`request.url` host does not match `origin` header from a forwarded action request" ,
44+ "`request.url` origin does not match `origin` header from a forwarded action request" ,
4545 ) ;
4646 } ) ;
47+
48+ it ( "should compare complete origins" , ( ) => {
49+ for ( let [ origin , requestUrl ] of [
50+ [ "http://example.com" , "https://example.com/action" ] ,
51+ [ "https://example.com" , "http://example.com/action" ] ,
52+ ] ) {
53+ let request = new Request ( requestUrl , {
54+ method : "POST" ,
55+ headers : { origin } ,
56+ } ) ;
57+
58+ expect ( ( ) => throwIfPotentialCSRFAttack ( request , undefined ) ) . toThrow ( ) ;
59+ }
60+ } ) ;
4761 } ) ;
4862
4963 describe ( "with allowed origins" , ( ) => {
@@ -59,6 +73,18 @@ describe("throwIfPotentialCSRFAttack", () => {
5973 ) . not . toThrow ( ) ;
6074 } ) ;
6175
76+ it ( "should support explicitly allowed hosts" , ( ) => {
77+ let request = new Request ( "https://example.com/action" , {
78+ method : "POST" ,
79+ headers : {
80+ origin : "http://example.com" ,
81+ } ,
82+ } ) ;
83+ expect ( ( ) =>
84+ throwIfPotentialCSRFAttack ( request , [ "example.com" ] ) ,
85+ ) . not . toThrow ( ) ;
86+ } ) ;
87+
6288 it ( "should not throw when origin matches a wildcard pattern" , ( ) => {
6389 let request = new Request ( "https://example.com/action" , {
6490 method : "POST" ,
@@ -93,7 +119,7 @@ describe("throwIfPotentialCSRFAttack", () => {
93119 expect ( ( ) =>
94120 throwIfPotentialCSRFAttack ( request , [ "trusted.com" , "*.safe.com" ] ) ,
95121 ) . toThrow (
96- "`request.url` host does not match `origin` header from a forwarded action request" ,
122+ "`request.url` origin does not match `origin` header from a forwarded action request" ,
97123 ) ;
98124 } ) ;
99125
@@ -134,7 +160,7 @@ describe("throwIfPotentialCSRFAttack", () => {
134160 } ,
135161 } ) ;
136162 expect ( ( ) => throwIfPotentialCSRFAttack ( request , undefined ) ) . toThrow (
137- "`request.url` host does not match `origin` header from a forwarded action request" ,
163+ "`request.url` origin does not match `origin` header from a forwarded action request" ,
138164 ) ;
139165 } ) ;
140166
@@ -181,7 +207,7 @@ describe("throwIfPotentialCSRFAttack", () => {
181207 } ,
182208 } ) ;
183209 expect ( ( ) => throwIfPotentialCSRFAttack ( request , undefined ) ) . toThrow (
184- "`request.url` host does not match `origin` header from a forwarded action request" ,
210+ "`request.url` origin does not match `origin` header from a forwarded action request" ,
185211 ) ;
186212 } ) ;
187213
@@ -195,7 +221,7 @@ describe("throwIfPotentialCSRFAttack", () => {
195221 expect ( ( ) =>
196222 throwIfPotentialCSRFAttack ( request , [ "" , "other.com" ] ) ,
197223 ) . toThrow (
198- "`request.url` host does not match `origin` header from a forwarded action request" ,
224+ "`request.url` origin does not match `origin` header from a forwarded action request" ,
199225 ) ;
200226 } ) ;
201227
@@ -219,7 +245,7 @@ describe("throwIfPotentialCSRFAttack", () => {
219245 } ,
220246 } ) ;
221247 expect ( ( ) => throwIfPotentialCSRFAttack ( request , undefined ) ) . toThrow (
222- "`request.url` host does not match `origin` header from a forwarded action request" ,
248+ "`request.url` origin does not match `origin` header from a forwarded action request" ,
223249 ) ;
224250 } ) ;
225251
@@ -243,7 +269,7 @@ describe("throwIfPotentialCSRFAttack", () => {
243269 } ,
244270 } ) ;
245271 expect ( ( ) => throwIfPotentialCSRFAttack ( request , [ "*" ] ) ) . toThrow (
246- "`request.url` host does not match `origin` header from a forwarded action request" ,
272+ "`request.url` origin does not match `origin` header from a forwarded action request" ,
247273 ) ;
248274 } ) ;
249275
0 commit comments