Skip to content

Commit

Permalink
fix for infinite recursion in Postman var sub (#2780)
Browse files Browse the repository at this point in the history
* fix for infinite recursion

* oneliner
  • Loading branch information
zricethezav committed May 2, 2024
1 parent 94a1653 commit 4ea3a13
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/sources/postman/substitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ func (s *Source) buildSubstitution(data string, metadata Metadata, combos *map[s
if slice.Metadata.CollectionInfo.PostmanID != "" && slice.Metadata.CollectionInfo.PostmanID != metadata.CollectionInfo.PostmanID {
continue
}
// to ensure we don't infinitely recurse, we will trim all `{{}}` from the values before replacement
d := strings.ReplaceAll(data, match, strings.Trim(slice.value, "{}"))

// to ensure we don't infinitely recurse, we will trim all `{{}}` from the values before replacement.
// this is to prevent the case where a variable is replaced with a value that contains the same variable causing
// an infinite loop
removedBrackets := strings.ReplaceAll(strings.ReplaceAll(slice.value, "{{", ""), "}}", "")

d := strings.ReplaceAll(data, match, removedBrackets)
s.buildSubstitution(d, metadata, combos)
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/sources/postman/substitution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func TestSource_BuildSubstituteSet(t *testing.T) {
}
s.sub.add(Metadata{Type: GLOBAL_TYPE}, "var1", "value1")
s.sub.add(Metadata{Type: GLOBAL_TYPE}, "var2", "value2")
s.sub.add(Metadata{Type: GLOBAL_TYPE}, "", "value2")
s.sub.add(Metadata{Type: GLOBAL_TYPE}, "continuation_token", "'{{continuation_token}}'") // this caused an infinite loop in the original implementation
s.sub.add(Metadata{Type: GLOBAL_TYPE}, "continuation_token2", "'{{{continuation_token2}}}'") // this caused an infinite loop in the original implementation

metadata := Metadata{
Type: GLOBAL_TYPE,
Expand All @@ -81,6 +84,8 @@ func TestSource_BuildSubstituteSet(t *testing.T) {
{"{{var2}}", []string{"value2"}},
{"{{var1}}:{{var2}}", []string{"value1:value2"}},
{"no variables", []string{"no variables"}},
{"{{var1}}:{{continuation_token}}", []string{"value1:'continuation_token'"}},
{"{{var1}}:{{continuation_token2}}", []string{"value1:'{continuation_token2}'"}},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 4ea3a13

Please sign in to comment.