Skip to content

Commit

Permalink
Merge pull request #916 from projectdiscovery/643-feature-env-variabl…
Browse files Browse the repository at this point in the history
…es-support

Adding support for global env variables
  • Loading branch information
ehsandeep committed Aug 9, 2021
2 parents a8509b8 + f016a9c commit 3ed04dd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions v2/cmd/nuclei/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ on extensive configurability, massive extensibility and ease of use.`)
flagSet.StringVarP(&options.ResolversFile, "resolvers", "r", "", "file containing resolver list for nuclei"),
flagSet.BoolVar(&options.SystemResolvers, "system-resolvers", false, "use system DNS resolving as error fallback"),
flagSet.BoolVar(&options.OfflineHTTP, "passive", false, "enable passive HTTP response processing mode"),
flagSet.BoolVar(&options.EnvironmentVariables, "env-vars", false, "Enable environment variables support"),
)

createGroup(flagSet, "interactsh", "interactsh",
Expand Down
28 changes: 28 additions & 0 deletions v2/pkg/protocols/common/generators/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package generators

import (
"os"

"github.com/projectdiscovery/stringsutil"
)

var envVars map[string]interface{}

func parseEnvVars() map[string]interface{} {
sliceEnvVars := os.Environ()
parsedEnvVars := make(map[string]interface{}, len(sliceEnvVars))
for _, envVar := range sliceEnvVars {
key, val := stringsutil.Before(envVar, "="), stringsutil.After(envVar, "=")
parsedEnvVars[key] = val
}
return parsedEnvVars
}

// EnvVars returns a map with all environment variables into a map
func EnvVars() map[string]interface{} {
if envVars == nil {
envVars = parseEnvVars()
}

return envVars
}
5 changes: 5 additions & 0 deletions v2/pkg/protocols/http/build_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (r *requestGenerator) Make(baseURL string, dynamicValues map[string]interfa
parsedString := parsed.String()
values["BaseURL"] = parsedString

// merge with env vars
if r.options.Options.EnvironmentVariables {
values = generators.MergeMaps(values, generators.EnvVars())
}

// If data contains \n it's a raw request, process it like raw. Else
// continue with the template based request flow.
if isRawRequest {
Expand Down
2 changes: 2 additions & 0 deletions v2/pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@ type Options struct {
UpdateNuclei bool
// NoUpdateTemplates disables checking for nuclei templates updates
NoUpdateTemplates bool
// EnvironmentVariables enables support for environment variables
EnvironmentVariables bool
}

0 comments on commit 3ed04dd

Please sign in to comment.