diff --git a/v2/go.mod b/v2/go.mod index 490908476..e21ed46d8 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -65,7 +65,7 @@ require ( moul.io/http2curl v1.0.0 ) -require github.com/projectdiscovery/folderutil v0.0.0-20211203091551-e81604e6940e +require github.com/projectdiscovery/folderutil v0.0.0-20211206102047-d6bf8e7490ff require ( git.mills.io/prologic/smtpd v0.0.0-20210710122116-a525b76c287a // indirect diff --git a/v2/go.sum b/v2/go.sum index bd73d8ced..3649ddbc9 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -595,10 +595,8 @@ github.com/projectdiscovery/fileutil v0.0.0-20210914153648-31f843feaad4/go.mod h github.com/projectdiscovery/fileutil v0.0.0-20210926202739-6050d0acf73c/go.mod h1:U+QCpQnX8o2N2w0VUGyAzjM3yBAe4BKedVElxiImsx0= github.com/projectdiscovery/fileutil v0.0.0-20210928100737-cab279c5d4b5 h1:2dbm7UhrAKnccZttr78CAmG768sSCd+MBn4ayLVDeqA= github.com/projectdiscovery/fileutil v0.0.0-20210928100737-cab279c5d4b5/go.mod h1:U+QCpQnX8o2N2w0VUGyAzjM3yBAe4BKedVElxiImsx0= -github.com/projectdiscovery/folderutil v0.0.0-20210804143510-68474319fd84 h1:+VqGxv8ywpIHwGGSCOcGn/q5kkuB6F1AZtY42I8VnXc= -github.com/projectdiscovery/folderutil v0.0.0-20210804143510-68474319fd84/go.mod h1:BMqXH4jNGByVdE2iLtKvc/6XStaiZRuCIaKv1vw9PnI= -github.com/projectdiscovery/folderutil v0.0.0-20211203091551-e81604e6940e h1:ozfSeEc5j1f7NCEZAiAskP/KYfBD/TzPmFTIfh+CEwE= -github.com/projectdiscovery/folderutil v0.0.0-20211203091551-e81604e6940e/go.mod h1:BMqXH4jNGByVdE2iLtKvc/6XStaiZRuCIaKv1vw9PnI= +github.com/projectdiscovery/folderutil v0.0.0-20211206102047-d6bf8e7490ff h1:ci7/Pq9xvrVFb94jeARYb45oSzs85NWG+Fxp/kjgHVc= +github.com/projectdiscovery/folderutil v0.0.0-20211206102047-d6bf8e7490ff/go.mod h1:BMqXH4jNGByVdE2iLtKvc/6XStaiZRuCIaKv1vw9PnI= github.com/projectdiscovery/goflags v0.0.7/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY= github.com/projectdiscovery/goflags v0.0.8-0.20211028121123-edf02bc05b1a h1:EzwVm8i4zmzqZX55vrDtyfogwHh8AAZ3cWCJe4fEduk= github.com/projectdiscovery/goflags v0.0.8-0.20211028121123-edf02bc05b1a/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY= diff --git a/v2/pkg/catalog/path.go b/v2/pkg/catalog/path.go index 76f387d63..06ebb2d88 100644 --- a/v2/pkg/catalog/path.go +++ b/v2/pkg/catalog/path.go @@ -4,6 +4,10 @@ import ( "fmt" "os" "path/filepath" + + "github.com/pkg/errors" + + "github.com/projectdiscovery/folderutil" ) // ResolvePath resolves the path to an absolute one in various ways. @@ -15,11 +19,10 @@ func (c *Catalog) ResolvePath(templateName, second string) (string, error) { if filepath.IsAbs(templateName) { return templateName, nil } - if second != "" { secondBasePath := filepath.Join(filepath.Dir(second), templateName) - if _, err := os.Stat(secondBasePath); !os.IsNotExist(err) { - return secondBasePath, nil + if potentialPath, err := c.tryResolve(secondBasePath); err != errNoValidCombination { + return potentialPath, nil } } @@ -29,15 +32,37 @@ func (c *Catalog) ResolvePath(templateName, second string) (string, error) { } templatePath := filepath.Join(curDirectory, templateName) - if _, err := os.Stat(templatePath); !os.IsNotExist(err) { - return templatePath, nil + if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination { + return potentialPath, nil } if c.templatesDirectory != "" { templatePath := filepath.Join(c.templatesDirectory, templateName) - if _, err := os.Stat(templatePath); !os.IsNotExist(err) { - return templatePath, nil + if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination { + return potentialPath, nil } } return "", fmt.Errorf("no such path found: %s", templateName) } + +var errNoValidCombination = errors.New("no valid combination found") + +// tryResolve attempts to load locate the target by iterating across all the folders tree +func (c *Catalog) tryResolve(fullpath string) (string, error) { + dir, filename := filepath.Split(fullpath) + pathInfo, err := folderutil.NewPathInfo(dir) + if err != nil { + return "", err + } + pathInfoItems, err := pathInfo.MeshWith(filename) + if err != nil { + return "", err + } + for _, pathInfoItem := range pathInfoItems { + if _, err := os.Stat(pathInfoItem); !os.IsNotExist(err) { + return pathInfoItem, nil + } + } + + return "", errNoValidCombination +} diff --git a/v2/pkg/protocols/common/generators/validate.go b/v2/pkg/protocols/common/generators/validate.go index b04f8034c..7e525112f 100644 --- a/v2/pkg/protocols/common/generators/validate.go +++ b/v2/pkg/protocols/common/generators/validate.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "strings" "github.com/projectdiscovery/folderutil" @@ -27,11 +28,13 @@ func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePat changed := false - templatePathInfo, err := folderutil.NewPathInfo(templatePath) + dir, filename := filepath.Split(filepath.Join(templatePath, payloadType)) + + templatePathInfo, err := folderutil.NewPathInfo(dir) if err != nil { return err } - payloadPathsToProbe, err := templatePathInfo.MeshWith(payloadType) + payloadPathsToProbe, err := templatePathInfo.MeshWith(filename) if err != nil { return err }