Use NewHTTPError to return better errors

That function is merged upstream, but the current version still depends
on some stuff not yet merged into upstream go-webdav.
This commit is contained in:
Conrad Hoffmann 2022-05-03 17:00:07 +02:00
parent c2f35df455
commit 4765adc1a3
4 changed files with 27 additions and 14 deletions

View file

@ -14,6 +14,7 @@ import (
"path"
"path/filepath"
"regexp"
"strings"
"github.com/emersion/go-ical"
"github.com/emersion/go-vcard"
@ -100,12 +101,19 @@ func (b *filesystemBackend) safeLocalPath(homeSetPath string, urlPath string) (s
dir, file := path.Split(urlPath)
// only accept resources in prefix, no subdirs for now
if dir != homeSetPath {
return "", fmt.Errorf("invalid request path %s", urlPath)
if strings.HasPrefix(dir, homeSetPath+"/") {
err := fmt.Errorf("invalid request path: %s", urlPath)
return "", webdav.NewHTTPError(400, err)
} else {
err := fmt.Errorf("Access to resource outside of home set: %s", urlPath)
return "", webdav.NewHTTPError(403, err)
}
}
// only accept simple file names for now
if !validFilenameRegex.MatchString(file) {
fmt.Printf("%s does not match regex!\n", file)
return "", fmt.Errorf("invalid file name")
debug.Printf("%s does not match regex!\n", file)
err := fmt.Errorf("invalid file name: %s", file)
return "", webdav.NewHTTPError(400, err)
}
// dir (= homeSetPath) is already included in path, so only file here
@ -264,6 +272,9 @@ func (b *filesystemBackend) GetAddressObject(ctx context.Context, objPath string
info, err := os.Stat(localPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, webdav.NewHTTPError(404, err)
}
return nil, err
}
@ -514,7 +525,7 @@ func (b *filesystemBackend) GetCalendarObject(ctx context.Context, objPath strin
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
debug.Printf("not found: %s", localPath)
return nil, nil
return nil, webdav.NewHTTPError(404, err)
}
return nil, err
}