11package disk
22
33import (
4+ "fmt"
45 "io/fs"
56 "os"
67 "path/filepath"
@@ -65,41 +66,34 @@ func (c *DiskCatalog) GetTemplatesPath(definitions []string) ([]string, map[stri
6566// or folders provided as in.
6667func (c * DiskCatalog ) GetTemplatePath (target string ) ([]string , error ) {
6768 processed := make (map [string ]struct {})
68- // Template input includes a wildcard
69- if strings .Contains (target , "*" ) {
70- matches , findErr := c .findGlobPathMatches (target , processed )
71- if findErr != nil {
72- return nil , errors .Wrap (findErr , "could not find glob matches" )
73- }
74- if len (matches ) == 0 {
75- return nil , errors .Errorf ("no templates found for path" )
76- }
77- return matches , nil
78- }
7969
80- // try to handle deprecated template paths
81- absPath := target
8270 if c .templatesFS == nil {
83- absPath = BackwardsCompatiblePaths (c .templatesDirectory , target )
84- if absPath != target && strings .TrimPrefix (absPath , c .templatesDirectory + string (filepath .Separator )) != target {
85- if config .DefaultConfig .LogAllEvents {
86- config .DefaultConfig .Logger .Print ().Msgf ("[%v] requested Template path %s is deprecated, please update to %s\n " , aurora .Yellow ("WRN" ).String (), target , absPath )
87- }
88- deprecatedPathsCounter ++
89- }
90-
9171 var err error
92- absPath , err = c .convertPathToAbsolute (absPath )
72+ target , err = c .convertPathToAbsolute (target )
9373 if err != nil {
9474 return nil , errors .Wrapf (err , "could not find template file" )
9575 }
9676 }
9777
98- // Template input is either a file or a directory
99- match , file , err := c .findFileMatches (absPath , processed )
78+ if strings .Contains (target , "*" ) {
79+ globMatches , err := c .findGlobPathMatches (target , processed )
80+ if err != nil {
81+ return nil , errors .Wrap (err , "could not globbing path" )
82+ }
83+
84+ if len (globMatches ) > 0 {
85+ return globMatches , nil
86+ } else {
87+ return globMatches , fmt .Errorf ("%w in path %q" , ErrNoTemplatesFound , target )
88+ }
89+ }
90+
91+ // `target` is either a file or a directory
92+ match , file , err := c .findFileMatches (target , processed )
10093 if err != nil {
10194 return nil , errors .Wrap (err , "could not find file" )
10295 }
96+
10397 if file {
10498 if match != "" {
10599 return []string {match }, nil
@@ -109,13 +103,15 @@ func (c *DiskCatalog) GetTemplatePath(target string) ([]string, error) {
109103
110104 // Recursively walk down the Templates directory and run all
111105 // the template file checks
112- matches , err := c .findDirectoryMatches (absPath , processed )
106+ matches , err := c .findDirectoryMatches (target , processed )
113107 if err != nil {
114108 return nil , errors .Wrap (err , "could not find directory matches" )
115109 }
110+
116111 if len (matches ) == 0 {
117- return nil , errors .Errorf ("no templates found in path %s " , absPath )
112+ return nil , fmt .Errorf ("%w in path %q " , ErrNoTemplatesFound , target )
118113 }
114+
119115 return matches , nil
120116}
121117
@@ -135,79 +131,37 @@ func (c *DiskCatalog) convertPathToAbsolute(t string) (string, error) {
135131
136132// findGlobPathMatches returns the matched files from a glob path
137133func (c * DiskCatalog ) findGlobPathMatches (absPath string , processed map [string ]struct {}) ([]string , error ) {
138- // to support globbing on old paths we use brute force to find matches with exit on first match
139134 // trim templateDir if any
140135 relPath := strings .TrimPrefix (absPath , c .templatesDirectory )
141136 // trim leading slash if any
142- relPath = strings .TrimPrefix (relPath , string (os .PathSeparator ))
143-
144- OldPathsResolver := func (inputGlob string ) []string {
145- templateDir := c .templatesDirectory
146- if c .templatesDirectory == "" {
147- templateDir = "./"
148- }
149-
150- if c .templatesFS == nil {
151- matches , _ := fs .Glob (os .DirFS (filepath .Join (templateDir , "http" )), inputGlob )
152- if len (matches ) != 0 {
153- return matches
154- }
155-
156- // condition to support network cve related globs
157- matches , _ = fs .Glob (os .DirFS (filepath .Join (templateDir , "network" )), inputGlob )
158- return matches
159- } else {
160- sub , err := fs .Sub (c .templatesFS , filepath .Join (templateDir , "http" ))
161- if err != nil {
162- return nil
163- }
164- matches , _ := fs .Glob (sub , inputGlob )
165- if len (matches ) != 0 {
166- return matches
167- }
168-
169- // condition to support network cve related globs
170- sub , err = fs .Sub (c .templatesFS , filepath .Join (templateDir , "network" ))
171- if err != nil {
172- return nil
173- }
174- matches , _ = fs .Glob (sub , inputGlob )
175- return matches
176- }
137+ if c .templatesFS != nil {
138+ // fs.FS always uses forward slashes
139+ relPath = strings .TrimPrefix (relPath , "/" )
140+ } else {
141+ relPath = strings .TrimPrefix (relPath , string (os .PathSeparator ))
177142 }
178143
179- var matched [] string
144+ var err error
180145 var matches []string
181- if c .templatesFS == nil {
182- var err error
183- matches , err = filepath .Glob (relPath )
184- if len (matches ) != 0 {
185- matched = append (matched , matches ... )
186- } else {
187- matched = append (matched , OldPathsResolver (relPath )... )
188- }
189- if err != nil && len (matched ) == 0 {
190- return nil , errors .Errorf ("wildcard found, but unable to glob: %s\n " , err )
191- }
192- } else {
193- var err error
146+
147+ if c .templatesFS != nil {
194148 matches , err = fs .Glob (c .templatesFS , relPath )
195- if len (matches ) != 0 {
196- matched = append (matched , matches ... )
197- } else {
198- matched = append (matched , OldPathsResolver (relPath )... )
199- }
200- if err != nil && len (matched ) == 0 {
201- return nil , errors .Errorf ("wildcard found, but unable to glob: %s\n " , err )
202- }
149+ } else {
150+ matches , err = filepath .Glob (absPath )
151+ }
152+
153+ if err != nil {
154+ return nil , err
203155 }
156+
204157 results := make ([]string , 0 , len (matches ))
205158 for _ , match := range matches {
206159 if _ , ok := processed [match ]; ! ok {
207160 processed [match ] = struct {}{}
208161 results = append (results , match )
209162 }
210163 }
164+
211165 return results , nil
212166}
213167
@@ -294,8 +248,11 @@ func (c *DiskCatalog) findDirectoryMatches(absPath string, processed map[string]
294248 return results , err
295249}
296250
297- // PrintDeprecatedPathsMsgIfApplicable prints a warning message if any deprecated paths are found
298- // Unless mode is silent warning message is printed
251+ // PrintDeprecatedPathsMsgIfApplicable prints a warning message if any
252+ // deprecated paths are found. Unless mode is silent warning message is printed.
253+ //
254+ // Deprecated: No longer used since the official Nuclei Templates repository
255+ // have restructured this a long time ago.
299256func PrintDeprecatedPathsMsgIfApplicable (isSilent bool ) {
300257 if ! updateutils .IsOutdated ("v9.4.3" , config .DefaultConfig .TemplateVersion ) {
301258 return
0 commit comments