Skip to content

Commit

Permalink
Merge pull request #53 from haya14busa/project-run4-security
Browse files Browse the repository at this point in the history
ProjectConf: hide secret environment variables from command executions
  • Loading branch information
haya14busa committed Dec 12, 2016
2 parents ba2a9ee + e96d92d commit f60bac3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions project/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"os"
"os/exec"

"golang.org/x/sync/errgroup"
Expand All @@ -13,6 +14,8 @@ import (

// Run runs reviewdog tasks based on Config.
func Run(ctx context.Context, conf *Config, c reviewdog.CommentService, d reviewdog.DiffService) error {
// environment variables for each commands
envs := filteredEnviron()
var g errgroup.Group
for _, runner := range conf.Runner {
fname := runner.Format
Expand All @@ -26,6 +29,7 @@ func Run(ctx context.Context, conf *Config, c reviewdog.CommentService, d review
}
rd := reviewdog.NewReviewdog(runner.Name, p, c, d)
cmd := exec.CommandContext(ctx, "sh", "-c", runner.Cmd)
cmd.Env = envs
stdout, err := cmd.StdoutPipe()
stderr, err := cmd.StderrPipe()
if err != nil {
Expand All @@ -43,3 +47,15 @@ func Run(ctx context.Context, conf *Config, c reviewdog.CommentService, d review
}
return nil
}

var secretEnvs = [...]string{
"REVIEWDOG_GITHUB_API_TOKEN",
}

func filteredEnviron() []string {
for _, name := range secretEnvs {
defer os.Setenv(name, os.Getenv(name))
os.Setenv(name, "")
}
return os.Environ()
}
25 changes: 25 additions & 0 deletions project/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package project
import (
"context"
"errors"
"os"
"strings"
"testing"

"github.com/haya14busa/reviewdog"
Expand Down Expand Up @@ -123,3 +125,26 @@ func TestRun(t *testing.T) {
})

}

func TestFilteredEnviron(t *testing.T) {
const name = "REVIEWDOG_GITHUB_API_TOKEN"
defer os.Setenv(name, os.Getenv(name))
os.Setenv(name, "value")

filtered := filteredEnviron()
if len(filtered) != len(os.Environ()) {
t.Errorf("len(filtered) != len(os.Environ()), %v != %v", len(filtered), len(os.Environ()))
}

for _, kv := range filtered {
if strings.HasPrefix(kv, name) && kv != name+"=" {
t.Errorf("filtered: %v, want %v=", kv, name)
}
}

for _, kv := range os.Environ() {
if strings.HasPrefix(kv, name) && kv != name+"=value" {
t.Errorf("envs: %v, want %v=value", kv, name)
}
}
}

0 comments on commit f60bac3

Please sign in to comment.