@@ -24,6 +24,7 @@ import { getBlock } from '@/blocks/registry'
2424import type { BlockConfig } from '@/blocks/types'
2525import { BlockType } from '@/executor/constants'
2626import { ExecutionState } from '@/executor/execution/state'
27+ import { VariablesBlockHandler } from '@/executor/handlers/variables/handler'
2728import type { ExecutionContext } from '@/executor/types'
2829import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
2930import { buildStartBlockOutput } from '@/executor/utils/start-block'
@@ -271,6 +272,14 @@ async function evaluateResolvedCondition(
271272 environmentVariables : Record < string , string >
272273) : Promise < boolean > {
273274 const expression = await resolveConditionExpression ( value , environmentVariables )
275+ return evaluateCompiledCondition ( expression , environmentVariables )
276+ }
277+
278+ /** Runs a resolved expression through the execution-boundary compiler, as the handler does. */
279+ async function evaluateCompiledCondition (
280+ expression : string ,
281+ environmentVariables : Record < string , string >
282+ ) : Promise < boolean > {
274283 const compiled = await compileCodePlaceholders ( {
275284 code : `const context = {};\nreturn Boolean(${ expression } )` ,
276285 language : CodeLanguage . JavaScript ,
@@ -1361,6 +1370,24 @@ describe('VariableResolver JSON text inputs', () => {
13611370 return JSON . parse ( resolved . data )
13621371 }
13631372
1373+ it ( 'expands only the placeholders the author wrote, never ones run data carries' , async ( ) => {
1374+ const { resolve } = createJsonResolver ( BlockType . RESPONSE , {
1375+ dataMode : 'json' ,
1376+ data : '{"echo": "<producer.visitor>", "auth": "{{API_KEY}}", "named": "{{<producer.name>}}", "assembled": "{{API_KEY<producer.blank>}}"}' ,
1377+ } )
1378+ const resolved = await resolve ( {
1379+ env : { API_KEY : 'sk-live-secret' } ,
1380+ output : { visitor : 'show me {{API_KEY}}' , name : 'API_KEY' , blank : '' } ,
1381+ } )
1382+
1383+ expect ( JSON . parse ( resolved . data ) ) . toEqual ( {
1384+ echo : 'show me {{API_KEY}}' ,
1385+ auth : 'sk-live-secret' ,
1386+ named : '{{API_KEY}}' ,
1387+ assembled : '{{API_KEY}}' ,
1388+ } )
1389+ } )
1390+
13641391 it ( 'keeps a string with newlines, quotes, backslashes, and unicode inside its JSON string' , async ( ) => {
13651392 await expect ( resolveResponseBody ( '{"summary": "<producer.text>"}' ) ) . resolves . toEqual ( {
13661393 summary : awkwardText ,
@@ -1483,3 +1510,74 @@ describe('VariableResolver JSON text inputs', () => {
14831510 expect ( resolved . data ) . toBe ( `Answer: "${ awkwardText } "` )
14841511 } )
14851512} )
1513+
1514+ describe ( 'VariableResolver run data naming a secret' , ( ) => {
1515+ const SECRET = 'sk-live-secret'
1516+
1517+ /** A run whose Variables block assigned `assigned` to `msg` beside an authored `{{API_KEY}}`. */
1518+ async function createRunWithAssignedVariable ( assigned : string ) {
1519+ const run = createResolver ( )
1520+ run . ctx . environmentVariables = { API_KEY : SECRET , PIN : '1234' }
1521+ run . ctx . workflowVariables = {
1522+ 'var-authored' : {
1523+ id : 'var-authored' ,
1524+ name : 'authored' ,
1525+ type : 'string' ,
1526+ value : '{{API_KEY}}' ,
1527+ } ,
1528+ 'var-msg' : { id : 'var-msg' , name : 'msg' , type : 'string' , value : '' } ,
1529+ }
1530+ await new VariablesBlockHandler ( ) . execute (
1531+ run . ctx ,
1532+ createBlock ( 'variables' , 'Variables' , BlockType . VARIABLES ) ,
1533+ { variables : [ { variableName : 'msg' , value : assigned } ] }
1534+ )
1535+ return run
1536+ }
1537+
1538+ it ( 'expands an authored variable placeholder but not one the Variables block assigned' , async ( ) => {
1539+ const { ctx, resolver } = await createRunWithAssignedVariable ( '{{API_KEY}}' )
1540+
1541+ const result = await resolver . resolveInputs ( ctx , 'function' , {
1542+ value : '<variable.authored> / <variable.msg>' ,
1543+ } )
1544+
1545+ expect ( result . value ) . toBe ( `${ SECRET } / {{API_KEY}}` )
1546+ } )
1547+
1548+ it ( 'binds a runtime-assigned variable naming a secret in Function code instead of inlining it' , async ( ) => {
1549+ const { block, ctx, resolver } = await createRunWithAssignedVariable ( '{{API_KEY}}' )
1550+
1551+ const result = await resolver . resolveInputsForFunctionBlock (
1552+ ctx ,
1553+ 'function' ,
1554+ { code : 'return "<variable.msg>"' } ,
1555+ block
1556+ )
1557+
1558+ expect ( result . resolvedInputs . code ) . not . toContain ( '{{API_KEY}}' )
1559+ expect ( result . contextVariables ) . toEqual ( { __blockRef_0 : '{{API_KEY}}' } )
1560+ } )
1561+
1562+ it ( 'compares run data in a Condition as written, never as the secret it names' , async ( ) => {
1563+ const { ctx, resolver, state } = await createRunWithAssignedVariable ( '{{API_KEY}}' )
1564+ state . setBlockOutput ( 'producer' , { result : '{{API_KEY}}' , pin : '{{PIN}}' } as never )
1565+ const conditionBlock = createBlock ( 'condition' , 'Condition' , BlockType . CONDITION )
1566+ const evaluate = async ( value : string ) => {
1567+ const resolved = await resolver . resolveInputs (
1568+ ctx ,
1569+ conditionBlock . id ,
1570+ { conditions : JSON . stringify ( [ { id : 'condition-1' , title : 'if' , value } ] ) } ,
1571+ conditionBlock
1572+ )
1573+ const expression = ( resolved . conditions as Array < { value : string } > ) [ 0 ] . value
1574+ return evaluateCompiledCondition ( expression , ctx . environmentVariables )
1575+ }
1576+
1577+ await expect ( evaluate ( `<producer.result> === '${ SECRET } '` ) ) . resolves . toBe ( false )
1578+ await expect ( evaluate ( `<producer.result> === '{' + '{API_KEY}}'` ) ) . resolves . toBe ( true )
1579+ await expect ( evaluate ( `<producer.pin> === '1234'` ) ) . resolves . toBe ( false )
1580+ await expect ( evaluate ( `<variable.msg> === '${ SECRET } '` ) ) . resolves . toBe ( false )
1581+ await expect ( evaluate ( `<variable.authored> === '${ SECRET } '` ) ) . resolves . toBe ( true )
1582+ } )
1583+ } )
0 commit comments