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

Sanitize archive folder name #2154

Merged
merged 4 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ contrib.go.opencensus.io/exporter/stackdriver v0.12.1/go.mod h1:iwB6wGarfphGGe/e
contrib.go.opencensus.io/integrations/ocsql v0.1.4/go.mod h1:8DsSdjz3F+APR+0z0WkU1aRorQCFfRxvqjUUPMbF3fE=
contrib.go.opencensus.io/resource v0.1.1/go.mod h1:F361eGI91LCmW1I/Saf+rX0+OFcigGlFvXwEGEnkRLA=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
get.porter.sh/magefiles v0.1.3 h1:91Y7vFDHGmMBbRfHQqEcIlqpp/RfDCMhyHGVusTYlmE=
get.porter.sh/magefiles v0.1.3/go.mod h1:Whw/DSX8dcgn7dUPb6csbY6PtuTy1wujwFR2G6ONf20=
get.porter.sh/magefiles v0.3.0 h1:uwCOTblBx2RFN2IEgIUDP4sNj/C4F26KqAdqSREJL7g=
get.porter.sh/magefiles v0.3.0/go.mod h1:lwDECEEivbBHACLImnDEhwlr8g3E4xZR1W0DO8FOGa8=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
Expand Down
27 changes: 21 additions & 6 deletions pkg/porter/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"time"

"get.porter.sh/porter/pkg"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/cnabio/cnab-go/imagestore/construction"
"github.com/docker/docker/pkg/archive"
"github.com/pkg/errors"
"github.com/spf13/afero"
)

// ArchiveOptions defines the valid options for performing an archive operation
Expand Down Expand Up @@ -88,14 +90,10 @@ type exporter struct {
}

func (ex *exporter) export() error {

name := ex.bundle.Name + "-" + ex.bundle.Version
archiveDir, err := ex.fs.TempDir("", name)
archiveDir, err := ex.createArchiveFolder(name)
if err != nil {
return err
}
if err := ex.fs.MkdirAll(archiveDir, 0644); err != nil {
return err
return fmt.Errorf("can not create archive folder: %w", err)
}
defer ex.fs.RemoveAll(archiveDir)

Expand Down Expand Up @@ -186,6 +184,23 @@ func (ex *exporter) addImage(image bundle.BaseImage) error {
return checkDigest(image, dig)
}

// createArchiveFolder set up a temporary directory for storing all data needed to archive a bundle.
// It sanitizes the name and make sure only the current user has full permission to it.
// If the name contains a path separator, all path separators will be replaced with "-".
func (ex *exporter) createArchiveFolder(name string) (string, error) {
cleanedPath := strings.ReplaceAll(afero.UnicodeSanitize(name), string(os.PathSeparator), "-")
archiveDir, err := ex.fs.TempDir("", cleanedPath)
if err != nil {
return "", fmt.Errorf("can not create a temporary archive folder: %w", err)
}

err = ex.fs.Chmod(archiveDir, pkg.FileModeDirectory)
if err != nil {
return "", fmt.Errorf("can not change permission for the temporary archive folder: %w", err)
}
return archiveDir, nil
}

// checkDigest compares the content digest of the given image to the given content digest and returns an error if they
// are both non-empty and do not match
func checkDigest(image bundle.BaseImage, dig string) error {
Expand Down
16 changes: 16 additions & 0 deletions pkg/porter/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

"get.porter.sh/porter/pkg"
"get.porter.sh/porter/tests"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -51,3 +53,17 @@ func TestArchive_Validate(t *testing.T) {
})
}
}

func TestArchive_ArchiveDirectory(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()
ex := exporter{
fs: p.FileSystem,
}

dir, err := ex.createArchiveFolder("examples/test-bundle-0.2.0")
require.NoError(t, err)
require.Contains(t, dir, "/tmp/examples-test-bundle-0.2.0")

tests.AssertDirectoryPermissionsEqual(t, dir, pkg.FileModeDirectory)
}