Repository navigation
fix(executor): never expand {{ENV}} placeholders that arrive as run data #10502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,27 +97,59 @@ function getNestedLargeValueMaterializationError(): Error { | |
| ) | ||
| } | ||
|
|
||
| /** A half-open `[start, end)` range of offsets in a resolved string. */ | ||
| interface TextSpan { | ||
| start: number | ||
| end: number | ||
| } | ||
|
|
||
| /** The output range a reference's value was written to. */ | ||
| interface Substitution extends TextSpan { | ||
| reference: string | ||
| } | ||
|
|
||
| /** | ||
| * Replaces each reference in `template`. Alongside the text it returns where each value landed, | ||
| * so a later `{{NAME}}` pass can tell run data apart from authored text. | ||
| */ | ||
| async function replaceValidReferencesAsync( | ||
| template: string, | ||
| replacer: (match: string, index: number, template: string) => Promise<string> | ||
| ): Promise<string> { | ||
| ): Promise<{ text: string; substitutions: Substitution[] }> { | ||
| const pattern = createReferencePattern() | ||
| const substitutions: Substitution[] = [] | ||
| let cursor = 0 | ||
| let result = '' | ||
| let text = '' | ||
| for (const match of template.matchAll(pattern)) { | ||
| const fullMatch = match[0] | ||
| const index = match.index ?? 0 | ||
| result += template.slice(cursor, index) | ||
| result += isLikelyReferenceSegment(fullMatch) | ||
| text += template.slice(cursor, index) | ||
| const replacement = isLikelyReferenceSegment(fullMatch) | ||
| ? await replacer(fullMatch, index, template) | ||
| : fullMatch | ||
| if (replacement !== fullMatch) { | ||
| substitutions.push({ | ||
| start: text.length, | ||
| end: text.length + replacement.length, | ||
| reference: fullMatch, | ||
| }) | ||
| } | ||
| text += replacement | ||
| cursor = index + fullMatch.length | ||
| } | ||
| return result + template.slice(cursor) | ||
| return { text: text + template.slice(cursor), substitutions } | ||
| } | ||
|
|
||
| /** | ||
| * Replaces each `{{NAME}}` in `template`, leaving as written any match that overlaps one of | ||
| * `runValueSpans`. Those spans hold run data (trigger payloads, block outputs), and expanding a | ||
| * placeholder found there would hand whoever supplied the data the plaintext of any secret they | ||
| * can name. An empty span still splits a match that straddles it, so `{{<ref>}}` cannot | ||
| * assemble a name from run data either. | ||
| */ | ||
| async function replaceEnvVarsAsync( | ||
| template: string, | ||
| runValueSpans: readonly TextSpan[], | ||
| replacer: (match: string, index: number) => Promise<string> | ||
| ): Promise<string> { | ||
| const pattern = createEnvVarPattern() | ||
|
|
@@ -126,9 +158,11 @@ async function replaceEnvVarsAsync( | |
| for (const match of template.matchAll(pattern)) { | ||
| const fullMatch = match[0] | ||
| const index = match.index ?? 0 | ||
| const end = index + fullMatch.length | ||
| if (runValueSpans.some((span) => span.start < end && index < span.end)) continue | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Empty run-data substitutions are never considered overlapping by this predicate, so they can assemble an environment placeholder that the next pass expands into a secret. Handle zero-length spans explicitly by blocking an environment match that strictly contains the insertion point. Prompt for AI agents |
||
| result += template.slice(cursor, index) | ||
| result += await replacer(fullMatch, index) | ||
| cursor = index + fullMatch.length | ||
| cursor = end | ||
| } | ||
| return result + template.slice(cursor) | ||
| } | ||
|
|
@@ -344,6 +378,7 @@ function isJsonTextInput(block: SerializedBlock | undefined, key: string): boole | |
| export class VariableResolver { | ||
| private resolvers: Resolver[] | ||
| private blockResolver: BlockResolver | ||
| private workflowResolver: WorkflowResolver | ||
|
|
||
| constructor( | ||
| workflow: SerializedWorkflow, | ||
|
|
@@ -352,10 +387,11 @@ export class VariableResolver { | |
| options: { navigatePathAsync?: AsyncPathNavigator } = {} | ||
| ) { | ||
| this.blockResolver = new BlockResolver(workflow, options.navigatePathAsync) | ||
| this.workflowResolver = new WorkflowResolver(workflowVariables, options.navigatePathAsync) | ||
| this.resolvers = [ | ||
| new LoopResolver(workflow, options.navigatePathAsync), | ||
| new ParallelResolver(workflow, options.navigatePathAsync), | ||
| new WorkflowResolver(workflowVariables, options.navigatePathAsync), | ||
| this.workflowResolver, | ||
| new EnvResolver(), | ||
| this.blockResolver, | ||
| ] | ||
|
|
@@ -645,7 +681,7 @@ export class VariableResolver { | |
| let displayCursor = 0 | ||
| const scan: CodeTemplateScan = { template } | ||
|
|
||
| const result = await replaceValidReferencesAsync(template, async (match, index) => { | ||
| const { text: result } = await replaceValidReferencesAsync(template, async (match, index) => { | ||
| if (replacementError) return match | ||
| displayResult += template.slice(displayCursor, index) | ||
| displayCursor = index + match.length | ||
|
|
@@ -804,7 +840,7 @@ export class VariableResolver { | |
| throw getNestedLargeValueMaterializationError() | ||
| } | ||
|
|
||
| if (this.canInlineResolvedCodeLiteral(effectiveValue, match)) { | ||
| if (this.canInlineResolvedCodeLiteral(effectiveValue, match, resolutionContext)) { | ||
| const replacement = this.blockResolver.formatValueForBlock( | ||
| effectiveValue, | ||
| BlockType.FUNCTION, | ||
|
|
@@ -1094,23 +1130,53 @@ export class VariableResolver { | |
| * is what let a runtime-assigned variable or loop item carrying trigger data close the | ||
| * string it landed in and run as code. | ||
| * | ||
| * The placeholder case is admitted only for a workflow variable, never for a loop item or | ||
| * any other run value. Whoever supplies the text picks which secret the compiler expands | ||
| * into the generated source, so that choice stays with the surface an author configures. | ||
| * The placeholder case is admitted only for an authored variable (`readsAuthoredVariable`), | ||
| * never for a loop item or any other run value. Whoever supplies the text picks which secret | ||
| * the compiler expands into the generated source, so that choice stays with the surface an | ||
| * author configures. | ||
| */ | ||
| private canInlineResolvedCodeLiteral(value: unknown, reference: string): boolean { | ||
| private canInlineResolvedCodeLiteral( | ||
| value: unknown, | ||
| reference: string, | ||
| context: ResolutionContext | ||
| ): boolean { | ||
| if (value === null || typeof value === 'number' || typeof value === 'boolean') { | ||
| return true | ||
| } | ||
| if (typeof value !== 'string') { | ||
| return false | ||
| } | ||
| if (parseReferencePath(reference)[0] !== REFERENCE.PREFIX.VARIABLE) { | ||
| if (!this.readsAuthoredVariable(reference, context)) { | ||
| return false | ||
| } | ||
| return createEnvVarPattern().test(value) && !/['"`$\\\n\r\u2028\u2029]/.test(value) | ||
| } | ||
|
|
||
| /** | ||
| * Whether `reference` reads a workflow variable that still holds its saved value: the one | ||
| * reference whose value an author configures to carry a `{{NAME}}` placeholder | ||
| * (`<variable.indirectSecret>`). A variable a Variables block reassigned may hold run data, | ||
| * so it reads like any other run value. | ||
| */ | ||
| private readsAuthoredVariable(reference: string, context: ResolutionContext): boolean { | ||
| if (parseReferencePath(reference)[0] !== REFERENCE.PREFIX.VARIABLE) return false | ||
| const variableId = this.workflowResolver.findVariableId(reference, context) | ||
| return ( | ||
| variableId !== undefined && | ||
| !context.executionContext.reassignedWorkflowVariableIds?.has(variableId) | ||
| ) | ||
| } | ||
|
|
||
| /** The substitutions whose text is run data rather than an authored variable's value. */ | ||
| private runValueSpans( | ||
| substitutions: readonly Substitution[], | ||
| context: ResolutionContext | ||
| ): TextSpan[] { | ||
| return substitutions.filter( | ||
| (substitution) => !this.readsAuthoredVariable(substitution.reference, context) | ||
| ) | ||
| } | ||
|
|
||
| private formatJavaScriptAsyncExpression( | ||
| expression: string, | ||
| scan: CodeTemplateScan, | ||
|
|
@@ -1867,7 +1933,7 @@ export class VariableResolver { | |
| const spliceReference = placeholderSplicer(template, options.jsonText) | ||
| let projectedReferenceResult = '' | ||
| let projectedReferenceCursor = 0 | ||
| let result = await replaceValidReferencesAsync(template, async (match, index) => { | ||
| const references = await replaceValidReferencesAsync(template, async (match, index) => { | ||
| if (replacementError) return match | ||
|
|
||
| projectedReferenceResult += template.slice(projectedReferenceCursor, index) | ||
|
|
@@ -1918,12 +1984,17 @@ export class VariableResolver { | |
| throw replacementError | ||
| } | ||
|
|
||
| const spliceEnvVar = placeholderSplicer(result, options.jsonText) | ||
| result = await replaceEnvVarsAsync(result, async (match, index) => { | ||
| const resolved = await this.resolveReference(match, resolutionContext) | ||
| if (typeof resolved !== 'string') return match | ||
| return spliceEnvVar(resolved, index, index + match.length, true) | ||
| }) | ||
| const spliceEnvVar = placeholderSplicer(references.text, options.jsonText) | ||
| const runValueSpans = this.runValueSpans(references.substitutions, resolutionContext) | ||
| const result = await replaceEnvVarsAsync( | ||
| references.text, | ||
| runValueSpans, | ||
| async (match, index) => { | ||
| const resolved = await this.resolveReference(match, resolutionContext) | ||
| if (typeof resolved !== 'string') return match | ||
| return spliceEnvVar(resolved, index, index + match.length, true) | ||
| } | ||
| ) | ||
| ctx.resolvedSecretTraceRegistry?.recordResolvedInputProjection( | ||
| options.inputPath, | ||
| result, | ||
|
|
@@ -1952,7 +2023,7 @@ export class VariableResolver { | |
|
|
||
| let projectedReferenceResult = '' | ||
| let projectedReferenceCursor = 0 | ||
| let result = await replaceValidReferencesAsync(template, async (match, index) => { | ||
| const references = await replaceValidReferencesAsync(template, async (match, index) => { | ||
| if (replacementError) return match | ||
|
|
||
| projectedReferenceResult += template.slice(projectedReferenceCursor, index) | ||
|
|
@@ -2002,7 +2073,8 @@ export class VariableResolver { | |
| throw replacementError | ||
| } | ||
|
|
||
| result = await replaceEnvVarsAsync(result, async (match) => { | ||
| const runValueSpans = this.runValueSpans(references.substitutions, resolutionContext) | ||
| const result = await replaceEnvVarsAsync(references.text, runValueSpans, async (match) => { | ||
| const resolved = await this.resolveReference(match, resolutionContext) | ||
| if (typeof resolved !== 'string') return match | ||
| return isStructurallyInertConditionLiteral(resolved) ? resolved : match | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Clearing
reassignedWorkflowVariableIdson run-from-block makes variables that a Variables block reassigned before the source snapshot look authored again. Their saved values can be run data, so a{{SECRET}}inside them could be expanded. Carry the saved set through instead of resetting it, or document this as an accepted risk.Prompt for AI agents