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: archive should not actually verify links #3103

Merged
merged 1 commit into from May 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/archive/tar/tar.go
Expand Up @@ -28,11 +28,6 @@ func (a Archive) Close() error {

// Add file to the archive.
func (a Archive) Add(f config.File) error {
file, err := os.Open(f.Source) // #nosec
if err != nil {
return err
}
defer file.Close()
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return err
Expand Down Expand Up @@ -69,6 +64,11 @@ func (a Archive) Add(f config.File) error {
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return nil
}
file, err := os.Open(f.Source) // #nosec
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(a.tw, file)
return err
}
10 changes: 3 additions & 7 deletions pkg/archive/tar/tar_test.go
Expand Up @@ -151,15 +151,11 @@ func TestTarFileInfo(t *testing.T) {
}

func TestTarInvalidLink(t *testing.T) {
tmp := t.TempDir()
f, err := os.Create(filepath.Join(tmp, "test.tar"))
require.NoError(t, err)
defer f.Close() // nolint: errcheck
archive := New(f)
archive := New(io.Discard)
defer archive.Close() // nolint: errcheck

require.EqualError(t, archive.Add(config.File{
require.NoError(t, archive.Add(config.File{
Source: "../testdata/badlink.txt",
Destination: "badlink.txt",
}), "open ../testdata/badlink.txt: no such file or directory")
}))
}
13 changes: 8 additions & 5 deletions pkg/archive/zip/zip.go
Expand Up @@ -34,11 +34,6 @@ func (a Archive) Close() error {

// Add a file to the zip archive.
func (a Archive) Add(f config.File) error {
file, err := os.Open(f.Source) // #nosec
if err != nil {
return err
}
defer file.Close()
info, err := os.Lstat(f.Source) // #nosec
if err != nil {
return err
Expand All @@ -62,6 +57,14 @@ func (a Archive) Add(f config.File) error {
if err != nil {
return err
}
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return nil
}
file, err := os.Open(f.Source) // #nosec
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(w, file)
return err
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/archive/zip/zip_test.go
Expand Up @@ -2,6 +2,7 @@ package zip

import (
"archive/zip"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -136,3 +137,13 @@ func TestZipFileInfo(t *testing.T) {
require.Equal(t, fs.FileMode(0o755), next.FileInfo().Mode())
}
}

func TestTarInvalidLink(t *testing.T) {
archive := New(io.Discard)
defer archive.Close() // nolint: errcheck

require.NoError(t, archive.Add(config.File{
Source: "../testdata/badlink.txt",
Destination: "badlink.txt",
}))
}