Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"strings"
)

const maxInterpolationPasses = 10
const (
maxInterpolationPasses = 10
maxInterpolatedLength = 1 << 20 // 1 MiB
)

var exprRE = regexp.MustCompile(`\$\{([^}]+)\}`)

Expand All @@ -18,15 +21,26 @@ func interpolate(s string, props map[string]string) string {
}
for range maxInterpolationPasses {
changed := false
capped := false
growth := 0
baseLen := len(s)
s = exprRE.ReplaceAllStringFunc(s, func(m string) string {
if capped {
return m
}
name := m[2 : len(m)-1]
if v, ok := lookup(props, name); ok {
growth += len(v) - len(m)
if baseLen+growth > maxInterpolatedLength {
capped = true
return m
}
changed = true
return v
}
return m
})
if !changed || !strings.Contains(s, "${") {
if capped || !changed || !strings.Contains(s, "${") {
break
}
}
Expand Down
11 changes: 11 additions & 0 deletions interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ func TestInterpolate(t *testing.T) {
}
}

func TestInterpolateAmplificationCapped(t *testing.T) {
props := map[string]string{
"bomb": "${bomb}${bomb}${bomb}${bomb}${bomb}",
}
result := interpolate("${bomb}", props)
// Without the cap, 10 passes of 5x self-reference would produce ~68 MiB.
if len(result) > maxInterpolatedLength {
t.Fatalf("interpolated length %d exceeds cap %d", len(result), maxInterpolatedLength)
}
}

func TestFirstExpr(t *testing.T) {
tests := []struct {
in string
Expand Down