Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible path traversal attack when supporting Helm values.yaml #2452

Merged
merged 12 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/user-guide/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ flag. The flag can be repeated to support multiple values files:
```bash
argocd app set helm-guestbook --values values-production.yaml
```
!!! note
Values files must be on the same directory or a subdirectory of the Helm application

## Helm Parameters

Expand Down
2 changes: 1 addition & 1 deletion reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func helmTemplate(appPath string, q *apiclient.ManifestRequest) ([]*unstructured
}
templateOpts.Values = appHelm.ValueFiles
if appHelm.Values != "" {
file, err := ioutil.TempFile("", "values-*.yaml")
file, err := ioutil.TempFile(appPath, "values-*.yaml")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of this change, all values stemming from Helm.Values will now have to be saved in a temp file within the application path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clever

if err != nil {
return nil, err
}
Expand Down
20 changes: 20 additions & 0 deletions reposerver/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,26 @@ func TestGenerateHelmWithValues(t *testing.T) {

}

// This tests against a path traversal attack. The requested value file (`../minio/values.yaml`) is outside the
// app path (`./util/helm/testdata/redis`)
func TestGenerateHelmWithValuesDirectoryTraversal(t *testing.T) {
service := newService("../..")

_, err := service.GenerateManifest(context.Background(), &apiclient.ManifestRequest{
Repo: &argoappv1.Repository{},
AppLabelValue: "test",
ApplicationSource: &argoappv1.ApplicationSource{
Path: "./util/helm/testdata/redis",
Helm: &argoappv1.ApplicationSourceHelm{
ValueFiles: []string{"../minio/values.yaml"},
Values: `cluster: {slaveCount: 2}`,
},
},
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "should be on or under current directory")
simster7 marked this conversation as resolved.
Show resolved Hide resolved
}

func TestGenerateNullList(t *testing.T) {
service := newService(".")

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestDeclarativeHelmInvalidValuesFile(t *testing.T) {
Then().
Expect(HealthIs(HealthStatusHealthy)).
Expect(SyncStatusIs(SyncStatusCodeUnknown)).
Expect(Condition(ApplicationConditionComparisonError, "open does-not-exist-values.yaml: no such file or directory"))
Expect(Condition(ApplicationConditionComparisonError, "does-not-exist-values.yaml: no such file or directory"))
simster7 marked this conversation as resolved.
Show resolved Hide resolved
}

func TestHelmRepo(t *testing.T) {
Expand Down
16 changes: 15 additions & 1 deletion util/helm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"

"github.com/argoproj/argo-cd/util/security"
simster7 marked this conversation as resolved.
Show resolved Hide resolved

"github.com/argoproj/argo-cd/util"

argoexec "github.com/argoproj/pkg/exec"
Expand Down Expand Up @@ -192,7 +195,18 @@ func (c *Cmd) template(chart string, opts *TemplateOpts) (string, error) {
args = append(args, "--set-string", key+"="+cleanSetParameters(val))
}
for _, val := range opts.Values {
simster7 marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, "--values", val)
absWorkDir, err := filepath.Abs(c.WorkDir)
if err != nil {
return "", err
}
if !filepath.IsAbs(val) {
val = filepath.Join(absWorkDir, val)
}
cleanVal, err := security.EnforceToCurrentRoot(absWorkDir, val)
if err != nil {
return "", err
}
args = append(args, "--values", cleanVal)
}

return c.run(args...)
Expand Down
24 changes: 24 additions & 0 deletions util/helm/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ func TestCmd_template_kubeVersion(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, s)
}

func TestCmd_template_PathTraversal(t *testing.T) {
cmd, err := NewCmd("./testdata/redis")
assert.NoError(t, err)
s, err := cmd.template(".", &TemplateOpts{
KubeVersion: "1.14",
Values: []string{"values.yaml"},
})
assert.NoError(t, err)
assert.NotEmpty(t, s)

_, err = cmd.template(".", &TemplateOpts{
KubeVersion: "1.14",
Values: []string{"../minio/values.yaml"},
})
assert.Error(t, err)

s, err = cmd.template(".", &TemplateOpts{
KubeVersion: "1.14",
Values: []string{"../minio/../redis/values.yaml"},
})
assert.NoError(t, err)
assert.NotEmpty(t, s)
}
35 changes: 35 additions & 0 deletions util/security/path_traversal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package security

import (
"fmt"
"path/filepath"
"strings"
)

// Ensure that `requestedPath` is on the same directory or any subdirectory of `currentRoot`. Both `currentRoot` and
// `requestedPath` must be absolute paths. They may contain any number of `./` or `/../` dir changes.
func EnforceToCurrentRoot(currentRoot, requestedPath string) (string, error) {
simster7 marked this conversation as resolved.
Show resolved Hide resolved
currentRoot = filepath.Clean(currentRoot)
requestedDir, requestedFile := parsePath(requestedPath)
if !isRequestedDirUnderCurrentRoot(currentRoot, requestedDir) {
return "", fmt.Errorf("requested path %s should be on or under current directory %s", requestedPath, currentRoot)
}
return requestedDir + string(filepath.Separator) + requestedFile, nil
}

func isRequestedDirUnderCurrentRoot(currentRoot, requestedDir string) bool {
if currentRoot == string(filepath.Separator) {
return true
} else if currentRoot == requestedDir {
return true
}
return strings.HasPrefix(requestedDir, currentRoot+string(filepath.Separator))
}

func parsePath(path string) (string, string) {
directory := filepath.Dir(path)
if directory == path {
return directory, ""
}
return directory, filepath.Base(path)
}
26 changes: 26 additions & 0 deletions util/security/path_traversal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package security

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEnforceToCurrentRoot(t *testing.T) {
cleanDir, err := EnforceToCurrentRoot("/home/argo/helmapp/", "/home/argo/helmapp/values.yaml")
assert.NoError(t, err)
assert.Equal(t, "/home/argo/helmapp/values.yaml", cleanDir)

// File is outside current working directory
_, err = EnforceToCurrentRoot("/home/argo/helmapp/", "/home/values.yaml")
assert.Error(t, err)

// File is outside current working directory
_, err = EnforceToCurrentRoot("/home/argo/helmapp/", "/home/argo/helmapp/../differentapp/values.yaml")
assert.Error(t, err)

// Goes back and forth, but still legal
cleanDir, err = EnforceToCurrentRoot("/home/argo/helmapp/", "/home/argo/helmapp/../../argo/helmapp/values.yaml")
assert.NoError(t, err)
assert.Equal(t, "/home/argo/helmapp/values.yaml", cleanDir)
}
simster7 marked this conversation as resolved.
Show resolved Hide resolved