1
0
mirror of https://github.com/golang/go synced 2024-11-18 20:04:52 -07:00

archive/zip: reject non-regular files in AddFS

When a filesystem with non-regular files is used
the resulting files inside the zip archive are empty.

In this case we can be explicit and return an error.

Fixes #61875

Change-Id: I1aeac544521002e0c4bad328e73f51852ad4eee3
GitHub-Last-Rev: 5e53dcfd7c
GitHub-Pull-Request: golang/go#61876
Reviewed-on: https://go-review.googlesource.com/c/go/+/517475
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Mauri de Souza Meneguzzo <mauri870@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Mauri de Souza Meneguzzo 2023-09-07 21:34:01 +00:00 committed by Gopher Robot
parent 6192f46155
commit 52fc777dcc
2 changed files with 26 additions and 0 deletions

View File

@ -504,6 +504,9 @@ func (w *Writer) AddFS(fsys fs.FS) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return errors.New("zip: cannot add non-regular file")
}
h, err := FileInfoHeader(info)
if err != nil {
return err

View File

@ -648,3 +648,26 @@ func TestWriterAddFS(t *testing.T) {
testReadFile(t, r.File[i], &wt)
}
}
func TestIssue61875(t *testing.T) {
buf := new(bytes.Buffer)
w := NewWriter(buf)
tests := []WriteTest{
{
Name: "symlink",
Data: []byte("../link/target"),
Method: Deflate,
Mode: 0755 | fs.ModeSymlink,
},
{
Name: "device",
Data: []byte(""),
Method: Deflate,
Mode: 0755 | fs.ModeDevice,
},
}
err := w.AddFS(writeTestsToFS(tests))
if err == nil {
t.Errorf("expected error, got nil")
}
}