Skip to content

Commit

Permalink
fix(template): fix template string escaping and resolution in Module …
Browse files Browse the repository at this point in the history
…configs (#6408)

* fix(template): skip un-escaping while module vars resolution

Patches commit 8df3a4f from #5680.
Module variables are resolved in a standalone function `resolveVariables`.
That function should also skip un-escaping.

* fix(template): fix function definition

Use its own argument instead of a closure.

* refactor(template): pull function `shouldUnescape` up

* fix(template): fix too greedy pattern for escaped string
  • Loading branch information
vvagaytsev authored Aug 28, 2024
1 parent 1a87cd3 commit 8df8015
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
9 changes: 8 additions & 1 deletion core/src/resolve-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,14 @@ export class ModuleResolver {
templateContextParams: ModuleConfigContextParams
): Promise<DeepPrimitiveMap> {
const moduleConfigContext = new ModuleConfigContext(templateContextParams)
const resolveOpts = { allowPartial: false }
const resolveOpts = {
allowPartial: false,
// Modules will be converted to actions later, and the actions will be properly unescaped.
// We avoid premature un-escaping here,
// because otherwise it will strip the escaped value in the module config
// to the normal template string in the converted action config.
unescape: false,
}

let varfileVars: DeepPrimitiveMap = {}
if (config.varfile) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/template-string/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TemplateString
/ $(.*) { return text() === "" ? [] : [{ resolved: text() }] }

FormatString
= EscapeStart SourceCharacter* FormatEndWithOptional {
= EscapeStart (!FormatEndWithOptional SourceCharacter)* FormatEndWithOptional {
if (options.unescape) {
return text().slice(1)
} else {
Expand Down
18 changes: 9 additions & 9 deletions core/src/template-string/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ export class TemplateError extends GardenError {
}
}

const shouldUnescape = (ctxOpts: ContextResolveOpts) => {
// Explicit non-escaping takes the highest priority.
if (ctxOpts.unescape === false) {
return false
}

return !!ctxOpts.unescape || !ctxOpts.allowPartial
}

/**
* Parse and resolve a templated string, with the given context. The template format is similar to native JS templated
* strings but only supports simple lookups from the given context, e.g. "prefix-${nested.key}-suffix", and not
Expand All @@ -120,15 +129,6 @@ export const resolveTemplateString = profile(function resolveTemplateString({
return string
}

const shouldUnescape = (ctxOpts: ContextResolveOpts) => {
// Explicit non-escaping takes the highest priority.
if (ctxOpts.unescape === false) {
return false
}

return !!ctxOpts.unescape || !contextOpts.allowPartial
}

try {
const parsed = parser.parse(string, {
getKey: (key: string[], resolveOpts?: ContextResolveOpts) => {
Expand Down

0 comments on commit 8df8015

Please sign in to comment.