mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
109 lines
3.4 KiB
Go
109 lines
3.4 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// TestRemoveEmptyFolderStaysInRoot checks that empty folders are removed up to
|
|
// the root, and that a symlinked folder pointing outside the root is neither
|
|
// followed nor removed.
|
|
func TestRemoveEmptyFolderStaysInRoot(t *testing.T) {
|
|
f := &File{Common: Common{Logger: zap.NewNop().Sugar()}}
|
|
base := t.TempDir()
|
|
outside := filepath.Join(base, "outside")
|
|
if err := os.MkdirAll(filepath.Join(outside, "sub"), 0o755); err != nil {
|
|
t.Fatalf("mkdir: %v", err)
|
|
}
|
|
rootDir := filepath.Join(base, "root")
|
|
if err := os.MkdirAll(filepath.Join(rootDir, "a", "b"), 0o755); err != nil {
|
|
t.Fatalf("mkdir: %v", err)
|
|
}
|
|
if err := os.Symlink(outside, filepath.Join(rootDir, "link")); err != nil {
|
|
t.Fatalf("symlink: %v", err)
|
|
}
|
|
root, err := os.OpenRoot(rootDir)
|
|
if err != nil {
|
|
t.Fatalf("open root: %v", err)
|
|
}
|
|
defer root.Close()
|
|
|
|
if err := f.RemoveEmptyFolderRecursively(root, "link/sub"); err != nil {
|
|
t.Fatalf("remove: %v", err)
|
|
}
|
|
if err := f.RemoveEmptyFolderRecursively(root, "link"); err != nil {
|
|
t.Fatalf("remove: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(outside, "sub")); err != nil {
|
|
t.Fatalf("folder outside the root was removed: %v", err)
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(rootDir, "link")); err != nil {
|
|
t.Fatalf("symlink was removed: %v", err)
|
|
}
|
|
|
|
if err := f.RemoveEmptyFolderRecursively(root, "a/b"); err != nil {
|
|
t.Fatalf("remove: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(rootDir, "a")); !os.IsNotExist(err) {
|
|
t.Fatalf("empty folders kept: %v", err)
|
|
}
|
|
if _, err := os.Stat(rootDir); err != nil {
|
|
t.Fatalf("root removed: %v", err)
|
|
}
|
|
|
|
// a delete through the root cannot reach outside it.
|
|
if err := os.WriteFile(filepath.Join(outside, "keep.txt"), []byte("x"), 0o644); err != nil {
|
|
t.Fatalf("write: %v", err)
|
|
}
|
|
if err := f.Delete(root, "link/keep.txt"); err == nil {
|
|
t.Fatalf("delete through a symlink out of the root succeeded")
|
|
}
|
|
if err := f.Delete(root, "../outside/keep.txt"); err == nil {
|
|
t.Fatalf("delete with a parent segment succeeded")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(outside, "keep.txt")); err != nil {
|
|
t.Fatalf("file outside the root was removed: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestUploadFileExclusive checks that a write without overwrite never replaces
|
|
// an existing file and that overwrite does.
|
|
func TestUploadFileExclusive(t *testing.T) {
|
|
f := &File{Common: Common{Logger: zap.NewNop().Sugar()}}
|
|
dir := t.TempDir()
|
|
root, err := os.OpenRoot(dir)
|
|
if err != nil {
|
|
t.Fatalf("open root: %v", err)
|
|
}
|
|
defer root.Close()
|
|
read := func() string {
|
|
b, err := os.ReadFile(filepath.Join(dir, "x", "y.txt"))
|
|
if err != nil {
|
|
t.Fatalf("read: %v", err)
|
|
}
|
|
return string(b)
|
|
}
|
|
if err := f.UploadFile(root, "x/y.txt", bytes.NewBufferString("first"), false); err != nil {
|
|
t.Fatalf("first write: %v", err)
|
|
}
|
|
if err := f.UploadFile(root, "x/y.txt", bytes.NewBufferString("second"), false); err == nil {
|
|
t.Fatalf("write without overwrite replaced an existing file")
|
|
}
|
|
if got := read(); got != "first" {
|
|
t.Fatalf("file changed: %q", got)
|
|
}
|
|
if err := f.UploadFile(root, "x/y.txt", bytes.NewBufferString("third"), true); err != nil {
|
|
t.Fatalf("overwrite: %v", err)
|
|
}
|
|
if got := read(); got != "third" {
|
|
t.Fatalf("overwrite did not apply: %q", got)
|
|
}
|
|
if err := f.UploadFile(root, "../escape.txt", bytes.NewBufferString("x"), false); err == nil {
|
|
t.Fatalf("write outside the root succeeded")
|
|
}
|
|
}
|