Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/common/generators/validate.go
2072 views
1
package generators
2
3
import (
4
"errors"
5
"fmt"
6
"path/filepath"
7
"strings"
8
9
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
10
"github.com/projectdiscovery/nuclei/v3/pkg/types"
11
fileutil "github.com/projectdiscovery/utils/file"
12
folderutil "github.com/projectdiscovery/utils/folder"
13
)
14
15
// validate validates the payloads if any.
16
func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePath string) error {
17
for name, payload := range payloads {
18
switch payloadType := payload.(type) {
19
case string:
20
// check if it's a multiline string list
21
if len(strings.Split(payloadType, "\n")) != 1 {
22
return errors.New("invalid number of lines in payload")
23
}
24
25
// For historical reasons, "validate" checks to see if the payload file exist.
26
// If we're using a custom helper function, then we need to skip any validation beyond just checking the string syntax.
27
// Actually attempting to load the file will determine whether or not it exists.
28
if g.options.LoadHelperFileFunction != nil {
29
return nil
30
}
31
32
// check if it's a file and try to load it
33
if fileutil.FileExists(payloadType) {
34
continue
35
}
36
// if file already exists in nuclei-templates directory, skip any further checks
37
if fileutil.FileExists(filepath.Join(config.DefaultConfig.GetTemplateDir(), payloadType)) {
38
continue
39
}
40
41
// in below code, we calculate all possible paths from root and try to resolve the payload
42
// at each level of the path. if the payload is found, we break the loop and continue
43
// ex: template-path: /home/user/nuclei-templates/cves/2020/CVE-2020-1234.yaml
44
// then we check if helper file "my-payload.txt" exists at below paths:
45
// 1. /home/user/nuclei-templates/cves/2020/my-payload.txt
46
// 2. /home/user/nuclei-templates/cves/my-payload.txt
47
// 3. /home/user/nuclei-templates/my-payload.txt
48
// 4. /home/user/my-payload.txt
49
// 5. /home/my-payload.txt
50
changed := false
51
52
dir, _ := filepath.Split(templatePath)
53
templatePathInfo, _ := folderutil.NewPathInfo(dir)
54
payloadPathsToProbe, _ := templatePathInfo.MeshWith(payloadType)
55
56
for _, payloadPath := range payloadPathsToProbe {
57
if fileutil.FileExists(payloadPath) {
58
payloads[name] = payloadPath
59
changed = true
60
break
61
}
62
}
63
if !changed {
64
return fmt.Errorf("the %s file for payload %s does not exist or does not contain enough elements", payloadType, name)
65
}
66
case interface{}:
67
loadedPayloads := types.ToStringSlice(payloadType)
68
if len(loadedPayloads) == 0 {
69
return fmt.Errorf("the payload %s does not contain enough elements", name)
70
}
71
default:
72
return fmt.Errorf("the payload %s has invalid type", name)
73
}
74
}
75
return nil
76
}
77
78