Skip to content

Commit

Permalink
fixup! fix(download): Escape all Go templating expressions after down…
Browse files Browse the repository at this point in the history
…loading
  • Loading branch information
Laubi committed Jan 23, 2025
1 parent c4dbeef commit 2f42d76
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions internal/template/escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
"bytes"
)

// EscapeGoTemplating replaces each occurrence of "{{" with "{{`{{`}}" and each occurrence of "}}" with "{{`}}`}}".
// Go templating uses `{{` and `}}` to delimit "actions" like data evaluation and control structures. See [text/template] for further details.
// This is used for both Jinja expressions that resources like Workflows use, but also occurrences in DQL queries and other places.
func EscapeGoTemplating(src []byte) []byte {
// UseGoTemplatesForDoubleCurlyBraces replaces each occurrence of "{{" with "{{`{{`}}" and each occurrence of "}}" with "{{`}}`}}".
// This ensures that when the returned string is used to render templates, e.g. during deployment, the "{{" and "}}" are not misinterpreted.
func UseGoTemplatesForDoubleCurlyBraces(src []byte) []byte {
src = bytes.ReplaceAll(src, []byte("{{"), []byte("{{`{{`")) // replace is divided in 2 steps to avoid replacing of closing brackets in the next step
src = bytes.ReplaceAll(src, []byte("}}"), []byte("{{`}}`}}"))
src = bytes.ReplaceAll(src, []byte("{{`{{`"), []byte("{{`{{`}}"))
Expand Down
2 changes: 1 addition & 1 deletion internal/template/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestEscapeGoTemplating(t *testing.T) {

for _, tt := range tc {
t.Run(tt.in, func(t *testing.T) {
out := EscapeGoTemplating([]byte(tt.in))
out := UseGoTemplatesForDoubleCurlyBraces([]byte(tt.in))

assert.Equal(t, tt.expected, string(out))
})
Expand Down

0 comments on commit 2f42d76

Please sign in to comment.