Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(hub): enforce password char and byte limits on first-run form (#2744, #2745)

Santhi Prakash9760e6ec524e
diff --git a/internal/site/src/components/login/auth-form.tsx b/internal/site/src/components/login/auth-form.tsx
index fb957a4e4..5847134a1 100644
--- a/internal/site/src/components/login/auth-form.tsx
+++ b/internal/site/src/components/login/auth-form.tsx
@@ -21,6 +21,7 @@ const emailSchema = v.pipe(v.string(), v.rfcEmail(t`Invalid email address.`))
const passwordSchema = v.pipe(
v.string(),
v.minLength(8, t`Password must be at least 8 characters.`),
+ v.maxLength(71, t`Password must be at most 71 characters.`),
v.maxBytes(72, t`Password must be less than 72 bytes.`)
)
diff --git a/internal/users/users.go b/internal/users/users.go
index 25cabaef0..c34823c71 100644
--- a/internal/users/users.go
+++ b/internal/users/users.go
@@ -99,6 +99,12 @@ func (um *UserManager) CreateFirstUser(e *core.RequestEvent) error {
if data.Email == "" || data.Password == "" {
return e.JSON(http.StatusBadRequest, map[string]string{"err": "Bad request"})
}
+ // bcrypt (used by the password field) rejects passwords longer than 72
+ // bytes; PocketBase's field validation counts characters, so a multibyte
+ // password can pass it and then fail hashing with a confusing error.
+ if len(data.Password) > 72 {
+ return e.JSON(http.StatusBadRequest, map[string]string{"err": "Password must be less than 72 bytes."})
+ }
err := um.app.RunInTransaction(func(txApp core.App) error {
totalUsers, err := txApp.CountRecords("users")
diff --git a/internal/users/users_test.go b/internal/users/users_test.go
index 002bcf9b5..ffff6283e 100644
--- a/internal/users/users_test.go
+++ b/internal/users/users_test.go
@@ -5,6 +5,7 @@ package users_test
import (
"errors"
"io"
+ "net/http"
"net/http/httptest"
"strings"
"testing"
@@ -18,6 +19,58 @@ import (
"github.com/stretchr/testify/require"
)
+func TestCreateFirstUserPasswordMaxLength(t *testing.T) {
+ newHub := func(t *testing.T) (*beszelTests.TestHub, func(string) (int, string)) {
+ h, err := beszelTests.NewTestHub(t.TempDir())
+ require.NoError(t, err)
+ h.StartHub()
+ um := users.NewUserManager(h.App)
+ return h, func(password string) (int, string) {
+ req := httptest.NewRequest("POST", "/api/beszel/create-user",
+ strings.NewReader(`{"email":"[email protected]","password":"`+password+`"}`))
+ req.Header.Set("Content-Type", "application/json")
+ res := httptest.NewRecorder()
+ require.NoError(t, um.CreateFirstUser(&core.RequestEvent{App: h.App, Event: router.Event{Request: req, Response: res}}))
+ return res.Code, res.Body.String()
+ }
+ }
+
+ t.Run("72 chars exceeds the PocketBase field max and 500s", func(t *testing.T) {
+ h, invoke := newHub(t)
+ defer h.Cleanup()
+ status, body := invoke(strings.Repeat("a", 72))
+ require.Equal(t, http.StatusInternalServerError, status)
+ require.Contains(t, body, "Must be less than 71 character(s)")
+ })
+
+ t.Run("71 chars succeeds", func(t *testing.T) {
+ h, invoke := newHub(t)
+ defer h.Cleanup()
+ status, body := invoke(strings.Repeat("a", 71))
+ require.Equal(t, http.StatusOK, status)
+ require.Contains(t, body, "User created")
+ })
+
+ // 37 'é' runes = 37 characters but 74 bytes in UTF-8: it passes the
+ // 71-character field max, then bcrypt rejects passwords over 72 bytes.
+ t.Run("37 multibyte chars (74 bytes) exceeds the bcrypt limit", func(t *testing.T) {
+ h, invoke := newHub(t)
+ defer h.Cleanup()
+ status, body := invoke(strings.Repeat("é", 37))
+ require.Equal(t, http.StatusBadRequest, status)
+ require.Contains(t, body, "must be less than 72 bytes")
+ })
+
+ // 36 'é' runes = 72 bytes exactly: still within the bcrypt limit.
+ t.Run("36 multibyte chars (exactly 72 bytes) succeeds", func(t *testing.T) {
+ h, invoke := newHub(t)
+ defer h.Cleanup()
+ status, body := invoke(strings.Repeat("é", 36))
+ require.Equal(t, http.StatusOK, status)
+ require.Contains(t, body, "User created")
+ })
+}
+
type blockedBody struct {
io.Reader
entered chan struct{}