@@ -256,6 +256,23 @@ class Gateway extends EventEmitter {
256256 ) ;
257257 }
258258
259+ __originError__ ( req , res , origin , headers = { } ) {
260+ headers = this . __createHeaders__ ( req , headers ) ;
261+ headers [ 'content-type' ] = 'application/json' ;
262+ return this . __endRequest__ (
263+ 403 ,
264+ this . __formatHeaders__ ( headers ) ,
265+ req ,
266+ res ,
267+ JSON . stringify ( {
268+ error : {
269+ type : 'OriginError' ,
270+ message : `Provided origin "${ origin } " can not access this resource` ,
271+ }
272+ } )
273+ ) ;
274+ }
275+
259276 __debugError__ ( req , res , message , headers = { } ) {
260277 message = message || `You do not have permission to debug this endpoint` ;
261278 headers = this . __createHeaders__ ( req , headers ) ;
@@ -1165,12 +1182,6 @@ class Gateway extends EventEmitter {
11651182 this . log ( req , `Request Received (Size ${ buffer . length } )` ) ;
11661183 this . resolve ( req , res , buffer , ( err , definition , data , buffer , proxyParameters ) => {
11671184
1168- let headers = { } ;
1169- headers [ 'x-execution-uuid' ] = req . _uuid ;
1170- if ( definition && definition . allowOrigin ) {
1171- headers [ 'access-control-allow-origin' ] = definition . allowOrigin ;
1172- }
1173-
11741185 if ( err ) {
11751186 if ( err . accessSourceError ) {
11761187 return this . __accessSourceError__ ( req , res , err . message ) ;
@@ -1203,6 +1214,20 @@ class Gateway extends EventEmitter {
12031214 return this . __clientError__ ( req , res , err . message , err . statusCode || 400 ) ;
12041215 }
12051216
1217+ let headers = { } ;
1218+ headers [ 'x-execution-uuid' ] = req . _uuid ;
1219+ if ( definition . origins ) {
1220+ if (
1221+ req . headers [ 'origin' ] &&
1222+ definition . origins . indexOf ( req . headers [ 'origin' ] ) !== - 1
1223+ ) {
1224+ headers [ 'access-control-allow-origin' ] = req . headers [ 'origin' ] ;
1225+ } else {
1226+ headers [ 'access-control-allow-origin' ] = '!' ;
1227+ return this . __originError__ ( req , res , req . headers [ 'origin' ] , headers ) ;
1228+ }
1229+ }
1230+
12061231 let [ contentType , ...contentTypeParameters ] = ( req . method === 'GET' || req . method === 'DELETE' )
12071232 ? [ 'application/x-www-form-urlencoded' ]
12081233 : ( req . headers [ 'content-type' ] || '' ) . split ( ';' ) ;
0 commit comments