Files
OpenList/server/webdav/prop.go
cff5597863 fix(webdav)!: correct COPY/MOVE semantics, dead property persistence, and lock enforcement (#3210)
* fix(webdav): correct COPY/MOVE semantics with named copy support

Signed-off-by: Lythen <intro-iu@outlook.com>

* feat(webdav): persist dead properties with MOVE migration

Signed-off-by: Lythen <intro-iu@outlook.com>

* fix(webdav): enforce locks on resolved paths, support shared locks

Signed-off-by: Lythen <intro-iu@outlook.com>

* fix(fs): fall back to transfer task for same-dir copy

Signed-off-by: Lythen <intro-iu@outlook.com>

* fix(webdav): escape LIKE patterns, check dstDir is dir, bound PROPFIND body

Signed-off-by: Lythen <intro-iu@outlook.com>

* fix(webdav): escape backslash in LIKE, reduce dead prop retry

Signed-off-by: Lythen <intro-iu@outlook.com>

* fix(webdav): isolate shared lock instances

- Track each lock token with independent details and expiry state.\n- Keep resource nodes for path conflict accounting only.\n- Cover independent shared-lock expiration in memLS tests.

* fix(webdav): preserve dead property lifecycle

- Copy and replace dead properties with COPY destinations.\n- Remove dead-property subtrees after successful DELETE.\n- Stage MOVE properties before clearing the destination subtree.\n- Add database-backed regression coverage for copy and delete.

* fix(webdav): protect overwrite copy and move

- Validate source objects before touching overwrite destinations.\n- Stage COPY content under a collision-free name and restore destinations on failure.\n- Preserve MOVE destinations with a backup until the resource operation succeeds.\n- Route named same-storage copies through the direct destination-name transfer path.

* fix(webdav): harden overwrite staging

- Skip NoOverwriteUpload backup handling for collision-free internal COPY staging.\n- Treat post-commit overwrite backup cleanup failures as warnings.

* test(webdav): update lock lookup expectations

- Compare lookup results with independent lock instances.\n- Keep invalid token expectations nil after the lock state split.

* fix(webdav): complete litmus compliance

- Preserve named same-storage collection copies with isolated native staging.\n- Evaluate WebDAV ETag conditions and distinguish 412 from 423 lock failures.\n- Return 201 for lock-null resources and cover the new condition behavior.

* fix(fs): harden named copy staging

- retry staging cleanup from the deferred path and report residual directories

- document hook context semantics and wrap staging operation errors

Signed-off-by: Lythen <intro-iu@outlook.com>

* fix(core): support Go 1.27 checks

- Replace dynamic format strings with explicit format verbs and guard optional network configuration.

- Stabilize multipart cleanup, proxy, search, and MCP tests.

Co-authored-by: Codex <267193182+codex@users.noreply.github.com>

* fix(webdav): coordinate file and property transactions

- Keep MOVE, COPY, and DELETE file changes coordinated with dead-property transactions.

- Compensate filesystem changes when file operations or database commits fail.

- Add WebDAV coverage for transaction and recovery failures.

Co-authored-by: Codex <267193182+codex@users.noreply.github.com>

* fix(webdav): reject writes without parent collections

- Require an existing parent collection for PUT.

- Reject MKCOL when its parent is missing or not a collection.

Co-authored-by: Codex <267193182+codex@users.noreply.github.com>

* fix(webdav): refresh parent before delete checks

- Refresh the parent listing before checking whether a DELETE target exists.

- Return 404 for missing resources, including storage-not-found errors.

Co-authored-by: Codex <267193182+codex@users.noreply.github.com>

---------

Signed-off-by: Lythen <intro-iu@outlook.com>
Co-authored-by: Intro <Intro-iu@outlook.com>
Co-authored-by: Codex <267193182+codex@users.noreply.github.com>
2026-10-20 11:12:10 +08:00

555 lines
17 KiB
Go

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webdav
import (
"bytes"
"context"
"encoding/xml"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/db"
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
"github.com/OpenListTeam/OpenList/v4/server/common"
"gorm.io/gorm"
)
// Proppatch describes a property update instruction as defined in RFC 4918.
// See http://www.webdav.org/specs/rfc4918.html#METHOD_PROPPATCH
type Proppatch struct {
// Remove specifies whether this patch removes properties. If it does not
// remove them, it sets them.
Remove bool
// Props contains the properties to be set or removed.
Props []Property
}
// Propstat describes a XML propstat element as defined in RFC 4918.
// See http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat
type Propstat struct {
// Props contains the properties for which Status applies.
Props []Property
// Status defines the HTTP status code of the properties in Prop.
// Allowed values include, but are not limited to the WebDAV status
// code extensions for HTTP/1.1.
// http://www.webdav.org/specs/rfc4918.html#status.code.extensions.to.http11
Status int
// XMLError contains the XML representation of the optional error element.
// XML content within this field must not rely on any predefined
// namespace declarations or prefixes. If empty, the XML error element
// is omitted.
XMLError string
// ResponseDescription contains the contents of the optional
// responsedescription field. If empty, the XML element is omitted.
ResponseDescription string
}
// makePropstats returns a slice containing those of x and y whose Props slice
// is non-empty. If both are empty, it returns a slice containing an otherwise
// zero Propstat whose HTTP status code is 200 OK.
func makePropstats(x, y Propstat) []Propstat {
pstats := make([]Propstat, 0, 2)
if len(x.Props) != 0 {
pstats = append(pstats, x)
}
if len(y.Props) != 0 {
pstats = append(pstats, y)
}
if len(pstats) == 0 {
pstats = append(pstats, Propstat{
Status: http.StatusOK,
})
}
return pstats
}
// DeadPropsHolder holds the dead properties of a resource.
//
// Dead properties are those properties that are explicitly defined. In
// comparison, live properties, such as DAV:getcontentlength, are implicitly
// defined by the underlying resource, and cannot be explicitly overridden or
// removed. See the Terminology section of
// http://www.webdav.org/specs/rfc4918.html#rfc.section.3
//
// There is a whitelist of the names of live properties. This package handles
// all live properties, and will only pass non-whitelisted names to the Patch
// method of DeadPropsHolder implementations.
type DeadPropsHolder interface {
// DeadProps returns a copy of the dead properties held.
DeadProps() (map[xml.Name]Property, error)
// Patch patches the dead properties held.
//
// Patching is atomic; either all or no patches succeed. It returns (nil,
// non-nil) if an internal server error occurred, otherwise the Propstats
// collectively contain one Property for each proposed patch Property. If
// all patches succeed, Patch returns a slice of length one and a Propstat
// element with a 200 OK HTTP status code. If none succeed, for reasons
// other than an internal server error, no Propstat has status 200 OK.
//
// For more details on when various HTTP status codes apply, see
// http://www.webdav.org/specs/rfc4918.html#PROPPATCH-status
Patch([]Proppatch) ([]Propstat, error)
}
// liveProps contains all supported properties.
var liveProps = map[xml.Name]struct {
// findFn implements the propfind function of this property. If nil,
// it indicates a hidden property.
findFn func(context.Context, LockSystem, string, model.Obj) (string, error)
// dir is true if the property applies to directories.
dir bool
}{
{Space: "DAV:", Local: "resourcetype"}: {
findFn: findResourceType,
dir: true,
},
{Space: "DAV:", Local: "displayname"}: {
findFn: findDisplayName,
dir: true,
},
{Space: "DAV:", Local: "getcontentlength"}: {
findFn: findContentLength,
dir: false,
},
{Space: "DAV:", Local: "getlastmodified"}: {
findFn: findLastModified,
// http://webdav.org/specs/rfc4918.html#PROPERTY_getlastmodified
// suggests that getlastmodified should only apply to GETable
// resources, and this package does not support GET on directories.
//
// Nonetheless, some WebDAV clients expect child directories to be
// sortable by getlastmodified date, so this value is true, not false.
// See golang.org/issue/15334.
dir: true,
},
{Space: "DAV:", Local: "creationdate"}: {
findFn: findCreationDate,
dir: true,
},
{Space: "DAV:", Local: "getcontentlanguage"}: {
findFn: nil,
dir: false,
},
{Space: "DAV:", Local: "getcontenttype"}: {
findFn: findContentType,
dir: false,
},
{Space: "DAV:", Local: "getetag"}: {
findFn: findETag,
// findETag implements ETag as the concatenated hex values of a file's
// modification time and size. This is not a reliable synchronization
// mechanism for directories, so we do not advertise getetag for DAV
// collections.
dir: false,
},
// TODO: The lockdiscovery property requires LockSystem to list the
// active locks on a resource.
{Space: "DAV:", Local: "lockdiscovery"}: {},
{Space: "DAV:", Local: "supportedlock"}: {
findFn: findSupportedLock,
dir: true,
},
{Space: "http://owncloud.org/ns", Local: "checksums"}: {
findFn: findChecksums,
dir: false,
},
}
// TODO(nigeltao) merge props and allprop?
// Props returns the status of the properties named pnames for resource name.
func props(tx *gorm.DB, ctx context.Context, ls LockSystem, name string, fi model.Obj, pnames []xml.Name) ([]Propstat, error) {
isDir := fi.IsDir()
deadProps, err := getDeadProps(tx, name)
if err != nil {
return nil, err
}
pstatOK := Propstat{Status: http.StatusOK}
pstatNotFound := Propstat{Status: http.StatusNotFound}
for _, pn := range pnames {
if dp, ok := deadProps[pn]; ok {
pstatOK.Props = append(pstatOK.Props, dp)
continue
}
if prop := liveProps[pn]; prop.findFn != nil && (prop.dir || !isDir) {
innerXML, err := prop.findFn(ctx, ls, fi.GetName(), fi)
if err != nil {
return nil, err
}
pstatOK.Props = append(pstatOK.Props, Property{XMLName: pn, InnerXML: []byte(innerXML)})
} else {
pstatNotFound.Props = append(pstatNotFound.Props, Property{XMLName: pn})
}
}
return makePropstats(pstatOK, pstatNotFound), nil
}
// Propnames returns the property names defined for resource name.
func propnames(tx *gorm.DB, name string, fi model.Obj) ([]xml.Name, error) {
isDir := fi.IsDir()
deadProps, err := getDeadProps(tx, name)
if err != nil {
return nil, err
}
pnames := make([]xml.Name, 0, len(liveProps)+len(deadProps))
for pn, prop := range liveProps {
if prop.findFn != nil && (prop.dir || !isDir) {
pnames = append(pnames, pn)
}
}
for pn := range deadProps {
pnames = append(pnames, pn)
}
return pnames, nil
}
// Allprop returns the properties defined for resource name and the properties named in include.
func allprop(tx *gorm.DB, ctx context.Context, ls LockSystem, name string, fi model.Obj, include []xml.Name) ([]Propstat, error) {
pnames, err := propnames(tx, name, fi)
if err != nil {
return nil, err
}
nameset := make(map[xml.Name]bool)
for _, pn := range pnames {
nameset[pn] = true
}
for _, pn := range include {
if !nameset[pn] {
pnames = append(pnames, pn)
}
}
return props(tx, ctx, ls, name, fi, pnames)
}
// Patch patches the properties of resource name.
func patch(ctx context.Context, ls LockSystem, name string, patches []Proppatch) ([]Propstat, error) {
conflict := false
loop:
for _, patch := range patches {
for _, p := range patch.Props {
if _, ok := liveProps[p.XMLName]; ok {
conflict = true
break loop
}
}
}
if conflict {
pstatForbidden := Propstat{
Status: http.StatusForbidden,
XMLError: `<D:cannot-modify-protected-property xmlns:D="DAV:"/>`,
}
pstatFailedDep := Propstat{
Status: StatusFailedDependency,
}
for _, patch := range patches {
for _, p := range patch.Props {
if _, ok := liveProps[p.XMLName]; ok {
pstatForbidden.Props = append(pstatForbidden.Props, Property{XMLName: p.XMLName})
} else {
pstatFailedDep.Props = append(pstatFailedDep.Props, Property{XMLName: p.XMLName})
}
}
}
return makePropstats(pstatForbidden, pstatFailedDep), nil
}
pstat := Propstat{Status: http.StatusOK}
var err error
for attempt := 0; attempt < 10; attempt++ {
pstat.Props = nil
err = withDeadPropsTransaction(ctx, func(tx *gorm.DB) error {
for _, patch := range patches {
for _, prop := range patch.Props {
if patch.Remove {
if err := tx.Where("path = ? AND namespace = ? AND name = ?", name, prop.XMLName.Space, prop.XMLName.Local).Delete(&model.WebDAVProperty{}).Error; err != nil {
return err
}
} else {
row := model.WebDAVProperty{
Path: name,
Namespace: prop.XMLName.Space,
Name: prop.XMLName.Local,
}
if err := tx.Where("path = ? AND namespace = ? AND name = ?", row.Path, row.Namespace, row.Name).
Assign(model.WebDAVProperty{Lang: prop.Lang, InnerXML: prop.InnerXML}).
FirstOrCreate(&row).Error; err != nil {
return err
}
}
pstat.Props = append(pstat.Props, Property{XMLName: prop.XMLName})
}
}
return nil
})
if err == nil || !strings.Contains(err.Error(), "database is locked") {
break
}
time.Sleep(time.Duration(attempt+1) * 20 * time.Millisecond)
}
if err != nil {
return nil, err
}
return []Propstat{pstat}, nil
}
func getDeadProps(tx *gorm.DB, path string) (map[xml.Name]Property, error) {
var rows []model.WebDAVProperty
if tx == nil {
return nil, errors.New("webdav property transaction is not initialized")
}
if err := tx.Where("path = ?", path).Find(&rows).Error; err != nil {
return nil, err
}
props := make(map[xml.Name]Property, len(rows))
for _, row := range rows {
props[xml.Name{Space: row.Namespace, Local: row.Name}] = Property{
XMLName: xml.Name{Space: row.Namespace, Local: row.Name},
Lang: row.Lang,
InnerXML: row.InnerXML,
}
}
return props, nil
}
func withDeadPropsTransaction(ctx context.Context, fn func(*gorm.DB) error) error {
database := db.GetDb()
if database == nil {
return errors.New("webdav property database is not initialized")
}
var transaction *gorm.DB
err := database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
transaction = tx
var probe int
if err := tx.Raw("SELECT 1").Scan(&probe).Error; err != nil {
return err
}
return fn(tx)
})
if err != nil && transaction != nil {
_ = transaction.Rollback().Error
}
return err
}
func escapeXML(s string) string {
for i := 0; i < len(s); i++ {
switch c := s[i]; {
case c == ' ' || c == '_' ||
('+' <= c && c <= '9') ||
('A' <= c && c <= 'Z') ||
('a' <= c && c <= 'z'):
continue
}
var buf bytes.Buffer
xml.EscapeText(&buf, []byte(s))
return buf.String()
}
return s
}
func deadPropsLikePath(path string) string {
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(path, "\\", "\\\\"), "%", "\\%"), "_", "\\_")
}
func deadPropsSubtree(tx *gorm.DB, path string) *gorm.DB {
return tx.Where("path = ? OR path LIKE ? ESCAPE '\\'", path, deadPropsLikePath(path)+"/%")
}
func deleteDeadProps(tx *gorm.DB, path string) error {
return deadPropsSubtree(tx, path).Delete(&model.WebDAVProperty{}).Error
}
func removeResourceWithDeadProps(ctx context.Context, path string, remove func() error) (removeErr, transactionErr error) {
transactionErr = withDeadPropsTransaction(ctx, func(tx *gorm.DB) error {
if err := deleteDeadProps(tx, path); err != nil {
return err
}
removeErr = remove()
return removeErr
})
return removeErr, transactionErr
}
func copyDeadProps(tx *gorm.DB, src, dst string) error {
var rows []model.WebDAVProperty
if err := deadPropsSubtree(tx, src).Find(&rows).Error; err != nil {
return err
}
if err := deadPropsSubtree(tx, dst).Delete(&model.WebDAVProperty{}).Error; err != nil {
return err
}
for _, row := range rows {
copy := row
copy.ID = 0
copy.Path = dst + strings.TrimPrefix(row.Path, src)
if err := tx.Create(&copy).Error; err != nil {
return err
}
}
return nil
}
func moveDeadProps(tx *gorm.DB, src, dst string) error {
var rows []model.WebDAVProperty
if err := deadPropsSubtree(tx, src).Find(&rows).Error; err != nil {
return err
}
// Clear destination only after the source rows are staged in memory.
if err := deadPropsSubtree(tx, dst).Delete(&model.WebDAVProperty{}).Error; err != nil {
return err
}
for _, row := range rows {
newPath := dst + strings.TrimPrefix(row.Path, src)
copy := row
copy.ID = 0
copy.Path = newPath
if err := tx.Create(&copy).Error; err != nil {
return err
}
if err := tx.Delete(&row).Error; err != nil {
return err
}
}
return nil
}
func findResourceType(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
if fi.IsDir() {
return `<D:collection xmlns:D="DAV:"/>`, nil
}
return "", nil
}
func findDisplayName(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
if slashClean(name) == "/" {
// Hide the real name of a possibly prefixed root directory.
return "", nil
}
return escapeXML(fi.GetName()), nil
}
func findContentLength(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
return strconv.FormatInt(fi.GetSize(), 10), nil
}
func findLastModified(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
return fi.ModTime().UTC().Format(http.TimeFormat), nil
}
func findCreationDate(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
userAgent := ctx.Value(conf.UserAgentKey).(string)
if strings.Contains(strings.ToLower(userAgent), "microsoft-webdav") {
return fi.CreateTime().UTC().Format(http.TimeFormat), nil
}
return fi.CreateTime().UTC().Format(time.RFC3339), nil
}
// ErrNotImplemented should be returned by optional interfaces if they
// want the original implementation to be used.
var ErrNotImplemented = errors.New("not implemented")
// ContentTyper is an optional interface for the os.FileInfo
// objects returned by the FileSystem.
//
// If this interface is defined then it will be used to read the
// content type from the object.
//
// If this interface is not defined the file will be opened and the
// content type will be guessed from the initial contents of the file.
type ContentTyper interface {
// ContentType returns the content type for the file.
//
// If this returns error ErrNotImplemented then the error will
// be ignored and the base implementation will be used
// instead.
ContentType(ctx context.Context) (string, error)
}
func findContentType(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
//if do, ok := fi.(ContentTyper); ok {
// ctype, err := do.ContentType(ctx)
// if err != ErrNotImplemented {
// return ctype, err
// }
//}
//f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
//if err != nil {
// return "", err
//}
//defer f.Close()
// This implementation is based on serveContent's code in the standard net/http package.
ctype := utils.GetMimeType(name)
return ctype, nil
//if ctype != "" {
// return ctype, nil
//}
//return "application/octet-stream", nil
// Read a chunk to decide between utf-8 text and binary.
//var buf [512]byte
//n, err := io.ReadFull(f, buf[:])
//if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
// return "", err
//}
//ctype = http.DetectContentType(buf[:n])
//// Rewind file.
//_, err = f.Seek(0, os.SEEK_SET)
//return ctype, err
}
// ETager is an optional interface for the os.FileInfo objects
// returned by the FileSystem.
//
// If this interface is defined then it will be used to read the ETag
// for the object.
//
// If this interface is not defined an ETag will be computed using the
// ModTime() and the Size() methods of the os.FileInfo object.
type ETager interface {
// ETag returns an ETag for the file. This should be of the
// form "value" or W/"value"
//
// If this returns error ErrNotImplemented then the error will
// be ignored and the base implementation will be used
// instead.
ETag(ctx context.Context) (string, error)
}
func findETag(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
if do, ok := fi.(ETager); ok {
etag, err := do.ETag(ctx)
if !errors.Is(err, ErrNotImplemented) {
return etag, err
}
}
return common.GetEtag(fi, fi.GetSize()), nil
}
func findSupportedLock(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
return `` +
`<D:lockentry xmlns:D="DAV:">` +
`<D:lockscope><D:exclusive/></D:lockscope>` +
`<D:locktype><D:write/></D:locktype>` +
`</D:lockentry>`, nil
}
func findChecksums(ctx context.Context, ls LockSystem, name string, fi model.Obj) (string, error) {
checksums := ""
for hashType, hashValue := range fi.GetHash().All() {
checksums += fmt.Sprintf("<checksum>%s:%s</checksum>", hashType.Name, hashValue)
}
return checksums, nil
}