Merge pull request #1561 from projectdiscovery/issue-1543-rand-seed

Implementing incremental expression replacement
dev
Sandeep Singh 2022-02-03 23:19:35 +05:30 committed by GitHub
commit 249937ca72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -41,7 +41,6 @@ func evaluate(data string, base map[string]interface{}) (string, error) {
// - complex: containing helper functions [ + variables]
// literals like {{2+2}} are not considered expressions
expressions := findExpressions(data, marker.ParenthesisOpen, marker.ParenthesisClose, mergeFunctions(dsl.HelperFunctions(), mapToFunctions(base)))
dynamicValues := make(map[string]interface{})
for _, expression := range expressions {
// replace variable placeholders with base values
expression = replacer.Replace(expression, base)
@ -54,10 +53,11 @@ func evaluate(data string, base map[string]interface{}) (string, error) {
if err != nil {
continue
}
dynamicValues[expression] = result
// replace incrementally
data = replacer.ReplaceOne(data, expression, result)
}
// Replacer dynamic values if any in raw request and parse it
return replacer.Replace(data, dynamicValues), nil
return data, nil
}
// maxIterations to avoid infinite loop

View File

@ -31,3 +31,14 @@ func Replace(template string, values map[string]interface{}) string {
final := replacer.Replace(template)
return final
}
// Replace replaces one placeholder in template with one value on the fly.
func ReplaceOne(template string, key string, value interface{}) string {
data := replaceOneWithMarkers(template, key, value, marker.ParenthesisOpen, marker.ParenthesisClose)
return replaceOneWithMarkers(data, key, value, marker.General, marker.General)
}
// replaceOneWithMarkers is a helper function that perform one time replacement
func replaceOneWithMarkers(template, key string, value interface{}, openMarker, closeMarker string) string {
return strings.Replace(template, openMarker+key+closeMarker, types.ToString(value), 1)
}