Skip to content

Commit 51ebf96

Browse files
authored
improve password length check (monkeytypegame#3973) lgutter
integratie check for too long passwords into isPasswordStrong, and consistently check for it wherever passwords can be created / changed. Co-authored-by: Liewe Gutter <[email protected]>
1 parent 801be2f commit 51ebf96

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

frontend/src/ts/controllers/account-controller.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,6 @@ async function signUp(): Promise<void> {
614614
return;
615615
}
616616

617-
if (password.length > 25) {
618-
LoginPage.hidePreloader();
619-
LoginPage.enableInputs();
620-
LoginPage.updateSignupButton();
621-
Notifications.add("Password is too long", 0);
622-
return;
623-
}
624-
625617
if (
626618
!email.match(
627619
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
@@ -650,10 +642,10 @@ async function signUp(): Promise<void> {
650642
return;
651643
}
652644

653-
// Force user to use a capital letter, number, special character when setting up an account and changing password
645+
// Force user to use a capital letter, number, special character and reasonable length when setting up an account and changing password
654646
if (!Misc.isLocalhost() && !Misc.isPasswordStrong(password)) {
655647
Notifications.add(
656-
"Password must contain at least one capital letter, number, a special character and at least 8 characters long",
648+
"Password must contain at least one capital letter, number, a special character and must be between 8 and 64 characters long",
657649
0,
658650
4
659651
);

frontend/src/ts/pages/login.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Page from "./page";
44
import * as Notifications from "../elements/notifications";
55
import { InputIndicator } from "../elements/input-indicator";
66
import * as Skeleton from "../popups/skeleton";
7+
import * as Misc from "../utils/misc";
78

89
export function enableSignUpButton(): void {
910
$(".page.pageLogin .register.side .button").removeClass("disabled");
@@ -112,22 +113,20 @@ const checkPassword = (): void => {
112113
".page.pageLogin .register.side .passwordInput"
113114
).val() as string;
114115

115-
// Force user to use a capital letter, number, special character when setting up an account and changing password
116-
if (password.length < 8) {
117-
passwordIndicator.show("short", "Password must be at least 8 characters");
118-
return;
119-
} else {
120-
const hasCapital = password.match(/[A-Z]/);
121-
const hasNumber = password.match(/[\d]/);
122-
const hasSpecial = password.match(/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/);
123-
if (!hasCapital || !hasNumber || !hasSpecial) {
116+
// Force user to use a capital letter, number, special character and reasonable length when setting up an account and changing password
117+
if (!Misc.isLocalhost() && !Misc.isPasswordStrong(password)) {
118+
if (password.length < 8) {
119+
passwordIndicator.show("short", "Password must be at least 8 characters");
120+
} else if (password.length > 64) {
121+
passwordIndicator.show("long", "Password must be at most 64 characters");
122+
} else {
124123
passwordIndicator.show(
125124
"weak",
126125
"Password must contain at least one capital letter, number, and special character"
127126
);
128-
} else {
129-
passwordIndicator.show("good", "Password is good");
130127
}
128+
} else {
129+
passwordIndicator.show("good", "Password is good");
131130
}
132131
updateSignupButton();
133132
};
@@ -208,6 +207,10 @@ const passwordIndicator = new InputIndicator(
208207
icon: "fa-times",
209208
level: -1,
210209
},
210+
long: {
211+
icon: "fa-times",
212+
level: -1,
213+
},
211214
weak: {
212215
icon: "fa-times",
213216
level: -1,

frontend/src/ts/popups/simple-popups.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ list["updatePassword"] = new SimplePopup(
537537
}
538538
if (!isLocalhost() && !isPasswordStrong(newPass)) {
539539
Notifications.add(
540-
"New password must contain at least one capital letter, number, a special character and at least 8 characters long",
540+
"New password must contain at least one capital letter, number, a special character and must be between 8 and 64 characters long",
541541
0,
542542
4
543543
);

frontend/src/ts/utils/misc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,8 @@ export function isPasswordStrong(password: string): boolean {
13301330
const hasNumber = !!password.match(/[\d]/);
13311331
const hasSpecial = !!password.match(/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/);
13321332
const isLong = password.length >= 8;
1333-
return hasCapital && hasNumber && hasSpecial && isLong;
1333+
const isShort = password.length <= 64;
1334+
return hasCapital && hasNumber && hasSpecial && isLong && isShort;
13341335
}
13351336

13361337
export function areUnsortedArraysEqual(a: unknown[], b: unknown[]): boolean {

frontend/static/email-handler.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@
182182
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/
183183
);
184184
const isLong = password.length >= 8;
185-
return hasCapital && hasNumber && hasSpecial && isLong;
185+
const isShort = password.length <= 64;
186+
return hasCapital && hasNumber && hasSpecial && isLong && isShort;
186187
}
187188

188189
function handleVerifyEmail(actionCode, continueUrl) {
@@ -244,7 +245,7 @@
244245

245246
if (!isPasswordStrong(newPassword)) {
246247
alert(
247-
"Password must be at least 8 characters long and contain at least one capital letter, one number and one special character."
248+
"Password must contain at least one capital letter, number, a special character and must be between 8 and 64 characters long"
248249
);
249250
showResetPassword();
250251
return;

0 commit comments

Comments
 (0)