Files
Dwi Siswanto 20cbf0bd73 refactor: use path-aware filesystem containment checks (#7420)
* feat(filepath): add IsPathWithinAnyDirectory func

Signed-off-by: Dwi Siswanto <git@dw1.io>

* refactor: use path-aware filesystem containment checks

Several filesystem trust boundaries relied on
lexical prefix checks to decide whether a path
fell under an allowed directory. That let sibling
paths be treated as if they were children of
trusted directories.

Replace those checks with canonical path
containment checks for helper payload loading,
template archive extraction, custom template
metadata, template path classification, and
headless screenshot output validation.

Signed-off-by: Dwi Siswanto <git@dw1.io>

* adding more tests

* fixing tests

* adding test

---------

Signed-off-by: Dwi Siswanto <git@dw1.io>
Co-authored-by: Mzack9999 <mzack9999@protonmail.com>
2026-06-04 21:03:51 +05:30

30 lines
921 B
Go

package utils
import (
"path/filepath"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/keys"
filepathutil "github.com/projectdiscovery/nuclei/v3/pkg/utils/filepath"
)
const (
// TemplatesRepoURL is the URL for files in nuclei-templates repository
TemplatesRepoURL = "https://cloud.projectdiscovery.io/public/"
)
// TemplatePathURL returns the Path and URL for the provided template
func TemplatePathURL(fullPath, templateId, templateVerifier string) (path string, url string) {
configData := config.DefaultConfig
if configData.TemplatesDirectory != "" && filepathutil.IsPathWithinDirectory(fullPath, configData.GetTemplateDir()) {
relPath, err := filepath.Rel(configData.GetTemplateDir(), fullPath)
if err == nil && relPath != "." {
path = relPath
}
}
if templateVerifier == keys.PDVerifier {
url = TemplatesRepoURL + templateId
}
return
}