sftpgo-mirror/internal/webdavd/internal_test.go

1697 lines
61 KiB
Go
Raw Normal View History

// Copyright (C) 2019-2023 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-08-11 21:56:10 +00:00
package webdavd
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/xml"
2020-08-11 21:56:10 +00:00
"fmt"
"io"
"net/http"
"net/http/httptest"
2020-08-11 21:56:10 +00:00
"os"
"path"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/drakkan/webdav"
2020-08-11 21:56:10 +00:00
"github.com/eikenb/pipeat"
"github.com/sftpgo/sdk"
2020-08-11 21:56:10 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2020-08-11 21:56:10 +00:00
"github.com/drakkan/sftpgo/v2/internal/common"
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/kms"
"github.com/drakkan/sftpgo/v2/internal/util"
"github.com/drakkan/sftpgo/v2/internal/vfs"
2020-08-11 21:56:10 +00:00
)
const (
testFile = "test_dav_file"
webDavCert = `-----BEGIN CERTIFICATE-----
MIICHTCCAaKgAwIBAgIUHnqw7QnB1Bj9oUsNpdb+ZkFPOxMwCgYIKoZIzj0EAwIw
RTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGElu
dGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDAyMDQwOTUzMDRaFw0zMDAyMDEw
OTUzMDRaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYD
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwdjAQBgcqhkjOPQIBBgUrgQQA
IgNiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVqWvrJ51t5OxV0v25NsOgR82CA
NXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIVCzgWkxiz7XE4lgUwX44FCXZM
3+JeUbKjUzBRMB0GA1UdDgQWBBRhLw+/o3+Z02MI/d4tmaMui9W16jAfBgNVHSME
GDAWgBRhLw+/o3+Z02MI/d4tmaMui9W16jAPBgNVHRMBAf8EBTADAQH/MAoGCCqG
SM49BAMCA2kAMGYCMQDqLt2lm8mE+tGgtjDmtFgdOcI72HSbRQ74D5rYTzgST1rY
/8wTi5xl8TiFUyLMUsICMQC5ViVxdXbhuG7gX6yEqSkMKZICHpO8hqFwOD/uaFVI
dV4vKmHUzwK/eIx+8Ay3neE=
-----END CERTIFICATE-----`
webDavKey = `-----BEGIN EC PARAMETERS-----
BgUrgQQAIg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDCfMNsN6miEE3rVyUPwElfiJSWaR5huPCzUenZOfJT04GAcQdWvEju3
UM2lmBLIXpGgBwYFK4EEACKhZANiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVq
WvrJ51t5OxV0v25NsOgR82CANXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIV
CzgWkxiz7XE4lgUwX44FCXZM3+JeUbI=
-----END EC PRIVATE KEY-----`
caCRT = `-----BEGIN CERTIFICATE-----
MIIE5jCCAs6gAwIBAgIBATANBgkqhkiG9w0BAQsFADATMREwDwYDVQQDEwhDZXJ0
QXV0aDAeFw0yMzAxMDMxMDIwNDdaFw0zMzAxMDMxMDMwNDZaMBMxETAPBgNVBAMT
CENlcnRBdXRoMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxq6Wl1Ih
hgvGdM8M2IVI7dwnv3yShJygZsnREQSEW0xeWJL5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8oDNLvUar/VnMrSUBxi/jo3j2
08IOKMKqSl+BX77ftgazhyL+hEgxlZuPKeqUuOWcNxuAs0vK6Gc5+Y9UpQEq78nH
uaPG3o9EBDf5eFKi76o+pVtqxrwhY88M/Yw0ykEA6Nf7RCo2ucdemg==
-----END RSA PRIVATE KEY-----`
2020-08-11 21:56:10 +00:00
)
// MockOsFs mockable OsFs
type MockOsFs struct {
vfs.Fs
err error
isAtomicUploadSupported bool
reader *pipeat.PipeReaderAt
2020-08-11 21:56:10 +00:00
}
// Name returns the name for the Fs implementation
func (fs *MockOsFs) Name() string {
2020-08-11 21:56:10 +00:00
return "mockOsFs"
}
// Open returns nil
func (fs *MockOsFs) Open(name string, offset int64) (vfs.File, *pipeat.PipeReaderAt, func(), error) {
if fs.reader != nil {
return nil, fs.reader, nil, nil
}
return fs.Fs.Open(name, offset)
2020-08-11 21:56:10 +00:00
}
// IsUploadResumeSupported returns true if resuming uploads is supported
func (*MockOsFs) IsUploadResumeSupported() bool {
2020-08-11 21:56:10 +00:00
return false
}
// IsAtomicUploadSupported returns true if atomic upload is supported
func (fs *MockOsFs) IsAtomicUploadSupported() bool {
2020-08-11 21:56:10 +00:00
return fs.isAtomicUploadSupported
}
// Remove removes the named file or (empty) directory.
func (fs *MockOsFs) Remove(name string, _ bool) error {
2020-08-11 21:56:10 +00:00
if fs.err != nil {
return fs.err
}
return os.Remove(name)
}
// Rename renames (moves) source to target
func (fs *MockOsFs) Rename(source, target string) (int, int64, error) {
err := os.Rename(source, target)
return -1, -1, err
2020-08-11 21:56:10 +00:00
}
// GetMimeType returns the content type
func (fs *MockOsFs) GetMimeType(_ string) (string, error) {
if fs.err != nil {
return "", fs.err
}
return "application/custom-mime", nil
2020-08-11 21:56:10 +00:00
}
func newMockOsFs(atomicUpload bool, connectionID, rootDir string, reader *pipeat.PipeReaderAt, err error) vfs.Fs {
2020-08-11 21:56:10 +00:00
return &MockOsFs{
Fs: vfs.NewOsFs(connectionID, rootDir, "", nil),
2020-08-11 21:56:10 +00:00
isAtomicUploadSupported: atomicUpload,
reader: reader,
err: err,
2020-08-11 21:56:10 +00:00
}
}
func TestUserInvalidParams(t *testing.T) {
u := &dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
Username: "username",
HomeDir: "invalid",
},
2020-08-11 21:56:10 +00:00
}
c := &Configuration{
Bindings: []Binding{
{
Port: 9000,
},
},
2020-08-11 21:56:10 +00:00
}
2021-01-03 08:51:54 +00:00
server := webDavServer{
config: c,
binding: c.Bindings[0],
}
2020-08-11 21:56:10 +00:00
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", u.Username), nil)
assert.NoError(t, err)
_, err = server.validateUser(u, req, dataprovider.LoginMethodPassword)
2020-08-11 21:56:10 +00:00
if assert.Error(t, err) {
assert.EqualError(t, err, fmt.Sprintf("cannot login user with invalid home dir: %q", u.HomeDir))
2020-08-11 21:56:10 +00:00
}
req.TLS = &tls.ConnectionState{}
2021-10-01 18:37:23 +00:00
writeLog(req, http.StatusOK, nil)
2020-08-11 21:56:10 +00:00
}
func TestAllowedProxyUnixDomainSocket(t *testing.T) {
b := Binding{
Address: filepath.Join(os.TempDir(), "sock"),
ProxyAllowed: []string{"127.0.0.1", "127.0.1.1"},
}
err := b.parseAllowedProxy()
assert.NoError(t, err)
if assert.Len(t, b.allowHeadersFrom, 1) {
assert.True(t, b.allowHeadersFrom[0](nil))
}
}
2020-08-11 21:56:10 +00:00
func TestRemoteAddress(t *testing.T) {
remoteAddr1 := "100.100.100.100"
remoteAddr2 := "172.172.172.172"
c := &Configuration{
Bindings: []Binding{
{
Port: 9000,
ProxyAllowed: []string{remoteAddr2, "10.8.0.0/30"},
},
},
}
server := webDavServer{
config: c,
binding: c.Bindings[0],
}
err := server.binding.parseAllowedProxy()
assert.NoError(t, err)
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.NoError(t, err)
assert.Empty(t, req.RemoteAddr)
trueClientIP := "True-Client-IP"
cfConnectingIP := "CF-Connecting-IP"
xff := "X-Forwarded-For"
xRealIP := "X-Real-IP"
req.Header.Set(trueClientIP, remoteAddr1)
ip := util.GetRealIP(req, trueClientIP, 0)
assert.Equal(t, remoteAddr1, ip)
ip = util.GetRealIP(req, trueClientIP, 2)
assert.Empty(t, ip)
req.Header.Del(trueClientIP)
req.Header.Set(cfConnectingIP, remoteAddr1)
ip = util.GetRealIP(req, cfConnectingIP, 0)
assert.Equal(t, remoteAddr1, ip)
req.Header.Del(cfConnectingIP)
req.Header.Set(xff, remoteAddr1)
ip = util.GetRealIP(req, xff, 0)
assert.Equal(t, remoteAddr1, ip)
// this will be ignored, remoteAddr1 is not allowed to se this header
req.Header.Set(xff, remoteAddr2)
req.RemoteAddr = remoteAddr1
ip = server.checkRemoteAddress(req)
assert.Equal(t, remoteAddr1, ip)
2020-08-11 21:56:10 +00:00
req.RemoteAddr = ""
ip = server.checkRemoteAddress(req)
assert.Empty(t, ip)
2020-08-11 21:56:10 +00:00
req.Header.Set(xff, fmt.Sprintf("%v , %v", remoteAddr2, remoteAddr1))
ip = util.GetRealIP(req, xff, 1)
assert.Equal(t, remoteAddr2, ip)
req.RemoteAddr = remoteAddr2
req.Header.Set(xff, fmt.Sprintf("%v,%v", "12.34.56.78", "172.16.2.4"))
server.binding.ClientIPHeaderDepth = 1
server.binding.ClientIPProxyHeader = xff
ip = server.checkRemoteAddress(req)
assert.Equal(t, "12.34.56.78", ip)
assert.Equal(t, ip, req.RemoteAddr)
req.RemoteAddr = remoteAddr2
req.Header.Set(xff, fmt.Sprintf("%v,%v", "12.34.56.79", "172.16.2.5"))
server.binding.ClientIPHeaderDepth = 0
ip = server.checkRemoteAddress(req)
assert.Equal(t, "172.16.2.5", ip)
assert.Equal(t, ip, req.RemoteAddr)
req.RemoteAddr = "10.8.0.2"
req.Header.Set(xff, remoteAddr1)
ip = server.checkRemoteAddress(req)
assert.Equal(t, remoteAddr1, ip)
assert.Equal(t, ip, req.RemoteAddr)
req.RemoteAddr = "10.8.0.3"
req.Header.Set(xff, "not an ip")
ip = server.checkRemoteAddress(req)
assert.Equal(t, "10.8.0.3", ip)
assert.Equal(t, ip, req.RemoteAddr)
2020-08-11 21:56:10 +00:00
req.Header.Del(xff)
2020-08-11 21:56:10 +00:00
req.RemoteAddr = ""
req.Header.Set(xRealIP, remoteAddr1)
ip = util.GetRealIP(req, "x-real-ip", 0)
assert.Equal(t, remoteAddr1, ip)
2020-08-11 21:56:10 +00:00
req.RemoteAddr = ""
}
func TestConnWithNilRequest(t *testing.T) {
c := &Connection{}
assert.Empty(t, c.GetClientVersion())
assert.Empty(t, c.GetCommand())
assert.Empty(t, c.GetRemoteAddress())
assert.True(t, c.getModificationTime().IsZero())
2020-08-11 21:56:10 +00:00
}
func TestResolvePathErrors(t *testing.T) {
ctx := context.Background()
user := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
HomeDir: "invalid",
},
2020-08-11 21:56:10 +00:00
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
fs := vfs.NewOsFs("connID", user.HomeDir, "", nil)
2020-08-11 21:56:10 +00:00
connection := &Connection{
2021-07-24 18:11:17 +00:00
BaseConnection: common.NewBaseConnection(fs.ConnectionID(), common.ProtocolWebDAV, "", "", user),
2020-08-11 21:56:10 +00:00
}
err := connection.Mkdir(ctx, "", os.ModePerm)
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
err = connection.Rename(ctx, "oldName", "newName")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
_, err = connection.Stat(ctx, "name")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
err = connection.RemoveAll(ctx, "")
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
_, err = connection.OpenFile(ctx, "", 0, os.ModePerm)
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrGenericFailure.Error())
}
if runtime.GOOS != "windows" {
user.HomeDir = filepath.Clean(os.TempDir())
connection.User = user
fs := vfs.NewOsFs("connID", connection.User.HomeDir, "", nil)
2020-08-11 21:56:10 +00:00
subDir := "sub"
testTxtFile := "file.txt"
err = os.MkdirAll(filepath.Join(os.TempDir(), subDir, subDir), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(filepath.Join(os.TempDir(), subDir, subDir, testTxtFile), []byte("content"), os.ModePerm)
2020-08-11 21:56:10 +00:00
assert.NoError(t, err)
err = os.Chmod(filepath.Join(os.TempDir(), subDir, subDir), 0001)
assert.NoError(t, err)
err = os.WriteFile(filepath.Join(os.TempDir(), testTxtFile), []byte("test content"), os.ModePerm)
assert.NoError(t, err)
2020-08-11 21:56:10 +00:00
err = connection.Rename(ctx, testTxtFile, path.Join(subDir, subDir, testTxtFile))
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrPermissionDenied.Error())
}
_, err = connection.putFile(fs, filepath.Join(connection.User.HomeDir, subDir, subDir, testTxtFile),
2020-08-11 21:56:10 +00:00
path.Join(subDir, subDir, testTxtFile))
if assert.Error(t, err) {
assert.EqualError(t, err, common.ErrPermissionDenied.Error())
}
err = os.Chmod(filepath.Join(os.TempDir(), subDir, subDir), os.ModePerm)
assert.NoError(t, err)
err = os.RemoveAll(filepath.Join(os.TempDir(), subDir))
assert.NoError(t, err)
err = os.Remove(filepath.Join(os.TempDir(), testTxtFile))
assert.NoError(t, err)
2020-08-11 21:56:10 +00:00
}
}
func TestFileAccessErrors(t *testing.T) {
ctx := context.Background()
user := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
HomeDir: filepath.Clean(os.TempDir()),
},
2020-08-11 21:56:10 +00:00
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
fs := vfs.NewOsFs("connID", user.HomeDir, "", nil)
2020-08-11 21:56:10 +00:00
connection := &Connection{
2021-07-24 18:11:17 +00:00
BaseConnection: common.NewBaseConnection(fs.ConnectionID(), common.ProtocolWebDAV, "", "", user),
2020-08-11 21:56:10 +00:00
}
missingPath := "missing path"
fsMissingPath := filepath.Join(user.HomeDir, missingPath)
err := connection.RemoveAll(ctx, missingPath)
assert.ErrorIs(t, err, os.ErrNotExist)
davFile, err := connection.getFile(fs, fsMissingPath, missingPath)
assert.NoError(t, err)
buf := make([]byte, 64)
_, err = davFile.Read(buf)
assert.ErrorIs(t, err, os.ErrNotExist)
err = davFile.Close()
assert.ErrorIs(t, err, os.ErrNotExist)
2020-08-11 21:56:10 +00:00
p := filepath.Join(user.HomeDir, "adir", missingPath)
_, err = connection.handleUploadToNewFile(fs, p, p, path.Join("adir", missingPath))
assert.ErrorIs(t, err, os.ErrNotExist)
_, err = connection.handleUploadToExistingFile(fs, p, "_"+p, 0, path.Join("adir", missingPath))
2020-08-11 21:56:10 +00:00
if assert.Error(t, err) {
assert.ErrorIs(t, err, os.ErrNotExist)
2020-08-11 21:56:10 +00:00
}
fs = newMockOsFs(false, fs.ConnectionID(), user.HomeDir, nil, nil)
_, err = connection.handleUploadToExistingFile(fs, p, p, 0, path.Join("adir", missingPath))
assert.ErrorIs(t, err, os.ErrNotExist)
2020-08-11 21:56:10 +00:00
f, err := os.CreateTemp("", "temp")
2020-08-11 21:56:10 +00:00
assert.NoError(t, err)
err = f.Close()
assert.NoError(t, err)
davFile, err = connection.handleUploadToExistingFile(fs, f.Name(), f.Name(), 123, f.Name())
2020-08-11 21:56:10 +00:00
if assert.NoError(t, err) {
transfer := davFile.(*webDavFile)
transfers := connection.GetTransfers()
if assert.Equal(t, 1, len(transfers)) {
assert.Equal(t, transfers[0].ID, transfer.GetID())
assert.Equal(t, int64(123), transfer.InitialSize)
err = transfer.Close()
assert.NoError(t, err)
assert.Equal(t, 0, len(connection.GetTransfers()))
}
// test PROPPATCH date parsing error
pstats, err := transfer.Patch([]webdav.Proppatch{
{
Props: []webdav.Property{
{
XMLName: xml.Name{
Space: "DAV",
Local: "getlastmodified",
},
InnerXML: []byte(`Wid, 04 Nov 2020 13:25:51 GMT`),
},
},
},
})
assert.NoError(t, err)
for _, pstat := range pstats {
assert.Equal(t, http.StatusForbidden, pstat.Status)
}
2020-08-11 21:56:10 +00:00
err = os.Remove(f.Name())
assert.NoError(t, err)
// the file is deleted PROPPATCH should fail
pstats, err = transfer.Patch([]webdav.Proppatch{
{
Props: []webdav.Property{
{
XMLName: xml.Name{
Space: "DAV",
Local: "getlastmodified",
},
InnerXML: []byte(`Wed, 04 Nov 2020 13:25:51 GMT`),
},
},
},
})
assert.NoError(t, err)
for _, pstat := range pstats {
assert.Equal(t, http.StatusForbidden, pstat.Status)
}
}
2020-08-11 21:56:10 +00:00
}
func TestCheckRequestMethodWithPrefix(t *testing.T) {
user := dataprovider.User{
BaseUser: sdk.BaseUser{
HomeDir: filepath.Clean(os.TempDir()),
Permissions: map[string][]string{
"/": {dataprovider.PermAny},
},
},
}
fs := vfs.NewOsFs("connID", user.HomeDir, "", nil)
connection := &Connection{
BaseConnection: common.NewBaseConnection(fs.ConnectionID(), common.ProtocolWebDAV, "", "", user),
}
server := webDavServer{
binding: Binding{
Prefix: "/dav",
},
}
req, err := http.NewRequest(http.MethodGet, "/../dav", nil)
require.NoError(t, err)
server.checkRequestMethod(context.Background(), req, connection)
require.Equal(t, "PROPFIND", req.Method)
require.Equal(t, "1", req.Header.Get("Depth"))
}
2020-08-11 21:56:10 +00:00
func TestContentType(t *testing.T) {
user := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
HomeDir: filepath.Clean(os.TempDir()),
},
2020-08-11 21:56:10 +00:00
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
fs := vfs.NewOsFs("connID", user.HomeDir, "", nil)
2020-08-11 21:56:10 +00:00
connection := &Connection{
2021-07-24 18:11:17 +00:00
BaseConnection: common.NewBaseConnection(fs.ConnectionID(), common.ProtocolWebDAV, "", "", user),
2020-08-11 21:56:10 +00:00
}
testFilePath := filepath.Join(user.HomeDir, testFile)
ctx := context.Background()
baseTransfer := common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile+".unknown",
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
fs = newMockOsFs(false, fs.ConnectionID(), user.GetHomeDir(), nil, nil)
err := os.WriteFile(testFilePath, []byte(""), os.ModePerm)
2020-08-11 21:56:10 +00:00
assert.NoError(t, err)
davFile := newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = fs
fi, err := davFile.Stat()
2020-08-11 21:56:10 +00:00
if assert.NoError(t, err) {
ctype, err := fi.(*webDavFileInfo).ContentType(ctx)
2020-08-11 21:56:10 +00:00
assert.NoError(t, err)
assert.Equal(t, "application/custom-mime", ctype)
2020-08-11 21:56:10 +00:00
}
_, err = davFile.Readdir(-1)
assert.Error(t, err)
err = davFile.Close()
assert.NoError(t, err)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile+".unknown1",
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
davFile = newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = vfs.NewOsFs("id", user.HomeDir, "", nil)
fi, err = davFile.Stat()
if assert.NoError(t, err) {
ctype, err := fi.(*webDavFileInfo).ContentType(ctx)
assert.NoError(t, err)
assert.Equal(t, "text/plain; charset=utf-8", ctype)
}
err = davFile.Close()
assert.NoError(t, err)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
davFile = newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = vfs.NewOsFs("id", user.HomeDir, "", nil)
fi, err = davFile.Stat()
if assert.NoError(t, err) {
ctype, err := fi.(*webDavFileInfo).ContentType(ctx)
assert.NoError(t, err)
assert.Equal(t, "application/octet-stream", ctype)
}
err = davFile.Close()
assert.NoError(t, err)
for i := 0; i < 2; i++ {
// the second time the cache will be used
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile+".custom",
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
davFile = newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = vfs.NewOsFs("id", user.HomeDir, "", nil)
fi, err = davFile.Stat()
if assert.NoError(t, err) {
ctype, err := fi.(*webDavFileInfo).ContentType(ctx)
assert.NoError(t, err)
assert.Equal(t, "text/plain; charset=utf-8", ctype)
}
err = davFile.Close()
assert.NoError(t, err)
}
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile+".sftpgo",
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
fs = newMockOsFs(false, fs.ConnectionID(), user.GetHomeDir(), nil, os.ErrInvalid)
davFile = newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = fs
fi, err = davFile.Stat()
if assert.NoError(t, err) {
ctype, err := fi.(*webDavFileInfo).ContentType(ctx)
assert.NoError(t, err)
assert.Equal(t, "application/sftpgo", ctype)
}
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile+".unknown2",
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
fs = newMockOsFs(false, fs.ConnectionID(), user.GetHomeDir(), nil, os.ErrInvalid)
davFile = newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = fs
fi, err = davFile.Stat()
if assert.NoError(t, err) {
ctype, err := fi.(*webDavFileInfo).ContentType(ctx)
assert.EqualError(t, err, webdav.ErrNotImplemented.Error(), "unexpected content type %q", ctype)
}
cache := mimeCache{
maxSize: 10,
mimeTypes: map[string]string{},
}
cache.addMimeToCache("", "")
cache.RLock()
assert.Len(t, cache.mimeTypes, 0)
cache.RUnlock()
2020-08-11 21:56:10 +00:00
err = os.Remove(testFilePath)
assert.NoError(t, err)
}
func TestTransferReadWriteErrors(t *testing.T) {
user := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
HomeDir: filepath.Clean(os.TempDir()),
},
2020-08-11 21:56:10 +00:00
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
fs := vfs.NewOsFs("connID", user.HomeDir, "", nil)
2020-08-11 21:56:10 +00:00
connection := &Connection{
2021-07-24 18:11:17 +00:00
BaseConnection: common.NewBaseConnection(fs.ConnectionID(), common.ProtocolWebDAV, "", "", user),
2020-08-11 21:56:10 +00:00
}
testFilePath := filepath.Join(user.HomeDir, testFile)
baseTransfer := common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferUpload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
davFile := newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
p := make([]byte, 1)
_, err := davFile.Read(p)
assert.EqualError(t, err, common.ErrOpUnsupported.Error())
r, w, err := pipeat.Pipe()
assert.NoError(t, err)
davFile = newWebDavFile(baseTransfer, nil, r)
2020-08-11 21:56:10 +00:00
davFile.Connection.RemoveTransfer(davFile.BaseTransfer)
davFile = newWebDavFile(baseTransfer, vfs.NewPipeWriter(w), nil)
2020-08-11 21:56:10 +00:00
davFile.Connection.RemoveTransfer(davFile.BaseTransfer)
err = r.Close()
assert.NoError(t, err)
err = w.Close()
assert.NoError(t, err)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
_, err = davFile.Read(p)
assert.True(t, fs.IsNotExist(err))
2020-08-11 21:56:10 +00:00
_, err = davFile.Stat()
assert.True(t, fs.IsNotExist(err))
2020-08-11 21:56:10 +00:00
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
err = os.WriteFile(testFilePath, []byte(""), os.ModePerm)
2020-08-11 21:56:10 +00:00
assert.NoError(t, err)
f, err := os.Open(testFilePath)
if assert.NoError(t, err) {
err = f.Close()
assert.NoError(t, err)
}
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
davFile.reader = f
err = davFile.Close()
assert.EqualError(t, err, common.ErrGenericFailure.Error())
err = davFile.Close()
assert.EqualError(t, err, common.ErrTransferClosed.Error())
_, err = davFile.Read(p)
assert.Error(t, err)
info, err := davFile.Stat()
if assert.NoError(t, err) {
assert.Equal(t, int64(0), info.Size())
}
r, w, err = pipeat.Pipe()
assert.NoError(t, err)
mockFs := newMockOsFs(false, fs.ConnectionID(), user.HomeDir, r, nil)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, mockFs, dataprovider.TransferQuota{})
davFile = newWebDavFile(baseTransfer, nil, nil)
writeContent := []byte("content\r\n")
go func() {
n, err := w.Write(writeContent)
assert.NoError(t, err)
assert.Equal(t, len(writeContent), n)
err = w.Close()
assert.NoError(t, err)
}()
p = make([]byte, 64)
n, err := davFile.Read(p)
assert.EqualError(t, err, io.EOF.Error())
assert.Equal(t, len(writeContent), n)
err = davFile.Close()
assert.NoError(t, err)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
davFile.writer = f
err = davFile.Close()
assert.EqualError(t, err, common.ErrGenericFailure.Error())
err = os.Remove(testFilePath)
assert.NoError(t, err)
}
func TestTransferSeek(t *testing.T) {
user := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
HomeDir: filepath.Clean(os.TempDir()),
},
2020-08-11 21:56:10 +00:00
}
user.Permissions = make(map[string][]string)
user.Permissions["/"] = []string{dataprovider.PermAny}
fs := newMockOsFs(true, "connID", user.HomeDir, nil, nil)
2020-08-11 21:56:10 +00:00
connection := &Connection{
2021-07-24 18:11:17 +00:00
BaseConnection: common.NewBaseConnection(fs.ConnectionID(), common.ProtocolWebDAV, "", "", user),
2020-08-11 21:56:10 +00:00
}
testFilePath := filepath.Join(user.HomeDir, testFile)
testFileContents := []byte("content")
baseTransfer := common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferUpload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile := newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
_, err := davFile.Seek(0, io.SeekStart)
assert.EqualError(t, err, common.ErrOpUnsupported.Error())
err = davFile.Close()
assert.NoError(t, err)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
_, err = davFile.Seek(0, io.SeekCurrent)
assert.True(t, fs.IsNotExist(err))
2020-08-11 21:56:10 +00:00
davFile.Connection.RemoveTransfer(davFile.BaseTransfer)
err = os.WriteFile(testFilePath, testFileContents, os.ModePerm)
2020-08-11 21:56:10 +00:00
assert.NoError(t, err)
f, err := os.Open(testFilePath)
if assert.NoError(t, err) {
err = f.Close()
assert.NoError(t, err)
}
baseTransfer = common.NewBaseTransfer(f, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
_, err = davFile.Seek(0, io.SeekStart)
assert.Error(t, err)
davFile.Connection.RemoveTransfer(davFile.BaseTransfer)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
res, err := davFile.Seek(0, io.SeekStart)
assert.NoError(t, err)
assert.Equal(t, int64(0), res)
err = davFile.Close()
assert.NoError(t, err)
2020-08-11 21:56:10 +00:00
davFile.Connection.RemoveTransfer(davFile.BaseTransfer)
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
res, err = davFile.Seek(0, io.SeekEnd)
assert.NoError(t, err)
assert.Equal(t, int64(len(testFileContents)), res)
err = davFile.updateStatInfo()
assert.NoError(t, err)
err = davFile.Close()
assert.NoError(t, err)
2020-08-11 21:56:10 +00:00
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath+"1", testFilePath+"1", testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile = newWebDavFile(baseTransfer, nil, nil)
_, err = davFile.Seek(0, io.SeekEnd)
assert.True(t, fs.IsNotExist(err))
davFile.Connection.RemoveTransfer(davFile.BaseTransfer)
fs = vfs.NewOsFs(fs.ConnectionID(), user.GetHomeDir(), "", nil)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath+"1", testFilePath+"1", testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile = newWebDavFile(baseTransfer, nil, nil)
_, err = davFile.Seek(0, io.SeekEnd)
assert.True(t, fs.IsNotExist(err))
davFile.Connection.RemoveTransfer(davFile.BaseTransfer)
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath, testFilePath, testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile = newWebDavFile(baseTransfer, nil, nil)
2020-08-11 21:56:10 +00:00
davFile.reader = f
r, _, err := pipeat.Pipe()
assert.NoError(t, err)
davFile.Fs = newMockOsFs(true, fs.ConnectionID(), user.GetHomeDir(), r, nil)
2020-08-11 21:56:10 +00:00
res, err = davFile.Seek(2, io.SeekStart)
assert.NoError(t, err)
assert.Equal(t, int64(2), res)
err = davFile.Close()
assert.NoError(t, err)
2020-08-11 21:56:10 +00:00
r, _, err = pipeat.Pipe()
assert.NoError(t, err)
davFile = newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = newMockOsFs(true, fs.ConnectionID(), user.GetHomeDir(), r, nil)
2020-08-11 21:56:10 +00:00
res, err = davFile.Seek(2, io.SeekEnd)
assert.NoError(t, err)
assert.Equal(t, int64(5), res)
err = davFile.Close()
assert.NoError(t, err)
2020-08-11 21:56:10 +00:00
baseTransfer = common.NewBaseTransfer(nil, connection.BaseConnection, nil, testFilePath+"1", testFilePath+"1", testFile,
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{AllowedTotalSize: 100})
davFile = newWebDavFile(baseTransfer, nil, nil)
davFile.Fs = newMockOsFs(true, fs.ConnectionID(), user.GetHomeDir(), nil, nil)
2020-08-11 21:56:10 +00:00
res, err = davFile.Seek(2, io.SeekEnd)
assert.True(t, fs.IsNotExist(err))
2020-08-11 21:56:10 +00:00
assert.Equal(t, int64(0), res)
assert.Len(t, common.Connections.GetStats(""), 0)
2020-08-11 21:56:10 +00:00
err = os.Remove(testFilePath)
assert.NoError(t, err)
}
func TestBasicUsersCache(t *testing.T) {
username := "webdav_internal_test"
password := "pwd"
u := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
Username: username,
Password: password,
HomeDir: filepath.Join(os.TempDir(), username),
Status: 1,
ExpirationDate: 0,
},
}
u.Permissions = make(map[string][]string)
u.Permissions["/"] = []string{dataprovider.PermAny}
err := dataprovider.AddUser(&u, "", "", "")
assert.NoError(t, err)
user, err := dataprovider.UserExists(u.Username, "")
assert.NoError(t, err)
c := &Configuration{
Bindings: []Binding{
{
Port: 9000,
},
},
Cache: Cache{
Users: UsersCacheConfig{
MaxSize: 50,
ExpirationTime: 1,
},
},
}
dataprovider.InitializeWebDAVUserCache(c.Cache.Users.MaxSize)
2021-01-03 08:51:54 +00:00
server := webDavServer{
config: c,
binding: c.Bindings[0],
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user.Username), nil)
assert.NoError(t, err)
ipAddr := "127.0.0.1"
_, _, _, _, err = server.authenticate(req, ipAddr) //nolint:dogsled
assert.Error(t, err)
now := time.Now()
req.SetBasicAuth(username, password)
_, isCached, _, loginMethod, err := server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMethod)
// now the user should be cached
cachedUser, ok := dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.False(t, cachedUser.IsExpired())
assert.True(t, cachedUser.Expiration.After(now.Add(time.Duration(c.Cache.Users.ExpirationTime)*time.Minute)))
// authenticate must return the cached user now
authUser, isCached, _, _, err := server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.True(t, isCached)
assert.Equal(t, cachedUser.User, authUser)
}
// a wrong password must fail
req.SetBasicAuth(username, "wrong")
_, _, _, _, err = server.authenticate(req, ipAddr) //nolint:dogsled
assert.EqualError(t, err, dataprovider.ErrInvalidCredentials.Error())
req.SetBasicAuth(username, password)
// force cached user expiration
cachedUser.Expiration = now
dataprovider.CacheWebDAVUser(cachedUser)
cachedUser, ok = dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.True(t, cachedUser.IsExpired())
}
// now authenticate should get the user from the data provider and update the cache
_, isCached, _, loginMethod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMethod)
cachedUser, ok = dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.False(t, cachedUser.IsExpired())
}
// cache is not invalidated after a user modification if the fs does not change
err = dataprovider.UpdateUser(&user, "", "", "")
assert.NoError(t, err)
_, ok = dataprovider.GetCachedWebDAVUser(username)
assert.True(t, ok)
folderName := "testFolder"
user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
BaseVirtualFolder: vfs.BaseVirtualFolder{
Name: folderName,
MappedPath: filepath.Join(os.TempDir(), "mapped"),
},
VirtualPath: "/vdir",
})
err = dataprovider.UpdateUser(&user, "", "", "")
assert.NoError(t, err)
_, ok = dataprovider.GetCachedWebDAVUser(username)
assert.False(t, ok)
_, isCached, _, loginMethod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMethod)
_, ok = dataprovider.GetCachedWebDAVUser(username)
assert.True(t, ok)
// cache is invalidated after user deletion
err = dataprovider.DeleteUser(user.Username, "", "", "")
assert.NoError(t, err)
_, ok = dataprovider.GetCachedWebDAVUser(username)
assert.False(t, ok)
err = dataprovider.DeleteFolder(folderName, "", "", "")
assert.NoError(t, err)
err = os.RemoveAll(u.GetHomeDir())
assert.NoError(t, err)
}
func TestCachedUserWithFolders(t *testing.T) {
username := "webdav_internal_folder_test"
password := "dav_pwd"
folderName := "test_folder"
u := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
Username: username,
Password: password,
HomeDir: filepath.Join(os.TempDir(), username),
Status: 1,
ExpirationDate: 0,
},
}
u.Permissions = make(map[string][]string)
u.Permissions["/"] = []string{dataprovider.PermAny}
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
BaseVirtualFolder: vfs.BaseVirtualFolder{
Name: folderName,
MappedPath: filepath.Join(os.TempDir(), folderName),
},
VirtualPath: "/vpath",
})
err := dataprovider.AddUser(&u, "", "", "")
assert.NoError(t, err)
user, err := dataprovider.UserExists(u.Username, "")
assert.NoError(t, err)
c := &Configuration{
Bindings: []Binding{
{
Port: 9000,
},
},
Cache: Cache{
Users: UsersCacheConfig{
MaxSize: 50,
ExpirationTime: 1,
},
},
}
dataprovider.InitializeWebDAVUserCache(c.Cache.Users.MaxSize)
server := webDavServer{
config: c,
binding: c.Bindings[0],
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user.Username), nil)
assert.NoError(t, err)
ipAddr := "127.0.0.1"
_, _, _, _, err = server.authenticate(req, ipAddr) //nolint:dogsled
assert.Error(t, err)
now := time.Now()
req.SetBasicAuth(username, password)
_, isCached, _, loginMethod, err := server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMethod)
// now the user should be cached
cachedUser, ok := dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.False(t, cachedUser.IsExpired())
assert.True(t, cachedUser.Expiration.After(now.Add(time.Duration(c.Cache.Users.ExpirationTime)*time.Minute)))
// authenticate must return the cached user now
authUser, isCached, _, _, err := server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.True(t, isCached)
assert.Equal(t, cachedUser.User, authUser)
}
folder, err := dataprovider.GetFolderByName(folderName)
assert.NoError(t, err)
// updating a used folder should invalidate the cache only if the fs changed
err = dataprovider.UpdateFolder(&folder, folder.Users, folder.Groups, "", "", "")
assert.NoError(t, err)
_, isCached, _, loginMethod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.True(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMethod)
cachedUser, ok = dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.False(t, cachedUser.IsExpired())
}
// changing the folder path should invalidate the cache
folder.MappedPath = filepath.Join(os.TempDir(), "anotherpath")
err = dataprovider.UpdateFolder(&folder, folder.Users, folder.Groups, "", "", "")
assert.NoError(t, err)
_, isCached, _, loginMethod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMethod)
cachedUser, ok = dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.False(t, cachedUser.IsExpired())
}
err = dataprovider.DeleteFolder(folderName, "", "", "")
assert.NoError(t, err)
// removing a used folder should invalidate the cache
_, isCached, _, loginMethod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMethod)
cachedUser, ok = dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.False(t, cachedUser.IsExpired())
}
err = dataprovider.DeleteUser(user.Username, "", "", "")
assert.NoError(t, err)
_, ok = dataprovider.GetCachedWebDAVUser(username)
assert.False(t, ok)
err = os.RemoveAll(u.GetHomeDir())
assert.NoError(t, err)
err = os.RemoveAll(folder.MappedPath)
assert.NoError(t, err)
}
func TestUsersCacheSizeAndExpiration(t *testing.T) {
username := "webdav_internal_test"
password := "pwd"
u := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
HomeDir: filepath.Join(os.TempDir(), username),
Status: 1,
ExpirationDate: 0,
},
}
u.Username = username + "1"
u.Password = password + "1"
u.Permissions = make(map[string][]string)
u.Permissions["/"] = []string{dataprovider.PermAny}
err := dataprovider.AddUser(&u, "", "", "")
assert.NoError(t, err)
user1, err := dataprovider.UserExists(u.Username, "")
assert.NoError(t, err)
u.Username = username + "2"
u.Password = password + "2"
err = dataprovider.AddUser(&u, "", "", "")
assert.NoError(t, err)
user2, err := dataprovider.UserExists(u.Username, "")
assert.NoError(t, err)
u.Username = username + "3"
u.Password = password + "3"
err = dataprovider.AddUser(&u, "", "", "")
assert.NoError(t, err)
user3, err := dataprovider.UserExists(u.Username, "")
assert.NoError(t, err)
u.Username = username + "4"
u.Password = password + "4"
err = dataprovider.AddUser(&u, "", "", "")
assert.NoError(t, err)
user4, err := dataprovider.UserExists(u.Username, "")
assert.NoError(t, err)
c := &Configuration{
Bindings: []Binding{
{
Port: 9000,
},
},
Cache: Cache{
Users: UsersCacheConfig{
MaxSize: 3,
ExpirationTime: 1,
},
},
}
dataprovider.InitializeWebDAVUserCache(c.Cache.Users.MaxSize)
2021-01-03 08:51:54 +00:00
server := webDavServer{
config: c,
binding: c.Bindings[0],
}
ipAddr := "127.0.1.1"
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user1.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user1.Username, password+"1")
_, isCached, _, loginMehod, err := server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user2.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user2.Username, password+"2")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user3.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user3.Username, password+"3")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
// the first 3 users are now cached
_, ok := dataprovider.GetCachedWebDAVUser(user1.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user2.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user3.Username)
assert.True(t, ok)
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user4.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user4.Username, password+"4")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
// user1, the first cached, should be removed now
_, ok = dataprovider.GetCachedWebDAVUser(user1.Username)
assert.False(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user2.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user3.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user4.Username)
assert.True(t, ok)
// a sleep ensures that expiration times are different
time.Sleep(20 * time.Millisecond)
// user1 logins, user2 should be removed
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user1.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user1.Username, password+"1")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
_, ok = dataprovider.GetCachedWebDAVUser(user2.Username)
assert.False(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user1.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user3.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user4.Username)
assert.True(t, ok)
// a sleep ensures that expiration times are different
time.Sleep(20 * time.Millisecond)
// user2 logins, user3 should be removed
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user2.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user2.Username, password+"2")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
_, ok = dataprovider.GetCachedWebDAVUser(user3.Username)
assert.False(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user1.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user2.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user4.Username)
assert.True(t, ok)
// a sleep ensures that expiration times are different
time.Sleep(20 * time.Millisecond)
// user3 logins, user4 should be removed
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user3.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user3.Username, password+"3")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
_, ok = dataprovider.GetCachedWebDAVUser(user4.Username)
assert.False(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user1.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user2.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user3.Username)
assert.True(t, ok)
// now remove user1 after an update
user1.HomeDir += "_mod"
err = dataprovider.UpdateUser(&user1, "", "", "")
assert.NoError(t, err)
_, ok = dataprovider.GetCachedWebDAVUser(user1.Username)
assert.False(t, ok)
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user4.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user4.Username, password+"4")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
// a sleep ensures that expiration times are different
time.Sleep(20 * time.Millisecond)
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/%v", user1.Username), nil)
assert.NoError(t, err)
req.SetBasicAuth(user1.Username, password+"1")
_, isCached, _, loginMehod, err = server.authenticate(req, ipAddr)
assert.NoError(t, err)
assert.False(t, isCached)
assert.Equal(t, dataprovider.LoginMethodPassword, loginMehod)
_, ok = dataprovider.GetCachedWebDAVUser(user2.Username)
assert.False(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user1.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user3.Username)
assert.True(t, ok)
_, ok = dataprovider.GetCachedWebDAVUser(user4.Username)
assert.True(t, ok)
err = dataprovider.DeleteUser(user1.Username, "", "", "")
assert.NoError(t, err)
err = dataprovider.DeleteUser(user2.Username, "", "", "")
assert.NoError(t, err)
err = dataprovider.DeleteUser(user3.Username, "", "", "")
assert.NoError(t, err)
err = dataprovider.DeleteUser(user4.Username, "", "", "")
assert.NoError(t, err)
err = os.RemoveAll(u.GetHomeDir())
assert.NoError(t, err)
}
func TestUserCacheIsolation(t *testing.T) {
dataprovider.InitializeWebDAVUserCache(10)
username := "webdav_internal_cache_test"
password := "dav_pwd"
u := dataprovider.User{
2021-07-11 13:26:51 +00:00
BaseUser: sdk.BaseUser{
Username: username,
Password: password,
HomeDir: filepath.Join(os.TempDir(), username),
Status: 1,
ExpirationDate: 0,
},
}
u.Permissions = make(map[string][]string)
u.Permissions["/"] = []string{dataprovider.PermAny}
err := dataprovider.AddUser(&u, "", "", "")
assert.NoError(t, err)
user, err := dataprovider.UserExists(u.Username, "")
assert.NoError(t, err)
cachedUser := &dataprovider.CachedUser{
User: user,
Expiration: time.Now().Add(24 * time.Hour),
Password: password,
LockSystem: webdav.NewMemLS(),
}
cachedUser.User.FsConfig.S3Config.AccessSecret = kms.NewPlainSecret("test secret")
err = cachedUser.User.FsConfig.S3Config.AccessSecret.Encrypt()
assert.NoError(t, err)
dataprovider.CacheWebDAVUser(cachedUser)
cachedUser, ok := dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
_, err = cachedUser.User.GetFilesystem("")
assert.NoError(t, err)
// the filesystem is now cached
}
cachedUser, ok = dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
assert.True(t, cachedUser.User.FsConfig.S3Config.AccessSecret.IsEncrypted())
err = cachedUser.User.FsConfig.S3Config.AccessSecret.Decrypt()
assert.NoError(t, err)
2021-07-11 13:26:51 +00:00
cachedUser.User.FsConfig.Provider = sdk.S3FilesystemProvider
_, err = cachedUser.User.GetFilesystem("")
assert.Error(t, err, "we don't have to get the previously cached filesystem!")
}
cachedUser, ok = dataprovider.GetCachedWebDAVUser(username)
if assert.True(t, ok) {
2021-07-11 13:26:51 +00:00
assert.Equal(t, sdk.LocalFilesystemProvider, cachedUser.User.FsConfig.Provider)
assert.False(t, cachedUser.User.FsConfig.S3Config.AccessSecret.IsEncrypted())
}
err = dataprovider.DeleteUser(username, "", "", "")
assert.NoError(t, err)
_, ok = dataprovider.GetCachedWebDAVUser(username)
assert.False(t, ok)
}
func TestRecoverer(t *testing.T) {
c := &Configuration{
Bindings: []Binding{
{
Port: 9000,
},
},
}
2021-01-03 08:51:54 +00:00
server := webDavServer{
config: c,
binding: c.Bindings[0],
}
rr := httptest.NewRecorder()
server.ServeHTTP(rr, nil)
assert.Equal(t, http.StatusInternalServerError, rr.Code)
}
func TestMimeCache(t *testing.T) {
cache := mimeCache{
maxSize: 0,
mimeTypes: make(map[string]string),
}
cache.addMimeToCache(".zip", "application/zip")
mtype := cache.getMimeFromCache(".zip")
assert.Equal(t, "", mtype)
cache.maxSize = 1
cache.addMimeToCache(".zip", "application/zip")
mtype = cache.getMimeFromCache(".zip")
assert.Equal(t, "application/zip", mtype)
cache.addMimeToCache(".jpg", "image/jpeg")
mtype = cache.getMimeFromCache(".jpg")
assert.Equal(t, "", mtype)
}
func TestVerifyTLSConnection(t *testing.T) {
oldCertMgr := certMgr
caCrlPath := filepath.Join(os.TempDir(), "testcrl.crt")
certPath := filepath.Join(os.TempDir(), "test.crt")
keyPath := filepath.Join(os.TempDir(), "test.key")
err := os.WriteFile(caCrlPath, []byte(caCRL), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(certPath, []byte(webDavCert), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(keyPath, []byte(webDavKey), os.ModePerm)
assert.NoError(t, err)
keyPairs := []common.TLSKeyPair{
{
Cert: certPath,
Key: keyPath,
ID: common.DefaultTLSKeyPaidID,
},
}
certMgr, err = common.NewCertManager(keyPairs, "", "webdav_test")
assert.NoError(t, err)
certMgr.SetCARevocationLists([]string{caCrlPath})
err = certMgr.LoadCRLs()
assert.NoError(t, err)
crt, err := tls.X509KeyPair([]byte(client1Crt), []byte(client1Key))
assert.NoError(t, err)
x509crt, err := x509.ParseCertificate(crt.Certificate[0])
assert.NoError(t, err)
server := webDavServer{}
state := tls.ConnectionState{
PeerCertificates: []*x509.Certificate{x509crt},
}
err = server.verifyTLSConnection(state)
assert.Error(t, err) // no verified certification chain
crt, err = tls.X509KeyPair([]byte(caCRT), []byte(caKey))
assert.NoError(t, err)
x509CAcrt, err := x509.ParseCertificate(crt.Certificate[0])
assert.NoError(t, err)
state.VerifiedChains = append(state.VerifiedChains, []*x509.Certificate{x509crt, x509CAcrt})
err = server.verifyTLSConnection(state)
assert.NoError(t, err)
crt, err = tls.X509KeyPair([]byte(client2Crt), []byte(client2Key))
assert.NoError(t, err)
x509crtRevoked, err := x509.ParseCertificate(crt.Certificate[0])
assert.NoError(t, err)
state.VerifiedChains = append(state.VerifiedChains, []*x509.Certificate{x509crtRevoked, x509CAcrt})
state.PeerCertificates = []*x509.Certificate{x509crtRevoked}
err = server.verifyTLSConnection(state)
assert.EqualError(t, err, common.ErrCrtRevoked.Error())
err = os.Remove(caCrlPath)
assert.NoError(t, err)
err = os.Remove(certPath)
assert.NoError(t, err)
err = os.Remove(keyPath)
assert.NoError(t, err)
certMgr = oldCertMgr
}
func TestMisc(t *testing.T) {
oldCertMgr := certMgr
certMgr = nil
err := ReloadCertificateMgr()
assert.Nil(t, err)
val := getConfigPath("", ".")
assert.Empty(t, val)
certMgr = oldCertMgr
}
func TestParseTime(t *testing.T) {
res, err := parseTime("Sat, 4 Feb 2023 17:00:50 GMT")
require.NoError(t, err)
require.Equal(t, int64(1675530050), res.Unix())
res, err = parseTime("Wed, 04 Nov 2020 13:25:51 GMT")
require.NoError(t, err)
require.Equal(t, int64(1604496351), res.Unix())
}
func TestConfigsFromProvider(t *testing.T) {
configDir := "."
err := dataprovider.UpdateConfigs(nil, "", "", "")
assert.NoError(t, err)
c := Configuration{
Bindings: []Binding{
{
Port: 1234,
},
},
}
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
configs := dataprovider.Configs{
ACME: &dataprovider.ACMEConfigs{
Domain: "domain.com",
Email: "info@domain.com",
HTTP01Challenge: dataprovider.ACMEHTTP01Challenge{Port: 80},
Protocols: 7,
},
}
err = dataprovider.UpdateConfigs(&configs, "", "", "")
assert.NoError(t, err)
util.CertsBasePath = ""
// crt and key empty
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
util.CertsBasePath = filepath.Clean(os.TempDir())
// crt not found
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
keyPairs := c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 0)
crtPath := filepath.Join(util.CertsBasePath, util.SanitizeDomain(configs.ACME.Domain)+".crt")
err = os.WriteFile(crtPath, nil, 0666)
assert.NoError(t, err)
// key not found
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
keyPairs = c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 0)
keyPath := filepath.Join(util.CertsBasePath, util.SanitizeDomain(configs.ACME.Domain)+".key")
err = os.WriteFile(keyPath, nil, 0666)
assert.NoError(t, err)
// acme cert used
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Equal(t, configs.ACME.Domain, c.acmeDomain)
keyPairs = c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 1)
assert.True(t, c.Bindings[0].EnableHTTPS)
// protocols does not match
configs.ACME.Protocols = 3
err = dataprovider.UpdateConfigs(&configs, "", "", "")
assert.NoError(t, err)
c.acmeDomain = ""
err = c.loadFromProvider()
assert.NoError(t, err)
assert.Empty(t, c.acmeDomain)
keyPairs = c.getKeyPairs(configDir)
assert.Len(t, keyPairs, 0)
err = os.Remove(crtPath)
assert.NoError(t, err)
err = os.Remove(keyPath)
assert.NoError(t, err)
util.CertsBasePath = ""
err = dataprovider.UpdateConfigs(nil, "", "", "")
assert.NoError(t, err)
}