Broken Authentication
# Vulnerable code (weak password policy)
if password == request.getParameter("password"):
# Allow login
# Secure code (implementing stronger password policies)
if validatePassword(request.getParameter("password")):
# Allow login
def login():
username = request.getParameter("username")
password = request.getParameter("password")
if verifyCredentials(username, password):
# Successful login
setSessionUser(username)
redirect("/dashboard")
else:
# Invalid credentials
displayErrorMessage("Invalid username or password.")
In this example, the login function assumes that the verifyCredentials
function adequately authenticates the user. However, if the implementation of verifyCredentials
is weak or does not properly hash passwords, an attacker can exploit this vulnerability by using weak or easily guessable passwords, potentially gaining unauthorized access to user accounts.
To mitigate this risk, strong password policies, secure storage of passwords (e.g., using salted and hashed passwords), and robust session management techniques (e.g., using secure session tokens) should be employed.
Last updated