[RM4341] Master Password Screen should give a proper error message if user provide empty password

Richard Yen <[email protected]>
Newsgroups gmane.comp.db.postgresql.pgadmin.devel
Message-ID <CAKH4vDjAT-CUefn1DQ=0KQDiJvTasjEgNkOvoAXYZUbB0gAEdw@mail.gmail.com>
Hello Hackers,

This seems to already have been processed, but I'm writing for
archiving/process-compliance

Submitting patch for RM 4341
<https://redmine.postgresql.org/issues/4341?issue_count=165&issue_position=74&next_issue_id=4339&prev_issue_id=4351>
.

This patch 1) adds detection of button-click, so that error message can be
displayed upon input of empty password, and 2) refactors code so that an
empty password when password exists will display "Incorrect Password"
instead of just refreshing the modal

Cheers,
--Richard
pgadmin4_RM4341.patch (application/octet-stream, 3 KB)
diff --git a/web/pgadmin/browser/__init__.py b/web/pgadmin/browser/__init__.py
index 3b20f2460..c5b339048 100644
--- a/web/pgadmin/browser/__init__.py
+++ b/web/pgadmin/browser/__init__.py
@@ -792,15 +792,16 @@ def set_master_password():
     # Master password is not applicable for server mode
     if not config.SERVER_MODE and config.MASTER_PASSWORD_REQUIRED:
 
+        # if master pass is set previously
+        if current_user.masterpass_check is not None:
+            if data.get('button_click') and not validate_master_password(data.get('password')):
+                return form_master_password_response(
+                    existing=True,
+                    present=False,
+                    errmsg=gettext("Incorrect master password")
+                )
+
         if data != '' and data.get('password', '') != '':
-            # if master pass is set previously
-            if current_user.masterpass_check is not None:
-                if not validate_master_password(data.get('password')):
-                    return form_master_password_response(
-                        existing=True,
-                        present=False,
-                        errmsg=gettext("Incorrect master password")
-                    )
 
             # store the master pass in the memory
             set_crypt_key(data.get('password'))
@@ -827,9 +828,14 @@ def set_master_password():
                 present=False,
             )
         elif not get_crypt_key()[0]:
+            error_message = None
+            if data.get('button_click') and data.get('password') == '':
+                # If user attempted to enter a blank password, then throw error
+                error_message = gettext("Master password cannot be empty")
             return form_master_password_response(
                 existing=False,
                 present=False,
+                errmsg=error_message
             )
 
     # if master password is disabled now, but was used once then
diff --git a/web/pgadmin/browser/static/js/browser.js b/web/pgadmin/browser/static/js/browser.js
index 7cedd1ca9..8a258478e 100644
--- a/web/pgadmin/browser/static/js/browser.js
+++ b/web/pgadmin/browser/static/js/browser.js
@@ -605,7 +605,7 @@ define('pgadmin.browser', [
                 /* OK Button */
                 self.set_master_password(
                   $('#frmMasterPassword #password').val(),
-                  parentDialog.set_callback,
+                  true,parentDialog.set_callback,
                 );
               } else if(event.index == 2) {
                 /* Cancel button */
@@ -677,12 +677,13 @@ define('pgadmin.browser', [
       });
     },
 
-    set_master_password: function(password='', set_callback=()=>{}) {
+    set_master_password: function(password='', button_click=false, set_callback=()=>{}) {
       let data=null, self = this;
 
       if(password != null || password!='') {
         data = JSON.stringify({
           'password': password,
+          'button_click': button_click,
         });
       }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.