@@ -21,6 +21,8 @@ var _ = require('lodash'),
2121 EXECUTION_COOKIES_EVENT_BASE = 'execution.cookies.' ,
2222 EXECUTION_SKIP_REQUEST_EVENT_BASE = 'execution.skipRequest.' ,
2323
24+ EXECUTION_VAULT_BASE = 'execution.vault.' ,
25+
2426 COOKIES_EVENT_STORE_ACTION = 'store' ,
2527 COOKIE_STORE_PUT_METHOD = 'putCookie' ,
2628 COOKIE_STORE_UPDATE_METHOD = 'updateCookie' ,
@@ -240,8 +242,17 @@ module.exports = {
240242
241243 packageResolver = _ . get ( this , 'options.script.packageResolver' ) ,
242244
245+ vaultSecrets = payload . context . vaultSecrets ,
246+ allowVaultAccess = _ . get ( vaultSecrets , '_.allowScriptAccess' ) ,
247+
243248 events ;
244249
250+ // Explicitly enable tracking for vault secrets here as this will
251+ // not be sent to sandbox who otherwise takes care of mutation tracking
252+ if ( allowVaultAccess ) {
253+ vaultSecrets . enableTracking ( { autoCompact : true } ) ;
254+ }
255+
245256 // @todo : find a better place to code this so that event is not aware of such options
246257 if ( abortOnFailure ) {
247258 abortOnError = true ;
@@ -387,6 +398,22 @@ module.exports = {
387398 }
388399 } . bind ( this ) ) ;
389400
401+ this . host . on ( EXECUTION_VAULT_BASE + executionId , function ( id , cmd , ...args ) {
402+ // Ensure error is string
403+ // TODO identify why error objects are not being serialized correctly
404+ const dispatch = ( e , r ) => { this . host . dispatch ( EXECUTION_VAULT_BASE + executionId , id , e , r ) ; } ;
405+
406+ if ( ! allowVaultAccess ) {
407+ return dispatch ( 'Vault access denied' ) ;
408+ }
409+
410+ if ( ! [ 'get' , 'set' , 'unset' ] . includes ( cmd ) ) {
411+ return dispatch ( `Invalid vault command: ${ cmd } ` ) ;
412+ }
413+
414+ dispatch ( null , vaultSecrets [ cmd ] ( ...args ) ) ;
415+ } . bind ( this ) ) ;
416+
390417 this . host . on ( EXECUTION_REQUEST_EVENT_BASE + executionId ,
391418 function ( scriptCursor , id , requestId , request ) {
392419 // remove files in request body if any
@@ -458,11 +485,7 @@ module.exports = {
458485 // @todo : Expose this as a property in Collection SDK's Script
459486 timeout : payload . scriptTimeout ,
460487 cursor : scriptCursor ,
461- context : {
462- ..._ . pick ( payload . context , SAFE_CONTEXT_VARIABLES ) ,
463- vaultSecrets : _ . get ( payload . context . vaultSecrets , '_.allowScriptAccess' ) ?
464- payload . context . vaultSecrets : undefined
465- } ,
488+ context : _ . pick ( payload . context , SAFE_CONTEXT_VARIABLES ) ,
466489 resolvedPackages : resolvedPackages ,
467490
468491 // legacy options
@@ -479,6 +502,7 @@ module.exports = {
479502 this . host . removeAllListeners ( EXECUTION_COOKIES_EVENT_BASE + executionId ) ;
480503 this . host . removeAllListeners ( EXECUTION_ERROR_EVENT_BASE + executionId ) ;
481504 this . host . removeAllListeners ( EXECUTION_SKIP_REQUEST_EVENT_BASE + executionId ) ;
505+ this . host . removeAllListeners ( EXECUTION_VAULT_BASE + executionId ) ;
482506
483507 // Handle async errors as well.
484508 // If there was an error running the script itself, that takes precedence
@@ -529,10 +553,16 @@ module.exports = {
529553 result && result . globals && ( result . globals = new sdk . VariableScope ( result . globals ) ) ;
530554 result && result . collectionVariables &&
531555 ( result . collectionVariables = new sdk . VariableScope ( result . collectionVariables ) ) ;
532- result && result . vaultSecrets &&
533- ( result . vaultSecrets = new sdk . VariableScope ( result . vaultSecrets ) ) ;
534556 result && result . request && ( result . request = new sdk . Request ( result . request ) ) ;
535557
558+ // vault secrets are not sent to sandbox, thus using the scope from run context.
559+ if ( allowVaultAccess && vaultSecrets ) {
560+ result . vaultSecrets = vaultSecrets ;
561+
562+ // Prevent mutations from being carry-forwarded to subsequent events
563+ vaultSecrets . disableTracking ( ) ;
564+ }
565+
536566 // @note Since [email protected] , response object is not included in the execution 537567 // result.
538568 // Refer: https://github.com/postmanlabs/postman-sandbox/pull/512
0 commit comments