touka/webdav/osfs.go
google-labs-jules[bot] 26cbf45074 feat: add native WebDAV submodule with usability helpers and fixes
This commit introduces a new, high-performance, and extensible WebDAV submodule, implemented natively without external dependencies. It also adds a high-level API to simplify common use cases and incorporates numerous fixes based on detailed code reviews.

Features:
- A core WebDAV handler supporting `PROPFIND`, `MKCOL`, `GET`, `PUT`, `DELETE`, `COPY`, `MOVE`, `LOCK`, `UNLOCK`.
- An extensible design with `FileSystem` and `LockSystem` interfaces.
- `MemFS`: A robust, tree-based in-memory filesystem for testing.
- `OSFS`: A secure OS-based filesystem with protection against path traversal and symlink attacks.
- `MemLock`: An in-memory locking system with graceful shutdown to prevent resource leaks.
- A high-level API (`webdav.Serve`, `webdav.Register`) for ease of use.

Fixes & Improvements:
- Security: Patched directory traversal and symlink vulnerabilities. Ensured secure lock token generation.
- RFC Compliance: Corrected status codes for `COPY`/`MOVE` (201 vs 204), `DELETE` on non-empty collections (409), and `Timeout` header parsing.
- Performance: Implemented `sync.Pool` for object reuse and `sync/atomic` for file size management to reduce GC pressure.
- Robustness: Fixed numerous bugs related to path handling, resource cleanup (goroutine leaks), and header parsing.

Integration:
- The Touka framework's core has been updated to recognize all necessary WebDAV methods.
- Includes comprehensive unit tests and a working example.
2025-12-11 07:33:34 +00:00

148 lines
3.5 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Copyright 2024 WJQSERVER. All rights reserved.
// All rights reserved by WJQSERVER, related rights can be exercised by the infinite-iroha organization.
package webdav
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/infinite-iroha/touka"
)
// OSFS is a WebDAV FileSystem that uses the local OS file system.
type OSFS struct {
RootDir string
}
// NewOSFS creates a new OSFS.
func NewOSFS(rootDir string) (*OSFS, error) {
rootDir, err := filepath.Abs(rootDir)
if err != nil {
return nil, err
}
return &OSFS{RootDir: rootDir}, nil
}
func (fs *OSFS) resolve(name string) (string, error) {
if strings.Contains(name, "..") {
return "", os.ErrPermission
}
path := filepath.Join(fs.RootDir, name)
// Evaluate symlinks, but only if the path exists.
if _, err := os.Lstat(path); err == nil {
path, err = filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
} else if !os.IsNotExist(err) {
return "", err
// For non-existent paths (like for PUT or MKCOL), we can't EvalSymlinks the full path.
// Instead, we resolve the parent and ensure it's within the root.
} else {
parentDir := filepath.Dir(path)
if _, err := os.Stat(parentDir); err == nil {
parentDir, err = filepath.EvalSymlinks(parentDir)
if err != nil {
return "", err
}
path = filepath.Join(parentDir, filepath.Base(path))
}
}
rel, err := filepath.Rel(fs.RootDir, path)
if err != nil {
return "", err
}
if strings.HasPrefix(rel, "..") {
return "", os.ErrPermission
}
return path, nil
}
// Mkdir creates a directory.
func (fs *OSFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
path, err := fs.resolve(name)
if err != nil {
return err
}
return os.Mkdir(path, perm)
}
// osFile is a wrapper around os.File that implements the File interface.
type osFile struct {
*os.File
}
// Stat returns the FileInfo structure describing file.
func (f *osFile) Stat() (ObjectInfo, error) {
fi, err := f.File.Stat()
if err != nil {
return nil, err
}
return fi, nil
}
// Readdir reads the contents of the directory associated with file and returns
// a slice of up to n FileInfo values, as would be returned by Lstat.
func (f *osFile) Readdir(count int) ([]ObjectInfo, error) {
fi, err := f.File.Readdir(count)
if err != nil {
return nil, err
}
oi := make([]ObjectInfo, len(fi))
for i := range fi {
oi[i] = fi[i]
}
return oi, nil
}
// OpenFile opens a file.
func (fs *OSFS) OpenFile(c *touka.Context, name string, flag int, perm os.FileMode) (File, error) {
path, err := fs.resolve(name)
if err != nil {
return nil, err
}
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
return &osFile{f}, nil
}
// RemoveAll removes a file or directory.
func (fs *OSFS) RemoveAll(ctx context.Context, name string) error {
path, err := fs.resolve(name)
if err != nil {
return err
}
return os.RemoveAll(path)
}
// Rename renames a file.
func (fs *OSFS) Rename(ctx context.Context, oldName, newName string) error {
oldPath, err := fs.resolve(oldName)
if err != nil {
return err
}
newPath, err := fs.resolve(newName)
if err != nil {
return err
}
return os.Rename(oldPath, newPath)
}
// Stat returns file info.
func (fs *OSFS) Stat(ctx context.Context, name string) (ObjectInfo, error) {
path, err := fs.resolve(name)
if err != nil {
return nil, err
}
return os.Stat(path)
}