@@ -19,6 +19,30 @@ describe("http", () => {
1919 expect ( http . relativeRoot ( "/foo/bar/" ) ) . toStrictEqual ( "./../.." )
2020 } )
2121
22+ describe ( "isTrustedOrigin" , ( ) => {
23+ it ( "should match exact origins" , ( ) => {
24+ expect ( http . isTrustedOrigin ( "localhost:8080" , [ "localhost:8080" ] ) ) . toBe ( true )
25+ expect ( http . isTrustedOrigin ( "example.com" , [ "example.com" ] ) ) . toBe ( true )
26+ expect ( http . isTrustedOrigin ( "example.com" , [ "other.com" ] ) ) . toBe ( false )
27+ } )
28+
29+ it ( "should match the wildcard *" , ( ) => {
30+ expect ( http . isTrustedOrigin ( "anything.example.com" , [ "*" ] ) ) . toBe ( true )
31+ expect ( http . isTrustedOrigin ( "localhost:8080" , [ "*" ] ) ) . toBe ( true )
32+ } )
33+
34+ it ( "should match *.example.com wildcard (same style as --proxy-domain)" , ( ) => {
35+ expect ( http . isTrustedOrigin ( "sub.example.com" , [ "*.example.com" ] ) ) . toBe ( true )
36+ expect ( http . isTrustedOrigin ( "example.com" , [ "*.example.com" ] ) ) . toBe ( true )
37+ expect ( http . isTrustedOrigin ( "evil.com" , [ "*.example.com" ] ) ) . toBe ( false )
38+ expect ( http . isTrustedOrigin ( "example.com.evil.com" , [ "*.example.com" ] ) ) . toBe ( false )
39+ } )
40+
41+ it ( "should return false for an empty trusted origins list" , ( ) => {
42+ expect ( http . isTrustedOrigin ( "example.com" , [ ] ) ) . toBe ( false )
43+ } )
44+ } )
45+
2246 describe ( "origin" , ( ) => {
2347 ; [
2448 {
@@ -54,6 +78,22 @@ describe("http", () => {
5478 host : "localhost:8080" ,
5579 expected : "malformed" , // Parsing fails completely.
5680 } ,
81+ {
82+ origin : "http://sub.example.com" ,
83+ host : "other.com" ,
84+ trustedOrigins : [ "*.example.com" ] ,
85+ } ,
86+ {
87+ origin : "http://evil.com" ,
88+ host : "other.com" ,
89+ trustedOrigins : [ "*.example.com" ] ,
90+ expected : "does not match" ,
91+ } ,
92+ {
93+ origin : "http://sub.example.com" ,
94+ host : "other.com" ,
95+ trustedOrigins : [ "*" ] ,
96+ } ,
5797 ] . forEach ( ( test ) => {
5898 ; [
5999 [ "host" , test . host ] ,
@@ -70,7 +110,9 @@ describe("http", () => {
70110 origin : test . origin ,
71111 [ key ] : value ,
72112 } ,
73- args : { } ,
113+ args : {
114+ "trusted-origins" : ( test as { trustedOrigins ?: string [ ] } ) . trustedOrigins ,
115+ } ,
74116 } )
75117 if ( typeof test . expected === "string" ) {
76118 expect ( ( ) => http . authenticateOrigin ( req ) ) . toThrow ( test . expected )
0 commit comments