Skip to content

Commit

Permalink
Merge pull request #173 from v-yarotsky/vy-env-var-whitelist
Browse files Browse the repository at this point in the history
Only expand concourse build metadata env vars
  • Loading branch information
Kristian committed Dec 9, 2019
2 parents 50bee87 + 62ff546 commit 87fdaed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
16 changes: 13 additions & 3 deletions out.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Put(request PutRequest, manager Github, inputDir string) (*PutResponse, err
description = string(content)
}

if err := manager.UpdateCommitStatus(version.Commit, p.BaseContext, p.Context, p.Status, os.ExpandEnv(p.TargetURL), description); err != nil {
if err := manager.UpdateCommitStatus(version.Commit, p.BaseContext, p.Context, p.Status, safeExpandEnv(p.TargetURL), description); err != nil {
return nil, fmt.Errorf("failed to set status: %s", err)
}
}
Expand All @@ -64,7 +64,7 @@ func Put(request PutRequest, manager Github, inputDir string) (*PutResponse, err

// Set comment if specified
if p := request.Params; p.Comment != "" {
err = manager.PostComment(version.PR, os.ExpandEnv(p.Comment))
err = manager.PostComment(version.PR, safeExpandEnv(p.Comment))
if err != nil {
return nil, fmt.Errorf("failed to post comment: %s", err)
}
Expand All @@ -78,7 +78,7 @@ func Put(request PutRequest, manager Github, inputDir string) (*PutResponse, err
}
comment := string(content)
if comment != "" {
err = manager.PostComment(version.PR, os.ExpandEnv(comment))
err = manager.PostComment(version.PR, safeExpandEnv(comment))
if err != nil {
return nil, fmt.Errorf("failed to post comment: %s", err)
}
Expand Down Expand Up @@ -140,3 +140,13 @@ func (p *PutParameters) Validate() error {

return nil
}

func safeExpandEnv(s string) string {
return os.Expand(s, func(v string) string {
switch v {
case "BUILD_ID", "BUILD_NAME", "BUILD_JOB_NAME", "BUILD_PIPELINE_NAME", "BUILD_TEAM_NAME", "ATC_EXTERNAL_URL":
return os.Getenv(v)
}
return "$" + v
})
}
22 changes: 20 additions & 2 deletions out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ func TestPut(t *testing.T) {
func TestVariableSubstitution(t *testing.T) {

var (
variableName = "EXAMPLE_VARIABLE"
variableValue = "value"
variableName = "BUILD_JOB_NAME"
variableValue = "my-job"
variableURL = "https://concourse-ci.org/"
)

Expand Down Expand Up @@ -271,6 +271,24 @@ func TestVariableSubstitution(t *testing.T) {
expectedTargetURL: fmt.Sprintf("%s%s", variableURL, variableValue),
pullRequest: createTestPR(1, "master", false, false, 0, nil),
},

{
description: "we do not substitute variables other then concourse build metadata",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
},
parameters: resource.PutParameters{
Comment: "$THIS_IS_NOT_SUBSTITUTED",
},
expectedComment: "$THIS_IS_NOT_SUBSTITUTED",
pullRequest: createTestPR(1, "master", false, false, 0, nil),
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 87fdaed

Please sign in to comment.