2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// 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
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-26 09:34:44 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-17 21:29:08 +00:00
|
|
|
"bytes"
|
2019-08-08 19:42:07 +00:00
|
|
|
"context"
|
2021-01-19 17:59:41 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2021-01-17 21:29:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-07-26 09:34:44 +00:00
|
|
|
"fmt"
|
2019-10-09 09:48:54 +00:00
|
|
|
"html/template"
|
2021-11-30 17:40:50 +00:00
|
|
|
"io"
|
2023-12-10 15:40:13 +00:00
|
|
|
"io/fs"
|
2019-07-26 09:34:44 +00:00
|
|
|
"net/http"
|
2019-08-08 19:42:07 +00:00
|
|
|
"net/http/httptest"
|
2020-01-31 18:04:00 +00:00
|
|
|
"net/url"
|
2020-01-19 06:41:05 +00:00
|
|
|
"os"
|
2021-01-17 21:29:08 +00:00
|
|
|
"path"
|
2021-01-19 17:59:41 +00:00
|
|
|
"path/filepath"
|
2020-02-03 23:08:00 +00:00
|
|
|
"runtime"
|
2020-01-31 18:04:00 +00:00
|
|
|
"strings"
|
2019-07-26 09:34:44 +00:00
|
|
|
"testing"
|
2021-01-17 21:29:08 +00:00
|
|
|
"time"
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-01-04 08:48:16 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2021-03-05 17:50:45 +00:00
|
|
|
"github.com/go-chi/jwtauth/v5"
|
2021-05-30 21:07:46 +00:00
|
|
|
"github.com/klauspost/compress/zip"
|
2022-11-16 18:04:50 +00:00
|
|
|
"github.com/lestrrat-go/jwx/v2/jwa"
|
|
|
|
"github.com/lestrrat-go/jwx/v2/jwt"
|
2021-02-03 07:55:28 +00:00
|
|
|
"github.com/rs/xid"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2023-06-03 14:17:32 +00:00
|
|
|
sdkkms "github.com/sftpgo/sdk/kms"
|
2023-05-12 16:34:59 +00:00
|
|
|
"github.com/sftpgo/sdk/plugin/notifier"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-29 18:02:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2023-02-24 19:08:14 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/acme"
|
2022-07-24 14:18:54 +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/plugin"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2019-08-03 11:19:00 +00:00
|
|
|
)
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
const (
|
|
|
|
httpdCert = `-----BEGIN CERTIFICATE-----
|
|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eTLWVbt9NpwJwB6DhPaWjalAug9HIiUjktiB
|
|
|
|
GcEYiQnBWn77X3DATOA8clAa/Yt9m2HKJIHkU1IV3ESZe+8Fh955PozJJlHu3yVb
|
|
|
|
Ap157PUHTriSnxyMF2Sb3EhX/rQkmbnbCqqygHC14iBy8MrKzLG00X6BelZV5n0D
|
|
|
|
8d85dwKBgGWY2nsaemPH/TiTVF6kW1IKSQoIyJChkngc+Xj/2aCCkkmAEn8eqncl
|
|
|
|
RKjnkiEZeG4+G91Xu7+HmcBLwV86k5I+tXK9O1Okomr6Zry8oqVcxU5TB6VRS+rA
|
|
|
|
ubwF00Drdvk2+kDZfxIM137nBiy7wgCJi2Ksm5ihN3dUF6Q0oNPl
|
2021-01-19 17:59:41 +00:00
|
|
|
-----END RSA PRIVATE KEY-----`
|
2022-02-27 12:08:47 +00:00
|
|
|
defaultAdminUsername = "admin"
|
2024-03-11 17:19:57 +00:00
|
|
|
defeaultUsername = "test_user"
|
2021-01-19 17:59:41 +00:00
|
|
|
)
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
var (
|
|
|
|
configDir = filepath.Join(".", "..", "..")
|
|
|
|
)
|
|
|
|
|
2021-05-30 21:07:46 +00:00
|
|
|
type failingWriter struct {
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:02:54 +00:00
|
|
|
func (r *failingWriter) Write(_ []byte) (n int, err error) {
|
2021-05-30 21:07:46 +00:00
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:02:54 +00:00
|
|
|
func (r *failingWriter) WriteHeader(_ int) {}
|
2021-05-30 21:07:46 +00:00
|
|
|
|
|
|
|
func (r *failingWriter) Header() http.Header {
|
|
|
|
return make(http.Header)
|
|
|
|
}
|
|
|
|
|
2020-12-29 18:02:56 +00:00
|
|
|
func TestShouldBind(t *testing.T) {
|
|
|
|
c := Conf{
|
2021-01-19 17:59:41 +00:00
|
|
|
Bindings: []Binding{
|
|
|
|
{
|
|
|
|
Port: 10000,
|
|
|
|
},
|
|
|
|
},
|
2020-12-29 18:02:56 +00:00
|
|
|
}
|
2022-09-22 15:27:00 +00:00
|
|
|
require.False(t, c.ShouldBind())
|
|
|
|
c.Bindings[0].EnableRESTAPI = true
|
2020-12-29 18:02:56 +00:00
|
|
|
require.True(t, c.ShouldBind())
|
|
|
|
|
2021-01-19 17:59:41 +00:00
|
|
|
c.Bindings[0].Port = 0
|
2020-12-29 18:02:56 +00:00
|
|
|
require.False(t, c.ShouldBind())
|
|
|
|
|
|
|
|
if runtime.GOOS != osWindows {
|
2021-01-19 17:59:41 +00:00
|
|
|
c.Bindings[0].Address = "/absolute/path"
|
2020-12-29 18:02:56 +00:00
|
|
|
require.True(t, c.ShouldBind())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:40:52 +00:00
|
|
|
func TestBrandingValidation(t *testing.T) {
|
2022-03-19 20:44:27 +00:00
|
|
|
b := Binding{
|
2022-05-13 17:40:52 +00:00
|
|
|
Branding: Branding{
|
|
|
|
WebAdmin: UIBranding{
|
|
|
|
LogoPath: "path1",
|
|
|
|
LoginImagePath: "login1.png",
|
2023-11-05 11:24:26 +00:00
|
|
|
DefaultCSS: []string{"my.css"},
|
2022-03-19 20:44:27 +00:00
|
|
|
},
|
2022-05-13 17:40:52 +00:00
|
|
|
WebClient: UIBranding{
|
|
|
|
FaviconPath: "favicon1.ico",
|
|
|
|
DisclaimerPath: "../path2",
|
|
|
|
ExtraCSS: []string{"1.css"},
|
2022-03-19 20:44:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-05-13 17:40:52 +00:00
|
|
|
b.checkBranding()
|
|
|
|
assert.Equal(t, "/favicon.ico", b.Branding.WebAdmin.FaviconPath)
|
|
|
|
assert.Equal(t, "/path1", b.Branding.WebAdmin.LogoPath)
|
|
|
|
assert.Equal(t, "/login1.png", b.Branding.WebAdmin.LoginImagePath)
|
2023-11-05 11:24:26 +00:00
|
|
|
assert.Equal(t, []string{"/my.css"}, b.Branding.WebAdmin.DefaultCSS)
|
2022-05-13 17:40:52 +00:00
|
|
|
assert.Len(t, b.Branding.WebAdmin.ExtraCSS, 0)
|
|
|
|
assert.Equal(t, "/favicon1.ico", b.Branding.WebClient.FaviconPath)
|
2023-12-28 18:59:06 +00:00
|
|
|
assert.Equal(t, path.Join(webStaticFilesPath, "/path2"), b.Branding.WebClient.DisclaimerPath)
|
2022-05-13 17:40:52 +00:00
|
|
|
assert.Equal(t, "/img/login_image.png", b.Branding.WebClient.LoginImagePath)
|
|
|
|
if assert.Len(t, b.Branding.WebClient.ExtraCSS, 1) {
|
|
|
|
assert.Equal(t, "/1.css", b.Branding.WebClient.ExtraCSS[0])
|
|
|
|
}
|
2023-12-28 18:59:06 +00:00
|
|
|
b.Branding.WebAdmin.DisclaimerPath = "https://example.com"
|
|
|
|
b.checkBranding()
|
|
|
|
assert.Equal(t, "https://example.com", b.Branding.WebAdmin.DisclaimerPath)
|
2022-03-19 20:44:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 12:08:47 +00:00
|
|
|
func TestRedactedConf(t *testing.T) {
|
|
|
|
c := Conf{
|
|
|
|
SigningPassphrase: "passphrase",
|
|
|
|
Setup: SetupConfig{
|
|
|
|
InstallationCode: "123",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
redactedField := "[redacted]"
|
|
|
|
redactedConf := c.getRedacted()
|
|
|
|
assert.Equal(t, redactedField, redactedConf.SigningPassphrase)
|
|
|
|
assert.Equal(t, redactedField, redactedConf.Setup.InstallationCode)
|
|
|
|
assert.NotEqual(t, c.SigningPassphrase, redactedConf.SigningPassphrase)
|
|
|
|
assert.NotEqual(t, c.Setup.InstallationCode, redactedConf.Setup.InstallationCode)
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:34:44 +00:00
|
|
|
func TestGetRespStatus(t *testing.T) {
|
|
|
|
var err error
|
2021-07-11 13:26:51 +00:00
|
|
|
err = util.NewMethodDisabledError("")
|
2019-07-26 09:34:44 +00:00
|
|
|
respStatus := getRespStatus(err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, respStatus)
|
2019-07-26 09:34:44 +00:00
|
|
|
err = fmt.Errorf("generic error")
|
|
|
|
respStatus = getRespStatus(err)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, respStatus)
|
2021-10-23 13:47:21 +00:00
|
|
|
respStatus = getRespStatus(plugin.ErrNoSearcher)
|
|
|
|
assert.Equal(t, http.StatusNotImplemented, respStatus)
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
func TestMappedStatusCode(t *testing.T) {
|
|
|
|
err := os.ErrPermission
|
|
|
|
code := getMappedStatusCode(err)
|
|
|
|
assert.Equal(t, http.StatusForbidden, code)
|
|
|
|
err = os.ErrNotExist
|
|
|
|
code = getMappedStatusCode(err)
|
|
|
|
assert.Equal(t, http.StatusNotFound, code)
|
2021-11-22 11:25:36 +00:00
|
|
|
err = common.ErrQuotaExceeded
|
|
|
|
code = getMappedStatusCode(err)
|
|
|
|
assert.Equal(t, http.StatusRequestEntityTooLarge, code)
|
2021-06-05 14:07:09 +00:00
|
|
|
err = os.ErrClosed
|
|
|
|
code = getMappedStatusCode(err)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, code)
|
2023-02-27 10:03:05 +00:00
|
|
|
err = &http.MaxBytesError{}
|
|
|
|
code = getMappedStatusCode(err)
|
|
|
|
assert.Equal(t, http.StatusRequestEntityTooLarge, code)
|
2021-06-05 14:07:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestGCSWebInvalidFormFile(t *testing.T) {
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", "test_username")
|
|
|
|
form.Set("fs_provider", "2")
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, webUserPath, strings.NewReader(form.Encode()))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
err := req.ParseForm()
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
_, err = getFsConfigFromPostFields(req)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.EqualError(t, err, http.ErrNotMultipart.Error())
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestInvalidToken(t *testing.T) {
|
2022-03-19 20:44:27 +00:00
|
|
|
server := httpdServer{}
|
|
|
|
server.initializeRouter()
|
2021-01-17 21:29:08 +00:00
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "admin",
|
|
|
|
}
|
|
|
|
errFake := errors.New("fake error")
|
|
|
|
asJSON, err := json.Marshal(admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ := http.NewRequest(http.MethodPut, path.Join(adminPath, admin.Username), bytes.NewBuffer(asJSON))
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("username", admin.Username)
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), jwtauth.ErrorCtxKey, errFake))
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
updateAdmin(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteAdmin(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
adminPwd := pwdChange{
|
2021-01-17 21:29:08 +00:00
|
|
|
CurrentPassword: "old",
|
|
|
|
NewPassword: "new",
|
|
|
|
}
|
|
|
|
asJSON, err = json.Marshal(adminPwd)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, "", bytes.NewBuffer(asJSON))
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), jwtauth.ErrorCtxKey, errFake))
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
changeAdminPassword(rr, req)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
adm := getAdminFromToken(req)
|
|
|
|
assert.Empty(t, adm.Username)
|
2021-06-05 14:07:09 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
readUserFolder(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getUserFile(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getUserFilesAsZipStream(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getShares(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getShareByID(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addShare(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateShare(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteShare(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
generateTOTPSecret(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
saveTOTPConfig(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getRecoveryCodes(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
generateRecoveryCodes(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2021-09-06 16:46:35 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2021-09-29 16:46:15 +00:00
|
|
|
getUserProfile(rr, req)
|
2021-09-06 16:46:35 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2021-09-29 16:46:15 +00:00
|
|
|
updateUserProfile(rr, req)
|
2021-09-06 16:46:35 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2024-03-11 17:19:57 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getWebTask(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2021-09-06 16:46:35 +00:00
|
|
|
rr = httptest.NewRecorder()
|
2021-09-29 16:46:15 +00:00
|
|
|
getAdminProfile(rr, req)
|
2021-09-06 16:46:35 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2021-09-29 16:46:15 +00:00
|
|
|
updateAdminProfile(rr, req)
|
2021-09-06 16:46:35 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2021-09-04 10:11:04 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
loadData(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
loadDataFromRequest(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addUser(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
disableUser2FA(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateUser(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteUser(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
2022-09-25 17:48:55 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getActiveConnections(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
2021-10-10 11:08:05 +00:00
|
|
|
|
2022-10-02 07:51:47 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
handleCloseConnection(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebRestore(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-10-10 11:08:05 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebAddUserPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-10-10 11:08:05 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebUpdateUserPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-10-10 11:08:05 +00:00
|
|
|
|
2022-01-12 18:01:19 +00:00
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebTemplateFolderPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-01-12 18:01:19 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebTemplateUserPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getAllAdmins(rr, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getAllUsers(rr, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-01-12 18:01:19 +00:00
|
|
|
|
2022-07-24 06:10:23 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addFolder(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateFolder(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2023-04-18 16:11:23 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getFolderByName(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteFolder(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-07-24 06:10:23 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAddFolderPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-07-24 06:10:23 +00:00
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebUpdateFolderPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-10-10 11:08:05 +00:00
|
|
|
|
2022-10-02 07:51:47 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebGetConnections(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-10-02 07:51:47 +00:00
|
|
|
|
2023-02-19 18:03:45 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebConfigsPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2023-02-19 18:03:45 +00:00
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addAdmin(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
disableAdmin2FA(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addAPIKey(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateAPIKey(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteAPIKey(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addGroup(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateGroup(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2023-04-18 16:11:23 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getGroupByName(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteGroup(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addEventAction(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2023-04-18 16:11:23 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getEventActionByName(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateEventAction(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteEventAction(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2023-04-18 16:11:23 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getEventRuleByName(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addEventRule(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateEventRule(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteEventRule(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getUsersQuotaScans(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateUserTransferQuotaUsage(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
doUpdateUserQuotaUsage(rr, req, "", quotaUsage{})
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
doStartUserQuotaScan(rr, req, "")
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getRetentionChecks(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addRole(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateRole(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteRole(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getUsers(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
getUserByUsername(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
searchFsEvents(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
searchProviderEvents(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2023-05-12 16:34:59 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
searchLogEvents(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
addIPListEntry(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
updateIPListEntry(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
deleteIPListEntry(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleGetWebUsers(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-11-16 18:04:50 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebUpdateUserGet(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-11-16 18:04:50 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebUpdateRolePost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-11-16 18:04:50 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAddRolePost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-11-16 18:04:50 +00:00
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebAddAdminPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-10-10 11:08:05 +00:00
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAddGroupPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-04-25 13:49:11 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebUpdateGroupPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-04-25 13:49:11 +00:00
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAddEventActionPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-07-11 06:17:36 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebUpdateEventActionPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-07-11 06:17:36 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAddEventRulePost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-07-11 06:17:36 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebUpdateEventRulePost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-07-11 06:17:36 +00:00
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebUpdateIPListEntryPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2023-02-09 08:33:33 +00:00
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebClientTwoFactorRecoveryPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, rr.Code)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebClientTwoFactorPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, rr.Code)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAdminTwoFactorRecoveryPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, rr.Code)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAdminTwoFactorPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, rr.Code)
|
2023-02-09 08:33:33 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebUpdateIPListEntryPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2023-02-09 08:33:33 +00:00
|
|
|
|
|
|
|
form := make(url.Values)
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webIPListPath+"/1", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rctx = chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("type", "1")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAddIPListEntryPost(rr, req)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code, rr.Body.String())
|
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2020-02-19 21:39:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestUpdateWebAdminInvalidClaims(t *testing.T) {
|
|
|
|
server := httpdServer{}
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "",
|
|
|
|
Password: "password",
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: admin.Username,
|
|
|
|
Permissions: admin.Permissions,
|
|
|
|
Signature: admin.GetSignature(),
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2022-03-24 21:03:17 +00:00
|
|
|
token, err := c.createTokenResponse(server.tokenAuth, tokenAudienceWebAdmin, "")
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
form := make(url.Values)
|
2022-03-26 07:41:50 +00:00
|
|
|
form.Set(csrfFormToken, createCSRFToken(""))
|
2021-01-17 21:29:08 +00:00
|
|
|
form.Set("status", "1")
|
2022-11-05 17:01:24 +00:00
|
|
|
form.Set("default_users_expiration", "30")
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, path.Join(webAdminPath, "admin"), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("username", "admin")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebUpdateAdminPost(rr, req)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 20:25:41 +00:00
|
|
|
func TestRetentionInvalidTokenClaims(t *testing.T) {
|
|
|
|
username := "retentionuser"
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Password: "pwd",
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
Status: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user.Filters.AllowAPIKeyAuth = true
|
2022-12-03 10:45:27 +00:00
|
|
|
err := dataprovider.AddUser(&user, "", "", "")
|
2021-10-02 20:25:41 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 19:50:38 +00:00
|
|
|
folderRetention := []dataprovider.FolderRetention{
|
2021-10-02 20:25:41 +00:00
|
|
|
{
|
|
|
|
Path: "/",
|
|
|
|
Retention: 0,
|
|
|
|
DeleteEmptyDirs: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
asJSON, err := json.Marshal(folderRetention)
|
|
|
|
assert.NoError(t, err)
|
2021-10-03 13:17:49 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, retentionBasePath+"/"+username+"/check?notifications=Email", bytes.NewBuffer(asJSON))
|
2021-10-02 20:25:41 +00:00
|
|
|
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("username", username)
|
|
|
|
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), jwtauth.ErrorCtxKey, errors.New("error")))
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
startRetentionCheck(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
2021-10-02 20:25:41 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-06-03 14:17:32 +00:00
|
|
|
func TestUpdateSMTPSecrets(t *testing.T) {
|
|
|
|
currentConfigs := &dataprovider.SMTPConfigs{
|
|
|
|
OAuth2: dataprovider.SMTPOAuth2{
|
|
|
|
ClientSecret: kms.NewPlainSecret("client secret"),
|
|
|
|
RefreshToken: kms.NewPlainSecret("refresh token"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
redactedClientSecret := kms.NewPlainSecret("secret")
|
|
|
|
redactedRefreshToken := kms.NewPlainSecret("token")
|
|
|
|
redactedClientSecret.SetStatus(sdkkms.SecretStatusRedacted)
|
|
|
|
redactedRefreshToken.SetStatus(sdkkms.SecretStatusRedacted)
|
|
|
|
newConfigs := &dataprovider.SMTPConfigs{
|
|
|
|
Password: kms.NewPlainSecret("pwd"),
|
|
|
|
OAuth2: dataprovider.SMTPOAuth2{
|
|
|
|
ClientSecret: redactedClientSecret,
|
|
|
|
RefreshToken: redactedRefreshToken,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
updateSMTPSecrets(newConfigs, currentConfigs)
|
|
|
|
assert.Nil(t, currentConfigs.Password)
|
|
|
|
assert.NotNil(t, newConfigs.Password)
|
|
|
|
assert.Equal(t, currentConfigs.OAuth2.ClientSecret, newConfigs.OAuth2.ClientSecret)
|
|
|
|
assert.Equal(t, currentConfigs.OAuth2.RefreshToken, newConfigs.OAuth2.RefreshToken)
|
|
|
|
|
|
|
|
clientSecret := kms.NewPlainSecret("plain secret")
|
|
|
|
refreshToken := kms.NewPlainSecret("plain token")
|
|
|
|
newConfigs = &dataprovider.SMTPConfigs{
|
|
|
|
Password: kms.NewPlainSecret("pwd"),
|
|
|
|
OAuth2: dataprovider.SMTPOAuth2{
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
RefreshToken: refreshToken,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
updateSMTPSecrets(newConfigs, currentConfigs)
|
|
|
|
assert.Equal(t, clientSecret, newConfigs.OAuth2.ClientSecret)
|
|
|
|
assert.Equal(t, refreshToken, newConfigs.OAuth2.RefreshToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOAuth2Redirect(t *testing.T) {
|
|
|
|
server := httpdServer{}
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest(http.MethodGet, webOAuth2RedirectPath+"?state=invalid", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
server.handleOAuth2TokenRedirect(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nOAuth2ErrorTitle)
|
2023-06-03 14:17:32 +00:00
|
|
|
|
|
|
|
ip := "127.1.1.4"
|
|
|
|
tokenString := createOAuth2Token(xid.New().String(), ip)
|
|
|
|
rr = httptest.NewRecorder()
|
2023-10-20 18:31:17 +00:00
|
|
|
req, err = http.NewRequest(http.MethodGet, webOAuth2RedirectPath+"?state="+tokenString, nil) //nolint:goconst
|
2023-06-03 14:17:32 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req.RemoteAddr = ip
|
|
|
|
server.handleOAuth2TokenRedirect(rr, req)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nOAuth2ErrorValidateState)
|
2023-06-03 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOAuth2Token(t *testing.T) {
|
|
|
|
// invalid token
|
|
|
|
_, err := verifyOAuth2Token("token", "")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unable to verify OAuth2 state")
|
|
|
|
}
|
|
|
|
// bad audience
|
|
|
|
claims := make(map[string]any)
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
claims[jwt.JwtIDKey] = xid.New().String()
|
|
|
|
claims[jwt.NotBeforeKey] = now.Add(-30 * time.Second)
|
|
|
|
claims[jwt.ExpirationKey] = now.Add(tokenDuration)
|
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceAPI}
|
|
|
|
|
|
|
|
_, tokenString, err := csrfTokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = verifyOAuth2Token(tokenString, "")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "invalid OAuth2 state")
|
|
|
|
}
|
|
|
|
// bad IP
|
|
|
|
tokenString = createOAuth2Token("state", "127.1.1.1")
|
|
|
|
_, err = verifyOAuth2Token(tokenString, "127.1.1.2")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "invalid OAuth2 state")
|
|
|
|
}
|
|
|
|
// ok
|
|
|
|
state := xid.New().String()
|
|
|
|
tokenString = createOAuth2Token(state, "127.1.1.3")
|
|
|
|
s, err := verifyOAuth2Token(tokenString, "127.1.1.3")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, state, s)
|
|
|
|
// no jti
|
|
|
|
claims = make(map[string]any)
|
|
|
|
|
|
|
|
claims[jwt.NotBeforeKey] = now.Add(-30 * time.Second)
|
|
|
|
claims[jwt.ExpirationKey] = now.Add(tokenDuration)
|
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceOAuth2, "127.1.1.4"}
|
|
|
|
_, tokenString, err = csrfTokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = verifyOAuth2Token(tokenString, "127.1.1.4")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "invalid OAuth2 state")
|
|
|
|
}
|
|
|
|
// encode error
|
|
|
|
csrfTokenAuth = jwtauth.New("HT256", util.GenerateRandomBytes(32), nil)
|
|
|
|
tokenString = createOAuth2Token(xid.New().String(), "")
|
|
|
|
assert.Empty(t, tokenString)
|
|
|
|
|
|
|
|
server := httpdServer{}
|
|
|
|
server.initializeRouter()
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
testReq := make(map[string]any)
|
|
|
|
testReq["base_redirect_url"] = "http://localhost:8082"
|
|
|
|
asJSON, err := json.Marshal(testReq)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, webOAuth2TokenPath, bytes.NewBuffer(asJSON))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
handleSMTPOAuth2TokenRequestPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "unable to create state token")
|
|
|
|
|
|
|
|
csrfTokenAuth = jwtauth.New(jwa.HS256.String(), util.GenerateRandomBytes(32), nil)
|
|
|
|
}
|
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
func TestCSRFToken(t *testing.T) {
|
|
|
|
// invalid token
|
2022-03-26 07:41:50 +00:00
|
|
|
err := verifyCSRFToken("token", "")
|
2021-02-03 07:55:28 +00:00
|
|
|
if assert.Error(t, err) {
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, err.Error(), "unable to verify form token")
|
2021-02-03 07:55:28 +00:00
|
|
|
}
|
|
|
|
// bad audience
|
2022-05-19 17:49:51 +00:00
|
|
|
claims := make(map[string]any)
|
2021-02-03 07:55:28 +00:00
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
claims[jwt.JwtIDKey] = xid.New().String()
|
|
|
|
claims[jwt.NotBeforeKey] = now.Add(-30 * time.Second)
|
|
|
|
claims[jwt.ExpirationKey] = now.Add(tokenDuration)
|
2022-03-26 07:41:50 +00:00
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceAPI}
|
2021-02-03 07:55:28 +00:00
|
|
|
|
|
|
|
_, tokenString, err := csrfTokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
2022-03-26 07:41:50 +00:00
|
|
|
err = verifyCSRFToken(tokenString, "")
|
2021-02-03 07:55:28 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "form token is not valid")
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
// bad IP
|
|
|
|
tokenString = createCSRFToken("127.1.1.1")
|
|
|
|
err = verifyCSRFToken(tokenString, "127.1.1.2")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "form token is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
claims[jwt.JwtIDKey] = xid.New().String()
|
|
|
|
claims[jwt.NotBeforeKey] = now.Add(-30 * time.Second)
|
|
|
|
claims[jwt.ExpirationKey] = now.Add(tokenDuration)
|
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceAPI}
|
|
|
|
_, tokenString, err = csrfTokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-12-03 17:33:08 +00:00
|
|
|
r := GetHTTPRouter(Binding{
|
|
|
|
Address: "",
|
|
|
|
Port: 8080,
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: true,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2021-12-03 17:33:08 +00:00
|
|
|
RenderOpenAPI: true,
|
|
|
|
})
|
2021-02-03 07:55:28 +00:00
|
|
|
fn := verifyCSRFHeader(r)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodDelete, path.Join(userPath, "username"), nil)
|
|
|
|
fn.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token")
|
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
// invalid audience
|
|
|
|
req.Header.Set(csrfHeaderToken, tokenString)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
fn.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "the token is not valid")
|
|
|
|
|
|
|
|
// invalid IP
|
|
|
|
tokenString = createCSRFToken("172.16.1.2")
|
2021-02-03 07:55:28 +00:00
|
|
|
req.Header.Set(csrfHeaderToken, tokenString)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
fn.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), "the token is not valid")
|
2021-02-03 07:55:28 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
csrfTokenAuth = jwtauth.New("PS256", util.GenerateRandomBytes(32), nil)
|
2022-03-26 07:41:50 +00:00
|
|
|
tokenString = createCSRFToken("")
|
2021-02-03 07:55:28 +00:00
|
|
|
assert.Empty(t, tokenString)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
csrfTokenAuth = jwtauth.New(jwa.HS256.String(), util.GenerateRandomBytes(32), nil)
|
2021-02-03 07:55:28 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 07:29:53 +00:00
|
|
|
func TestCreateShareCookieError(t *testing.T) {
|
|
|
|
username := "share_user"
|
2023-10-20 18:31:17 +00:00
|
|
|
pwd := util.GenerateUniqueID()
|
2023-02-12 07:29:53 +00:00
|
|
|
user := &dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Password: pwd,
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
Status: 1,
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := dataprovider.AddUser(user, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
share := &dataprovider.Share{
|
|
|
|
Name: "test share cookie error",
|
|
|
|
ShareID: util.GenerateUniqueID(),
|
|
|
|
Scope: dataprovider.ShareScopeRead,
|
|
|
|
Password: pwd,
|
|
|
|
Paths: []string{"/"},
|
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
err = dataprovider.AddShare(share, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
server := httpdServer{
|
|
|
|
tokenAuth: jwtauth.New("TS256", util.GenerateRandomBytes(32), nil),
|
|
|
|
}
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("share_password", pwd)
|
|
|
|
form.Set(csrfFormToken, createCSRFToken("127.0.0.1"))
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("id", share.ShareID)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest(http.MethodPost, path.Join(webClientPubSharesPath, share.ShareID, "login"),
|
|
|
|
bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.RemoteAddr = "127.0.0.1:2345"
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
server.handleClientShareLoginPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nError500Message)
|
2023-02-12 07:29:53 +00:00
|
|
|
|
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestCreateTokenError(t *testing.T) {
|
|
|
|
server := httpdServer{
|
2021-07-11 13:26:51 +00:00
|
|
|
tokenAuth: jwtauth.New("PS256", util.GenerateRandomBytes(32), nil),
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
admin := dataprovider.Admin{
|
2022-02-27 12:08:47 +00:00
|
|
|
Username: defaultAdminUsername,
|
2021-01-17 21:29:08 +00:00
|
|
|
Password: "password",
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, tokenPath, nil)
|
2019-07-26 09:34:44 +00:00
|
|
|
|
2022-03-24 21:03:17 +00:00
|
|
|
server.generateAndSendToken(rr, req, admin, "")
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "u",
|
2023-10-20 18:31:17 +00:00
|
|
|
Password: util.GenerateUniqueID(),
|
2021-07-11 13:26:51 +00:00
|
|
|
},
|
2021-06-05 14:07:09 +00:00
|
|
|
}
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userTokenPath, nil)
|
|
|
|
|
|
|
|
server.generateAndSendUserToken(rr, req, "", user)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", admin.Username)
|
|
|
|
form.Set("password", admin.Password)
|
2022-03-26 07:41:50 +00:00
|
|
|
form.Set(csrfFormToken, createCSRFToken("127.0.0.1"))
|
2022-02-13 13:30:20 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
2021-01-17 21:29:08 +00:00
|
|
|
req.RemoteAddr = "127.0.0.1:1234"
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2021-05-06 19:35:43 +00:00
|
|
|
server.handleWebAdminLoginPost(rr, req)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
// req with no content type
|
2022-02-13 13:30:20 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminLoginPath, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = httptest.NewRecorder()
|
2021-05-06 19:35:43 +00:00
|
|
|
server.handleWebAdminLoginPost(rr, req)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2021-09-04 10:11:04 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminSetupPath, nil)
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-24 21:03:17 +00:00
|
|
|
server.loginAdmin(rr, req, &admin, false, nil, "")
|
2021-01-17 21:29:08 +00:00
|
|
|
// req with no POST body
|
2022-02-13 13:30:20 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webAdminLoginPath+"?a=a%C3%AO%GG", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
2021-05-06 19:35:43 +00:00
|
|
|
server.handleWebAdminLoginPost(rr, req)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2022-07-11 06:17:36 +00:00
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webAdminLoginPath+"?a=a%C3%A1%G2", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
2022-02-13 13:30:20 +00:00
|
|
|
server.handleWebAdminChangePwdPost(rr, req)
|
2021-09-29 16:46:15 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webAdminLoginPath+"?a=a%C3%A2%G3", nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
_, err := getAdminFromPostFields(req)
|
2020-11-25 21:26:34 +00:00
|
|
|
assert.Error(t, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2023-10-20 18:31:17 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminEventActionPath+"?a=a%C3%A2%GG", nil)
|
2022-07-11 06:17:36 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
_, err = getEventActionFromPostFields(req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2023-10-20 18:31:17 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminEventRulePath+"?a=a%C3%A3%GG", nil)
|
2022-07-11 06:17:36 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
_, err = getEventRuleFromPostFields(req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webIPListPath+"/1?a=a%C3%AO%GG", nil)
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
_, err = getIPListEntryFromPostFields(req, dataprovider.IPListTypeAllowList)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2023-02-12 07:29:53 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, path.Join(webClientSharePath, "shareID", "login?a=a%C3%AO%GG"), bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleClientShareLoginPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientLoginPath+"?a=a%C3%AO%GG", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebClientLoginPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webChangeClientPwdPath+"?a=a%C3%AO%GA", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
2022-02-13 13:30:20 +00:00
|
|
|
server.handleWebClientChangePwdPost(rr, req)
|
2021-09-29 16:46:15 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientProfilePath+"?a=a%C3%AO%GB", bytes.NewBuffer([]byte(form.Encode())))
|
2021-08-17 16:08:32 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebClientProfilePost(rr, req)
|
2021-08-17 16:08:32 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code, rr.Body.String())
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminProfilePath+"?a=a%C3%AO%GB", bytes.NewBuffer([]byte(form.Encode())))
|
2021-08-17 16:08:32 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebAdminProfilePost(rr, req)
|
2021-08-17 16:08:32 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code, rr.Body.String())
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminTwoFactorPath+"?a=a%C3%AO%GC", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAdminTwoFactorPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-09-04 10:11:04 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminTwoFactorRecoveryPath+"?a=a%C3%AO%GD", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAdminTwoFactorRecoveryPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-09-04 10:11:04 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientTwoFactorPath+"?a=a%C3%AO%GC", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebClientTwoFactorPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-09-04 10:11:04 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientTwoFactorRecoveryPath+"?a=a%C3%AO%GD", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebClientTwoFactorRecoveryPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-09-04 10:11:04 +00:00
|
|
|
|
2021-11-13 12:25:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminForgotPwdPath+"?a=a%C3%A1%GD", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebAdminForgotPwdPost(rr, req)
|
2021-11-13 12:25:43 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-11-13 12:25:43 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientForgotPwdPath+"?a=a%C2%A1%GD", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebClientForgotPwdPost(rr, req)
|
2021-11-13 12:25:43 +00:00
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-11-13 12:25:43 +00:00
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminResetPwdPath+"?a=a%C3%AO%JD", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAdminPasswordResetPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-11-13 12:25:43 +00:00
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminRolePath+"?a=a%C3%AO%JE", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebAddRolePost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2022-11-16 18:04:50 +00:00
|
|
|
|
2021-11-13 12:25:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientResetPwdPath+"?a=a%C3%AO%JD", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
server.handleWebClientPasswordResetPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidForm)
|
2021-11-13 12:25:43 +00:00
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webChangeClientPwdPath+"?a=a%K3%AO%GA", bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
_, err = getShareFromPostFields(req)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "invalid URL escape")
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
username := "webclientuser"
|
2021-06-05 14:07:09 +00:00
|
|
|
user = dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Password: "clientpwd",
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
Status: 1,
|
|
|
|
Description: "test user",
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
2021-08-17 16:08:32 +00:00
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user.Filters.AllowAPIKeyAuth = true
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user, "", "", "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
form = make(url.Values)
|
|
|
|
form.Set("username", user.Username)
|
|
|
|
form.Set("password", "clientpwd")
|
2022-03-26 07:41:50 +00:00
|
|
|
form.Set(csrfFormToken, createCSRFToken("127.0.0.1"))
|
2021-05-06 19:35:43 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
req.RemoteAddr = "127.0.0.1:4567"
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
server.handleWebClientLoginPost(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
err = authenticateUserWithAPIKey(username, "", server.tokenAuth, req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, "", "", "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
2022-07-21 16:42:22 +00:00
|
|
|
err = os.RemoveAll(user.HomeDir)
|
|
|
|
assert.NoError(t, err)
|
2021-08-17 16:08:32 +00:00
|
|
|
|
|
|
|
admin.Username += "1"
|
|
|
|
admin.Status = 1
|
|
|
|
admin.Filters.AllowAPIKeyAuth = true
|
|
|
|
admin.Permissions = []string{dataprovider.PermAdminAny}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddAdmin(&admin, "", "", "")
|
2021-08-17 16:08:32 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = authenticateAdminWithAPIKey(admin.Username, "", server.tokenAuth, req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteAdmin(admin.Username, "", "", "")
|
2021-08-17 16:08:32 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIKeyAuthForbidden(t *testing.T) {
|
2021-12-03 17:33:08 +00:00
|
|
|
r := GetHTTPRouter(Binding{
|
|
|
|
Address: "",
|
|
|
|
Port: 8080,
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: true,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2021-12-03 17:33:08 +00:00
|
|
|
RenderOpenAPI: true,
|
|
|
|
})
|
2021-08-17 16:08:32 +00:00
|
|
|
fn := forbidAPIKeyAuthentication(r)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, versionPath, nil)
|
|
|
|
fn.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "Invalid token claims")
|
2019-07-26 09:34:44 +00:00
|
|
|
}
|
2019-08-03 11:19:00 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestJWTTokenValidation(t *testing.T) {
|
2021-07-11 13:26:51 +00:00
|
|
|
tokenAuth := jwtauth.New(jwa.HS256.String(), util.GenerateRandomBytes(32), nil)
|
2022-05-19 17:49:51 +00:00
|
|
|
claims := make(map[string]any)
|
2022-02-27 12:08:47 +00:00
|
|
|
claims["username"] = defaultAdminUsername
|
2021-01-17 21:29:08 +00:00
|
|
|
claims[jwt.ExpirationKey] = time.Now().UTC().Add(-1 * time.Hour)
|
|
|
|
token, _, err := tokenAuth.Encode(claims)
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-19 08:41:15 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
server := httpdServer{
|
|
|
|
binding: Binding{
|
|
|
|
Address: "",
|
|
|
|
Port: 8080,
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: true,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2022-03-19 20:44:27 +00:00
|
|
|
RenderOpenAPI: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
server.initializeRouter()
|
|
|
|
r := server.router
|
2021-05-06 19:35:43 +00:00
|
|
|
fn := jwtAuthenticatorAPI(r)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, userPath, nil)
|
|
|
|
ctx := jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
2020-01-31 18:04:00 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
fn = jwtAuthenticatorWebAdmin(r)
|
2021-01-17 21:29:08 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
2021-05-06 19:35:43 +00:00
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
2022-02-13 13:30:20 +00:00
|
|
|
assert.Equal(t, webAdminLoginPath, rr.Header().Get("Location"))
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
fn = jwtAuthenticatorWebClient(r)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
2021-01-17 21:29:08 +00:00
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.Equal(t, webClientLoginPath, rr.Header().Get("Location"))
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
errTest := errors.New("test error")
|
2022-03-19 20:44:27 +00:00
|
|
|
permFn := server.checkPerm(dataprovider.PermAdminAny)
|
2021-01-17 21:29:08 +00:00
|
|
|
fn = permFn(r)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, userPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
permFn = server.checkPerm(dataprovider.PermAdminAny)
|
2021-01-17 21:29:08 +00:00
|
|
|
fn = permFn(r)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webUserPath, nil)
|
|
|
|
req.RequestURI = webUserPath
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2022-03-19 20:44:27 +00:00
|
|
|
permClientFn := server.checkHTTPUserPerm(sdk.WebClientPubKeyChangeDisabled)
|
2021-05-06 19:35:43 +00:00
|
|
|
fn = permClientFn(r)
|
|
|
|
rr = httptest.NewRecorder()
|
2021-09-29 16:46:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientProfilePath, nil)
|
|
|
|
req.RequestURI = webClientProfilePath
|
2021-05-06 19:35:43 +00:00
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2021-06-05 14:07:09 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2022-06-14 16:30:57 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, userProfilePath, nil)
|
|
|
|
req.RequestURI = userProfilePath
|
2021-06-05 14:07:09 +00:00
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
2022-03-06 15:57:13 +00:00
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
2022-12-11 16:15:34 +00:00
|
|
|
fn = server.checkAuthRequirements(r)
|
2022-03-06 15:57:13 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientProfilePath, nil)
|
|
|
|
req.RequestURI = webClientProfilePath
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
2024-02-21 19:45:10 +00:00
|
|
|
fn = server.checkAuthRequirements(r)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webGroupsPath, nil)
|
|
|
|
req.RequestURI = webGroupsPath
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
|
|
|
2022-03-06 15:57:13 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, userSharesPath, nil)
|
|
|
|
req.RequestURI = userSharesPath
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, errTest)
|
2021-06-05 14:07:09 +00:00
|
|
|
fn.ServeHTTP(rr, req.WithContext(ctx))
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestUpdateContextFromCookie(t *testing.T) {
|
|
|
|
server := httpdServer{
|
2021-07-11 13:26:51 +00:00
|
|
|
tokenAuth: jwtauth.New(jwa.HS256.String(), util.GenerateRandomBytes(32), nil),
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, tokenPath, nil)
|
2022-05-19 17:49:51 +00:00
|
|
|
claims := make(map[string]any)
|
2021-01-17 21:29:08 +00:00
|
|
|
claims["a"] = "b"
|
|
|
|
token, _, err := server.tokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
2021-01-02 18:33:24 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
ctx := jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.updateContextFromCookie(req.WithContext(ctx))
|
2020-02-03 23:08:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func TestCookieExpiration(t *testing.T) {
|
|
|
|
server := httpdServer{
|
2021-07-11 13:26:51 +00:00
|
|
|
tokenAuth: jwtauth.New(jwa.HS256.String(), util.GenerateRandomBytes(32), nil),
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
err := errors.New("test error")
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
ctx := jwtauth.NewContext(req.Context(), nil, err)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie := rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
2022-05-19 17:49:51 +00:00
|
|
|
claims := make(map[string]any)
|
2021-01-17 21:29:08 +00:00
|
|
|
claims["a"] = "b"
|
|
|
|
token, _, err := server.tokenAuth.Encode(claims)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "newtestadmin",
|
|
|
|
Password: "password",
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
claims = make(map[string]any)
|
2021-01-17 21:29:08 +00:00
|
|
|
claims[claimUsernameKey] = admin.Username
|
|
|
|
claims[claimPermissionsKey] = admin.Permissions
|
|
|
|
claims[jwt.SubjectKey] = admin.GetSignature()
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().Add(1 * time.Minute)
|
2022-03-26 07:41:50 +00:00
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceAPI}
|
2021-01-17 21:29:08 +00:00
|
|
|
token, _, err = server.tokenAuth.Encode(claims)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin.Status = 0
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddAdmin(&admin, "", "", "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin.Status = 1
|
|
|
|
admin.Filters.AllowList = []string{"172.16.1.0/24"}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateAdmin(&admin, "", "", "")
|
2020-09-04 19:08:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
admin, err = dataprovider.AdminExists(admin.Username)
|
2020-09-04 19:08:09 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-19 17:49:51 +00:00
|
|
|
claims = make(map[string]any)
|
2021-01-17 21:29:08 +00:00
|
|
|
claims[claimUsernameKey] = admin.Username
|
|
|
|
claims[claimPermissionsKey] = admin.Permissions
|
|
|
|
claims[jwt.SubjectKey] = admin.GetSignature()
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().Add(1 * time.Minute)
|
2022-03-26 07:41:50 +00:00
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceAPI}
|
2021-01-17 21:29:08 +00:00
|
|
|
token, _, err = server.tokenAuth.Encode(claims)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
req.RemoteAddr = "192.168.8.1:1234"
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
req.RemoteAddr = "172.16.1.12:4567"
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.True(t, strings.HasPrefix(cookie, "jwt="))
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteAdmin(admin.Username, "", "", "")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
// now check client cookie expiration
|
|
|
|
username := "client"
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: username,
|
|
|
|
Password: "clientpwd",
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), username),
|
|
|
|
Status: 1,
|
|
|
|
Description: "test user",
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{"*"}
|
|
|
|
|
2022-05-19 17:49:51 +00:00
|
|
|
claims = make(map[string]any)
|
2021-05-06 19:35:43 +00:00
|
|
|
claims[claimUsernameKey] = user.Username
|
|
|
|
claims[claimPermissionsKey] = user.Filters.WebClient
|
|
|
|
claims[jwt.SubjectKey] = user.GetSignature()
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().Add(1 * time.Minute)
|
2022-03-26 07:41:50 +00:00
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceWebClient}
|
2021-05-06 19:35:43 +00:00
|
|
|
token, _, err = server.tokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
// the password will be hashed and so the signature will change
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user, "", "", "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err = dataprovider.UserExists(user.Username, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Filters.AllowedIP = []string{"172.16.4.0/24"}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, "", "", "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err = dataprovider.UserExists(user.Username, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-19 17:49:51 +00:00
|
|
|
claims = make(map[string]any)
|
2021-05-06 19:35:43 +00:00
|
|
|
claims[claimUsernameKey] = user.Username
|
|
|
|
claims[claimPermissionsKey] = user.Filters.WebClient
|
|
|
|
claims[jwt.SubjectKey] = user.GetSignature()
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().Add(1 * time.Minute)
|
2022-03-26 07:41:50 +00:00
|
|
|
claims[jwt.AudienceKey] = []string{tokenAudienceWebClient}
|
2021-05-06 19:35:43 +00:00
|
|
|
token, _, err = server.tokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
req.RemoteAddr = "172.16.3.12:4567"
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Empty(t, cookie)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
req.RemoteAddr = "172.16.4.16:4567"
|
|
|
|
ctx = jwtauth.NewContext(req.Context(), token, nil)
|
2021-05-11 04:54:06 +00:00
|
|
|
server.checkCookieExpiration(rr, req.WithContext(ctx))
|
2021-05-06 19:35:43 +00:00
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.NotEmpty(t, cookie)
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(user.Username, "", "", "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetURLParam(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, adminPwdPath, nil)
|
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("val", "testuser%C3%A0")
|
|
|
|
rctx.URLParams.Add("inval", "testuser%C3%AO%GG")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
|
|
|
escaped := getURLParam(req, "val")
|
|
|
|
assert.Equal(t, "testuserà", escaped)
|
|
|
|
escaped = getURLParam(req, "inval")
|
|
|
|
assert.Equal(t, "testuser%C3%AO%GG", escaped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangePwdValidationErrors(t *testing.T) {
|
|
|
|
err := doChangeAdminPassword(nil, "", "", "")
|
|
|
|
require.Error(t, err)
|
|
|
|
err = doChangeAdminPassword(nil, "a", "b", "c")
|
|
|
|
require.Error(t, err)
|
|
|
|
err = doChangeAdminPassword(nil, "a", "a", "a")
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodPut, adminPwdPath, nil)
|
|
|
|
err = doChangeAdminPassword(req, "currentpwd", "newpwd", "newpwd")
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderUnexistingFolder(t *testing.T) {
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, folderPath, nil)
|
2023-04-18 16:11:23 +00:00
|
|
|
renderFolder(rr, req, "path not mapped", &jwtTokenClaims{}, http.StatusOK)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, rr.Code)
|
2019-08-03 11:19:00 +00:00
|
|
|
}
|
2019-08-08 19:42:07 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestCloseConnectionHandler(t *testing.T) {
|
2022-10-02 07:51:47 +00:00
|
|
|
tokenAuth := jwtauth.New(jwa.HS256.String(), util.GenerateRandomBytes(32), nil)
|
|
|
|
claims := make(map[string]any)
|
|
|
|
claims["username"] = defaultAdminUsername
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().UTC().Add(1 * time.Hour)
|
|
|
|
token, _, err := tokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req, err := http.NewRequest(http.MethodDelete, activeConnectionsPath+"/connectionID", nil)
|
|
|
|
assert.NoError(t, err)
|
2019-08-08 19:42:07 +00:00
|
|
|
rctx := chi.NewRouteContext()
|
|
|
|
rctx.URLParams.Add("connectionID", "")
|
|
|
|
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
2022-10-02 07:51:47 +00:00
|
|
|
req = req.WithContext(context.WithValue(req.Context(), jwtauth.TokenCtxKey, token))
|
2019-08-08 19:42:07 +00:00
|
|
|
rr := httptest.NewRecorder()
|
2019-08-24 12:41:15 +00:00
|
|
|
handleCloseConnection(rr, req)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
2022-10-02 07:51:47 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), "connectionID is mandatory")
|
2019-08-08 19:42:07 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
|
|
|
|
func TestRenderInvalidTemplate(t *testing.T) {
|
|
|
|
tmpl, err := template.New("test").Parse("{{.Count}}")
|
2020-05-03 13:24:26 +00:00
|
|
|
if assert.NoError(t, err) {
|
2021-05-06 19:35:43 +00:00
|
|
|
noMatchTmpl := "no_match"
|
|
|
|
adminTemplates[noMatchTmpl] = tmpl
|
2019-10-07 16:19:01 +00:00
|
|
|
rw := httptest.NewRecorder()
|
2021-05-06 19:35:43 +00:00
|
|
|
renderAdminTemplate(rw, noMatchTmpl, map[string]string{})
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rw.Code)
|
|
|
|
clientTemplates[noMatchTmpl] = tmpl
|
|
|
|
renderClientTemplate(rw, noMatchTmpl, map[string]string{})
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rw.Code)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
|
|
|
|
func TestQuotaScanInvalidFs(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2021-03-21 18:15:47 +00:00
|
|
|
FsConfig: vfs.Filesystem{
|
2021-07-11 13:26:51 +00:00
|
|
|
Provider: sdk.S3FilesystemProvider,
|
2020-01-19 06:41:05 +00:00
|
|
|
},
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
common.QuotaScans.AddUserQuotaScan(user.Username, "")
|
2021-06-07 19:52:43 +00:00
|
|
|
err := doUserQuotaScan(user)
|
2020-05-03 13:24:26 +00:00
|
|
|
assert.Error(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2021-01-19 17:59:41 +00:00
|
|
|
|
|
|
|
func TestVerifyTLSConnection(t *testing.T) {
|
|
|
|
oldCertMgr := certMgr
|
|
|
|
|
|
|
|
caCrlPath := filepath.Join(os.TempDir(), "testcrl.crt")
|
|
|
|
certPath := filepath.Join(os.TempDir(), "testh.crt")
|
|
|
|
keyPath := filepath.Join(os.TempDir(), "testh.key")
|
2021-02-25 20:53:04 +00:00
|
|
|
err := os.WriteFile(caCrlPath, []byte(caCRL), os.ModePerm)
|
2021-01-19 17:59:41 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(certPath, []byte(httpdCert), os.ModePerm)
|
2021-01-19 17:59:41 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(keyPath, []byte(httpdKey), os.ModePerm)
|
2021-01-19 17:59:41 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-05-21 14:34:47 +00:00
|
|
|
keyPairs := []common.TLSKeyPair{
|
|
|
|
{
|
|
|
|
Cert: certPath,
|
|
|
|
Key: keyPath,
|
|
|
|
ID: common.DefaultTLSKeyPaidID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
certMgr, err = common.NewCertManager(keyPairs, "", "httpd_test")
|
2021-01-19 17:59:41 +00:00
|
|
|
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 := httpdServer{}
|
|
|
|
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
|
|
|
|
}
|
2021-01-25 20:31:33 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestGetFolderFromTemplate(t *testing.T) {
|
|
|
|
folder := vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: "Folder%name%",
|
|
|
|
Description: "Folder %name% desc",
|
|
|
|
}
|
|
|
|
folderName := "folderTemplate"
|
|
|
|
folderTemplate := getFolderFromTemplate(folder, folderName)
|
|
|
|
require.Equal(t, folderName, folderTemplate.Name)
|
|
|
|
require.Equal(t, fmt.Sprintf("Folder%v", folderName), folderTemplate.MappedPath)
|
|
|
|
require.Equal(t, fmt.Sprintf("Folder %v desc", folderName), folderTemplate.Description)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
folder.FsConfig.Provider = sdk.CryptedFilesystemProvider
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("%name%")
|
|
|
|
folderTemplate = getFolderFromTemplate(folder, folderName)
|
|
|
|
require.Equal(t, folderName, folderTemplate.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
folder.FsConfig.Provider = sdk.GCSFilesystemProvider
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.FsConfig.GCSConfig.KeyPrefix = "prefix%name%/"
|
|
|
|
folderTemplate = getFolderFromTemplate(folder, folderName)
|
|
|
|
require.Equal(t, fmt.Sprintf("prefix%v/", folderName), folderTemplate.FsConfig.GCSConfig.KeyPrefix)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
folder.FsConfig.Provider = sdk.AzureBlobFilesystemProvider
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.FsConfig.AzBlobConfig.KeyPrefix = "a%name%"
|
|
|
|
folder.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret("pwd%name%")
|
|
|
|
folderTemplate = getFolderFromTemplate(folder, folderName)
|
|
|
|
require.Equal(t, "a"+folderName, folderTemplate.FsConfig.AzBlobConfig.KeyPrefix)
|
|
|
|
require.Equal(t, "pwd"+folderName, folderTemplate.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
folder.FsConfig.Provider = sdk.SFTPFilesystemProvider
|
2021-03-21 18:15:47 +00:00
|
|
|
folder.FsConfig.SFTPConfig.Prefix = "%name%"
|
|
|
|
folder.FsConfig.SFTPConfig.Username = "sftp_%name%"
|
|
|
|
folder.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("sftp%name%")
|
|
|
|
folderTemplate = getFolderFromTemplate(folder, folderName)
|
|
|
|
require.Equal(t, folderName, folderTemplate.FsConfig.SFTPConfig.Prefix)
|
|
|
|
require.Equal(t, "sftp_"+folderName, folderTemplate.FsConfig.SFTPConfig.Username)
|
|
|
|
require.Equal(t, "sftp"+folderName, folderTemplate.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
func TestGetUserFromTemplate(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Status: 1,
|
|
|
|
},
|
2021-01-25 20:31:33 +00:00
|
|
|
}
|
|
|
|
user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2021-02-01 18:04:15 +00:00
|
|
|
Name: "Folder%username%",
|
2021-01-25 20:31:33 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
username := "userTemplate"
|
|
|
|
password := "pwdTemplate"
|
|
|
|
templateFields := userTemplateFields{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
userTemplate := getUserFromTemplate(user, templateFields)
|
|
|
|
require.Len(t, userTemplate.VirtualFolders, 1)
|
2021-02-01 18:04:15 +00:00
|
|
|
require.Equal(t, "Folder"+username, userTemplate.VirtualFolders[0].Name)
|
2021-01-25 20:31:33 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.CryptedFilesystemProvider
|
2021-01-25 20:31:33 +00:00
|
|
|
user.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret("%password%")
|
|
|
|
userTemplate = getUserFromTemplate(user, templateFields)
|
|
|
|
require.Equal(t, password, userTemplate.FsConfig.CryptConfig.Passphrase.GetPayload())
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.GCSFilesystemProvider
|
2021-01-25 20:31:33 +00:00
|
|
|
user.FsConfig.GCSConfig.KeyPrefix = "%username%%password%"
|
|
|
|
userTemplate = getUserFromTemplate(user, templateFields)
|
|
|
|
require.Equal(t, username+password, userTemplate.FsConfig.GCSConfig.KeyPrefix)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.AzureBlobFilesystemProvider
|
2021-01-25 20:31:33 +00:00
|
|
|
user.FsConfig.AzBlobConfig.KeyPrefix = "a%username%"
|
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret("pwd%password%%username%")
|
|
|
|
userTemplate = getUserFromTemplate(user, templateFields)
|
|
|
|
require.Equal(t, "a"+username, userTemplate.FsConfig.AzBlobConfig.KeyPrefix)
|
|
|
|
require.Equal(t, "pwd"+password+username, userTemplate.FsConfig.AzBlobConfig.AccountKey.GetPayload())
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.SFTPFilesystemProvider
|
2021-01-25 20:31:33 +00:00
|
|
|
user.FsConfig.SFTPConfig.Prefix = "%username%"
|
|
|
|
user.FsConfig.SFTPConfig.Username = "sftp_%username%"
|
|
|
|
user.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("sftp%password%")
|
|
|
|
userTemplate = getUserFromTemplate(user, templateFields)
|
|
|
|
require.Equal(t, username, userTemplate.FsConfig.SFTPConfig.Prefix)
|
|
|
|
require.Equal(t, "sftp_"+username, userTemplate.FsConfig.SFTPConfig.Username)
|
|
|
|
require.Equal(t, "sftp"+password, userTemplate.FsConfig.SFTPConfig.Password.GetPayload())
|
|
|
|
}
|
2021-01-26 21:35:36 +00:00
|
|
|
|
|
|
|
func TestJWTTokenCleanup(t *testing.T) {
|
|
|
|
server := httpdServer{
|
2021-07-11 13:26:51 +00:00
|
|
|
tokenAuth: jwtauth.New(jwa.HS256.String(), util.GenerateRandomBytes(32), nil),
|
2021-01-26 21:35:36 +00:00
|
|
|
}
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "newtestadmin",
|
|
|
|
Password: "password",
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
claims := make(map[string]any)
|
2021-01-26 21:35:36 +00:00
|
|
|
claims[claimUsernameKey] = admin.Username
|
|
|
|
claims[claimPermissionsKey] = admin.Permissions
|
|
|
|
claims[jwt.SubjectKey] = admin.GetSignature()
|
|
|
|
claims[jwt.ExpirationKey] = time.Now().Add(1 * time.Minute)
|
|
|
|
_, token, err := server.tokenAuth.Encode(claims)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, versionPath, nil)
|
|
|
|
assert.True(t, isTokenInvalidated(req))
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
|
|
|
|
|
2023-10-31 18:56:59 +00:00
|
|
|
invalidatedJWTTokens.Add(token, time.Now().Add(-tokenDuration).UTC())
|
2021-01-26 21:35:36 +00:00
|
|
|
require.True(t, isTokenInvalidated(req))
|
2021-09-04 10:11:04 +00:00
|
|
|
startCleanupTicker(100 * time.Millisecond)
|
2021-01-26 21:35:36 +00:00
|
|
|
assert.Eventually(t, func() bool { return !isTokenInvalidated(req) }, 1*time.Second, 200*time.Millisecond)
|
2021-09-04 10:11:04 +00:00
|
|
|
stopCleanupTicker()
|
2021-01-26 21:35:36 +00:00
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
|
2023-10-31 18:56:59 +00:00
|
|
|
func TestDbTokenManager(t *testing.T) {
|
|
|
|
if !isSharedProviderSupported() {
|
|
|
|
t.Skip("this test it is not available with this provider")
|
|
|
|
}
|
|
|
|
mgr := newTokenManager(1)
|
|
|
|
dbTokenManager := mgr.(*dbTokenManager)
|
|
|
|
testToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiV2ViQWRtaW4iLCI6OjEiXSwiZXhwIjoxNjk4NjYwMDM4LCJqdGkiOiJja3ZuazVrYjF1aHUzZXRmZmhyZyIsIm5iZiI6MTY5ODY1ODgwOCwicGVybWlzc2lvbnMiOlsiKiJdLCJzdWIiOiIxNjk3ODIwNDM3NTMyIiwidXNlcm5hbWUiOiJhZG1pbiJ9.LXuFFksvnSuzHqHat6r70yR0jEulNRju7m7SaWrOfy8; csrftoken=mP0C7DqjwpAXsptO2gGCaYBkYw3oNMWB"
|
|
|
|
key := dbTokenManager.getKey(testToken)
|
|
|
|
require.Len(t, key, 64)
|
|
|
|
dbTokenManager.Add(testToken, time.Now().Add(-tokenDuration).UTC())
|
|
|
|
isInvalidated := dbTokenManager.Get(testToken)
|
|
|
|
assert.True(t, isInvalidated)
|
|
|
|
dbTokenManager.Cleanup()
|
|
|
|
isInvalidated = dbTokenManager.Get(testToken)
|
|
|
|
assert.False(t, isInvalidated)
|
|
|
|
dbTokenManager.Add(testToken, time.Now().Add(tokenDuration).UTC())
|
|
|
|
isInvalidated = dbTokenManager.Get(testToken)
|
|
|
|
assert.True(t, isInvalidated)
|
|
|
|
dbTokenManager.Cleanup()
|
|
|
|
isInvalidated = dbTokenManager.Get(testToken)
|
|
|
|
assert.True(t, isInvalidated)
|
|
|
|
err := dataprovider.DeleteSharedSession(key)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-06-09 06:37:37 +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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
func TestProxyHeaders(t *testing.T) {
|
|
|
|
username := "adminTest"
|
|
|
|
password := "testPwd"
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
Status: 1,
|
|
|
|
Filters: dataprovider.AdminFilters{
|
|
|
|
AllowList: []string{"172.19.2.0/24"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err := dataprovider.AddAdmin(&admin, "", "", "")
|
2021-05-11 04:54:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
testIP := "10.29.1.9"
|
|
|
|
validForwardedFor := "172.19.2.6"
|
|
|
|
b := Binding{
|
2022-05-28 17:47:23 +00:00
|
|
|
Address: "",
|
|
|
|
Port: 8080,
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: false,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2022-05-28 17:47:23 +00:00
|
|
|
ProxyAllowed: []string{testIP, "10.8.0.0/30"},
|
|
|
|
ClientIPProxyHeader: "x-forwarded-for",
|
2021-05-11 04:54:06 +00:00
|
|
|
}
|
|
|
|
err = b.parseAllowedProxy()
|
|
|
|
assert.NoError(t, err)
|
2021-11-21 08:32:51 +00:00
|
|
|
server := newHttpdServer(b, "", "", CorsConfig{Enabled: true}, "")
|
2021-05-11 04:54:06 +00:00
|
|
|
server.initializeRouter()
|
|
|
|
testServer := httptest.NewServer(server.router)
|
|
|
|
defer testServer.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("X-Forwarded-For", validForwardedFor)
|
|
|
|
req.Header.Set(xForwardedProto, "https")
|
|
|
|
req.RemoteAddr = "127.0.0.1:123"
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, rr.Code)
|
|
|
|
assert.Contains(t, rr.Body.String(), "login from IP 127.0.0.1 not allowed")
|
|
|
|
|
|
|
|
req.RemoteAddr = testIP
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
req.RemoteAddr = "10.8.0.2"
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
form := make(url.Values)
|
|
|
|
form.Set("username", username)
|
|
|
|
form.Set("password", password)
|
2022-03-26 07:41:50 +00:00
|
|
|
form.Set(csrfFormToken, createCSRFToken(testIP))
|
2022-02-13 13:30:20 +00:00
|
|
|
req, err = http.NewRequest(http.MethodPost, webAdminLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
2021-05-11 04:54:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req.RemoteAddr = testIP
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidCredentials)
|
2021-05-11 04:54:06 +00:00
|
|
|
|
2022-03-26 07:41:50 +00:00
|
|
|
form.Set(csrfFormToken, createCSRFToken(validForwardedFor))
|
2022-02-13 13:30:20 +00:00
|
|
|
req, err = http.NewRequest(http.MethodPost, webAdminLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
2021-05-11 04:54:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req.RemoteAddr = testIP
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("X-Forwarded-For", validForwardedFor)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code, rr.Body.String())
|
|
|
|
cookie := rr.Header().Get("Set-Cookie")
|
|
|
|
assert.NotContains(t, cookie, "Secure")
|
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
req, err = http.NewRequest(http.MethodPost, webAdminLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
2021-05-11 04:54:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req.RemoteAddr = testIP
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("X-Forwarded-For", validForwardedFor)
|
|
|
|
req.Header.Set(xForwardedProto, "https")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code, rr.Body.String())
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.Contains(t, cookie, "Secure")
|
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
req, err = http.NewRequest(http.MethodPost, webAdminLoginPath, bytes.NewBuffer([]byte(form.Encode())))
|
2021-05-11 04:54:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
req.RemoteAddr = testIP
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("X-Forwarded-For", validForwardedFor)
|
|
|
|
req.Header.Set(xForwardedProto, "http")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code, rr.Body.String())
|
|
|
|
cookie = rr.Header().Get("Set-Cookie")
|
|
|
|
assert.NotContains(t, cookie, "Secure")
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteAdmin(username, "", "", "")
|
2021-05-11 04:54:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:07:46 +00:00
|
|
|
func TestRecoverer(t *testing.T) {
|
|
|
|
recoveryPath := "/recovery"
|
|
|
|
b := Binding{
|
|
|
|
Address: "",
|
|
|
|
Port: 8080,
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: false,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
2021-11-21 08:32:51 +00:00
|
|
|
server := newHttpdServer(b, "../static", "", CorsConfig{}, "../openapi")
|
2021-05-30 21:07:46 +00:00
|
|
|
server.initializeRouter()
|
2024-02-15 20:13:45 +00:00
|
|
|
server.router.Get(recoveryPath, func(_ http.ResponseWriter, _ *http.Request) {
|
2021-05-30 21:07:46 +00:00
|
|
|
panic("panic")
|
|
|
|
})
|
|
|
|
testServer := httptest.NewServer(server.router)
|
|
|
|
defer testServer.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, recoveryPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code, rr.Body.String())
|
|
|
|
|
|
|
|
server.router = chi.NewRouter()
|
2022-01-04 08:48:16 +00:00
|
|
|
server.router.Use(middleware.Recoverer)
|
2024-02-15 20:13:45 +00:00
|
|
|
server.router.Get(recoveryPath, func(_ http.ResponseWriter, _ *http.Request) {
|
2021-05-30 21:07:46 +00:00
|
|
|
panic("panic")
|
|
|
|
})
|
|
|
|
testServer = httptest.NewServer(server.router)
|
|
|
|
defer testServer.Close()
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, recoveryPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rr.Code, rr.Body.String())
|
|
|
|
}
|
|
|
|
|
2024-02-04 17:16:10 +00:00
|
|
|
func TestStreamJSONArray(t *testing.T) {
|
2024-02-15 19:53:56 +00:00
|
|
|
dataGetter := func(_, _ int) ([]byte, int, error) {
|
2024-02-04 17:16:10 +00:00
|
|
|
return nil, 0, nil
|
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
streamJSONArray(rr, 10, dataGetter)
|
|
|
|
assert.Equal(t, `[]`, rr.Body.String())
|
|
|
|
|
|
|
|
data := []int{}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
data = append(data, i)
|
|
|
|
}
|
|
|
|
|
2024-02-15 20:13:45 +00:00
|
|
|
dataGetter = func(_, offset int) ([]byte, int, error) {
|
2024-02-04 17:16:10 +00:00
|
|
|
if offset >= len(data) {
|
|
|
|
return nil, 0, nil
|
|
|
|
}
|
|
|
|
val := data[offset]
|
|
|
|
data, err := json.Marshal([]int{val})
|
|
|
|
return data, 1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
streamJSONArray(rr, 1, dataGetter)
|
|
|
|
assert.Equal(t, `[0,1,2,3,4,5,6,7,8,9]`, rr.Body.String())
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:07:46 +00:00
|
|
|
func TestCompressorAbortHandler(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
rcv := recover()
|
|
|
|
assert.Equal(t, http.ErrAbortHandler, rcv)
|
|
|
|
}()
|
|
|
|
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolHTTP, "", "", dataprovider.User{}),
|
2021-05-30 21:07:46 +00:00
|
|
|
request: nil,
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
share := &dataprovider.Share{}
|
|
|
|
renderCompressedFiles(&failingWriter{}, connection, "", nil, share)
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 17:16:10 +00:00
|
|
|
func TestStreamDataAbortHandler(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
rcv := recover()
|
|
|
|
assert.Equal(t, http.ErrAbortHandler, rcv)
|
|
|
|
}()
|
|
|
|
|
|
|
|
streamData(&failingWriter{}, []byte(`["a":"b"]`))
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:07:46 +00:00
|
|
|
func TestZipErrors(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
HomeDir: filepath.Clean(os.TempDir()),
|
|
|
|
},
|
2021-05-30 21:07:46 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolHTTP, "", "", user),
|
2021-05-30 21:07:46 +00:00
|
|
|
request: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
testDir := filepath.Join(os.TempDir(), "testDir")
|
|
|
|
err := os.MkdirAll(testDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
wr := zip.NewWriter(&failingWriter{})
|
|
|
|
err = wr.Close()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "write error")
|
|
|
|
}
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
err = addZipEntry(wr, connection, "/"+filepath.Base(testDir), "/", 0)
|
2021-05-30 21:07:46 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "write error")
|
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
err = addZipEntry(wr, connection, "/"+filepath.Base(testDir), "/", 2000)
|
|
|
|
assert.ErrorIs(t, err, util.ErrRecursionTooDeep)
|
2021-05-30 21:07:46 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
err = addZipEntry(wr, connection, "/"+filepath.Base(testDir), path.Join("/", filepath.Base(testDir), "dir"), 0)
|
2022-09-19 15:06:42 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "is outside base dir")
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:07:46 +00:00
|
|
|
testFilePath := filepath.Join(testDir, "ziptest.zip")
|
2021-07-11 13:26:51 +00:00
|
|
|
err = os.WriteFile(testFilePath, util.GenerateRandomBytes(65535), os.ModePerm)
|
2021-05-30 21:07:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = addZipEntry(wr, connection, path.Join("/", filepath.Base(testDir), filepath.Base(testFilePath)),
|
2024-02-15 19:53:56 +00:00
|
|
|
"/"+filepath.Base(testDir), 0)
|
2021-05-30 21:07:46 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "write error")
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.User.Permissions["/"] = []string{dataprovider.PermListItems}
|
|
|
|
err = addZipEntry(wr, connection, path.Join("/", filepath.Base(testDir), filepath.Base(testFilePath)),
|
2024-02-15 19:53:56 +00:00
|
|
|
"/"+filepath.Base(testDir), 0)
|
2021-05-30 21:07:46 +00:00
|
|
|
assert.ErrorIs(t, err, os.ErrPermission)
|
|
|
|
|
|
|
|
// creating a virtual folder to a missing path stat is ok but readdir fails
|
|
|
|
user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: filepath.Join(os.TempDir(), "mapped"),
|
|
|
|
},
|
|
|
|
VirtualPath: "/vpath",
|
|
|
|
})
|
|
|
|
connection.User = user
|
|
|
|
wr = zip.NewWriter(bytes.NewBuffer(make([]byte, 0)))
|
2024-02-15 19:53:56 +00:00
|
|
|
err = addZipEntry(wr, connection, user.VirtualFolders[0].VirtualPath, "/", 0)
|
2021-05-30 21:07:46 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.Filters.FilePatterns = append(user.Filters.FilePatterns, sdk.PatternsFilter{
|
2021-05-30 21:07:46 +00:00
|
|
|
Path: "/",
|
|
|
|
DeniedPatterns: []string{"*.zip"},
|
|
|
|
})
|
2024-02-15 19:53:56 +00:00
|
|
|
err = addZipEntry(wr, connection, "/"+filepath.Base(testDir), "/", 0)
|
2021-05-30 21:07:46 +00:00
|
|
|
assert.ErrorIs(t, err, os.ErrPermission)
|
|
|
|
|
|
|
|
err = os.RemoveAll(testDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func TestWebAdminRedirect(t *testing.T) {
|
|
|
|
b := Binding{
|
|
|
|
Address: "",
|
|
|
|
Port: 8080,
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: false,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-11-21 08:32:51 +00:00
|
|
|
server := newHttpdServer(b, "../static", "", CorsConfig{}, "../openapi")
|
2021-05-06 19:35:43 +00:00
|
|
|
server.initializeRouter()
|
|
|
|
testServer := httptest.NewServer(server.router)
|
|
|
|
defer testServer.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, webRootPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
2022-01-26 19:46:33 +00:00
|
|
|
assert.Equal(t, http.StatusFound, rr.Code, rr.Body.String())
|
2022-02-13 13:30:20 +00:00
|
|
|
assert.Equal(t, webAdminLoginPath, rr.Header().Get("Location"))
|
2021-05-06 19:35:43 +00:00
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, webBasePath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
testServer.Config.Handler.ServeHTTP(rr, req)
|
2022-01-26 19:46:33 +00:00
|
|
|
assert.Equal(t, http.StatusFound, rr.Code, rr.Body.String())
|
2022-02-13 13:30:20 +00:00
|
|
|
assert.Equal(t, webAdminLoginPath, rr.Header().Get("Location"))
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseRangeRequests(t *testing.T) {
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=24-24"
|
|
|
|
fileSize := int64(169740)
|
|
|
|
rangeHeader := "bytes=24-24"
|
|
|
|
offset, size, err := parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp := fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 24-24/169740", resp)
|
|
|
|
require.Equal(t, int64(1), size)
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=24-"
|
|
|
|
rangeHeader = "bytes=24-"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 24-169739/169740", resp)
|
|
|
|
require.Equal(t, int64(169716), size)
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=-1"
|
|
|
|
rangeHeader = "bytes=-1"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 169739-169739/169740", resp)
|
|
|
|
require.Equal(t, int64(1), size)
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=-100"
|
|
|
|
rangeHeader = "bytes=-100"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 169640-169739/169740", resp)
|
|
|
|
require.Equal(t, int64(100), size)
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=20-30"
|
|
|
|
rangeHeader = "bytes=20-30"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 20-30/169740", resp)
|
|
|
|
require.Equal(t, int64(11), size)
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=20-169739"
|
|
|
|
rangeHeader = "bytes=20-169739"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 20-169739/169740", resp)
|
|
|
|
require.Equal(t, int64(169720), size)
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=20-169740"
|
|
|
|
rangeHeader = "bytes=20-169740"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 20-169739/169740", resp)
|
|
|
|
require.Equal(t, int64(169720), size)
|
|
|
|
// curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=20-169741"
|
|
|
|
rangeHeader = "bytes=20-169741"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 20-169739/169740", resp)
|
|
|
|
require.Equal(t, int64(169720), size)
|
|
|
|
//curl --verbose "http://127.0.0.1:8080/static/css/sb-admin-2.min.css" -H "Range: bytes=0-" > /dev/null
|
|
|
|
rangeHeader = "bytes=0-"
|
|
|
|
offset, size, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp = fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, fileSize)
|
|
|
|
assert.Equal(t, "bytes 0-169739/169740", resp)
|
|
|
|
require.Equal(t, int64(169740), size)
|
|
|
|
// now test errors
|
|
|
|
rangeHeader = "bytes=0-a"
|
|
|
|
_, _, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.Error(t, err)
|
|
|
|
rangeHeader = "bytes="
|
|
|
|
_, _, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.Error(t, err)
|
|
|
|
rangeHeader = "bytes=-"
|
|
|
|
_, _, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.Error(t, err)
|
|
|
|
rangeHeader = "bytes=500-300"
|
|
|
|
_, _, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.Error(t, err)
|
|
|
|
rangeHeader = "bytes=5000000"
|
|
|
|
_, _, err = parseRangeRequest(rangeHeader[6:], fileSize)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestHeaderErrors(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
req.Header.Set("If-Unmodified-Since", "not a date")
|
|
|
|
res := checkIfUnmodifiedSince(req, time.Now())
|
|
|
|
assert.Equal(t, condNone, res)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientFilesPath, nil)
|
|
|
|
res = checkIfModifiedSince(req, time.Now())
|
|
|
|
assert.Equal(t, condNone, res)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientFilesPath, nil)
|
|
|
|
res = checkIfRange(req, time.Now())
|
|
|
|
assert.Equal(t, condNone, res)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
req.Header.Set("If-Modified-Since", "not a date")
|
|
|
|
res = checkIfModifiedSince(req, time.Now())
|
|
|
|
assert.Equal(t, condNone, res)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
req.Header.Set("If-Range", time.Now().Format(http.TimeFormat))
|
|
|
|
res = checkIfRange(req, time.Time{})
|
|
|
|
assert.Equal(t, condFalse, res)
|
|
|
|
|
|
|
|
req.Header.Set("If-Range", "invalid if range date")
|
|
|
|
res = checkIfRange(req, time.Now())
|
|
|
|
assert.Equal(t, condFalse, res)
|
|
|
|
modTime := getFileObjectModTime(time.Time{})
|
|
|
|
assert.Empty(t, modTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnection(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test_httpd_user",
|
|
|
|
HomeDir: filepath.Clean(os.TempDir()),
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
FsConfig: vfs.Filesystem{
|
2021-07-11 13:26:51 +00:00
|
|
|
Provider: sdk.GCSFilesystemProvider,
|
2021-05-06 19:35:43 +00:00
|
|
|
GCSConfig: vfs.GCSFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseGCSFsConfig: sdk.BaseGCSFsConfig{
|
|
|
|
Bucket: "test_bucket_name",
|
2021-07-11 13:26:51 +00:00
|
|
|
},
|
2022-01-06 09:11:47 +00:00
|
|
|
Credentials: kms.NewPlainSecret("invalid JSON payload"),
|
2021-05-06 19:35:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolHTTP, "", "", user),
|
2021-05-06 19:35:43 +00:00
|
|
|
request: nil,
|
|
|
|
}
|
|
|
|
assert.Empty(t, connection.GetClientVersion())
|
|
|
|
assert.Empty(t, connection.GetRemoteAddress())
|
|
|
|
assert.Empty(t, connection.GetCommand())
|
|
|
|
name := "missing file name"
|
2021-05-26 05:48:37 +00:00
|
|
|
_, err := connection.getFileReader(name, 0, http.MethodGet)
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.Error(t, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
connection.User.FsConfig.Provider = sdk.LocalFilesystemProvider
|
2021-05-26 05:48:37 +00:00
|
|
|
_, err = connection.getFileReader(name, 0, http.MethodGet)
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
|
|
}
|
|
|
|
|
2021-07-23 08:19:27 +00:00
|
|
|
func TestGetFileWriterErrors(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test_httpd_user",
|
|
|
|
HomeDir: "invalid",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolHTTP, "", "", user),
|
2021-07-23 08:19:27 +00:00
|
|
|
request: nil,
|
|
|
|
}
|
|
|
|
_, err := connection.getFileWriter("name")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
user.FsConfig.Provider = sdk.S3FilesystemProvider
|
|
|
|
user.FsConfig.S3Config = vfs.S3FsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseS3FsConfig: sdk.BaseS3FsConfig{
|
|
|
|
Bucket: "b",
|
|
|
|
Region: "us-west-1",
|
|
|
|
AccessKey: "key",
|
2021-07-23 08:19:27 +00:00
|
|
|
},
|
2022-01-06 09:11:47 +00:00
|
|
|
AccessSecret: kms.NewPlainSecret("secret"),
|
2021-07-23 08:19:27 +00:00
|
|
|
}
|
|
|
|
connection = &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolHTTP, "", "", user),
|
2021-07-23 08:19:27 +00:00
|
|
|
request: nil,
|
|
|
|
}
|
|
|
|
_, err = connection.getFileWriter("/path")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2021-11-30 17:40:50 +00:00
|
|
|
func TestThrottledHandler(t *testing.T) {
|
|
|
|
tr := &throttledReader{
|
|
|
|
r: io.NopCloser(bytes.NewBuffer(nil)),
|
|
|
|
}
|
2022-01-20 17:19:20 +00:00
|
|
|
assert.Equal(t, int64(0), tr.GetTruncatedSize())
|
2021-11-30 17:40:50 +00:00
|
|
|
err := tr.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, tr.GetRealFsPath("real path"))
|
|
|
|
assert.False(t, tr.SetTimes("p", time.Now(), time.Now()))
|
|
|
|
_, err = tr.Truncate("", 0)
|
|
|
|
assert.ErrorIs(t, err, vfs.ErrVfsUnsupported)
|
2022-01-20 17:19:20 +00:00
|
|
|
err = tr.GetAbortError()
|
|
|
|
assert.ErrorIs(t, err, common.ErrTransferAborted)
|
2021-11-30 17:40:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func TestHTTPDFile(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test_httpd_user",
|
|
|
|
HomeDir: filepath.Clean(os.TempDir()),
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolHTTP, "", "", user),
|
2021-05-06 19:35:43 +00:00
|
|
|
request: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
fs, err := user.GetFilesystem("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
name := "fileName"
|
|
|
|
p := filepath.Join(os.TempDir(), name)
|
|
|
|
err = os.WriteFile(p, []byte("contents"), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
file, err := os.Open(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, connection.BaseConnection, nil, p, p, name, common.TransferDownload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2021-07-23 08:19:27 +00:00
|
|
|
httpdFile := newHTTPDFile(baseTransfer, nil, nil)
|
2021-05-06 19:35:43 +00:00
|
|
|
// the file is closed, read should fail
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
_, err = httpdFile.Read(buf)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = httpdFile.Close()
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = httpdFile.Close()
|
|
|
|
assert.ErrorIs(t, err, common.ErrTransferClosed)
|
2021-05-30 21:07:46 +00:00
|
|
|
err = os.Remove(p)
|
|
|
|
assert.NoError(t, err)
|
2021-07-23 08:19:27 +00:00
|
|
|
|
|
|
|
httpdFile.writer = file
|
|
|
|
httpdFile.File = nil
|
|
|
|
httpdFile.ErrTransfer = nil
|
|
|
|
err = httpdFile.closeIO()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Error(t, httpdFile.ErrTransfer)
|
|
|
|
assert.Equal(t, err, httpdFile.ErrTransfer)
|
2022-01-20 17:19:20 +00:00
|
|
|
httpdFile.SignalClose(nil)
|
2021-11-30 17:40:50 +00:00
|
|
|
_, err = httpdFile.Write(nil)
|
2022-01-20 17:19:20 +00:00
|
|
|
assert.ErrorIs(t, err, common.ErrQuotaExceeded)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeUserPwd(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, webChangeClientPwdPath, nil)
|
|
|
|
err := doChangeUserPassword(req, "", "", "")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "please provide the current password and the new one two times")
|
|
|
|
}
|
|
|
|
err = doChangeUserPassword(req, "a", "b", "c")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "the two password fields do not match")
|
|
|
|
}
|
|
|
|
err = doChangeUserPassword(req, "a", "b", "b")
|
|
|
|
if assert.Error(t, err) {
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, err.Error(), errInvalidTokenClaims.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
func TestWebUserInvalidClaims(t *testing.T) {
|
2021-05-06 19:35:43 +00:00
|
|
|
server := httpdServer{}
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "",
|
|
|
|
Password: "pwd",
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: user.Username,
|
|
|
|
Permissions: nil,
|
|
|
|
Signature: user.GetSignature(),
|
|
|
|
}
|
2022-03-24 21:03:17 +00:00
|
|
|
token, err := c.createTokenResponse(server.tokenAuth, tokenAudienceWebClient, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, webClientFilesPath, nil)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2021-12-03 17:33:08 +00:00
|
|
|
server.handleClientGetFiles(rr, req)
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-05-27 07:40:46 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2021-07-26 18:55:49 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientDirsPath, nil)
|
2021-05-27 07:40:46 +00:00
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2021-12-03 17:33:08 +00:00
|
|
|
server.handleClientGetDirContents(rr, req)
|
2021-05-27 07:40:46 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorDirList403)
|
2021-05-30 21:07:46 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2021-06-05 14:07:09 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientDownloadZipPath, nil)
|
2021-05-30 21:07:46 +00:00
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebClientDownloadZip(rr, req)
|
2021-05-30 21:07:46 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-10-09 12:17:28 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientEditFilePath, nil)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleClientEditFile(rr, req)
|
2021-10-09 12:17:28 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-11-06 13:13:20 +00:00
|
|
|
|
2022-08-28 12:41:42 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientSharePath, nil)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
|
|
|
server.handleClientAddShareGet(rr, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-08-28 12:41:42 +00:00
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientSharePath, nil)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleClientUpdateShareGet(rr, req)
|
2021-11-06 13:13:20 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-11-06 13:13:20 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientSharePath, nil)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleClientAddSharePost(rr, req)
|
2021-11-06 13:13:20 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-11-06 13:13:20 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodPost, webClientSharePath+"/id", nil)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleClientUpdateSharePost(rr, req)
|
2021-11-06 13:13:20 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-11-06 13:13:20 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
2024-02-04 13:33:51 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientSharesPath+jsonAPISuffix, nil)
|
2021-11-06 13:13:20 +00:00
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2024-02-04 13:33:51 +00:00
|
|
|
getAllShares(rr, req)
|
2021-11-06 13:13:20 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2022-09-22 18:41:28 +00:00
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, webClientViewPDFPath, nil)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
|
|
|
server.handleClientGetPDF(rr, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2023-12-10 15:40:13 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorInvalidToken)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
func TestInvalidClaims(t *testing.T) {
|
2021-05-06 19:35:43 +00:00
|
|
|
server := httpdServer{}
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "",
|
|
|
|
Password: "pwd",
|
|
|
|
},
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: user.Username,
|
|
|
|
Permissions: nil,
|
|
|
|
Signature: user.GetSignature(),
|
|
|
|
}
|
2022-03-24 21:03:17 +00:00
|
|
|
token, err := c.createTokenResponse(server.tokenAuth, tokenAudienceWebClient, "")
|
2021-05-06 19:35:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
form := make(url.Values)
|
2022-03-26 07:41:50 +00:00
|
|
|
form.Set(csrfFormToken, createCSRFToken(""))
|
2021-05-06 19:35:43 +00:00
|
|
|
form.Set("public_keys", "")
|
2021-09-29 16:46:15 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, webClientProfilePath, bytes.NewBuffer([]byte(form.Encode())))
|
2021-08-17 16:08:32 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebClientProfilePost(rr, req)
|
2021-11-06 13:13:20 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2021-08-17 16:08:32 +00:00
|
|
|
|
|
|
|
admin := dataprovider.Admin{
|
|
|
|
Username: "",
|
|
|
|
Password: user.Password,
|
|
|
|
}
|
|
|
|
c = jwtTokenClaims{
|
|
|
|
Username: admin.Username,
|
|
|
|
Permissions: nil,
|
|
|
|
Signature: admin.GetSignature(),
|
|
|
|
}
|
2022-03-24 21:03:17 +00:00
|
|
|
token, err = c.createTokenResponse(server.tokenAuth, tokenAudienceWebAdmin, "")
|
2021-08-17 16:08:32 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
form = make(url.Values)
|
2022-03-26 07:41:50 +00:00
|
|
|
form.Set(csrfFormToken, createCSRFToken(""))
|
2021-08-17 16:08:32 +00:00
|
|
|
form.Set("allow_api_key_auth", "")
|
2021-09-29 16:46:15 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, webAdminProfilePath, bytes.NewBuffer([]byte(form.Encode())))
|
2021-08-17 16:08:32 +00:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("jwt=%v", token["access_token"]))
|
2022-03-19 20:44:27 +00:00
|
|
|
server.handleWebAdminProfilePost(rr, req)
|
2021-11-06 13:13:20 +00:00
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
2021-05-11 04:54:06 +00:00
|
|
|
|
|
|
|
func TestTLSReq(t *testing.T) {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, webClientLoginPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.TLS = &tls.ConnectionState{}
|
|
|
|
assert.True(t, isTLS(req))
|
|
|
|
req.TLS = nil
|
|
|
|
ctx := context.WithValue(req.Context(), forwardedProtoKey, "https")
|
|
|
|
assert.True(t, isTLS(req.WithContext(ctx)))
|
|
|
|
ctx = context.WithValue(req.Context(), forwardedProtoKey, "http")
|
|
|
|
assert.False(t, isTLS(req.WithContext(ctx)))
|
|
|
|
assert.Equal(t, "context value forwarded proto", forwardedProtoKey.String())
|
|
|
|
}
|
2021-07-01 18:17:40 +00:00
|
|
|
|
|
|
|
func TestSigningKey(t *testing.T) {
|
|
|
|
signingPassphrase := "test"
|
|
|
|
server1 := httpdServer{
|
|
|
|
signingPassphrase: signingPassphrase,
|
|
|
|
}
|
|
|
|
server1.initializeRouter()
|
|
|
|
|
|
|
|
server2 := httpdServer{
|
|
|
|
signingPassphrase: signingPassphrase,
|
|
|
|
}
|
|
|
|
server2.initializeRouter()
|
|
|
|
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "",
|
|
|
|
Password: "pwd",
|
|
|
|
},
|
2021-07-01 18:17:40 +00:00
|
|
|
}
|
|
|
|
c := jwtTokenClaims{
|
|
|
|
Username: user.Username,
|
|
|
|
Permissions: nil,
|
|
|
|
Signature: user.GetSignature(),
|
|
|
|
}
|
2022-03-24 21:03:17 +00:00
|
|
|
token, err := c.createTokenResponse(server1.tokenAuth, tokenAudienceWebClient, "")
|
2021-07-01 18:17:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
accessToken := token["access_token"].(string)
|
|
|
|
assert.NotEmpty(t, accessToken)
|
|
|
|
_, err = server1.tokenAuth.Decode(accessToken)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = server2.tokenAuth.Decode(accessToken)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-07-27 16:43:00 +00:00
|
|
|
|
|
|
|
func TestLoginLinks(t *testing.T) {
|
|
|
|
b := Binding{
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: false,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2021-07-27 16:43:00 +00:00
|
|
|
}
|
|
|
|
assert.False(t, b.showClientLoginURL())
|
|
|
|
b = Binding{
|
|
|
|
EnableWebAdmin: false,
|
|
|
|
EnableWebClient: true,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2021-07-27 16:43:00 +00:00
|
|
|
}
|
|
|
|
assert.False(t, b.showAdminLoginURL())
|
|
|
|
b = Binding{
|
|
|
|
EnableWebAdmin: true,
|
|
|
|
EnableWebClient: true,
|
2022-09-22 15:27:00 +00:00
|
|
|
EnableRESTAPI: true,
|
2021-07-27 16:43:00 +00:00
|
|
|
}
|
|
|
|
assert.True(t, b.showAdminLoginURL())
|
|
|
|
assert.True(t, b.showClientLoginURL())
|
|
|
|
b.HideLoginURL = 3
|
|
|
|
assert.False(t, b.showAdminLoginURL())
|
|
|
|
assert.False(t, b.showClientLoginURL())
|
|
|
|
b.HideLoginURL = 1
|
|
|
|
assert.True(t, b.showAdminLoginURL())
|
|
|
|
assert.False(t, b.showClientLoginURL())
|
|
|
|
b.HideLoginURL = 2
|
|
|
|
assert.False(t, b.showAdminLoginURL())
|
|
|
|
assert.True(t, b.showClientLoginURL())
|
|
|
|
}
|
2021-11-13 12:25:43 +00:00
|
|
|
|
|
|
|
func TestResetCodesCleanup(t *testing.T) {
|
|
|
|
resetCode := newResetCode(util.GenerateUniqueID(), false)
|
|
|
|
resetCode.ExpiresAt = time.Now().Add(-1 * time.Minute).UTC()
|
2022-05-19 17:49:51 +00:00
|
|
|
err := resetCodesMgr.Add(resetCode)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
resetCodesMgr.Cleanup()
|
|
|
|
_, err = resetCodesMgr.Get(resetCode.Code)
|
|
|
|
assert.Error(t, err)
|
2021-11-13 12:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserCanResetPassword(t *testing.T) {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, webClientLoginPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.RemoteAddr = "172.16.9.2:55080"
|
|
|
|
|
|
|
|
u := dataprovider.User{}
|
|
|
|
assert.True(t, isUserAllowedToResetPassword(req, &u))
|
|
|
|
u.Filters.DeniedProtocols = []string{common.ProtocolHTTP}
|
|
|
|
assert.False(t, isUserAllowedToResetPassword(req, &u))
|
|
|
|
u.Filters.DeniedProtocols = nil
|
|
|
|
u.Filters.WebClient = []string{sdk.WebClientPasswordResetDisabled}
|
|
|
|
assert.False(t, isUserAllowedToResetPassword(req, &u))
|
|
|
|
u.Filters.WebClient = nil
|
|
|
|
u.Filters.DeniedLoginMethods = []string{dataprovider.LoginMethodPassword}
|
|
|
|
assert.False(t, isUserAllowedToResetPassword(req, &u))
|
|
|
|
u.Filters.DeniedLoginMethods = nil
|
|
|
|
u.Filters.AllowedIP = []string{"127.0.0.1/8"}
|
|
|
|
assert.False(t, isUserAllowedToResetPassword(req, &u))
|
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
|
2022-02-06 15:46:43 +00:00
|
|
|
func TestBrowsableSharePaths(t *testing.T) {
|
|
|
|
share := dataprovider.Share{
|
2022-05-09 17:09:43 +00:00
|
|
|
Paths: []string{"/"},
|
|
|
|
Username: defaultAdminUsername,
|
|
|
|
}
|
|
|
|
_, err := getUserForShare(share)
|
|
|
|
if assert.Error(t, err) {
|
2023-01-25 17:49:03 +00:00
|
|
|
assert.ErrorIs(t, err, util.ErrNotFound)
|
2022-02-06 15:46:43 +00:00
|
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "/share", nil)
|
|
|
|
require.NoError(t, err)
|
2023-12-28 17:43:07 +00:00
|
|
|
name, err := getBrowsableSharedPath(share.Paths[0], req)
|
2022-02-06 15:46:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/", name)
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "/share?path=abc", nil)
|
|
|
|
require.NoError(t, err)
|
2023-12-28 17:43:07 +00:00
|
|
|
name, err = getBrowsableSharedPath(share.Paths[0], req)
|
2022-02-06 15:46:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/abc", name)
|
|
|
|
|
|
|
|
share.Paths = []string{"/a/b/c"}
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "/share?path=abc", nil)
|
|
|
|
require.NoError(t, err)
|
2023-12-28 17:43:07 +00:00
|
|
|
name, err = getBrowsableSharedPath(share.Paths[0], req)
|
2022-02-06 15:46:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/a/b/c/abc", name)
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "/share?path=%2Fabc/d", nil)
|
|
|
|
require.NoError(t, err)
|
2023-12-28 17:43:07 +00:00
|
|
|
name, err = getBrowsableSharedPath(share.Paths[0], req)
|
2022-02-06 15:46:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/a/b/c/abc/d", name)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "/share?path=%2Fabc%2F..%2F..", nil)
|
|
|
|
require.NoError(t, err)
|
2023-12-28 17:43:07 +00:00
|
|
|
_, err = getBrowsableSharedPath(share.Paths[0], req)
|
2022-02-06 15:46:43 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "/share?path=%2Fabc%2F..", nil)
|
|
|
|
require.NoError(t, err)
|
2023-12-28 17:43:07 +00:00
|
|
|
name, err = getBrowsableSharedPath(share.Paths[0], req)
|
2022-02-06 15:46:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/a/b/c", name)
|
|
|
|
|
|
|
|
share = dataprovider.Share{
|
|
|
|
Paths: []string{"/a", "/b"},
|
|
|
|
}
|
|
|
|
}
|
2022-02-17 17:22:27 +00:00
|
|
|
|
|
|
|
func TestSecureMiddlewareIntegration(t *testing.T) {
|
|
|
|
forwardedHostHeader := "X-Forwarded-Host"
|
|
|
|
server := httpdServer{
|
|
|
|
binding: Binding{
|
|
|
|
ProxyAllowed: []string{"192.168.1.0/24"},
|
|
|
|
Security: SecurityConf{
|
|
|
|
Enabled: true,
|
|
|
|
AllowedHosts: []string{"*.sftpgo.com"},
|
|
|
|
AllowedHostsAreRegex: true,
|
|
|
|
HostsProxyHeaders: []string{forwardedHostHeader},
|
|
|
|
HTTPSProxyHeaders: []HTTPSProxyHeader{
|
|
|
|
{
|
|
|
|
Key: xForwardedProto,
|
|
|
|
Value: "https",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
STSSeconds: 31536000,
|
|
|
|
STSIncludeSubdomains: true,
|
|
|
|
STSPreload: true,
|
|
|
|
ContentTypeNosniff: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
enableWebAdmin: true,
|
|
|
|
enableWebClient: true,
|
2022-09-22 15:27:00 +00:00
|
|
|
enableRESTAPI: true,
|
2022-02-17 17:22:27 +00:00
|
|
|
}
|
|
|
|
server.binding.Security.updateProxyHeaders()
|
|
|
|
err := server.binding.parseAllowedProxy()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{forwardedHostHeader, xForwardedProto}, server.binding.Security.proxyHeaders)
|
|
|
|
assert.Equal(t, map[string]string{xForwardedProto: "https"}, server.binding.Security.getHTTPSProxyHeaders())
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
r, err := http.NewRequest(http.MethodGet, webClientLoginPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.Host = "127.0.0.1"
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r.Header.Set(forwardedHostHeader, "www.sftpgo.com")
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
// the header should be removed
|
|
|
|
assert.Empty(t, r.Header.Get(forwardedHostHeader))
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r.Host = "test.sftpgo.com"
|
|
|
|
r.Header.Set(forwardedHostHeader, "test.example.com")
|
|
|
|
r.RemoteAddr = "192.168.1.1"
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rr.Code)
|
|
|
|
assert.NotEmpty(t, r.Header.Get(forwardedHostHeader))
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r.Header.Set(forwardedHostHeader, "www.sftpgo.com")
|
|
|
|
r.RemoteAddr = "192.168.1.1"
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
assert.NotEmpty(t, r.Header.Get(forwardedHostHeader))
|
|
|
|
assert.Empty(t, rr.Header().Get("Strict-Transport-Security"))
|
|
|
|
assert.Equal(t, "nosniff", rr.Header().Get("X-Content-Type-Options"))
|
|
|
|
// now set the X-Forwarded-Proto to https, we should get the Strict-Transport-Security header
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r.Host = "test.sftpgo.com"
|
|
|
|
r.Header.Set(xForwardedProto, "https")
|
|
|
|
r.RemoteAddr = "192.168.1.3"
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
assert.NotEmpty(t, r.Header.Get(forwardedHostHeader))
|
|
|
|
assert.Equal(t, "max-age=31536000; includeSubDomains; preload", rr.Header().Get("Strict-Transport-Security"))
|
|
|
|
assert.Equal(t, "nosniff", rr.Header().Get("X-Content-Type-Options"))
|
|
|
|
|
|
|
|
server.binding.Security.Enabled = false
|
|
|
|
server.binding.Security.updateProxyHeaders()
|
|
|
|
assert.Len(t, server.binding.Security.proxyHeaders, 0)
|
|
|
|
}
|
2022-02-27 12:08:47 +00:00
|
|
|
|
2022-07-26 16:24:17 +00:00
|
|
|
func TestGetCompressedFileName(t *testing.T) {
|
|
|
|
username := "test"
|
|
|
|
res := getCompressedFileName(username, []string{"single dir"})
|
|
|
|
require.Equal(t, fmt.Sprintf("%s-single dir.zip", username), res)
|
|
|
|
res = getCompressedFileName(username, []string{"file1", "file2"})
|
|
|
|
require.Equal(t, fmt.Sprintf("%s-download.zip", username), res)
|
|
|
|
res = getCompressedFileName(username, []string{"file1.txt"})
|
|
|
|
require.Equal(t, fmt.Sprintf("%s-file1.zip", username), res)
|
|
|
|
// now files with full paths
|
|
|
|
res = getCompressedFileName(username, []string{"/dir/single dir"})
|
|
|
|
require.Equal(t, fmt.Sprintf("%s-single dir.zip", username), res)
|
|
|
|
res = getCompressedFileName(username, []string{"/adir/file1", "/adir/file2"})
|
|
|
|
require.Equal(t, fmt.Sprintf("%s-download.zip", username), res)
|
|
|
|
res = getCompressedFileName(username, []string{"/sub/dir/file1.txt"})
|
|
|
|
require.Equal(t, fmt.Sprintf("%s-file1.zip", username), res)
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:27:00 +00:00
|
|
|
func TestRESTAPIDisabled(t *testing.T) {
|
|
|
|
server := httpdServer{
|
|
|
|
enableWebAdmin: true,
|
|
|
|
enableWebClient: true,
|
|
|
|
enableRESTAPI: false,
|
|
|
|
}
|
|
|
|
server.initializeRouter()
|
|
|
|
assert.False(t, server.enableRESTAPI)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
r, err := http.NewRequest(http.MethodGet, healthzPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodGet, tokenPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusNotFound, rr.Code)
|
|
|
|
}
|
|
|
|
|
2022-02-27 12:08:47 +00:00
|
|
|
func TestWebAdminSetupWithInstallCode(t *testing.T) {
|
|
|
|
installationCode = "1234"
|
|
|
|
// delete all the admins
|
|
|
|
admins, err := dataprovider.GetAdmins(100, 0, dataprovider.OrderASC)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, admin := range admins {
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteAdmin(admin.Username, "", "", "")
|
2022-02-27 12:08:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// close the provider and initializes it without creating the default admin
|
|
|
|
providerConf := dataprovider.GetProviderConfig()
|
|
|
|
providerConf.CreateDefaultAdmin = false
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
2022-07-24 14:18:54 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2022-02-27 12:08:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
server := httpdServer{
|
|
|
|
enableWebAdmin: true,
|
|
|
|
enableWebClient: true,
|
2022-09-22 15:27:00 +00:00
|
|
|
enableRESTAPI: true,
|
2022-02-27 12:08:47 +00:00
|
|
|
}
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
r, err := http.NewRequest(http.MethodGet, webAdminSetupPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
for _, webURL := range []string{"/", webBasePath, webBaseAdminPath, webAdminLoginPath, webClientLoginPath} {
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodGet, webURL, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
|
|
|
assert.Equal(t, webAdminSetupPath, rr.Header().Get("Location"))
|
|
|
|
}
|
|
|
|
|
|
|
|
form := make(url.Values)
|
2022-03-26 07:41:50 +00:00
|
|
|
csrfToken := createCSRFToken("")
|
2022-02-27 12:08:47 +00:00
|
|
|
form.Set("_form_token", csrfToken)
|
2022-04-03 06:52:36 +00:00
|
|
|
form.Set("install_code", installationCode+"5")
|
2022-02-27 12:08:47 +00:00
|
|
|
form.Set("username", defaultAdminUsername)
|
|
|
|
form.Set("password", "password")
|
|
|
|
form.Set("confirm_password", "password")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodPost, webAdminSetupPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorSetupInstallCode)
|
2022-02-27 12:08:47 +00:00
|
|
|
|
|
|
|
_, err = dataprovider.AdminExists(defaultAdminUsername)
|
|
|
|
assert.Error(t, err)
|
2022-04-03 06:52:36 +00:00
|
|
|
form.Set("install_code", installationCode)
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodPost, webAdminSetupPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
2024-02-04 19:58:29 +00:00
|
|
|
assert.Equal(t, webAdminMFAPath, rr.Header().Get("Location"))
|
2022-04-03 06:52:36 +00:00
|
|
|
|
|
|
|
_, err = dataprovider.AdminExists(defaultAdminUsername)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// delete the admin and test the installation code resolver
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.DeleteAdmin(defaultAdminUsername, "", "", "")
|
2022-04-03 06:52:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
2022-07-24 14:18:54 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2022-04-03 06:52:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-15 20:13:45 +00:00
|
|
|
SetInstallationCodeResolver(func(_ string) string {
|
2022-04-03 06:52:36 +00:00
|
|
|
return "5678"
|
|
|
|
})
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodGet, webAdminSetupPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
for _, webURL := range []string{"/", webBasePath, webBaseAdminPath, webAdminLoginPath, webClientLoginPath} {
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodGet, webURL, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
|
|
|
assert.Equal(t, webAdminSetupPath, rr.Header().Get("Location"))
|
|
|
|
}
|
|
|
|
|
|
|
|
form = make(url.Values)
|
|
|
|
csrfToken = createCSRFToken("")
|
|
|
|
form.Set("_form_token", csrfToken)
|
|
|
|
form.Set("install_code", installationCode)
|
|
|
|
form.Set("username", defaultAdminUsername)
|
|
|
|
form.Set("password", "password")
|
|
|
|
form.Set("confirm_password", "password")
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodPost, webAdminSetupPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
2024-02-03 11:42:05 +00:00
|
|
|
assert.Contains(t, rr.Body.String(), util.I18nErrorSetupInstallCode)
|
2022-04-03 06:52:36 +00:00
|
|
|
|
|
|
|
_, err = dataprovider.AdminExists(defaultAdminUsername)
|
|
|
|
assert.Error(t, err)
|
|
|
|
form.Set("install_code", "5678")
|
2022-02-27 12:08:47 +00:00
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodPost, webAdminSetupPath, bytes.NewBuffer([]byte(form.Encode())))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusFound, rr.Code)
|
2024-02-04 19:58:29 +00:00
|
|
|
assert.Equal(t, webAdminMFAPath, rr.Header().Get("Location"))
|
2022-02-27 12:08:47 +00:00
|
|
|
|
|
|
|
_, err = dataprovider.AdminExists(defaultAdminUsername)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf.CreateDefaultAdmin = true
|
2022-07-24 14:18:54 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2022-02-27 12:08:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
installationCode = ""
|
2022-04-03 06:52:36 +00:00
|
|
|
SetInstallationCodeResolver(nil)
|
2022-02-27 12:08:47 +00:00
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
|
|
|
|
func TestDbResetCodeManager(t *testing.T) {
|
|
|
|
if !isSharedProviderSupported() {
|
|
|
|
t.Skip("this test it is not available with this provider")
|
|
|
|
}
|
|
|
|
mgr := newResetCodeManager(1)
|
|
|
|
resetCode := newResetCode("admin", true)
|
|
|
|
err := mgr.Add(resetCode)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
codeGet, err := mgr.Get(resetCode.Code)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, resetCode, codeGet)
|
|
|
|
err = mgr.Delete(resetCode.Code)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = mgr.Delete(resetCode.Code)
|
|
|
|
if assert.Error(t, err) {
|
2023-01-25 17:49:03 +00:00
|
|
|
assert.ErrorIs(t, err, util.ErrNotFound)
|
2022-05-19 17:49:51 +00:00
|
|
|
}
|
|
|
|
_, err = mgr.Get(resetCode.Code)
|
2024-03-11 17:19:57 +00:00
|
|
|
assert.ErrorIs(t, err, util.ErrNotFound)
|
2022-05-19 17:49:51 +00:00
|
|
|
// add an expired reset code
|
|
|
|
resetCode = newResetCode("user", false)
|
|
|
|
resetCode.ExpiresAt = time.Now().Add(-24 * time.Hour)
|
|
|
|
err = mgr.Add(resetCode)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = mgr.Get(resetCode.Code)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "reset code expired")
|
|
|
|
}
|
|
|
|
mgr.Cleanup()
|
|
|
|
_, err = mgr.Get(resetCode.Code)
|
2024-03-11 17:19:57 +00:00
|
|
|
assert.ErrorIs(t, err, util.ErrNotFound)
|
2022-05-19 17:49:51 +00:00
|
|
|
|
|
|
|
dbMgr, ok := mgr.(*dbResetCodeManager)
|
|
|
|
if assert.True(t, ok) {
|
|
|
|
_, err = dbMgr.decodeData("astring")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 16:15:34 +00:00
|
|
|
func TestDecodeToken(t *testing.T) {
|
|
|
|
nodeID := "nodeID"
|
|
|
|
token := map[string]any{
|
|
|
|
claimUsernameKey: defaultAdminUsername,
|
|
|
|
claimPermissionsKey: []string{dataprovider.PermAdminAny},
|
|
|
|
jwt.SubjectKey: "",
|
|
|
|
claimNodeID: nodeID,
|
|
|
|
claimMustChangePasswordKey: false,
|
|
|
|
claimMustSetSecondFactorKey: true,
|
|
|
|
}
|
|
|
|
c := jwtTokenClaims{}
|
|
|
|
c.Decode(token)
|
|
|
|
assert.Equal(t, defaultAdminUsername, c.Username)
|
|
|
|
assert.Equal(t, nodeID, c.NodeID)
|
|
|
|
assert.False(t, c.MustChangePassword)
|
|
|
|
assert.True(t, c.MustSetTwoFactorAuth)
|
|
|
|
|
|
|
|
token[claimMustChangePasswordKey] = 10
|
|
|
|
c = jwtTokenClaims{}
|
|
|
|
c.Decode(token)
|
|
|
|
assert.False(t, c.MustChangePassword)
|
|
|
|
|
|
|
|
token[claimMustChangePasswordKey] = true
|
|
|
|
c = jwtTokenClaims{}
|
|
|
|
c.Decode(token)
|
|
|
|
assert.True(t, c.MustChangePassword)
|
|
|
|
|
|
|
|
claims := c.asMap()
|
|
|
|
assert.Equal(t, token, claims)
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:45:27 +00:00
|
|
|
func TestEventRoleFilter(t *testing.T) {
|
|
|
|
defaultVal := "default"
|
|
|
|
req, err := http.NewRequest(http.MethodGet, fsEventsPath+"?role=role1", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
role := getRoleFilterForEventSearch(req, defaultVal)
|
|
|
|
assert.Equal(t, defaultVal, role)
|
|
|
|
role = getRoleFilterForEventSearch(req, "")
|
|
|
|
assert.Equal(t, "role1", role)
|
|
|
|
}
|
|
|
|
|
2022-12-07 17:47:38 +00:00
|
|
|
func TestEventsCSV(t *testing.T) {
|
|
|
|
e := fsEvent{
|
|
|
|
Status: 1,
|
|
|
|
}
|
|
|
|
data := e.getCSVData()
|
2023-02-13 11:58:21 +00:00
|
|
|
assert.Equal(t, "OK", data[5])
|
2022-12-07 17:47:38 +00:00
|
|
|
e.Status = 2
|
|
|
|
data = e.getCSVData()
|
2023-02-13 11:58:21 +00:00
|
|
|
assert.Equal(t, "KO", data[5])
|
2022-12-07 17:47:38 +00:00
|
|
|
e.Status = 3
|
|
|
|
data = e.getCSVData()
|
2023-02-13 11:58:21 +00:00
|
|
|
assert.Equal(t, "Quota exceeded", data[5])
|
2022-12-07 17:47:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 18:25:20 +00:00
|
|
|
func TestConfigsFromProvider(t *testing.T) {
|
|
|
|
err := dataprovider.UpdateConfigs(nil, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
c := Conf{
|
|
|
|
Bindings: []Binding{
|
|
|
|
{
|
|
|
|
Port: 1234,
|
|
|
|
},
|
2023-02-24 19:08:14 +00:00
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Security: SecurityConf{
|
|
|
|
Enabled: true,
|
|
|
|
HTTPSRedirect: true,
|
|
|
|
},
|
|
|
|
},
|
2023-02-23 18:25:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2023-02-24 19:08:14 +00:00
|
|
|
assert.False(t, c.Bindings[1].EnableHTTPS)
|
2023-02-23 18:25:20 +00:00
|
|
|
// protocols does not match
|
|
|
|
configs.ACME.Protocols = 6
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-02-24 19:08:14 +00:00
|
|
|
func TestHTTPSRedirect(t *testing.T) {
|
|
|
|
acmeWebRoot := filepath.Join(os.TempDir(), "acme")
|
|
|
|
err := os.MkdirAll(acmeWebRoot, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tokenName := "token"
|
|
|
|
err = os.WriteFile(filepath.Join(acmeWebRoot, tokenName), []byte("val"), 0666)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
acmeConfig := acme.Configuration{
|
|
|
|
HTTP01Challenge: acme.HTTP01Challenge{WebRoot: acmeWebRoot},
|
|
|
|
}
|
|
|
|
err = acme.Initialize(acmeConfig, configDir, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
forwardedHostHeader := "X-Forwarded-Host"
|
|
|
|
server := httpdServer{
|
|
|
|
binding: Binding{
|
|
|
|
Security: SecurityConf{
|
|
|
|
Enabled: true,
|
|
|
|
HTTPSRedirect: true,
|
|
|
|
HostsProxyHeaders: []string{forwardedHostHeader},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
server.initializeRouter()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
r, err := http.NewRequest(http.MethodGet, path.Join(acmeChallengeURI, tokenName), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.Host = "localhost"
|
|
|
|
r.RequestURI = path.Join(acmeChallengeURI, tokenName)
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodGet, webAdminLoginPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.RequestURI = webAdminLoginPath
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, rr.Code, rr.Body.String())
|
|
|
|
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodGet, webAdminLoginPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.RequestURI = webAdminLoginPath
|
|
|
|
r.Header.Set(forwardedHostHeader, "sftpgo.com")
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, rr.Code, rr.Body.String())
|
|
|
|
assert.Contains(t, rr.Body.String(), "https://sftpgo.com")
|
|
|
|
|
|
|
|
server.binding.Security.HTTPSHost = "myhost:1044"
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
r, err = http.NewRequest(http.MethodGet, webAdminLoginPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.RequestURI = webAdminLoginPath
|
|
|
|
server.router.ServeHTTP(rr, r)
|
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, rr.Code, rr.Body.String())
|
|
|
|
assert.Contains(t, rr.Body.String(), "https://myhost:1044")
|
|
|
|
|
|
|
|
err = os.RemoveAll(acmeWebRoot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-05-12 16:34:59 +00:00
|
|
|
func TestGetLogEventString(t *testing.T) {
|
|
|
|
assert.Equal(t, "Login failed", getLogEventString(notifier.LogEventTypeLoginFailed))
|
|
|
|
assert.Equal(t, "Login with non-existent user", getLogEventString(notifier.LogEventTypeLoginNoUser))
|
|
|
|
assert.Equal(t, "No login tried", getLogEventString(notifier.LogEventTypeNoLoginTried))
|
|
|
|
assert.Equal(t, "Algorithm negotiation failed", getLogEventString(notifier.LogEventTypeNotNegotiated))
|
2024-04-10 16:39:08 +00:00
|
|
|
assert.Equal(t, "Login succeeded", getLogEventString(notifier.LogEventTypeLoginOK))
|
2023-05-12 16:34:59 +00:00
|
|
|
assert.Empty(t, getLogEventString(0))
|
|
|
|
}
|
|
|
|
|
2023-06-16 19:06:21 +00:00
|
|
|
func TestUserQuotaUsage(t *testing.T) {
|
|
|
|
usage := userQuotaUsage{
|
|
|
|
QuotaSize: 100,
|
|
|
|
}
|
|
|
|
require.True(t, usage.HasQuotaInfo())
|
|
|
|
require.NotEmpty(t, usage.GetQuotaSize())
|
|
|
|
providerConf := dataprovider.GetProviderConfig()
|
|
|
|
quotaTracking := dataprovider.GetQuotaTracking()
|
|
|
|
providerConf.TrackQuota = 0
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, usage.HasQuotaInfo())
|
|
|
|
providerConf.TrackQuota = quotaTracking
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
usage.QuotaSize = 0
|
|
|
|
assert.False(t, usage.HasQuotaInfo())
|
|
|
|
assert.Empty(t, usage.GetQuotaSize())
|
|
|
|
assert.Equal(t, 0, usage.GetQuotaSizePercentage())
|
|
|
|
assert.False(t, usage.IsQuotaSizeLow())
|
|
|
|
assert.False(t, usage.IsDiskQuotaLow())
|
|
|
|
assert.False(t, usage.IsQuotaLow())
|
|
|
|
usage.UsedQuotaSize = 9
|
|
|
|
assert.NotEmpty(t, usage.GetQuotaSize())
|
|
|
|
usage.QuotaSize = 10
|
|
|
|
assert.True(t, usage.IsQuotaSizeLow())
|
|
|
|
assert.True(t, usage.IsDiskQuotaLow())
|
|
|
|
assert.True(t, usage.IsQuotaLow())
|
|
|
|
usage.DownloadDataTransfer = 1
|
|
|
|
assert.True(t, usage.HasQuotaInfo())
|
|
|
|
assert.True(t, usage.HasTranferQuota())
|
|
|
|
assert.Empty(t, usage.GetQuotaFiles())
|
|
|
|
assert.Equal(t, 0, usage.GetQuotaFilesPercentage())
|
|
|
|
usage.QuotaFiles = 1
|
|
|
|
assert.NotEmpty(t, usage.GetQuotaFiles())
|
|
|
|
usage.QuotaFiles = 0
|
|
|
|
usage.UsedQuotaFiles = 9
|
|
|
|
assert.NotEmpty(t, usage.GetQuotaFiles())
|
|
|
|
usage.QuotaFiles = 10
|
|
|
|
usage.DownloadDataTransfer = 0
|
|
|
|
assert.True(t, usage.IsQuotaFilesLow())
|
|
|
|
assert.True(t, usage.IsDiskQuotaLow())
|
|
|
|
assert.False(t, usage.IsTotalTransferQuotaLow())
|
|
|
|
assert.False(t, usage.IsUploadTransferQuotaLow())
|
|
|
|
assert.False(t, usage.IsDownloadTransferQuotaLow())
|
|
|
|
assert.Equal(t, 0, usage.GetTotalTransferQuotaPercentage())
|
|
|
|
assert.Equal(t, 0, usage.GetUploadTransferQuotaPercentage())
|
|
|
|
assert.Equal(t, 0, usage.GetDownloadTransferQuotaPercentage())
|
|
|
|
assert.Empty(t, usage.GetTotalTransferQuota())
|
|
|
|
assert.Empty(t, usage.GetUploadTransferQuota())
|
|
|
|
assert.Empty(t, usage.GetDownloadTransferQuota())
|
|
|
|
usage.TotalDataTransfer = 3
|
|
|
|
usage.UsedUploadDataTransfer = 1 * 1048576
|
|
|
|
assert.NotEmpty(t, usage.GetTotalTransferQuota())
|
|
|
|
usage.TotalDataTransfer = 0
|
|
|
|
assert.NotEmpty(t, usage.GetTotalTransferQuota())
|
|
|
|
assert.NotEmpty(t, usage.GetUploadTransferQuota())
|
|
|
|
usage.UploadDataTransfer = 2
|
|
|
|
assert.NotEmpty(t, usage.GetUploadTransferQuota())
|
|
|
|
usage.UsedDownloadDataTransfer = 1 * 1048576
|
|
|
|
assert.NotEmpty(t, usage.GetDownloadTransferQuota())
|
|
|
|
usage.DownloadDataTransfer = 2
|
|
|
|
assert.NotEmpty(t, usage.GetDownloadTransferQuota())
|
|
|
|
assert.False(t, usage.IsTransferQuotaLow())
|
|
|
|
usage.UsedDownloadDataTransfer = 8 * 1048576
|
|
|
|
usage.TotalDataTransfer = 10
|
|
|
|
assert.True(t, usage.IsTotalTransferQuotaLow())
|
|
|
|
assert.True(t, usage.IsTransferQuotaLow())
|
|
|
|
usage.TotalDataTransfer = 0
|
|
|
|
usage.UploadDataTransfer = 0
|
|
|
|
usage.DownloadDataTransfer = 0
|
|
|
|
assert.False(t, usage.IsTransferQuotaLow())
|
|
|
|
usage.UploadDataTransfer = 10
|
|
|
|
usage.UsedUploadDataTransfer = 9 * 1048576
|
|
|
|
assert.True(t, usage.IsUploadTransferQuotaLow())
|
|
|
|
assert.True(t, usage.IsTransferQuotaLow())
|
|
|
|
usage.DownloadDataTransfer = 10
|
|
|
|
usage.UsedDownloadDataTransfer = 9 * 1048576
|
|
|
|
assert.True(t, usage.IsDownloadTransferQuotaLow())
|
|
|
|
assert.True(t, usage.IsTransferQuotaLow())
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:10:03 +00:00
|
|
|
func TestShareRedirectURL(t *testing.T) {
|
|
|
|
shareID := util.GenerateUniqueID()
|
|
|
|
base := path.Join(webClientPubSharesPath, shareID)
|
|
|
|
next := path.Join(webClientPubSharesPath, shareID, "browse")
|
|
|
|
ok, res := checkShareRedirectURL(next, base)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, next, res)
|
|
|
|
next = path.Join(webClientPubSharesPath, shareID, "browse") + "?a=b"
|
|
|
|
ok, res = checkShareRedirectURL(next, base)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, next, res)
|
|
|
|
next = path.Join(webClientPubSharesPath, shareID)
|
|
|
|
ok, res = checkShareRedirectURL(next, base)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, path.Join(base, "download"), res)
|
|
|
|
next = path.Join(webClientEditFilePath, shareID)
|
|
|
|
ok, res = checkShareRedirectURL(next, base)
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, res)
|
|
|
|
next = path.Join(webClientPubSharesPath, shareID) + "?compress=false&a=b"
|
|
|
|
ok, res = checkShareRedirectURL(next, base)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, path.Join(base, "download?compress=false&a=b"), res)
|
|
|
|
next = path.Join(webClientPubSharesPath, shareID) + "?compress=true&b=c"
|
|
|
|
ok, res = checkShareRedirectURL(next, base)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, path.Join(base, "download?compress=true&b=c"), res)
|
|
|
|
ok, res = checkShareRedirectURL("http://foo\x7f.com/ab", "http://foo\x7f.com/")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, res)
|
|
|
|
ok, res = checkShareRedirectURL("http://foo.com/?foo\nbar", "http://foo.com")
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Empty(t, res)
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:40:13 +00:00
|
|
|
func TestI18NMessages(t *testing.T) {
|
|
|
|
msg := i18nListDirMsg(http.StatusForbidden)
|
|
|
|
require.Equal(t, util.I18nErrorDirList403, msg)
|
|
|
|
msg = i18nListDirMsg(http.StatusInternalServerError)
|
|
|
|
require.Equal(t, util.I18nErrorDirListGeneric, msg)
|
|
|
|
msg = i18nFsMsg(http.StatusForbidden)
|
|
|
|
require.Equal(t, util.I18nError403Message, msg)
|
|
|
|
msg = i18nFsMsg(http.StatusInternalServerError)
|
|
|
|
require.Equal(t, util.I18nErrorFsGeneric, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestI18NErrors(t *testing.T) {
|
|
|
|
err := util.NewValidationError("error text")
|
|
|
|
errI18n := util.NewI18nError(err, util.I18nError500Message)
|
|
|
|
assert.ErrorIs(t, errI18n, util.ErrValidation)
|
|
|
|
assert.Equal(t, err.Error(), errI18n.Error())
|
|
|
|
assert.Equal(t, util.I18nError500Message, getI18NErrorString(errI18n, ""))
|
2023-12-12 17:04:14 +00:00
|
|
|
assert.Equal(t, util.I18nError500Message, errI18n.Message)
|
|
|
|
assert.Equal(t, "{}", errI18n.Args())
|
|
|
|
var e1 *util.ValidationError
|
|
|
|
assert.ErrorAs(t, errI18n, &e1)
|
|
|
|
var e2 *util.I18nError
|
|
|
|
assert.ErrorAs(t, errI18n, &e2)
|
2023-12-10 15:40:13 +00:00
|
|
|
err2 := util.NewI18nError(fs.ErrNotExist, util.I18nError500Message)
|
|
|
|
assert.ErrorIs(t, err2, &util.I18nError{})
|
|
|
|
assert.ErrorIs(t, err2, fs.ErrNotExist)
|
|
|
|
assert.NotErrorIs(t, err2, fs.ErrExist)
|
|
|
|
assert.Equal(t, util.I18nError403Message, getI18NErrorString(fs.ErrClosed, util.I18nError403Message))
|
|
|
|
errorString := getI18NErrorString(nil, util.I18nError500Message)
|
|
|
|
assert.Equal(t, util.I18nError500Message, errorString)
|
|
|
|
errI18nWrap := util.NewI18nError(errI18n, util.I18nError404Message)
|
2023-12-12 17:04:14 +00:00
|
|
|
assert.Equal(t, util.I18nError500Message, errI18nWrap.Message)
|
|
|
|
errI18n = util.NewI18nError(err, util.I18nError500Message, util.I18nErrorArgs(map[string]any{"a": "b"}))
|
|
|
|
assert.Equal(t, util.I18nError500Message, errI18n.Message)
|
|
|
|
assert.Equal(t, `{"a":"b"}`, errI18n.Args())
|
2023-12-10 15:40:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 17:49:51 +00:00
|
|
|
func isSharedProviderSupported() bool {
|
|
|
|
// SQLite shares the implementation with other SQL-based provider but it makes no sense
|
|
|
|
// to use it outside test cases
|
|
|
|
switch dataprovider.GetProviderStatus().Driver {
|
|
|
|
case dataprovider.MySQLDataProviderName, dataprovider.PGSQLDataProviderName,
|
|
|
|
dataprovider.CockroachDataProviderName, dataprovider.SQLiteDataProviderName:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|