Path: blob/dev/pkg/protocols/common/generators/validate.go
2844 views
package generators12import (3"fmt"4"path/filepath"5"strings"67"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"8"github.com/projectdiscovery/nuclei/v3/pkg/types"9fileutil "github.com/projectdiscovery/utils/file"10folderutil "github.com/projectdiscovery/utils/folder"11)1213// validate validates the payloads if any.14func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePath string) error {15for name, payload := range payloads {16switch payloadType := payload.(type) {17case string:18if strings.ContainsRune(payloadType, '\n') {19continue20}2122// For historical reasons, "validate" checks to see if the payload file exist.23// If we're using a custom helper function, then we need to skip any validation beyond just checking the string syntax.24// Actually attempting to load the file will determine whether or not it exists.25if g.options.LoadHelperFileFunction != nil {26return nil27}2829// check if it's a file and try to load it30if fileutil.FileExists(payloadType) {31continue32}33// if file already exists in nuclei-templates directory, skip any further checks34if fileutil.FileExists(filepath.Join(config.DefaultConfig.GetTemplateDir(), payloadType)) {35continue36}3738// in below code, we calculate all possible paths from root and try to resolve the payload39// at each level of the path. if the payload is found, we break the loop and continue40// ex: template-path: /home/user/nuclei-templates/cves/2020/CVE-2020-1234.yaml41// then we check if helper file "my-payload.txt" exists at below paths:42// 1. /home/user/nuclei-templates/cves/2020/my-payload.txt43// 2. /home/user/nuclei-templates/cves/my-payload.txt44// 3. /home/user/nuclei-templates/my-payload.txt45// 4. /home/user/my-payload.txt46// 5. /home/my-payload.txt47changed := false4849dir, _ := filepath.Split(templatePath)50templatePathInfo, _ := folderutil.NewPathInfo(dir)51payloadPathsToProbe, _ := templatePathInfo.MeshWith(payloadType)5253for _, payloadPath := range payloadPathsToProbe {54if fileutil.FileExists(payloadPath) {55payloads[name] = payloadPath56changed = true57break58}59}60if !changed {61return fmt.Errorf("the %s file for payload %s does not exist or does not contain enough elements", payloadType, name)62}63case interface{}:64loadedPayloads := types.ToStringSlice(payloadType)65if len(loadedPayloads) == 0 {66return fmt.Errorf("the payload %s does not contain enough elements", name)67}68default:69return fmt.Errorf("the payload %s has invalid type", name)70}71}72return nil73}747576