feat: add folder uploads and archives with simpler account and plan forms
This commit is contained in:
1 parent
9afd7d0067
commit
faaa614cee
41 files changed
+1027
-71
No files matched your search
@@ -1,8 +1,10 @@
|
||||
package nodefiles
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -75,3 +77,65 @@ func TestExtractRejectsZipSlipSymlinksAndBombs(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTarArchivesExtractSafely(t *testing.T) {
|
||||
for _, extension := range []string{".tar", ".tar.gz", ".tgz"} {
|
||||
t.Run(extension, func(t *testing.T) {
|
||||
for _, name := range []string{"./folder/hello.txt", "../escape", "/escape", "link", "hard", ".user.ini"} {
|
||||
dir := t.TempDir()
|
||||
var raw bytes.Buffer
|
||||
writer := tar.NewWriter(&raw)
|
||||
kind := byte(tar.TypeReg)
|
||||
size := int64(5)
|
||||
if name == "link" {
|
||||
kind = tar.TypeSymlink
|
||||
size = 0
|
||||
}
|
||||
if name == "hard" {
|
||||
kind = tar.TypeLink
|
||||
size = 0
|
||||
}
|
||||
if err := writer.WriteHeader(&tar.Header{Name: name, Typeflag: kind, Linkname: "/etc/passwd", Mode: 0777, Size: size}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if size > 0 {
|
||||
writer.Write([]byte("hello"))
|
||||
}
|
||||
writer.Close()
|
||||
data := raw.Bytes()
|
||||
if extension != ".tar" {
|
||||
var compressed bytes.Buffer
|
||||
gz := gzip.NewWriter(&compressed)
|
||||
gz.Write(data)
|
||||
gz.Close()
|
||||
data = compressed.Bytes()
|
||||
}
|
||||
os.WriteFile(filepath.Join(dir, "archive"+extension), data, 0600)
|
||||
out := Execute(dir, Request{Operation: "extract", Path: "archive" + extension, To: "expanded", Budget: 100})
|
||||
if name == "./folder/hello.txt" {
|
||||
if out.Error != "" {
|
||||
t.Fatal(out.Error)
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(dir, "expanded/folder/hello.txt"))
|
||||
if err != nil || string(content) != "hello" {
|
||||
t.Fatal("content mismatch")
|
||||
}
|
||||
// The archive mode must not restore executable or special permission bits.
|
||||
info, _ := os.Stat(filepath.Join(dir, "expanded/folder/hello.txt"))
|
||||
if info.Mode().Perm()&0111 != 0 {
|
||||
t.Fatal("archive permissions trusted")
|
||||
}
|
||||
small := Execute(dir, Request{Operation: "extract", Path: "archive" + extension, To: "too-small", Budget: 4})
|
||||
if small.Error == "" {
|
||||
t.Fatal("quota ignored")
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(dir, "too-small")); !os.IsNotExist(err) {
|
||||
t.Fatal("failed extraction was published")
|
||||
}
|
||||
} else if out.Error == "" {
|
||||
t.Fatalf("unsafe member accepted: %s", name)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user