Defensive data handling is a critical security practice that protects your application from malicious input. The Normo Unsecure PWA currently has no input validation, no sanitization, and no exception handling, making it vulnerable to various attacks.
Current Vulnerabilities in the App:
No input validation on user registration or login forms
Direct insertion of user data into database without sanitization
import redef simple_check_password(password: str) -> bool: if not issubclass(type(password), str): return False if len(password) < 8: return False if len(password) > 20: return False if re.search(r"[ ]", password): return False if not re.search(r"[A-Z]", password): return False if not re.search(r"[a-z]", password): return False if not re.search(r"[0-9]", password): return False if not re.search(r"[@$!%*?&]", password): return False return True
import data_handler as sanitiserimport logginglogger = logging.getLogger(__name__)logging.basicConfig( filename='security_log.log', encoding='utf-8', level=logging.DEBUG, format='%(asctime)s %(message)s')if __name__ == '__main__': password = 123 # Invalid type try: validated = sanitiser.check_password(password) print(f"Password as byte string: {validated.hex()} is ready to be encrypted") except TypeError: logger.error(f"Type error for password: {password}") print("TypeError has been logged") except ValueError as inst: print(f"Not a valid password because it has {inst.args}.") except Exception as inst: print(f"Log as a {type(inst)}")
import reimport htmldef check_password(password: str) -> bytes: """Validate password meets security requirements""" if not issubclass(type(password), str): raise TypeError("Expected a string") if len(password) < 8: raise ValueError("less than 8 characters") if len(password) > 20: raise ValueError("more than 20 characters") if not re.search(r"[A-Z]", password): raise ValueError("does not contain uppercase letters") if not re.search(r"[a-z]", password): raise ValueError("does not contain lowercase letters") if not re.search(r"[0-9]", password): raise ValueError("does not contain a digit") if not re.search(r"[@$!%*?&]", password): raise ValueError("does not contain special characters") return password.encode()def check_email(email: str) -> bool: """Validate email format""" return bool(re.fullmatch(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email))def make_web_safe(string: str) -> str: """Sanitize string for web output""" return html.escape(string)def validate_name(name: str) -> bool: """Validate name contains only alphabets""" return name.isalpha()