mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2025-12-22 20:05:27 +00:00
making markers parametric + simple match/replace for basic variables
This commit is contained in:
parent
369255a4fe
commit
adf4721833
@ -33,11 +33,14 @@ func EvaluateByte(data []byte, base map[string]interface{}) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func evaluate(data string, base map[string]interface{}) (string, error) {
|
func evaluate(data string, base map[string]interface{}) (string, error) {
|
||||||
|
// replace simple placeholders (key => value) MarkerOpen + key + MarkerClose and General + key + General to value
|
||||||
|
data = replacer.Replace(data, base)
|
||||||
|
|
||||||
// expressions can be:
|
// expressions can be:
|
||||||
// - simple: containing base values keys (variables)
|
// - simple: containing base values keys (variables)
|
||||||
// - complex: containing helper functions [ + variables]
|
// - complex: containing helper functions [ + variables]
|
||||||
// literals like {{2+2}} are not considered expressions
|
// literals like {{2+2}} are not considered expressions
|
||||||
expressions := findExpressions(data, mergeFunctions(dsl.HelperFunctions(), mapToFunctions(base)))
|
expressions := findExpressions(data, marker.ParenthesisOpen, marker.ParenthesisClose, mergeFunctions(dsl.HelperFunctions(), mapToFunctions(base)))
|
||||||
dynamicValues := make(map[string]interface{})
|
dynamicValues := make(map[string]interface{})
|
||||||
for _, expression := range expressions {
|
for _, expression := range expressions {
|
||||||
// replace variable placeholders with base values
|
// replace variable placeholders with base values
|
||||||
@ -60,7 +63,7 @@ func evaluate(data string, base map[string]interface{}) (string, error) {
|
|||||||
// maxIterations to avoid infinite loop
|
// maxIterations to avoid infinite loop
|
||||||
const maxIterations = 250
|
const maxIterations = 250
|
||||||
|
|
||||||
func findExpressions(data string, functions map[string]govaluate.ExpressionFunction) []string {
|
func findExpressions(data, OpenMarker, CloseMarker string, functions map[string]govaluate.ExpressionFunction) []string {
|
||||||
var (
|
var (
|
||||||
iterations int
|
iterations int
|
||||||
exps []string
|
exps []string
|
||||||
@ -72,13 +75,13 @@ func findExpressions(data string, functions map[string]govaluate.ExpressionFunct
|
|||||||
}
|
}
|
||||||
iterations++
|
iterations++
|
||||||
// attempt to find open markers
|
// attempt to find open markers
|
||||||
indexOpenMarker := strings.Index(data, marker.ParenthesisOpen)
|
indexOpenMarker := strings.Index(data, OpenMarker)
|
||||||
// exits if not found
|
// exits if not found
|
||||||
if indexOpenMarker < 0 {
|
if indexOpenMarker < 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
indexOpenMarkerOffset := indexOpenMarker + len(marker.ParenthesisOpen)
|
indexOpenMarkerOffset := indexOpenMarker + len(OpenMarker)
|
||||||
|
|
||||||
shouldSearchCloseMarker := true
|
shouldSearchCloseMarker := true
|
||||||
closeMarkerFound := false
|
closeMarkerFound := false
|
||||||
@ -88,13 +91,13 @@ func findExpressions(data string, functions map[string]govaluate.ExpressionFunct
|
|||||||
skip := indexOpenMarkerOffset
|
skip := indexOpenMarkerOffset
|
||||||
for shouldSearchCloseMarker {
|
for shouldSearchCloseMarker {
|
||||||
// attempt to find close marker
|
// attempt to find close marker
|
||||||
indexCloseMarker = stringsutil.IndexAt(innerData, marker.ParenthesisClose, skip)
|
indexCloseMarker = stringsutil.IndexAt(innerData, CloseMarker, skip)
|
||||||
// if no close markers are found exit
|
// if no close markers are found exit
|
||||||
if indexCloseMarker < 0 {
|
if indexCloseMarker < 0 {
|
||||||
shouldSearchCloseMarker = false
|
shouldSearchCloseMarker = false
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
indexCloseMarkerOffset = indexCloseMarker + len(marker.ParenthesisClose)
|
indexCloseMarkerOffset = indexCloseMarker + len(CloseMarker)
|
||||||
|
|
||||||
potentialMatch = innerData[indexOpenMarkerOffset:indexCloseMarker]
|
potentialMatch = innerData[indexOpenMarkerOffset:indexCloseMarker]
|
||||||
if isExpression(potentialMatch, functions) {
|
if isExpression(potentialMatch, functions) {
|
||||||
@ -152,3 +155,11 @@ func getFunctionsNames(m map[string]govaluate.ExpressionFunction) []string {
|
|||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getMapKeysAsValues(m map[string]interface{}) map[string]interface{} {
|
||||||
|
mapOut := make(map[string]interface{})
|
||||||
|
for k := range m {
|
||||||
|
mapOut[k] = k
|
||||||
|
}
|
||||||
|
return mapOut
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user