Improving path handling on windows

This commit is contained in:
Mzack9999 2021-12-06 11:38:22 +01:00
parent df55f7a2eb
commit a72425289b
4 changed files with 40 additions and 14 deletions

View File

@ -65,7 +65,7 @@ require (
moul.io/http2curl v1.0.0 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 ( require (
git.mills.io/prologic/smtpd v0.0.0-20210710122116-a525b76c287a // indirect git.mills.io/prologic/smtpd v0.0.0-20210710122116-a525b76c287a // indirect

View File

@ -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-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 h1:2dbm7UhrAKnccZttr78CAmG768sSCd+MBn4ayLVDeqA=
github.com/projectdiscovery/fileutil v0.0.0-20210928100737-cab279c5d4b5/go.mod h1:U+QCpQnX8o2N2w0VUGyAzjM3yBAe4BKedVElxiImsx0= 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-20211206102047-d6bf8e7490ff h1:ci7/Pq9xvrVFb94jeARYb45oSzs85NWG+Fxp/kjgHVc=
github.com/projectdiscovery/folderutil v0.0.0-20210804143510-68474319fd84/go.mod h1:BMqXH4jNGByVdE2iLtKvc/6XStaiZRuCIaKv1vw9PnI= github.com/projectdiscovery/folderutil v0.0.0-20211206102047-d6bf8e7490ff/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/goflags v0.0.7/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY= 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 h1:EzwVm8i4zmzqZX55vrDtyfogwHh8AAZ3cWCJe4fEduk=
github.com/projectdiscovery/goflags v0.0.8-0.20211028121123-edf02bc05b1a/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY= github.com/projectdiscovery/goflags v0.0.8-0.20211028121123-edf02bc05b1a/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY=

View File

@ -4,6 +4,10 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"github.com/pkg/errors"
"github.com/projectdiscovery/folderutil"
) )
// ResolvePath resolves the path to an absolute one in various ways. // 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) { if filepath.IsAbs(templateName) {
return templateName, nil return templateName, nil
} }
if second != "" { if second != "" {
secondBasePath := filepath.Join(filepath.Dir(second), templateName) secondBasePath := filepath.Join(filepath.Dir(second), templateName)
if _, err := os.Stat(secondBasePath); !os.IsNotExist(err) { if potentialPath, err := c.tryResolve(secondBasePath); err != errNoValidCombination {
return secondBasePath, nil return potentialPath, nil
} }
} }
@ -29,15 +32,37 @@ func (c *Catalog) ResolvePath(templateName, second string) (string, error) {
} }
templatePath := filepath.Join(curDirectory, templateName) templatePath := filepath.Join(curDirectory, templateName)
if _, err := os.Stat(templatePath); !os.IsNotExist(err) { if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination {
return templatePath, nil return potentialPath, nil
} }
if c.templatesDirectory != "" { if c.templatesDirectory != "" {
templatePath := filepath.Join(c.templatesDirectory, templateName) templatePath := filepath.Join(c.templatesDirectory, templateName)
if _, err := os.Stat(templatePath); !os.IsNotExist(err) { if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination {
return templatePath, nil return potentialPath, nil
} }
} }
return "", fmt.Errorf("no such path found: %s", templateName) 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
}

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/projectdiscovery/folderutil" "github.com/projectdiscovery/folderutil"
@ -27,11 +28,13 @@ func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePat
changed := false changed := false
templatePathInfo, err := folderutil.NewPathInfo(templatePath) dir, filename := filepath.Split(filepath.Join(templatePath, payloadType))
templatePathInfo, err := folderutil.NewPathInfo(dir)
if err != nil { if err != nil {
return err return err
} }
payloadPathsToProbe, err := templatePathInfo.MeshWith(payloadType) payloadPathsToProbe, err := templatePathInfo.MeshWith(filename)
if err != nil { if err != nil {
return err return err
} }