2022-08-10 23:35:58 +05:30
|
|
|
package disk
|
2020-12-29 18:02:45 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-09-20 17:11:22 -04:00
|
|
|
"io/fs"
|
2020-12-29 18:02:45 +05:30
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2023-05-04 01:43:41 +05:30
|
|
|
"strings"
|
2021-12-06 11:38:22 +01:00
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
2023-05-04 01:43:41 +05:30
|
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
|
|
|
|
urlutil "github.com/projectdiscovery/utils/url"
|
2020-12-29 18:02:45 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ResolvePath resolves the path to an absolute one in various ways.
|
|
|
|
|
//
|
|
|
|
|
// It checks if the filename is an absolute path, looks in the current directory
|
|
|
|
|
// or checking the nuclei templates directory. If a second path is given,
|
|
|
|
|
// it also tries to find paths relative to that second path.
|
2022-08-10 23:35:58 +05:30
|
|
|
func (c *DiskCatalog) ResolvePath(templateName, second string) (string, error) {
|
2021-08-23 16:11:26 +03:00
|
|
|
if filepath.IsAbs(templateName) {
|
2020-12-29 18:02:45 +05:30
|
|
|
return templateName, nil
|
|
|
|
|
}
|
2024-09-20 17:11:22 -04:00
|
|
|
if c.templatesFS != nil {
|
|
|
|
|
if potentialPath, err := c.tryResolve(templateName); err != errNoValidCombination {
|
|
|
|
|
return potentialPath, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-29 18:02:45 +05:30
|
|
|
if second != "" {
|
2021-08-23 14:53:37 +03:00
|
|
|
secondBasePath := filepath.Join(filepath.Dir(second), templateName)
|
2021-12-06 11:38:22 +01:00
|
|
|
if potentialPath, err := c.tryResolve(secondBasePath); err != errNoValidCombination {
|
|
|
|
|
return potentialPath, nil
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 17:11:22 -04:00
|
|
|
if c.templatesFS == nil {
|
|
|
|
|
curDirectory, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2020-12-29 18:02:45 +05:30
|
|
|
|
2024-09-20 17:11:22 -04:00
|
|
|
templatePath := filepath.Join(curDirectory, templateName)
|
|
|
|
|
if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination {
|
|
|
|
|
return potentialPath, nil
|
|
|
|
|
}
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
|
2024-09-20 17:11:22 -04:00
|
|
|
templatePath := filepath.Join(config.DefaultConfig.GetTemplateDir(), templateName)
|
2023-10-13 13:17:27 +05:30
|
|
|
if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination {
|
|
|
|
|
return potentialPath, nil
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
2023-10-13 13:17:27 +05:30
|
|
|
|
2020-12-29 18:02:45 +05:30
|
|
|
return "", fmt.Errorf("no such path found: %s", templateName)
|
|
|
|
|
}
|
2021-12-06 11:38:22 +01:00
|
|
|
|
|
|
|
|
var errNoValidCombination = errors.New("no valid combination found")
|
|
|
|
|
|
|
|
|
|
// tryResolve attempts to load locate the target by iterating across all the folders tree
|
2022-08-10 23:35:58 +05:30
|
|
|
func (c *DiskCatalog) tryResolve(fullPath string) (string, error) {
|
2024-09-20 17:11:22 -04:00
|
|
|
if c.templatesFS == nil {
|
|
|
|
|
if fileutil.FileOrFolderExists(fullPath) {
|
|
|
|
|
return fullPath, nil
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if _, err := fs.Stat(c.templatesFS, fullPath); err == nil {
|
|
|
|
|
return fullPath, nil
|
|
|
|
|
}
|
2021-12-06 11:38:22 +01:00
|
|
|
}
|
|
|
|
|
return "", errNoValidCombination
|
|
|
|
|
}
|
2023-05-04 01:43:41 +05:30
|
|
|
|
|
|
|
|
// BackwardsCompatiblePaths returns new paths for all old/legacy template paths
|
|
|
|
|
// Note: this is a temporary function and will be removed in the future release
|
|
|
|
|
func BackwardsCompatiblePaths(templateDir string, oldPath string) string {
|
|
|
|
|
// TODO: remove this function in the future release
|
|
|
|
|
// 1. all http related paths are now moved at path /http
|
|
|
|
|
// 2. network related CVES are now moved at path /network/cves
|
|
|
|
|
newPathCallback := func(path string) string {
|
|
|
|
|
// trim prefix slash if any
|
|
|
|
|
path = strings.TrimPrefix(path, "/")
|
|
|
|
|
// try to resolve path at /http subdirectory
|
|
|
|
|
if fileutil.FileOrFolderExists(filepath.Join(templateDir, "http", path)) {
|
|
|
|
|
return filepath.Join(templateDir, "http", path)
|
|
|
|
|
// try to resolve path at /network/cves subdirectory
|
|
|
|
|
} else if strings.HasPrefix(path, "cves") && fileutil.FileOrFolderExists(filepath.Join(templateDir, "network", "cves", path)) {
|
|
|
|
|
return filepath.Join(templateDir, "network", "cves", path)
|
|
|
|
|
}
|
|
|
|
|
// most likely the path is not found
|
|
|
|
|
return filepath.Join(templateDir, path)
|
|
|
|
|
}
|
|
|
|
|
switch {
|
|
|
|
|
case fileutil.FileOrFolderExists(oldPath):
|
|
|
|
|
// new path specified skip processing
|
|
|
|
|
return oldPath
|
|
|
|
|
case filepath.IsAbs(oldPath):
|
|
|
|
|
tmp := strings.TrimPrefix(oldPath, templateDir)
|
|
|
|
|
if tmp == oldPath {
|
|
|
|
|
// user provided absolute path which is not in template directory
|
|
|
|
|
// skip processing
|
|
|
|
|
return oldPath
|
|
|
|
|
}
|
|
|
|
|
// trim the template directory from the path
|
|
|
|
|
return newPathCallback(tmp)
|
|
|
|
|
case strings.Contains(oldPath, urlutil.SchemeSeparator):
|
2023-08-01 14:33:43 -04:00
|
|
|
// scheme separator is used to identify the path as url
|
2023-05-04 01:43:41 +05:30
|
|
|
// TBD: add support for url directories ??
|
|
|
|
|
return oldPath
|
|
|
|
|
case strings.Contains(oldPath, "*"):
|
|
|
|
|
// this is most likely a glob path skip processing
|
|
|
|
|
return oldPath
|
|
|
|
|
default:
|
|
|
|
|
// this is most likely a relative path
|
|
|
|
|
return newPathCallback(oldPath)
|
|
|
|
|
}
|
|
|
|
|
}
|