mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-14 17:15:24 +03:00
fix(web): refuse oversized login usernames before recording them
A failed login keeps its username in the in-memory log buffer, the limiter table and the notifier queue, and the route accepted any body the global cap allowed. The login route now takes at most 16 KiB and answers a username over 256 bytes with the generic failure before any of those see it; the user setters refuse such a name too, so no stored username can be one the login route turns away.
This commit is contained in:
@@ -2,6 +2,7 @@ package panel
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -128,6 +129,9 @@ func (s *UserService) BumpLoginEpoch() error {
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateUser(id int, username string, password string) error {
|
||||
if len(username) > MaxUsernameLength {
|
||||
return fmt.Errorf("username can not be longer than %d bytes", MaxUsernameLength)
|
||||
}
|
||||
db := database.GetDB()
|
||||
hashedPassword, err := crypto.HashPasswordAsBcrypt(password)
|
||||
if err != nil {
|
||||
@@ -157,6 +161,8 @@ func (s *UserService) UpdateUser(id int, username string, password string) error
|
||||
func (s *UserService) UpdateFirstUser(username string, password string) error {
|
||||
if username == "" {
|
||||
return errors.New("username can not be empty")
|
||||
} else if len(username) > MaxUsernameLength {
|
||||
return fmt.Errorf("username can not be longer than %d bytes", MaxUsernameLength)
|
||||
} else if password == "" {
|
||||
return errors.New("password can not be empty")
|
||||
}
|
||||
@@ -181,3 +187,7 @@ func (s *UserService) UpdateFirstUser(username string, password string) error {
|
||||
user.LoginEpoch++
|
||||
return db.Save(user).Error
|
||||
}
|
||||
|
||||
// MaxUsernameLength bounds a panel username: the login route refuses longer names before
|
||||
// logging or recording them, so no stored name may exceed it.
|
||||
const MaxUsernameLength = 256
|
||||
|
||||
Reference in New Issue
Block a user