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