From 2b4b0588862697412155ed606446b020bca73a1a Mon Sep 17 00:00:00 2001 From: alban-stourbe-wmx <159776828+alban-stourbe-wmx@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:04:13 +0100 Subject: [PATCH] handle env variables in dynamic secret file (#5835) * handle env variables in dynamic secret file * inject more variables from -v and -env-vars * use expand with env * fix missing replacer --------- Co-authored-by: Tarun Koyalwar --- internal/runner/lazy.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/runner/lazy.go b/internal/runner/lazy.go index 900850b673..5cb91cfd09 100644 --- a/internal/runner/lazy.go +++ b/internal/runner/lazy.go @@ -3,6 +3,7 @@ package runner import ( "context" "fmt" + "strings" "github.com/projectdiscovery/nuclei/v3/pkg/authprovider/authx" "github.com/projectdiscovery/nuclei/v3/pkg/catalog" @@ -10,9 +11,12 @@ import ( "github.com/projectdiscovery/nuclei/v3/pkg/output" "github.com/projectdiscovery/nuclei/v3/pkg/protocols" "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs" + "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators" "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/helpers/writer" + "github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/replacer" "github.com/projectdiscovery/nuclei/v3/pkg/scan" "github.com/projectdiscovery/nuclei/v3/pkg/types" + "github.com/projectdiscovery/utils/env" errorutil "github.com/projectdiscovery/utils/errors" ) @@ -75,7 +79,25 @@ func GetLazyAuthFetchCallback(opts *AuthLazyFetchOptions) authx.LazyFetchSecret vars := map[string]interface{}{} mainCtx := context.Background() ctx := scan.NewScanContext(mainCtx, contextargs.NewWithInput(mainCtx, d.Input)) + + cliVars := map[string]interface{}{} + if opts.ExecOpts.Options != nil { + // gets variables passed from cli -v and -env-vars + cliVars = generators.BuildPayloadFromOptions(opts.ExecOpts.Options) + } + for _, v := range d.Variables { + // Check if the template has any env variables and expand them + if strings.HasPrefix(v.Value, "$") { + env.ExpandWithEnv(&v.Value) + } + if strings.Contains(v.Value, "{{") { + // if variables had value like {{username}}, then replace it with the value from cliVars + // variables: + // - key: username + // value: {{username}} + v.Value = replacer.Replace(v.Value, cliVars) + } vars[v.Key] = v.Value ctx.Input.Add(v.Key, v.Value) }