Skip to content

Commit

Permalink
add environment privacy options
Browse files Browse the repository at this point in the history
  • Loading branch information
cdzombak committed Sep 11, 2023
1 parent 5ace940 commit 0d540d1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ If you plan to use the `RUNNER_OUTFD_PID` and `RUNNER_OUTFD_STD[OUT|ERR]` variab
- `-version`: Print version and exit.
- `-work-dir string`: Set the working directory for the program.

- `RUNNER_CENSOR_ENV` (environment variable only): Colon-separated list of environment variables whose values will be censored in output.
- `RUNNER_HIDE_ENV` (environment variable only): Colon-separated list of environment variables which will be entirely omitted from output.

#### Run as another user

- `-gid int`: Run the program as the given GID. Ignored on Windows. (If provided, runner must be run as `root` or with `CAP_SETGID`.)
Expand Down
42 changes: 42 additions & 0 deletions envprivacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"os"
"strings"
)

const (
minLenForCensorHint = 5
)

func hiddenEnvVars() []string {
return strings.Split(os.Getenv(HideEnvVarsEnvVar), ":")
}

func censoredEnvVars() []string {
return strings.Split(os.Getenv(CensorEnvVarsEnvVar), ":")
}

func shouldHideEnvVar(varName string) bool {
return stringSliceContains(hiddenEnvVars(), varName)
}

func censoredEnvVarValue(varName, value string) string {
if !stringSliceContains(censoredEnvVars(), varName) && varName != SMTPPassEnvVar {
return value
}
if len(value) < minLenForCensorHint {
return fmt.Sprintf("[%d chars]", len(value))
}
return fmt.Sprintf("%c[%d chars]%c", value[0], len(value)-2, value[len(value)-1])
}

func stringSliceContains(slice []string, value string) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
16 changes: 14 additions & 2 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (
OutFdStderrEnvVar = "RUNNER_OUTFD_STDERR"

LogDirEnvVar = "RUNNER_LOG_DIR"

HideEnvVarsEnvVar = "RUNNER_HIDE_ENV"
CensorEnvVarsEnvVar = "RUNNER_CENSOR_ENV"
)

func usage() {
Expand All @@ -47,6 +50,10 @@ func usage() {
"containerization situations. The container must be run with --cap-add CAP_SYS_PTRACE.\n", OutFdPidEnvVar)
fmt.Printf("\nOptions:\n")
flag.PrintDefaults()
fmt.Printf("\nEnvironment variable-only options:\n")
fmt.Printf(" %s\n \tColon-separated list of environment variables whose values will be censored in output."+
"\n \tRUNNER_SMTP_PASS is always censored.\n", CensorEnvVarsEnvVar)
fmt.Printf(" %s\n \tColon-separated list of environment variables which will be entirely omitted from output.\n", HideEnvVarsEnvVar)
fmt.Printf("\nVersion:\n runner %s\n", version)
fmt.Printf("\nGitHub:\n https://github.com/cdzombak/runner\n")
fmt.Printf("\nAuthor:\n Chris Dzombak <https://www.dzombak.com>\n")
Expand Down Expand Up @@ -289,8 +296,13 @@ func main() {
)
if !*hideEnv {
output = output + "Environment:\n"
for _, v := range os.Environ() {
output = output + fmt.Sprintf("\t%s\n", v)
for _, envVar := range os.Environ() {
envVarPair := strings.SplitN(envVar, "=", 2)
envVarName := envVarPair[0]
if shouldHideEnvVar(envVarName) {
continue
}
output = output + fmt.Sprintf("\t%s=%s\n", envVarName, censoredEnvVarValue(envVarName, envVarPair[1]))
}
output = output + "\n"
}
Expand Down

0 comments on commit 0d540d1

Please sign in to comment.