Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/catalog/disk/path.go
2070 views
1
package disk
2
3
import (
4
"fmt"
5
"io/fs"
6
"os"
7
"path/filepath"
8
"strings"
9
10
"github.com/pkg/errors"
11
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
12
fileutil "github.com/projectdiscovery/utils/file"
13
urlutil "github.com/projectdiscovery/utils/url"
14
)
15
16
// ResolvePath resolves the path to an absolute one in various ways.
17
//
18
// It checks if the filename is an absolute path, looks in the current directory
19
// or checking the nuclei templates directory. If a second path is given,
20
// it also tries to find paths relative to that second path.
21
func (c *DiskCatalog) ResolvePath(templateName, second string) (string, error) {
22
if filepath.IsAbs(templateName) {
23
return templateName, nil
24
}
25
if c.templatesFS != nil {
26
if potentialPath, err := c.tryResolve(templateName); err != errNoValidCombination {
27
return potentialPath, nil
28
}
29
}
30
if second != "" {
31
secondBasePath := filepath.Join(filepath.Dir(second), templateName)
32
if potentialPath, err := c.tryResolve(secondBasePath); err != errNoValidCombination {
33
return potentialPath, nil
34
}
35
}
36
37
if c.templatesFS == nil {
38
curDirectory, err := os.Getwd()
39
if err != nil {
40
return "", err
41
}
42
43
templatePath := filepath.Join(curDirectory, templateName)
44
if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination {
45
return potentialPath, nil
46
}
47
}
48
49
templatePath := filepath.Join(config.DefaultConfig.GetTemplateDir(), templateName)
50
if potentialPath, err := c.tryResolve(templatePath); err != errNoValidCombination {
51
return potentialPath, nil
52
}
53
54
return "", fmt.Errorf("no such path found: %s", templateName)
55
}
56
57
var errNoValidCombination = errors.New("no valid combination found")
58
59
// tryResolve attempts to load locate the target by iterating across all the folders tree
60
func (c *DiskCatalog) tryResolve(fullPath string) (string, error) {
61
if c.templatesFS == nil {
62
if fileutil.FileOrFolderExists(fullPath) {
63
return fullPath, nil
64
}
65
} else {
66
if _, err := fs.Stat(c.templatesFS, fullPath); err == nil {
67
return fullPath, nil
68
}
69
}
70
return "", errNoValidCombination
71
}
72
73
// BackwardsCompatiblePaths returns new paths for all old/legacy template paths
74
// Note: this is a temporary function and will be removed in the future release
75
func BackwardsCompatiblePaths(templateDir string, oldPath string) string {
76
// TODO: remove this function in the future release
77
// 1. all http related paths are now moved at path /http
78
// 2. network related CVES are now moved at path /network/cves
79
newPathCallback := func(path string) string {
80
// trim prefix slash if any
81
path = strings.TrimPrefix(path, "/")
82
// try to resolve path at /http subdirectory
83
if fileutil.FileOrFolderExists(filepath.Join(templateDir, "http", path)) {
84
return filepath.Join(templateDir, "http", path)
85
// try to resolve path at /network/cves subdirectory
86
} else if strings.HasPrefix(path, "cves") && fileutil.FileOrFolderExists(filepath.Join(templateDir, "network", "cves", path)) {
87
return filepath.Join(templateDir, "network", "cves", path)
88
}
89
// most likely the path is not found
90
return filepath.Join(templateDir, path)
91
}
92
switch {
93
case fileutil.FileOrFolderExists(oldPath):
94
// new path specified skip processing
95
return oldPath
96
case filepath.IsAbs(oldPath):
97
tmp := strings.TrimPrefix(oldPath, templateDir)
98
if tmp == oldPath {
99
// user provided absolute path which is not in template directory
100
// skip processing
101
return oldPath
102
}
103
// trim the template directory from the path
104
return newPathCallback(tmp)
105
case strings.Contains(oldPath, urlutil.SchemeSeparator):
106
// scheme separator is used to identify the path as url
107
// TBD: add support for url directories ??
108
return oldPath
109
case strings.Contains(oldPath, "*"):
110
// this is most likely a glob path skip processing
111
return oldPath
112
default:
113
// this is most likely a relative path
114
return newPathCallback(oldPath)
115
}
116
}
117
118