mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-17 14:29:42 -05:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa4dd3ae95 | ||
|
|
9f4b15442b | ||
|
|
759342bef3 | ||
|
|
c4072608d6 |
No files matched your search
@@ -2,6 +2,12 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. For commit guidelines, please refer to [Standard Version](https://github.com/conventional-changelog/standard-version).
|
||||
|
||||
## v2.0.1-stable
|
||||
|
||||
**Security**:
|
||||
- [High] New accounts named with ".." breaks out of scoped folder on creation (GHSA-c84q-q5g6-r47f) -- reporter @dylanzonix
|
||||
- [Medium] Fixed session token collision issue when multiple users login simultaneously. (GHSA-c9cr-w724-gqj3) (#3135) Thanks @dl-pt reporters: @Ishtiaque-Ahmed-Risat @AlexWhiplash @tonghuaroot
|
||||
|
||||
## v2.0.0-stable
|
||||
|
||||
This version represents the most significant change to date. It **requires** both a database migration and config structural changes. See the [migration guide](https://filebrowserquantum.com/en/docs/getting-started/v2/migration/) for step-by-step upgrade instructions, [About v2.0.0](https://filebrowserquantum.com/en/docs/getting-started/v2/about/) for a full summary.
|
||||
|
||||
@@ -6,9 +6,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/database/access"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/utils"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
|
||||
)
|
||||
|
||||
// Auther is the authentication interface.
|
||||
@@ -39,12 +40,17 @@ func MakeSignedTokenAPI(user *users.User, name string, duration time.Duration, p
|
||||
if _, ok := user.Tokens[name]; ok {
|
||||
return "", users.AuthToken{}, fmt.Errorf("key already exists with same name %v ", name)
|
||||
}
|
||||
id, err := utils.RandomHex(16)
|
||||
if err != nil {
|
||||
return "", users.AuthToken{}, fmt.Errorf("generate token id: %w", err)
|
||||
}
|
||||
now := time.Now()
|
||||
expires := now.Add(duration)
|
||||
// Create minimal token with only JWT standard claims
|
||||
claim := users.AuthToken{
|
||||
MinimalAuthToken: users.MinimalAuthToken{
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ID: id,
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
ExpiresAt: jwt.NewNumericDate(expires),
|
||||
Issuer: FB_ISSUER,
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
|
||||
)
|
||||
|
||||
func TestSignedTokensAreUniqueAtSameTime(t *testing.T) {
|
||||
origKey := settings.Config.Auth.Key
|
||||
settings.Config.Auth.Key = "test-signing-key-for-token-uniqueness"
|
||||
t.Cleanup(func() { settings.Config.Auth.Key = origKey })
|
||||
|
||||
// Freeze the clock so every token has identical issue and expiry times.
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
now := time.Now()
|
||||
tokens := make(map[string]users.AuthToken)
|
||||
for _, userID := range []uint64{1, 2} {
|
||||
user := &users.User{ID: userID}
|
||||
for _, minimal := range []bool{true, false} {
|
||||
for range 2 {
|
||||
tokenString, claim, err := MakeSignedTokenAPI(user, "test-token", time.Hour, users.Permissions{Api: true}, minimal)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, exists := tokens[tokenString]; exists {
|
||||
t.Fatalf("token collision for user %d with minimal=%v", userID, minimal)
|
||||
}
|
||||
tokens[tokenString] = claim
|
||||
}
|
||||
}
|
||||
}
|
||||
ids := make(map[string]bool)
|
||||
for tokenString, claim := range tokens {
|
||||
var parsed users.MinimalAuthToken
|
||||
if _, err := jwt.ParseWithClaims(tokenString, &parsed, JWTSigningKeyFunc()); err != nil {
|
||||
t.Fatalf("parse signed token: %v", err)
|
||||
}
|
||||
if parsed.ID == "" || ids[parsed.ID] {
|
||||
t.Fatal("each signed token must have a unique nonempty id")
|
||||
}
|
||||
ids[parsed.ID] = true
|
||||
if parsed.ID != claim.ID {
|
||||
t.Fatal("signed token id must match returned metadata")
|
||||
}
|
||||
if parsed.IssuedAt == nil || !parsed.IssuedAt.Equal(now) || parsed.ExpiresAt == nil || !parsed.ExpiresAt.Equal(now.Add(time.Hour)) {
|
||||
t.Fatal("token uniqueness must not depend on changing timestamps")
|
||||
}
|
||||
assertMinimalJWTClaims(t, tokenString)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -21,3 +21,16 @@ func TestValidateUsernameAcceptsNormalName(t *testing.T) {
|
||||
t.Fatalf("expected valid username, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateUsernameRejectsUnsafeNames(t *testing.T) {
|
||||
for _, username := range []string{"..", "....", ".", "-", "al..ice", "bob/", "./bob", `bob\`, "alice/docs", " alice", "alice "} {
|
||||
if err := ValidateUsername(username); err == nil {
|
||||
t.Fatalf("expected %q to be rejected", username)
|
||||
}
|
||||
}
|
||||
for _, username := range []string{"alice", "first.last", "j.r.r", "a-b_c", "user@example.com", "José", "李雷", ".alice", "users"} {
|
||||
if err := ValidateUsername(username); err != nil {
|
||||
t.Fatalf("expected %q to be accepted, got %v", username, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,10 @@ var reservedUsernames = map[string]struct{}{
|
||||
strings.ToLower(AnonymousUserName): {},
|
||||
}
|
||||
|
||||
// ValidateUsername rejects empty and reserved login names.
|
||||
// ValidateUsername rejects empty and reserved login names, and names that do not map to their
|
||||
// own createUserDir home directory.
|
||||
func ValidateUsername(username string) error {
|
||||
raw := username
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
return fmt.Errorf("username is empty")
|
||||
@@ -56,6 +58,11 @@ func ValidateUsername(username string) error {
|
||||
if _, reserved := reservedUsernames[strings.ToLower(username)]; reserved {
|
||||
return fmt.Errorf("username %q is reserved", username)
|
||||
}
|
||||
// The per-user home directory is derived from CleanUsername(username), so reject names
|
||||
// that it would rewrite, the names "." and "-", and names containing path separators.
|
||||
if CleanUsername(raw) != raw || raw == "." || raw == "-" || strings.ContainsAny(raw, `/\`) {
|
||||
return fmt.Errorf("username %q contains characters that are not allowed", raw)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,87 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/auth"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/state"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
|
||||
)
|
||||
|
||||
func TestSessionTokensRemainIndependent(t *testing.T) {
|
||||
setupTestEnv(t)
|
||||
origKey := settings.Config.Auth.Key
|
||||
origExp := settings.Config.Auth.TokenExpirationHours
|
||||
settings.Config.Auth.Key = "test-signing-key-for-independent-sessions"
|
||||
settings.Config.Auth.TokenExpirationHours = 2
|
||||
t.Cleanup(func() {
|
||||
settings.Config.Auth.Key = origKey
|
||||
settings.Config.Auth.TokenExpirationHours = origExp
|
||||
})
|
||||
|
||||
user := createTokenAuthUser(t, "session-user", users.Permissions{Api: true})
|
||||
admin := createTokenAuthUser(t, "session-admin", users.Permissions{Admin: true, Api: true})
|
||||
owners := []*users.User{user, user, admin}
|
||||
tokens := make([]string, len(owners))
|
||||
for i, owner := range owners {
|
||||
var err error
|
||||
tokens[i], err = mintAndRegisterSessionToken(owner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, token := range tokens {
|
||||
if userID, isSession, ok := state.HashedTokenOwner(token); !ok || !isSession || userID != owners[i].ID {
|
||||
t.Fatalf("session %d lost its owner: userID=%d isSession=%v ok=%v", i, userID, isSession, ok)
|
||||
}
|
||||
want := http.StatusForbidden
|
||||
if owners[i].Permissions.Admin {
|
||||
want = http.StatusOK
|
||||
}
|
||||
if rec := cookieRequest(t, withAdmin(mockHandler), token); rec.Code != want {
|
||||
t.Fatalf("session %d: got status %d, want %d", i, rec.Code, want)
|
||||
}
|
||||
}
|
||||
|
||||
if err := state.RevokeToken(tokens[0]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i, token := range tokens {
|
||||
want := http.StatusOK
|
||||
if i == 0 {
|
||||
want = http.StatusUnauthorized
|
||||
}
|
||||
if rec := cookieRequest(t, withUser(mockHandler), token); rec.Code != want {
|
||||
t.Fatalf("session %d after revocation: got status %d, want %d", i, rec.Code, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionTokenWithoutIDRemainsValid(t *testing.T) {
|
||||
setupTestEnv(t)
|
||||
origKey := settings.Config.Auth.Key
|
||||
settings.Config.Auth.Key = "test-signing-key-for-legacy-session"
|
||||
t.Cleanup(func() { settings.Config.Auth.Key = origKey })
|
||||
|
||||
user := createTokenAuthUser(t, "legacy-session-user", users.Permissions{Api: true})
|
||||
now := time.Now()
|
||||
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.RegisteredClaims{
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
ExpiresAt: jwt.NewNumericDate(now.Add(time.Hour)),
|
||||
Issuer: auth.FB_ISSUER,
|
||||
}).SignedString([]byte(settings.Config.Auth.Key))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := state.RegisterSessionToken(token, user.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rec := cookieRequest(t, withUser(mockHandler), token); rec.Code != http.StatusOK {
|
||||
t.Fatalf("legacy session: got status %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceSessionTokenKeepsOldTokenWhenMintFails(t *testing.T) {
|
||||
setupTestEnv(t)
|
||||
user := createTokenAuthUser(t, "rotation-user", users.Permissions{Api: true})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
|
||||
@@ -22,12 +23,23 @@ func ExpandBackendScopesForCreateUserDir(u *users.User) {
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if source.Config.CreateUserDir && scopeBaseName(scope.Scope) != cleanedUserName {
|
||||
u.BackendScopes[i].Scope = joinUnixScopePath(scope.Scope, cleanedUserName)
|
||||
if !source.Config.CreateUserDir {
|
||||
continue
|
||||
}
|
||||
// This runs more than once for a new user, so a scope that already ends in the user's
|
||||
// directory is left alone. The source's defaultUserScope itself is not a user's home:
|
||||
// a user named after its last element ("users" with "/users") gets "/users/users".
|
||||
if scopeBaseName(scope.Scope) == cleanedUserName && !sameScopePath(scope.Scope, source.Config.DefaultUserScope) {
|
||||
continue
|
||||
}
|
||||
u.BackendScopes[i].Scope = joinUnixScopePath(scope.Scope, cleanedUserName)
|
||||
}
|
||||
}
|
||||
|
||||
func sameScopePath(a, b string) bool {
|
||||
return path.Clean("/"+a) == path.Clean("/"+b)
|
||||
}
|
||||
|
||||
func scopeBaseName(scope string) string {
|
||||
scope = strings.TrimSuffix(scope, "/")
|
||||
if scope == "" || scope == "/" {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
|
||||
)
|
||||
|
||||
func TestExpandBackendScopesForCreateUserDirNeverKeepsDefaultScope(t *testing.T) {
|
||||
saved := Config
|
||||
defer func() { Config = saved }()
|
||||
Config = Settings{Server: Server{SourceMap: map[string]*Source{
|
||||
"/srv": {Path: "/srv", Name: "main", Config: SourceConfig{CreateUserDir: true, DefaultUserScope: "/users"}},
|
||||
}}}
|
||||
cases := map[string]string{"bob": "/users/bob", "users": "/users/users", "first.last": "/users/first.last"}
|
||||
for name, want := range cases {
|
||||
u := &users.User{
|
||||
FrontendUser: users.FrontendUser{Username: name},
|
||||
BackendScopes: []users.BackendScope{{Path: "/srv", Scope: "/users"}},
|
||||
}
|
||||
ExpandBackendScopesForCreateUserDir(u)
|
||||
ExpandBackendScopesForCreateUserDir(u) // runs more than once per new user; must be idempotent
|
||||
if got := u.BackendScopes[0].Scope; got != want {
|
||||
t.Fatalf("%s: scope=%q want %q", name, got, want)
|
||||
}
|
||||
}
|
||||
// An admin-assigned scope that already ends in the username is kept.
|
||||
u := &users.User{
|
||||
FrontendUser: users.FrontendUser{Username: "bob"},
|
||||
BackendScopes: []users.BackendScope{{Path: "/srv", Scope: "/users/bob"}},
|
||||
}
|
||||
ExpandBackendScopesForCreateUserDir(u)
|
||||
if got := u.BackendScopes[0].Scope; got != "/users/bob" {
|
||||
t.Fatalf("explicit scope changed: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user