Path: blob/dev/pkg/protocols/common/protocolstate/file.go
2072 views
package protocolstate12import (3"strings"45"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"6"github.com/projectdiscovery/nuclei/v3/pkg/types"7"github.com/projectdiscovery/utils/errkit"8fileutil "github.com/projectdiscovery/utils/file"9mapsutil "github.com/projectdiscovery/utils/maps"10)1112var (13// LfaAllowed means local file access is allowed14LfaAllowed *mapsutil.SyncLockMap[string, bool]15)1617func init() {18LfaAllowed = mapsutil.NewSyncLockMap[string, bool]()19}2021// IsLfaAllowed returns whether local file access is allowed22func IsLfaAllowed(options *types.Options) bool {23if GetLfaAllowed(options) {24return true25}2627// Otherwise look into dialers28dialers, ok := dialers.Get(options.ExecutionId)29if ok && dialers != nil {30dialers.Lock()31defer dialers.Unlock()3233return dialers.LocalFileAccessAllowed34}3536// otherwise just return option value37return options.AllowLocalFileAccess38}3940func SetLfaAllowed(options *types.Options) {41_ = LfaAllowed.Set(options.ExecutionId, options.AllowLocalFileAccess)42}4344func GetLfaAllowed(options *types.Options) bool {45allowed, ok := LfaAllowed.Get(options.ExecutionId)4647return ok && allowed48}4950func NormalizePathWithExecutionId(executionId string, filePath string) (string, error) {51options := &types.Options{52ExecutionId: executionId,53}54return NormalizePath(options, filePath)55}5657// Normalizepath normalizes path and returns absolute path58// it returns error if path is not allowed59// this respects the sandbox rules and only loads files from60// allowed directories61func NormalizePath(options *types.Options, filePath string) (string, error) {62// TODO: this should be tied to executionID using *types.Options63if IsLfaAllowed(options) {64// if local file access is allowed, we can return the absolute path65return filePath, nil66}67cleaned, err := fileutil.ResolveNClean(filePath, config.DefaultConfig.GetTemplateDir())68if err != nil {69return "", errkit.Wrapf(err, "could not resolve and clean path %v", filePath)70}71// only allow files inside nuclei-templates directory72// even current working directory is not allowed73if strings.HasPrefix(cleaned, config.DefaultConfig.GetTemplateDir()) {74return cleaned, nil75}76return "", errkit.Newf("path %v is outside nuclei-template directory and -lfa is not enabled", filePath)77}787980