56 lines
1.5 KiB
Go
Raw Normal View History

2020-12-29 12:08:46 +05:30
package generators
import (
"errors"
"fmt"
2021-12-06 11:38:22 +01:00
"path/filepath"
2020-12-29 12:08:46 +05:30
"strings"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
fileutil "github.com/projectdiscovery/utils/file"
folderutil "github.com/projectdiscovery/utils/folder"
2020-12-29 12:08:46 +05:30
)
// validate validates the payloads if any.
func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePath string) error {
2020-12-29 12:08:46 +05:30
for name, payload := range payloads {
switch payloadType := payload.(type) {
2020-12-29 12:08:46 +05:30
case string:
// check if it's a multiline string list
if len(strings.Split(payloadType, "\n")) != 1 {
2020-12-29 12:08:46 +05:30
return errors.New("invalid number of lines in payload")
}
2021-12-06 16:04:39 +01:00
// check if it's a file and try to load it
if fileutil.FileExists(payloadType) {
2020-12-29 12:08:46 +05:30
continue
}
changed := false
2021-12-06 21:05:45 +01:00
dir, _ := filepath.Split(templatePath)
templatePathInfo, _ := folderutil.NewPathInfo(dir)
payloadPathsToProbe, _ := templatePathInfo.MeshWith(payloadType)
2021-12-06 11:38:22 +01:00
for _, payloadPath := range payloadPathsToProbe {
if fileutil.FileExists(payloadPath) {
payloads[name] = payloadPath
2020-12-29 12:08:46 +05:30
changed = true
break
}
}
if !changed {
return fmt.Errorf("the %s file for payload %s does not exist or does not contain enough elements", payloadType, name)
2020-12-29 12:08:46 +05:30
}
case interface{}:
loadedPayloads := types.ToStringSlice(payloadType)
2020-12-29 12:08:46 +05:30
if len(loadedPayloads) == 0 {
return fmt.Errorf("the payload %s does not contain enough elements", name)
}
default:
return fmt.Errorf("the payload %s has invalid type", name)
}
}
return nil
}