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: toctou for files #443

Merged
merged 4 commits into from
Feb 23, 2024
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: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b
github.com/defenseunicorns/zarf v0.32.3
github.com/fsnotify/fsnotify v1.7.0
github.com/goccy/go-yaml v1.11.3
github.com/mholt/archiver/v3 v3.5.1
github.com/mholt/archiver/v4 v4.0.0-alpha.8
Expand Down Expand Up @@ -197,7 +198,6 @@ require (
github.com/fluxcd/pkg/apis/kustomize v1.3.0 // indirect
github.com/fluxcd/pkg/apis/meta v1.3.0 // indirect
github.com/fluxcd/source-controller/api v1.2.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
Expand Down
22 changes: 13 additions & 9 deletions src/pkg/bundle/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (tp *tarballBundleProvider) CreateBundleSBOM(extractSBOM bool) error {
return err
}
// make tmp dir for pkg SBOM extraction
err = os.Mkdir(filepath.Join(tp.dst, config.BundleSBOM), 0700)
err = os.Mkdir(filepath.Join(tp.dst, config.BundleSBOM), 0o700)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,26 +117,30 @@ func (tp *tarballBundleProvider) getBundleManifest() error {
if tp.bundleRootManifest != nil {
return nil
}
// Create a secure temporary directory for handling files
secureTempDir, err := zarfUtils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return fmt.Errorf("failed to create a secure temporary directory: %w", err)
}
defer os.RemoveAll(secureTempDir) // Ensure cleanup of the temp directory

if err := av3.Extract(tp.src, "index.json", tp.dst); err != nil {
if err := av3.Extract(tp.src, "index.json", secureTempDir); err != nil {
return fmt.Errorf("failed to extract index.json from %s: %w", tp.src, err)
}

indexPath := filepath.Join(tp.dst, "index.json")
indexPath := filepath.Join(secureTempDir, "index.json")

defer os.Remove(indexPath)

b, err := os.ReadFile(indexPath)
if err != nil {
return err
return fmt.Errorf("failed to read index.json: %w", err)
}

var index ocispec.Index

if err := json.Unmarshal(b, &index); err != nil {
return err
return fmt.Errorf("failed to unmarshal index.json: %w", err)
}

// local bundles only have one manifest entry in their index.json
bundleManifestDesc := index.Manifests[0]
tp.bundleRootDesc = bundleManifestDesc
Expand All @@ -147,11 +151,11 @@ func (tp *tarballBundleProvider) getBundleManifest() error {

manifestRelativePath := filepath.Join(config.BlobsDir, bundleManifestDesc.Digest.Encoded())

if err := av3.Extract(tp.src, manifestRelativePath, tp.dst); err != nil {
if err := av3.Extract(tp.src, manifestRelativePath, secureTempDir); err != nil {
return fmt.Errorf("failed to extract %s from %s: %w", bundleManifestDesc.Digest.Encoded(), tp.src, err)
}

manifestPath := filepath.Join(tp.dst, manifestRelativePath)
manifestPath := filepath.Join(secureTempDir, manifestRelativePath)

defer os.Remove(manifestPath)

Expand Down
Loading