Skip to content

Commit e61ca0c

Browse files
Use fs.FS when explicitly given (#5312)
* Use the `templateFS` if it's there when calling `OpenFile` * Add a new constructor * More refactoring * Both of my use cases are working * Fix for legacy assumptions * minor update: remove gologger debug stmts --------- Co-authored-by: Tarun Koyalwar <[email protected]>
1 parent 1c51a6b commit e61ca0c

File tree

3 files changed

+136
-48
lines changed

3 files changed

+136
-48
lines changed

lib/sdk_private.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ func (e *NucleiEngine) init(ctx context.Context) error {
231231
// and also upgrade templates to latest version if available
232232
installer.NucleiSDKVersionCheck()
233233

234-
return e.processUpdateCheckResults()
234+
if DefaultConfig.CanCheckForUpdates() {
235+
return e.processUpdateCheckResults()
236+
}
237+
return nil
235238
}
236239

237240
type syncOnce struct {

pkg/catalog/disk/catalog.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,41 @@ import (
1111
// DiskCatalog is a template catalog helper implementation based on disk
1212
type DiskCatalog struct {
1313
templatesDirectory string
14-
templatesFS fs.FS // TODO: Refactor to use this
14+
templatesFS fs.FS // Due to issues with how Go has implemented fs.FS, we'll have to also implement normal os operations, as well. See: https://github.com/golang/go/issues/44279
1515
}
1616

1717
// NewCatalog creates a new Catalog structure using provided input items
1818
// using disk based items
1919
func NewCatalog(directory string) *DiskCatalog {
2020
catalog := &DiskCatalog{templatesDirectory: directory}
21-
if directory != "" {
22-
catalog.templatesFS = os.DirFS(directory)
23-
} else {
24-
catalog.templatesFS = os.DirFS(config.DefaultConfig.GetTemplateDir())
21+
if directory == "" {
22+
catalog.templatesDirectory = config.DefaultConfig.GetTemplateDir()
23+
}
24+
return catalog
25+
}
26+
27+
// NewFSCatalog creates a new Catalog structure using provided input items
28+
// using the fs.FS as its filesystem.
29+
func NewFSCatalog(fs fs.FS, directory string) *DiskCatalog {
30+
catalog := &DiskCatalog{
31+
templatesDirectory: directory,
32+
templatesFS: fs,
2533
}
2634
return catalog
2735
}
2836

2937
// OpenFile opens a file and returns an io.ReadCloser to the file.
3038
// It is used to read template and payload files based on catalog responses.
3139
func (d *DiskCatalog) OpenFile(filename string) (io.ReadCloser, error) {
32-
file, err := os.Open(filename)
33-
if err != nil {
34-
if file, errx := os.Open(BackwardsCompatiblePaths(d.templatesDirectory, filename)); errx == nil {
35-
return file, nil
40+
if d.templatesFS == nil {
41+
file, err := os.Open(filename)
42+
if err != nil {
43+
if file, errx := os.Open(BackwardsCompatiblePaths(d.templatesDirectory, filename)); errx == nil {
44+
return file, nil
45+
}
3646
}
47+
return file, err
3748
}
38-
return file, err
49+
50+
return d.templatesFS.Open(filename)
3951
}

pkg/catalog/disk/find.go

Lines changed: 110 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,21 @@ func (c *DiskCatalog) GetTemplatePath(target string) ([]string, error) {
7979
}
8080

8181
// try to handle deprecated template paths
82-
absPath := BackwardsCompatiblePaths(c.templatesDirectory, target)
83-
if absPath != target && strings.TrimPrefix(absPath, c.templatesDirectory+string(filepath.Separator)) != target {
84-
if config.DefaultConfig.LogAllEvents {
85-
gologger.DefaultLogger.Print().Msgf("[%v] requested Template path %s is deprecated, please update to %s\n", aurora.Yellow("WRN").String(), target, absPath)
82+
absPath := target
83+
if c.templatesFS == nil {
84+
absPath = BackwardsCompatiblePaths(c.templatesDirectory, target)
85+
if absPath != target && strings.TrimPrefix(absPath, c.templatesDirectory+string(filepath.Separator)) != target {
86+
if config.DefaultConfig.LogAllEvents {
87+
gologger.DefaultLogger.Print().Msgf("[%v] requested Template path %s is deprecated, please update to %s\n", aurora.Yellow("WRN").String(), target, absPath)
88+
}
89+
deprecatedPathsCounter++
8690
}
87-
deprecatedPathsCounter++
88-
}
8991

90-
absPath, err := c.convertPathToAbsolute(absPath)
91-
if err != nil {
92-
return nil, errors.Wrapf(err, "could not find template file")
92+
var err error
93+
absPath, err = c.convertPathToAbsolute(absPath)
94+
if err != nil {
95+
return nil, errors.Wrapf(err, "could not find template file")
96+
}
9397
}
9498

9599
// Template input is either a file or a directory
@@ -143,24 +147,60 @@ func (c *DiskCatalog) findGlobPathMatches(absPath string, processed map[string]s
143147
if c.templatesDirectory == "" {
144148
templateDir = "./"
145149
}
146-
matches, _ := fs.Glob(os.DirFS(filepath.Join(templateDir, "http")), inputGlob)
147-
if len(matches) != 0 {
150+
151+
if c.templatesFS == nil {
152+
matches, _ := fs.Glob(os.DirFS(filepath.Join(templateDir, "http")), inputGlob)
153+
if len(matches) != 0 {
154+
return matches
155+
}
156+
157+
// condition to support network cve related globs
158+
matches, _ = fs.Glob(os.DirFS(filepath.Join(templateDir, "network")), inputGlob)
159+
return matches
160+
} else {
161+
sub, err := fs.Sub(c.templatesFS, filepath.Join(templateDir, "http"))
162+
if err != nil {
163+
return nil
164+
}
165+
matches, _ := fs.Glob(sub, inputGlob)
166+
if len(matches) != 0 {
167+
return matches
168+
}
169+
170+
// condition to support network cve related globs
171+
sub, err = fs.Sub(c.templatesFS, filepath.Join(templateDir, "network"))
172+
if err != nil {
173+
return nil
174+
}
175+
matches, _ = fs.Glob(sub, inputGlob)
148176
return matches
149177
}
150-
// condition to support network cve related globs
151-
matches, _ = fs.Glob(os.DirFS(filepath.Join(templateDir, "network")), inputGlob)
152-
return matches
153178
}
154179

155180
var matched []string
156-
matches, err := fs.Glob(c.templatesFS, relPath)
157-
if len(matches) != 0 {
158-
matched = append(matched, matches...)
181+
var matches []string
182+
if c.templatesFS == nil {
183+
var err error
184+
matches, err = filepath.Glob(relPath)
185+
if len(matches) != 0 {
186+
matched = append(matched, matches...)
187+
} else {
188+
matched = append(matched, OldPathsResolver(relPath)...)
189+
}
190+
if err != nil && len(matched) == 0 {
191+
return nil, errors.Errorf("wildcard found, but unable to glob: %s\n", err)
192+
}
159193
} else {
160-
matched = append(matched, OldPathsResolver(relPath)...)
161-
}
162-
if err != nil && len(matched) == 0 {
163-
return nil, errors.Errorf("wildcard found, but unable to glob: %s\n", err)
194+
var err error
195+
matches, err = fs.Glob(c.templatesFS, relPath)
196+
if len(matches) != 0 {
197+
matched = append(matched, matches...)
198+
} else {
199+
matched = append(matched, OldPathsResolver(relPath)...)
200+
}
201+
if err != nil && len(matched) == 0 {
202+
return nil, errors.Errorf("wildcard found, but unable to glob: %s\n", err)
203+
}
164204
}
165205
results := make([]string, 0, len(matches))
166206
for _, match := range matches {
@@ -175,11 +215,23 @@ func (c *DiskCatalog) findGlobPathMatches(absPath string, processed map[string]s
175215
// findFileMatches finds if a path is an absolute file. If the path
176216
// is a file, it returns true otherwise false with no errors.
177217
func (c *DiskCatalog) findFileMatches(absPath string, processed map[string]struct{}) (match string, matched bool, err error) {
178-
info, err := os.Stat(absPath)
218+
if c.templatesFS != nil {
219+
absPath = strings.TrimPrefix(absPath, "/")
220+
}
221+
var info fs.File
222+
if c.templatesFS == nil {
223+
info, err = os.Open(absPath)
224+
} else {
225+
info, err = c.templatesFS.Open(absPath)
226+
}
227+
if err != nil {
228+
return "", false, err
229+
}
230+
stat, err := info.Stat()
179231
if err != nil {
180232
return "", false, err
181233
}
182-
if !info.Mode().IsRegular() {
234+
if !stat.Mode().IsRegular() {
183235
return "", false, nil
184236
}
185237
if _, ok := processed[absPath]; !ok {
@@ -192,22 +244,43 @@ func (c *DiskCatalog) findFileMatches(absPath string, processed map[string]struc
192244
// findDirectoryMatches finds matches for templates from a directory
193245
func (c *DiskCatalog) findDirectoryMatches(absPath string, processed map[string]struct{}) ([]string, error) {
194246
var results []string
195-
err := filepath.WalkDir(
196-
absPath,
197-
func(path string, d fs.DirEntry, err error) error {
198-
// continue on errors
199-
if err != nil {
247+
var err error
248+
if c.templatesFS == nil {
249+
err = filepath.WalkDir(
250+
absPath,
251+
func(path string, d fs.DirEntry, err error) error {
252+
// continue on errors
253+
if err != nil {
254+
return nil
255+
}
256+
if !d.IsDir() && config.GetTemplateFormatFromExt(path) != config.Unknown {
257+
if _, ok := processed[path]; !ok {
258+
results = append(results, path)
259+
processed[path] = struct{}{}
260+
}
261+
}
200262
return nil
201-
}
202-
if !d.IsDir() && config.GetTemplateFormatFromExt(path) != config.Unknown {
203-
if _, ok := processed[path]; !ok {
204-
results = append(results, path)
205-
processed[path] = struct{}{}
263+
},
264+
)
265+
} else {
266+
err = fs.WalkDir(
267+
c.templatesFS,
268+
absPath,
269+
func(path string, d fs.DirEntry, err error) error {
270+
// continue on errors
271+
if err != nil {
272+
return nil
206273
}
207-
}
208-
return nil
209-
},
210-
)
274+
if !d.IsDir() && config.GetTemplateFormatFromExt(path) != config.Unknown {
275+
if _, ok := processed[path]; !ok {
276+
results = append(results, path)
277+
processed[path] = struct{}{}
278+
}
279+
}
280+
return nil
281+
},
282+
)
283+
}
211284
return results, err
212285
}
213286

0 commit comments

Comments
 (0)