@@ -42,10 +42,13 @@ async function setupServerPath() {
4242 return path . join ( tmpDir , 'httptoolkit-server' , 'bin' , 'run' ) ;
4343}
4444
45- const buildGraphql = ( url : string ) => getGraphQL ( url , {
46- asJSON : true ,
45+ const buildGraphql = (
46+ url : string ,
4747 // Pretend to be a browser on the real site:
48- headers : { 'origin' : 'https://app.httptoolkit.tech' }
48+ headers = { 'origin' : 'https://app.httptoolkit.tech' }
49+ ) => getGraphQL ( url , {
50+ asJSON : true ,
51+ headers
4952} ) ;
5053
5154describe ( 'Integration test' , function ( ) {
@@ -152,6 +155,38 @@ describe('Integration test', function () {
152155 } ) ;
153156 } ) ;
154157
158+ it ( 'rejects all requests with invalid origins' , async ( ) => {
159+ const graphql = buildGraphql ( 'http://localhost:45457/' , {
160+ origin : 'https://unknown.test'
161+ } ) ;
162+
163+ const restWrongOriginResponse = await fetch ( 'http://localhost:45457/version' , {
164+ headers : { 'origin' : 'https://unknown.test' }
165+ } ) ;
166+
167+ const restNoOriginResponse = await fetch ( 'http://localhost:45457/version' , {
168+ headers : { }
169+ } ) ;
170+
171+ expect ( restWrongOriginResponse . status ) . to . equal ( 403 ) ;
172+ expect ( restNoOriginResponse . status ) . to . equal ( 403 ) ;
173+
174+ try {
175+ await graphql ( `
176+ query getVersion {
177+ version
178+ }
179+ ` ) ( ) ;
180+ expect . fail ( 'GraphQL request with invalid origin should fail' ) ;
181+ } catch ( errorResponse : any ) {
182+ // GraphQL.js handles errors weirdly, and just throws the response body. Oh well,
183+ // it's good enough to test this anyway:
184+ expect ( errorResponse ) . to . deep . equal ( {
185+ error : { message : 'Invalid CORS headers' }
186+ } ) ;
187+ }
188+ } ) ;
189+
155190 it ( 'exposes the system configuration via REST' , async ( ) => {
156191 const response = await fetch ( 'http://localhost:45457/config?proxyPort=8000' , {
157192 headers : { 'origin' : 'https://app.httptoolkit.tech' }
0 commit comments