Path: blob/dev/pkg/protocols/common/generators/validate.go
2072 views
package generators12import (3"errors"4"fmt"5"path/filepath"6"strings"78"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"9"github.com/projectdiscovery/nuclei/v3/pkg/types"10fileutil "github.com/projectdiscovery/utils/file"11folderutil "github.com/projectdiscovery/utils/folder"12)1314// validate validates the payloads if any.15func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePath string) error {16for name, payload := range payloads {17switch payloadType := payload.(type) {18case string:19// check if it's a multiline string list20if len(strings.Split(payloadType, "\n")) != 1 {21return errors.New("invalid number of lines in payload")22}2324// For historical reasons, "validate" checks to see if the payload file exist.25// If we're using a custom helper function, then we need to skip any validation beyond just checking the string syntax.26// Actually attempting to load the file will determine whether or not it exists.27if g.options.LoadHelperFileFunction != nil {28return nil29}3031// check if it's a file and try to load it32if fileutil.FileExists(payloadType) {33continue34}35// if file already exists in nuclei-templates directory, skip any further checks36if fileutil.FileExists(filepath.Join(config.DefaultConfig.GetTemplateDir(), payloadType)) {37continue38}3940// in below code, we calculate all possible paths from root and try to resolve the payload41// at each level of the path. if the payload is found, we break the loop and continue42// ex: template-path: /home/user/nuclei-templates/cves/2020/CVE-2020-1234.yaml43// then we check if helper file "my-payload.txt" exists at below paths:44// 1. /home/user/nuclei-templates/cves/2020/my-payload.txt45// 2. /home/user/nuclei-templates/cves/my-payload.txt46// 3. /home/user/nuclei-templates/my-payload.txt47// 4. /home/user/my-payload.txt48// 5. /home/my-payload.txt49changed := false5051dir, _ := filepath.Split(templatePath)52templatePathInfo, _ := folderutil.NewPathInfo(dir)53payloadPathsToProbe, _ := templatePathInfo.MeshWith(payloadType)5455for _, payloadPath := range payloadPathsToProbe {56if fileutil.FileExists(payloadPath) {57payloads[name] = payloadPath58changed = true59break60}61}62if !changed {63return fmt.Errorf("the %s file for payload %s does not exist or does not contain enough elements", payloadType, name)64}65case interface{}:66loadedPayloads := types.ToStringSlice(payloadType)67if len(loadedPayloads) == 0 {68return fmt.Errorf("the payload %s does not contain enough elements", name)69}70default:71return fmt.Errorf("the payload %s has invalid type", name)72}73}74return nil75}767778