Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: password validation (#13554)

ecmadao3d8d998ab1fa
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 4056e99689..6e2c6aff0e 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
+ "github.com/dlclark/regexp2"
"github.com/pquerna/otp/totp"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc"
@@ -42,6 +43,7 @@ type CreateUserFunc func(ctx context.Context, user *store.UserMessage, firstEndU
var (
invalidUserOrPasswordError = status.Errorf(codes.Unauthenticated, "The email or password is not valid.")
+ passwordValidRegex = regexp2.MustCompile(`^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$`, 0)
)
// AuthService implements the auth service.
@@ -186,6 +188,9 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe
}
password := request.User.Password
+ if isMatch, _ := passwordValidRegex.MatchString(password); !isMatch {
+ return nil, status.Errorf(codes.InvalidArgument, "password at least contains one letter and one number, no less than 8 characters")
+ }
if request.User.UserType == v1pb.UserType_SERVICE_ACCOUNT {
pwd, err := common.RandomString(20)
if err != nil {
@@ -322,6 +327,9 @@ func (s *AuthService) UpdateUser(ctx context.Context, request *v1pb.UpdateUserRe
if user.Type != api.EndUser {
return nil, status.Errorf(codes.InvalidArgument, "password can be mutated for end users only")
}
+ if isMatch, _ := passwordValidRegex.MatchString(request.User.Password); !isMatch {
+ return nil, status.Errorf(codes.InvalidArgument, "password at least contains one letter and one number, no less than 8 characters")
+ }
passwordPatch = &request.User.Password
case "service_key":
if user.Type != api.ServiceAccount {
diff --git a/backend/tests/tests.go b/backend/tests/tests.go
index 9a95121a1b..c5f989e242 100644
--- a/backend/tests/tests.go
+++ b/backend/tests/tests.go
@@ -500,7 +500,7 @@ func (ctl *controller) signupAndLogin(ctx context.Context) (string, error) {
if _, err := ctl.authServiceClient.CreateUser(ctx, &v1pb.CreateUserRequest{
User: &v1pb.User{
- Password: "1024",
+ Password: "1024bytebase",
Title: "demo",
UserType: v1pb.UserType_USER,
},
@@ -509,7 +509,7 @@ func (ctl *controller) signupAndLogin(ctx context.Context) (string, error) {
}
resp, err := ctl.authServiceClient.Login(ctx, &v1pb.LoginRequest{
- Password: "1024",
+ Password: "1024bytebase",
})
if err != nil {
return "", err
diff --git a/frontend/src/components/User/Settings/CreateUserDrawer.vue b/frontend/src/components/User/Settings/CreateUserDrawer.vue
index 91e1bac3ca..d8013cfb96 100644
--- a/frontend/src/components/User/Settings/CreateUserDrawer.vue
+++ b/frontend/src/components/User/Settings/CreateUserDrawer.vue
@@ -84,28 +84,43 @@
</div>
</NFormItem>
<NFormItem :label="$t('settings.profile.password')">
- <NInput
- v-model:value="state.user.password"
- type="password"
- :input-props="{ autocomplete: 'new-password' }"
- :placeholder="$t('common.sensitive-placeholder')"
- />
+ <div class="w-full space-y-1">
+ <span
+ :class="[
+ 'textinfolabel text-sm',
+ state.passwordHint ? '!text-error' : '',
+ ]"
+ >
+ {{ $t("settings.profile.password-hint") }}
+ </span>
+ <NInput
+ v-model:value="state.user.password"
+ type="password"
+ :status="state.passwordHint ? 'error' : undefined"
+ :input-props="{ autocomplete: 'new-password' }"
+ :placeholder="$t('common.sensitive-placeholder')"
+ @blur="checkPassword"
+ />
+ </div>
</NFormItem>
<NFormItem :label="$t('settings.profile.password-confirm')">
<div class="w-full flex flex-col justify-start items-start">
<NInput
v-model:value="state.passwordConfirm"
type="password"
+ :status="state.passwordMismatch ? 'error' : undefined"
:input-props="{ autocomplete: 'new-password' }"
:placeholder="
$t('settings.profile.password-confirm-placeholder')
"
+ @blur="checkPasswordMismatch"
/>
<span
- v-if="passwordMismatch"
+ v-if="state.passwordMismatch"
class="text-error text-sm mt-1 pl-1"
- >{{ $t("settings.profile.password-mismatch") }}</span
>
+ {{ $t("settings.profile.password-mismatch") }}
+ </span>
</div>
</NFormItem>
</template>
@@ -201,6 +216,8 @@ interface LocalState {
user: User;
roles: string[];
passwordConfirm: string;
+ passwordHint: boolean;
+ passwordMismatch: boolean;
}
const serviceAccountEmailSuffix = "service.bytebase.com";
@@ -242,6 +259,8 @@ const state = reactive<LocalState>({
user: initUser(),
roles: initRoles(),
passwordConfirm: "",
+ passwordHint: false,
+ passwordMismatch: false,
});
const workspaceDomain = computed(() => {
@@ -253,12 +272,21 @@ const workspaceDomain = computed(() => {
const isCreating = computed(() => !props.user);
-const passwordMismatch = computed(() => {
- return (
+const checkPassword = () => {
+ const pwd = state.user?.password ?? "";
+ if (!pwd) {
+ state.passwordHint = false;
+ return;
+ }
+ state.passwordHint = !/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(pwd);
+ return;
+};
+
+const checkPasswordMismatch = () => {
+ state.passwordMismatch =
!isEmpty(state.user?.password) &&
- state.user?.password !== state.passwordConfirm
- );
-});
+ state.user?.password !== state.passwordConfirm;
+};
const rolesChanged = computed(() => {
if (isCreating.value) {
@@ -275,7 +303,7 @@ const allowConfirm = computed(() => {
if (
!isCreating.value &&
- (passwordMismatch.value ||
+ (state.passwordMismatch ||
(getUpdateMaskFromUsers(props.user!, state.user).length == 0 &&
!rolesChanged.value))
) {
@@ -333,7 +361,9 @@ const tryCreateOrUpdateUser = async () => {
const createdUser = await userStore.createUser({
...state.user,
title: state.user.title || extractUserTitle(state.user.email),
- password: state.user.password || randomString(20),
+ password:
+ state.user.password ||
+ randomString(10) + randomString(10, "0123456789"),
});
await workspaceStore.patchIamPolicy([
{
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index df9cfccf23..a92b13ee1e 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -487,6 +487,7 @@
"phone-tips": "Be sure to include the country code",
"country-code": "Country Code",
"password": "Password",
+ "password-hint": "Minimum eight characters, at least contains one letter and one number.",
"password-confirm": "Password Confirm",
"password-confirm-placeholder": "Confirm new password",
"password-mismatch": "New password mismatch",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 963102324e..113b45fbaa 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -487,6 +487,7 @@
"phone-tips": "Asegúrese de incluir el código del país.",
"country-code": "Códigos de país y de área",
"password": "Contraseña",
+ "password-hint": "Mínimo ocho caracteres, al menos contiene una letra y un número.",
"password-confirm": "Confirmar contraseña",
"password-confirm-placeholder": "Confirmar nueva contraseña",
"password-mismatch": "Nueva contraseña no coincide",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index e4de505a05..cd7081c113 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -487,6 +487,7 @@
"phone-tips": "国コードを必ず含めてください",
"country-code": "国または地域コード",
"password": "パスワード",
+ "password-hint": "最低 8 文字で、少なくとも 1 つの文字と 1 つの数字が含まれます。",
"password-confirm": "パスワードの確認",
"password-confirm-placeholder": "新しいパスワードを確認します",
"password-mismatch": "新しいパスワードが一致しません",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index cd6a86a1ae..d9602acd95 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -487,6 +487,7 @@
"phone-tips": "请包含国家代码前缀",
"country-code": "国家或地区代码",
"password": "密码",
+ "password-hint": "最少 8 个字符,其中至少包含 1 个字母和 1 个数字。",
"password-confirm": "确认密码",
"password-confirm-placeholder": "确认新密码",
"password-mismatch": "新密码不匹配",
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index cfb2f2a661..d972872018 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -145,16 +145,17 @@ export function isValidEmail(email: string) {
return re.test(email);
}
-export function randomString(n?: number): string {
+export function randomString(
+ n: number,
+ candidate: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+): string {
if (!n) {
n = 16;
}
let result = "";
- const characters =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- const charactersLength = characters.length;
+ const charactersLength = candidate.length;
for (let i = 0; i < n; i++) {
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
+ result += candidate.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
diff --git a/go.mod b/go.mod
index c1b4e303f3..81e553d64e 100644
--- a/go.mod
+++ b/go.mod
@@ -229,6 +229,7 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
+ github.com/dlclark/regexp2 v1.11.4
github.com/elastic/go-elasticsearch/v8 v8.14.0
github.com/envoyproxy/go-control-plane v0.12.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
diff --git a/go.sum b/go.sum
index 0ef3d20f33..d448984abd 100644
--- a/go.sum
+++ b/go.sum
@@ -915,6 +915,8 @@ github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WA
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
+github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
+github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=