524 lines
12 KiB
Go
524 lines
12 KiB
Go
package nodefiles
|
|
|
|
import (
|
|
"archive/tar"
|
|
"archive/zip"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const maxExpanded = 256 << 20
|
|
|
|
type TrashEntry struct {
|
|
ID string `json:"id"`
|
|
OriginalPath string `json:"originalPath"`
|
|
Name string `json:"name"`
|
|
DeletedAt time.Time `json:"deletedAt"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
func isAdvanced(op string) bool {
|
|
switch op {
|
|
case "move", "copy", "zip", "extract", "trash", "trash_list", "restore":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// IsProtectedPath identifies platform-owned configuration and runtime paths.
|
|
func IsProtectedPath(name string) bool {
|
|
return protected(path.Clean(strings.ReplaceAll(name, "\\", "/")))
|
|
}
|
|
|
|
func protected(name string) bool {
|
|
for _, v := range strings.Split(name, "/") {
|
|
if v == ".sea-runtime" || v == ".user.ini" || v == ".sea-trash" || strings.HasPrefix(v, ".sea-op-") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func userPath(root *os.Root, name string) error {
|
|
if name == "." || path.Clean(name) != name || protected(name) {
|
|
return fs.ErrPermission
|
|
}
|
|
return check(root, name)
|
|
}
|
|
func absent(root *os.Root, name string) error {
|
|
if _, e := root.Lstat(name); e == nil {
|
|
return fs.ErrExist
|
|
} else if !errors.Is(e, fs.ErrNotExist) {
|
|
return e
|
|
}
|
|
return nil
|
|
}
|
|
func opID() (string, error) {
|
|
b := make([]byte, 16)
|
|
_, e := rand.Read(b)
|
|
return hex.EncodeToString(b), e
|
|
}
|
|
func advanced(root *os.Root, q Request, out *Response) error {
|
|
if q.Operation == "trash_list" {
|
|
return listTrash(root, out)
|
|
}
|
|
if q.Operation == "restore" {
|
|
if len(q.Path) != 32 || strings.Trim(q.Path, "0123456789abcdef") != "" {
|
|
return fs.ErrPermission
|
|
}
|
|
if e := userPath(root, q.To); e != nil {
|
|
return e
|
|
}
|
|
if e := absent(root, q.To); e != nil {
|
|
return e
|
|
}
|
|
from := ".sea-trash/" + q.Path
|
|
if e := check(root, from); e != nil {
|
|
return e
|
|
}
|
|
if e := root.MkdirAll(path.Dir(q.To), 0770); e != nil {
|
|
return e
|
|
}
|
|
if e := root.Rename(from, q.To); e != nil {
|
|
return e
|
|
}
|
|
_ = root.Remove(from + ".json")
|
|
return nil
|
|
}
|
|
if e := userPath(root, q.Path); e != nil {
|
|
return e
|
|
}
|
|
if q.Operation == "trash" {
|
|
id, e := opID()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if e = root.MkdirAll(".sea-trash", 0770); e != nil {
|
|
return e
|
|
}
|
|
if e = check(root, ".sea-trash"); e != nil {
|
|
return e
|
|
}
|
|
i, e := root.Lstat(q.Path)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
entry := TrashEntry{ID: id, OriginalPath: q.Path, Name: path.Base(q.Path), DeletedAt: time.Now().UTC(), Size: i.Size()}
|
|
raw, _ := json.Marshal(entry)
|
|
f, e := root.OpenFile(".sea-trash/"+id+".json", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
_, e = f.Write(raw)
|
|
closeErr := f.Close()
|
|
if e == nil {
|
|
e = closeErr
|
|
}
|
|
if e == nil {
|
|
e = root.Rename(q.Path, ".sea-trash/"+id)
|
|
}
|
|
if e != nil {
|
|
root.Remove(".sea-trash/" + id + ".json")
|
|
}
|
|
return e
|
|
}
|
|
if e := userPath(root, q.To); e != nil {
|
|
return e
|
|
}
|
|
if e := absent(root, q.To); e != nil {
|
|
return e
|
|
}
|
|
if q.To == q.Path || strings.HasPrefix(q.To, q.Path+"/") {
|
|
return fs.ErrPermission
|
|
}
|
|
if q.Operation == "move" {
|
|
return root.Rename(q.Path, q.To)
|
|
}
|
|
budget := q.Budget
|
|
if budget > maxExpanded {
|
|
budget = maxExpanded
|
|
}
|
|
id, e := opID()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
stage := path.Join(path.Dir(q.To), ".sea-op-"+id)
|
|
defer root.RemoveAll(stage)
|
|
switch q.Operation {
|
|
case "copy":
|
|
if e = copyTree(root, q.Path, stage, &budget, new(int)); e != nil {
|
|
return e
|
|
}
|
|
case "zip":
|
|
f, e := root.OpenFile(stage, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
z := zip.NewWriter(&budgetWriter{w: f, left: budget})
|
|
e = zipTree(root, z, q.Path, path.Base(q.Path), new(int), newZipBudget())
|
|
if closeErr := z.Close(); e == nil {
|
|
e = closeErr
|
|
}
|
|
if closeErr := f.Close(); e == nil {
|
|
e = closeErr
|
|
}
|
|
if e != nil {
|
|
return e
|
|
}
|
|
case "extract":
|
|
f, e := root.Open(q.Path)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
i, e := f.Stat()
|
|
if e != nil || !i.Mode().IsRegular() || i.Size() > MaxFileBytes {
|
|
f.Close()
|
|
return fs.ErrPermission
|
|
}
|
|
data, e := io.ReadAll(io.LimitReader(f, MaxFileBytes+1))
|
|
f.Close()
|
|
if e != nil || len(data) > MaxFileBytes {
|
|
return fs.ErrPermission
|
|
}
|
|
lower := strings.ToLower(q.Path)
|
|
if strings.HasSuffix(lower, ".tar") || strings.HasSuffix(lower, ".tar.gz") || strings.HasSuffix(lower, ".tgz") {
|
|
if e = extractTar(root, stage, data, strings.HasSuffix(lower, ".gz") || strings.HasSuffix(lower, ".tgz"), budget); e != nil {
|
|
return e
|
|
}
|
|
break
|
|
}
|
|
z, e := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if len(z.File) > 10000 {
|
|
return fs.ErrPermission
|
|
}
|
|
if e = root.Mkdir(stage, 0770); e != nil {
|
|
return e
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, entry := range z.File {
|
|
name := strings.TrimSuffix(entry.Name, "/")
|
|
if name == "" || path.Clean(name) != name || !validName(name) || strings.HasPrefix(name, "/") || protected(name) || entry.Mode()&fs.ModeSymlink != 0 || (!entry.Mode().IsRegular() && !entry.FileInfo().IsDir()) || seen[name] {
|
|
return fs.ErrPermission
|
|
}
|
|
seen[name] = true
|
|
target := path.Join(stage, name)
|
|
if e = check(root, target); e != nil {
|
|
return e
|
|
}
|
|
if entry.FileInfo().IsDir() {
|
|
if e = root.MkdirAll(target, 0770); e != nil {
|
|
return e
|
|
}
|
|
continue
|
|
}
|
|
if entry.UncompressedSize64 > budget {
|
|
return fs.ErrPermission
|
|
}
|
|
if e = root.MkdirAll(path.Dir(target), 0770); e != nil {
|
|
return e
|
|
}
|
|
input, e := entry.Open()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
output, e := root.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
|
if e != nil {
|
|
input.Close()
|
|
return e
|
|
}
|
|
n, e := io.Copy(output, io.LimitReader(input, int64(budget)+1))
|
|
input.Close()
|
|
output.Close()
|
|
if e != nil || uint64(n) > budget {
|
|
return fs.ErrPermission
|
|
}
|
|
budget -= uint64(n)
|
|
}
|
|
default:
|
|
return fs.ErrPermission
|
|
}
|
|
if e = check(root, q.To); e != nil {
|
|
return e
|
|
}
|
|
if e = absent(root, q.To); e != nil {
|
|
return e
|
|
}
|
|
return root.Rename(stage, q.To)
|
|
}
|
|
|
|
type budgetWriter struct {
|
|
w io.Writer
|
|
left uint64
|
|
}
|
|
|
|
func (w *budgetWriter) Write(b []byte) (int, error) {
|
|
if uint64(len(b)) > w.left {
|
|
return 0, fs.ErrPermission
|
|
}
|
|
n, e := w.w.Write(b)
|
|
w.left -= uint64(n)
|
|
return n, e
|
|
}
|
|
func copyTree(root *os.Root, from, to string, budget *uint64, count *int) error {
|
|
*count++
|
|
if *count > 10000 {
|
|
return fs.ErrPermission
|
|
}
|
|
if e := check(root, from); e != nil {
|
|
return e
|
|
}
|
|
i, e := root.Lstat(from)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if i.IsDir() {
|
|
if e = root.Mkdir(to, 0770); e != nil {
|
|
return e
|
|
}
|
|
dir, e := root.Open(from)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
entries, e := dir.ReadDir(10001)
|
|
dir.Close()
|
|
if e != nil && e != io.EOF {
|
|
return e
|
|
}
|
|
for _, entry := range entries {
|
|
source := path.Join(from, entry.Name())
|
|
if protected(source) {
|
|
continue
|
|
}
|
|
if e = copyTree(root, source, path.Join(to, entry.Name()), budget, count); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
if !i.Mode().IsRegular() || uint64(i.Size()) > *budget {
|
|
return fs.ErrPermission
|
|
}
|
|
input, e := root.Open(from)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
defer input.Close()
|
|
output, e := root.OpenFile(to, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
n, e := io.Copy(output, io.LimitReader(input, int64(*budget)+1))
|
|
output.Close()
|
|
if e != nil || uint64(n) > *budget {
|
|
return fs.ErrPermission
|
|
}
|
|
*budget -= uint64(n)
|
|
return nil
|
|
}
|
|
func zipTree(root *os.Root, z *zip.Writer, name, archiveName string, count *int, inputBudget *uint64) error {
|
|
*count++
|
|
if *count > 10000 {
|
|
return fs.ErrPermission
|
|
}
|
|
if e := check(root, name); e != nil {
|
|
return e
|
|
}
|
|
i, e := root.Lstat(name)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if i.IsDir() {
|
|
dir, e := root.Open(name)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
entries, e := dir.ReadDir(10001)
|
|
dir.Close()
|
|
if e != nil && e != io.EOF {
|
|
return e
|
|
}
|
|
for _, entry := range entries {
|
|
source := path.Join(name, entry.Name())
|
|
if protected(source) {
|
|
continue
|
|
}
|
|
if e = zipTree(root, z, source, path.Join(archiveName, entry.Name()), count, inputBudget); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
if !i.Mode().IsRegular() {
|
|
return fs.ErrPermission
|
|
}
|
|
if uint64(i.Size()) > *inputBudget {
|
|
return fs.ErrPermission
|
|
}
|
|
*inputBudget -= uint64(i.Size())
|
|
header, e := zip.FileInfoHeader(i)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
header.Name = archiveName
|
|
header.Method = zip.Deflate
|
|
dst, e := z.CreateHeader(header)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
f, e := root.Open(name)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
defer f.Close()
|
|
_, e = io.Copy(dst, io.LimitReader(f, maxExpanded+1))
|
|
return e
|
|
}
|
|
func listTrash(root *os.Root, out *Response) error {
|
|
out.Trash = []TrashEntry{}
|
|
if e := check(root, ".sea-trash"); e != nil {
|
|
return e
|
|
}
|
|
dir, e := root.Open(".sea-trash")
|
|
if errors.Is(e, fs.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if e != nil {
|
|
return e
|
|
}
|
|
entries, e := dir.ReadDir(10001)
|
|
dir.Close()
|
|
if e != nil && e != io.EOF {
|
|
return e
|
|
}
|
|
if len(entries) > 10000 {
|
|
return fs.ErrPermission
|
|
}
|
|
for _, entry := range entries {
|
|
if !strings.HasSuffix(entry.Name(), ".json") {
|
|
continue
|
|
}
|
|
name := ".sea-trash/" + entry.Name()
|
|
if check(root, name) != nil {
|
|
continue
|
|
}
|
|
f, e := root.Open(name)
|
|
if e != nil {
|
|
continue
|
|
}
|
|
var v TrashEntry
|
|
e = json.NewDecoder(io.LimitReader(f, 4096)).Decode(&v)
|
|
f.Close()
|
|
if e == nil && len(v.ID) == 32 && strings.Trim(v.ID, "0123456789abcdef") == "" && entry.Name() == v.ID+".json" && userPath(root, v.OriginalPath) == nil {
|
|
if _, e = root.Lstat(".sea-trash/" + v.ID); e == nil {
|
|
out.Trash = append(out.Trash, v)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func newZipBudget() *uint64 { v := uint64(maxExpanded); return &v }
|
|
|
|
// TAR members are written into a private staging directory and published only on success.
|
|
func extractTar(root *os.Root, stage string, data []byte, compressed bool, budget uint64) error {
|
|
var input io.Reader = bytes.NewReader(data)
|
|
if compressed {
|
|
gz, err := gzip.NewReader(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer gz.Close()
|
|
input = gz
|
|
}
|
|
// Bound metadata, padding and decompression work as well as extracted file data.
|
|
limited := &io.LimitedReader{R: input, N: int64(maxExpanded) + (8 << 20)}
|
|
reader := tar.NewReader(limited)
|
|
if err := root.Mkdir(stage, 0770); err != nil {
|
|
return err
|
|
}
|
|
seen := map[string]bool{}
|
|
for count := 0; ; count++ {
|
|
header, err := reader.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count >= 10000 {
|
|
return fs.ErrPermission
|
|
}
|
|
name := strings.TrimSuffix(header.Name, "/")
|
|
for strings.HasPrefix(name, "./") {
|
|
name = strings.TrimPrefix(name, "./")
|
|
}
|
|
if (name == "." || name == "") && header.Typeflag == tar.TypeDir {
|
|
continue
|
|
}
|
|
if name == "" || path.Clean(name) != name || !validName(name) || strings.HasPrefix(name, "/") || protected(name) || seen[name] {
|
|
return fs.ErrPermission
|
|
}
|
|
if header.Typeflag != tar.TypeDir && header.Typeflag != tar.TypeReg && header.Typeflag != tar.TypeRegA {
|
|
return fs.ErrPermission
|
|
}
|
|
for key := range header.PAXRecords {
|
|
if strings.HasPrefix(key, "GNU.sparse") {
|
|
return fs.ErrPermission
|
|
}
|
|
}
|
|
seen[name] = true
|
|
target := path.Join(stage, name)
|
|
if err = check(root, target); err != nil {
|
|
return err
|
|
}
|
|
if header.Typeflag == tar.TypeDir {
|
|
if err = root.MkdirAll(target, 0770); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
if header.Size < 0 || uint64(header.Size) > budget {
|
|
return fs.ErrPermission
|
|
}
|
|
if err = root.MkdirAll(path.Dir(target), 0770); err != nil {
|
|
return err
|
|
}
|
|
output, err := root.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, copyErr := io.Copy(output, io.LimitReader(reader, header.Size+1))
|
|
closeErr := output.Close()
|
|
if copyErr != nil {
|
|
return copyErr
|
|
}
|
|
if closeErr != nil {
|
|
return closeErr
|
|
}
|
|
if n != header.Size {
|
|
return io.ErrUnexpectedEOF
|
|
}
|
|
budget -= uint64(n)
|
|
}
|
|
_, err := io.Copy(io.Discard, limited)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if limited.N <= 0 {
|
|
return fs.ErrPermission
|
|
}
|
|
return nil
|
|
}
|