74 lines
1.8 KiB
Go
Raw Normal View History

2020-12-29 12:08:46 +05:30
package generators
import (
"errors"
"fmt"
"os"
2021-12-06 11:38:22 +01:00
"path/filepath"
2020-12-29 12:08:46 +05:30
"strings"
"github.com/projectdiscovery/folderutil"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
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")
}
// check if it's a worldlist file and try to load it
if fileExists(payloadType) {
2020-12-29 12:08:46 +05:30
continue
}
changed := false
2021-12-06 11:38:22 +01:00
dir, filename := filepath.Split(filepath.Join(templatePath, payloadType))
templatePathInfo, err := folderutil.NewPathInfo(dir)
if err != nil {
return err
}
2021-12-06 11:38:22 +01:00
payloadPathsToProbe, err := templatePathInfo.MeshWith(filename)
if err != nil {
return err
}
for _, payloadPath := range payloadPathsToProbe {
if 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
}
// fileExists checks if a file exists and is not a directory
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
if info == nil {
return false
}
2020-12-29 12:08:46 +05:30
return !info.IsDir()
}