2023-09-26 16:55:25 +05:30
|
|
|
package protocolstate
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-20 05:28:23 +05:30
|
|
|
"fmt"
|
2023-10-13 13:17:27 +05:30
|
|
|
"strings"
|
2023-09-26 16:55:25 +05:30
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
2025-07-18 13:40:58 -05:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2025-08-20 05:28:23 +05:30
|
|
|
"github.com/projectdiscovery/utils/errkit"
|
2023-09-26 16:55:25 +05:30
|
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
2025-07-18 13:40:58 -05:00
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
2023-09-26 16:55:25 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2025-07-09 14:47:26 -05:00
|
|
|
// LfaAllowed means local file access is allowed
|
2025-07-18 13:40:58 -05:00
|
|
|
LfaAllowed *mapsutil.SyncLockMap[string, bool]
|
2023-09-26 16:55:25 +05:30
|
|
|
)
|
|
|
|
|
|
2025-07-18 13:40:58 -05:00
|
|
|
func init() {
|
|
|
|
|
LfaAllowed = mapsutil.NewSyncLockMap[string, bool]()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsLfaAllowed returns whether local file access is allowed
|
|
|
|
|
func IsLfaAllowed(options *types.Options) bool {
|
|
|
|
|
if GetLfaAllowed(options) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise look into dialers
|
|
|
|
|
dialers, ok := dialers.Get(options.ExecutionId)
|
|
|
|
|
if ok && dialers != nil {
|
|
|
|
|
dialers.Lock()
|
|
|
|
|
defer dialers.Unlock()
|
|
|
|
|
|
|
|
|
|
return dialers.LocalFileAccessAllowed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// otherwise just return option value
|
|
|
|
|
return options.AllowLocalFileAccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetLfaAllowed(options *types.Options) {
|
|
|
|
|
_ = LfaAllowed.Set(options.ExecutionId, options.AllowLocalFileAccess)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetLfaAllowed(options *types.Options) bool {
|
|
|
|
|
allowed, ok := LfaAllowed.Get(options.ExecutionId)
|
|
|
|
|
|
|
|
|
|
return ok && allowed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NormalizePathWithExecutionId(executionId string, filePath string) (string, error) {
|
|
|
|
|
options := &types.Options{
|
|
|
|
|
ExecutionId: executionId,
|
|
|
|
|
}
|
|
|
|
|
return NormalizePath(options, filePath)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 16:55:25 +05:30
|
|
|
// Normalizepath normalizes path and returns absolute path
|
|
|
|
|
// it returns error if path is not allowed
|
|
|
|
|
// this respects the sandbox rules and only loads files from
|
|
|
|
|
// allowed directories
|
2025-07-18 13:40:58 -05:00
|
|
|
func NormalizePath(options *types.Options, filePath string) (string, error) {
|
|
|
|
|
// TODO: this should be tied to executionID using *types.Options
|
|
|
|
|
if IsLfaAllowed(options) {
|
|
|
|
|
// if local file access is allowed, we can return the absolute path
|
2023-10-13 13:17:27 +05:30
|
|
|
return filePath, nil
|
2023-09-26 16:55:25 +05:30
|
|
|
}
|
2023-10-13 13:17:27 +05:30
|
|
|
cleaned, err := fileutil.ResolveNClean(filePath, config.DefaultConfig.GetTemplateDir())
|
|
|
|
|
if err != nil {
|
2025-08-20 05:28:23 +05:30
|
|
|
return "", errkit.Append(errkit.New(fmt.Sprintf("could not resolve and clean path %v", filePath)), err)
|
2023-09-26 16:55:25 +05:30
|
|
|
}
|
2023-10-13 13:17:27 +05:30
|
|
|
// only allow files inside nuclei-templates directory
|
2023-09-26 16:55:25 +05:30
|
|
|
// even current working directory is not allowed
|
2023-10-13 13:17:27 +05:30
|
|
|
if strings.HasPrefix(cleaned, config.DefaultConfig.GetTemplateDir()) {
|
|
|
|
|
return cleaned, nil
|
2023-09-26 16:55:25 +05:30
|
|
|
}
|
2025-08-20 05:28:23 +05:30
|
|
|
return "", errkit.New(fmt.Sprintf("path %v is outside nuclei-template directory and -lfa is not enabled", filePath)).Build()
|
2023-09-26 16:55:25 +05:30
|
|
|
}
|