Repository: duncanscanga/VDRS-Solutions
Issue category: Security Vulnerability

Test SQL Injection: Register Function (name, email)



Functions to edit:
app/models.py:register

Patch:
diff --git a/.gitignore b/.gitignore
index c5586c4..cc6983a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,6 +23,8 @@ docs/_build/
 .vscode
 db.sqlite
 **/__pycache__/
+**/latest_logs
+**/archived_logs
 
 # Coverage reports
 htmlcov/
diff --git a/app/models.py b/app/models.py
index 5ae4e11..562268e 100644
--- a/app/models.py
+++ b/app/models.py
@@ -142,6 +142,10 @@ def register(name, email, real_name, password):
       Returns:
         True if registration succeeded otherwise False
     '''
+    # Check that the email has to follow addr-spec defined in RFC 5322
+    if not email_check(email):
+        return False
+
     # check if the email has been used:
     existed = User.query.filter_by(email=email).all()
     if len(existed) > 0:
@@ -151,10 +155,6 @@ def register(name, email, real_name, password):
     if not not_empty(email) and not not_empty(password):
         return False
 
-    # Check that the email has to follow addr-spec defined in RFC 5322
-    if not email_check(email):
-        return False
-
     # Check that the password has to meet the required complexity:
     # minimum length 6, at least one upper case, at least one lower
     # case, and at least one special character.

Test patch: diff --git a/app_test/frontend/test_listing.py b/app_test/frontend/test_listing.py
index bc47c57..c6a75da 100644
--- a/app_test/frontend/test_listing.py
+++ b/app_test/frontend/test_listing.py
@@ -1,5 +1,4 @@
 from seleniumbase import BaseCase
-
 from app_test.conftest import base_url
 from app.models import Booking, Listing, User
 from app.models import db
@@ -425,7 +424,7 @@ def test_listing_helper(self, *_):
         self.open(base_url + "/register")
         # fill details
         self.type("#email", "test2@test.com")
-        self.type("#real_name", "real username")
+        self.type("#real_name", "Test Test")
         self.type("#name", "Test")
         self.type("#password", "Test!123")
         self.type("#password2", "Test!123")
diff --git a/app_test/frontend/test_login.py b/app_test/frontend/test_login.py
index 51a0023..e964bfa 100644
--- a/app_test/frontend/test_login.py
+++ b/app_test/frontend/test_login.py
@@ -1,5 +1,4 @@
 from seleniumbase import BaseCase
-
 from app_test.conftest import base_url
 from app.models import User
 from app.models import db
diff --git a/app_test/frontend/test_user_update.py b/app_test/frontend/test_user_update.py
index 5451461..48c040e 100644
--- a/app_test/frontend/test_user_update.py
+++ b/app_test/frontend/test_user_update.py
@@ -1,5 +1,4 @@
 from seleniumbase import BaseCase
-
 from app_test.conftest import base_url
 from app.models import User
 from app.models import db
diff --git a/app_test/injection_tests.py b/app_test/injection_tests.py
index 599299e..aad70bc 100644
--- a/app_test/injection_tests.py
+++ b/app_test/injection_tests.py
@@ -6,16 +6,35 @@
 
 def test_sqli_register():
     '''
-    Function to test SQL Injection handling for Register method
+    Function to test SQL Injection handling for registration
     '''
     injection_file = open('app_test/Generic_SQLI.txt', 'r')
     lines = injection_file.readlines()
 
     for line in lines:
+        test_name_parameter(line)
+        test_email_parameter(line)
         real_name_register(line)
         password_register(line)
 
 
+def test_name_parameter(line):
+    '''
+    Function to test SQL Injection handling for registration
+    parameter 'name'.
+    '''
+    assert register(line, 'realname@gmail.com',
+                    'real name', '12345Aa#') is False
+
+
+def test_email_parameter(line):
+    '''
+    Function to test SQL Injection handling for registration
+    parameter 'name'.
+    '''
+    assert register('user50', line, 'real name', '12345Aa#') is False
+
+
 def test_sqli_create_listing():
     '''
     Function to test SQL Injection handling for Create Listing methods