修复旧页面登录资源恢复并明确显示名称限制
This commit is contained in:
1 parent
362f5548b7
commit
80dab4728b
10 files changed
+117
-11
No files matched your search
@@ -30,7 +30,7 @@ func UpdateProfile(ctx *appcontext.Context, w http.ResponseWriter, req *http.Req
|
||||
}
|
||||
name, err := validation.NormalizeDisplayName(input.Name)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ParamsError, "请输入有效的显示名称", "").FPrint(w)
|
||||
response.NewErrorResponse(response.ParamsError, displayNameValidationMessage, "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if db.Instance == nil {
|
||||
|
||||
@@ -26,6 +26,8 @@ import (
|
||||
|
||||
const maxUserRequestBytes int64 = 64 << 10
|
||||
|
||||
const displayNameValidationMessage = "显示名称须为 1–10 个字符,且不能包含控制字符"
|
||||
|
||||
func decodeUserRequest(w http.ResponseWriter, req *http.Request, dst any) error {
|
||||
req.Body = http.MaxBytesReader(w, req.Body, maxUserRequestBytes)
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
@@ -89,7 +91,7 @@ func CreateUser(ctx *context.Context, w http.ResponseWriter, req *http.Request)
|
||||
}
|
||||
username, err := validation.NormalizeDisplayName(reqData.Username)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ParamsError, "参数错误", "").FPrint(w)
|
||||
response.NewErrorResponse(response.ParamsError, displayNameValidationMessage, "").FPrint(w)
|
||||
return
|
||||
}
|
||||
user := models.User{Name: username, Account: account, Disabled: reqData.Disabled, SessionVersion: 1, SecurityVersion: 1}
|
||||
@@ -462,7 +464,7 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if reqData.Username != "" {
|
||||
username, validationErr := validation.NormalizeDisplayName(reqData.Username)
|
||||
if validationErr != nil {
|
||||
response.NewErrorResponse(response.ParamsError, "参数错误", "").FPrint(w)
|
||||
response.NewErrorResponse(response.ParamsError, displayNameValidationMessage, "").FPrint(w)
|
||||
return
|
||||
}
|
||||
user.Name = username
|
||||
|
||||
@@ -54,6 +54,43 @@ func TestEditUserProtectsAdministrativeCredentialsAndRevokesUserSessions(t *test
|
||||
RecentAuth: true, RecoveryCodeConfigured: true,
|
||||
}
|
||||
|
||||
for _, invalidName := range []string{strings.Repeat("名", 11), "bad\nname", " "} {
|
||||
for _, operation := range []string{"create", "edit"} {
|
||||
payload := map[string]any{"username": invalidName, "disabled": 0}
|
||||
if operation == "create" {
|
||||
payload["account"] = "invalid-name"
|
||||
payload["password"] = "UserPass123"
|
||||
} else {
|
||||
payload["id"] = regular.ID
|
||||
}
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/user/"+operation, strings.NewReader(string(body)))
|
||||
if operation == "create" {
|
||||
CreateUser(ctx, recorder, request)
|
||||
} else {
|
||||
EditUser(ctx, recorder, request)
|
||||
}
|
||||
var rejected struct {
|
||||
ErrorNo int `json:"errorNo"`
|
||||
ErrorMsg string `json:"errorMsg"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &rejected); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rejected.ErrorNo == 0 || rejected.ErrorMsg != displayNameValidationMessage {
|
||||
t.Fatalf("%s returned an unclear name validation error: %+v", operation, rejected)
|
||||
}
|
||||
}
|
||||
}
|
||||
var unchangedUser models.User
|
||||
if _, err := db.Instance.ID(regular.ID).Get(&unchangedUser); err != nil || unchangedUser.Name != regular.Name {
|
||||
t.Fatalf("invalid name changed the user: %v", err)
|
||||
}
|
||||
|
||||
result := editUserRequest(t, ctx, `{"id":`+itoa(admin.ID)+`,"password":"BypassPass123","disabled":0}`)
|
||||
if result == 0 {
|
||||
t.Fatal("administrator changed the current password without current-password verification")
|
||||
|
||||
@@ -19,6 +19,14 @@ func TestNormalizeAccount(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNormalizeDisplayName(t *testing.T) {
|
||||
for _, value := range []string{strings.Repeat("名", MaxNameRunes), strings.Repeat("😀", MaxNameRunes)} {
|
||||
if normalized, err := NormalizeDisplayName(" " + value + " "); err != nil || normalized != value {
|
||||
t.Fatalf("valid Unicode name rejected: %q %v", value, err)
|
||||
}
|
||||
if _, err := NormalizeDisplayName(value + "名"); err == nil {
|
||||
t.Fatal("oversized Unicode name accepted")
|
||||
}
|
||||
}
|
||||
if value, err := NormalizeDisplayName(" Test User "); err != nil || value != "Test User" {
|
||||
t.Fatalf("valid display name rejected: %q %v", value, err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user