1
0
mirror of https://github.com/golang/go synced 2024-11-23 20:40:07 -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
This commit is contained in:
Mauri de Souza Meneguzzo 2023-08-08 21:16:49 -03:00
parent 6e43407931
commit 5e53dcfd7c
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")
}
}