Skip to content

Commit e4a6f9e

Browse files
committed
Added minimum password length requirement (even in GUI)
1 parent d864009 commit e4a6f9e

File tree

4 files changed

+81
-15
lines changed

4 files changed

+81
-15
lines changed

src/FreeGPT4_Server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ def save_settings():
520520
confirm_password = request.form.get("confirm_password", "")
521521
if new_password != confirm_password:
522522
raise ValidationError("Passwords do not match")
523+
if len(new_password) < 8:
524+
raise ValidationError("Password must be at least 8 characters long")
523525
settings_update["password"] = new_password
524526

525527
# Handle private mode token
@@ -651,6 +653,8 @@ def save_user_settings(username):
651653
confirm_password = request.form.get("confirm_password", "")
652654
if new_password != confirm_password:
653655
raise ValidationError("Passwords do not match")
656+
if len(new_password) < 8:
657+
raise ValidationError("Password must be at least 8 characters long")
654658
settings_update["password"] = new_password
655659

656660
# Save user settings

src/database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,10 @@ def create_user(self, username: str, password: Optional[str] = None) -> str:
288288
# Validate password
289289
if password is None:
290290
password = username # Default password
291+
is_valid, error_msg = validate_password(password, 1)
292+
else:
293+
is_valid, error_msg = validate_password(password, config.security.password_min_length)
291294

292-
is_valid, error_msg = validate_password(password, config.security.password_min_length)
293295
if not is_valid:
294296
raise ValidationError(error_msg)
295297

src/static/js/script.js

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,76 @@ function checkAllProxies() {
206206
}
207207
}
208208

209-
// Checks if new password and confirm password match
210-
function checkPasswordMatch() {
209+
// Validates password length (minimum 8 characters)
210+
function validatePasswordLength() {
211211
var newPassword = document.getElementById("new_password").value;
212212
var confirmPassword = document.getElementById("confirm_password").value;
213-
if (newPassword == confirmPassword) {
214-
if (newPassword.length > 0) {
215-
replaceElement("error_password", "success_password");
216-
return true;
213+
214+
// Check if either password field is not empty and has less than 8 characters
215+
if ((newPassword.length > 0 && newPassword.length < 8) ||
216+
(confirmPassword.length > 0 && confirmPassword.length < 8)) {
217+
showElement("error_password_length");
218+
hideElement("error_password");
219+
hideElement("success_password");
220+
return false;
221+
} else {
222+
hideElement("error_password_length");
223+
// Only validate match if both fields have valid length
224+
if (newPassword.length >= 8 && confirmPassword.length >= 8) {
225+
return validatePasswordMatch();
217226
}
227+
return true;
228+
}
229+
}
230+
231+
// Checks if new password and confirm password match (internal function)
232+
function validatePasswordMatch() {
233+
var newPassword = document.getElementById("new_password").value;
234+
var confirmPassword = document.getElementById("confirm_password").value;
235+
236+
if (newPassword == confirmPassword) {
237+
hideElement("error_password");
238+
hideElement("error_password_length");
239+
showElement("success_password");
240+
return true;
218241
} else {
219-
replaceElement("success_password", "error_password");
242+
hideElement("success_password");
243+
hideElement("error_password_length");
244+
showElement("error_password");
245+
return false;
246+
}
247+
}
248+
249+
// Checks if new password and confirm password match (main function for compatibility)
250+
function checkPasswordMatch() {
251+
var newPassword = document.getElementById("new_password").value;
252+
var confirmPassword = document.getElementById("confirm_password").value;
253+
254+
// Check length first
255+
if ((newPassword.length > 0 && newPassword.length < 8) ||
256+
(confirmPassword.length > 0 && confirmPassword.length < 8)) {
257+
showElement("error_password_length");
258+
hideElement("error_password");
259+
hideElement("success_password");
260+
return false;
261+
}
262+
263+
// Hide length error if length is valid
264+
hideElement("error_password_length");
265+
266+
// Check match if both passwords have content
267+
if (newPassword.length > 0 && confirmPassword.length > 0) {
268+
return validatePasswordMatch();
269+
}
270+
271+
// If passwords are empty, hide all messages
272+
if (newPassword.length === 0 && confirmPassword.length === 0) {
273+
hideElement("error_password");
274+
hideElement("error_password_length");
275+
hideElement("success_password");
220276
}
221-
return false;
277+
278+
return newPassword.length >= 8 && confirmPassword.length >= 8;
222279
}
223280

224281
// Opens the update password form
@@ -232,6 +289,7 @@ function cancelPasswordUpdate() {
232289
hideElement("password_update");
233290
hideElement("success_password");
234291
hideElement("error_password");
292+
hideElement("error_password_length");
235293
replaceElement("cancel_password_update", "open_password_update");
236294
document.getElementById("new_password").value = "";
237295
document.getElementById("confirm_password").value = "";
@@ -240,9 +298,10 @@ function cancelPasswordUpdate() {
240298
// Enables the save button if the password is correct
241299
function enableSaveButton() {
242300
var pass_length = document.getElementById("password").value.length;
243-
// var isPasswordUpdateClosed = document.getElementById("password_update").hidden;
244-
var isPasswordUpdateClosed = document.getElementById("password_update").classList.contains("hidden")
245-
if ((checkPasswordMatch() || isPasswordUpdateClosed) && pass_length > 0) {
301+
var isPasswordUpdateClosed = document.getElementById("password_update").classList.contains("hidden");
302+
var isPasswordValid = isPasswordUpdateClosed || checkPasswordMatch();
303+
304+
if (isPasswordValid && pass_length > 0) {
246305
replaceElement('save_label_dummy', 'save_label');
247306
} else {
248307
replaceElement('save_label', 'save_label_dummy');

src/templates/settings.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,13 @@ <h3 class="title blue text-xl">Server</h3>
202202
<b>Password:</b>
203203
<div id="password_update" class="mt-4 hidden">
204204
<p class="text-sm"> <b> Set Password </b></p>
205-
<input type="password" id="new_password" name="new_password" class="input outline-none py-1 px-2 rounded-lg inter" placeholder="Your new Password" onchange="enableSaveButton();" autocomplete="new-password"></input>
205+
<input type="password" id="new_password" name="new_password" class="input outline-none py-1 px-2 rounded-lg inter" placeholder="Your new Password" onchange="enableSaveButton();" oninput="validatePasswordLength();" autocomplete="new-password" minlength="8"></input>
206206
<p class="text-sm mt-4"> <b> Confirm Password </b></p>
207-
<input type="password" id="confirm_password" name="confirm_password" class="input outline-none py-1 px-2 rounded-lg inter" placeholder="New Password Again" onchange="enableSaveButton();" autocomplete="new-password"></input>
207+
<input type="password" id="confirm_password" name="confirm_password" class="input outline-none py-1 px-2 rounded-lg inter" placeholder="New Password Again" onchange="enableSaveButton();" oninput="validatePasswordLength();" autocomplete="new-password" minlength="8"></input>
208+
<p id="error_password_length" class="text-sm label_red mt-1 hidden"> <b> Error: </b> Password must be at least 8 characters long </p>
208209
<p id="error_password" class="text-sm label_red mt-1 hidden"> <b> Error: </b> Passwords do not match </p>
209210
<p id="success_password" class="text-sm label_green mt-1 hidden">
210-
<b> Success: </b> Passwords match. <br>
211+
<b> Success: </b> Passwords match and meet requirements. <br>
211212
<i> When entering the old password below to confirm, the password will be updated </i>
212213
</p>
213214
</div>

0 commit comments

Comments
 (0)