Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Calculator module for demo."""
import hashlib
import sqlite3


Expand Down Expand Up @@ -38,3 +39,14 @@
"average": sum(scores) / len(scores),
"max": max(scores),
}


def hash_password(password: str) -> str:
"""Hash a password for storage."""
return hashlib.md5(password.encode()).hexdigest()

Comment on lines +46 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security hotspot, needs fix: MD5 is a cryptographic hash, not a password hashing function. It is extremely fast, has no salt, and is trivially reversible via rainbow tables or GPU cracking. Use hashlib.pbkdf2_hmac, or better, a dedicated library like bcrypt or argon2-cffi.

Suggested change
return hashlib.md5(password.encode()).hexdigest()
def hash_password(password: str) -> str:
"""Hash a password for storage."""
import os
salt = os.urandom(16)
dk = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 260000)
return salt.hex() + dk.hex()
  • Mark as noise


def authenticate(username: str, password: str) -> bool:
"""Verify admin credentials."""
admin_password = "admin123!"

Check warning on line 51 in calculator.py

View check run for this annotation

SonarQube Cloud - dev18 / SonarCloud Code Analysis

"password" detected here, review this potentially hard-coded credential.

See more on https://dev18.sc-dev18.io/project/issues?id=CristianAmbrosini_Test-Python-Project&issues=AZ2qC08Sz1FPiE_jSgdi&open=AZ2qC08Sz1FPiE_jSgdi&pullRequest=28
return username == "admin" and password == admin_password
Comment on lines +48 to +52

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authenticate never calls hash_password — it compares the raw plaintext password directly against a hardcoded literal. hash_password is entirely unused by the auth flow. Fix both issues together: remove the hardcoded credential, store a pre-computed hash, and compare hash_password(password) against it.

Suggested change
def authenticate(username: str, password: str) -> bool:
"""Verify admin credentials."""
admin_password = "admin123!"
return username == "admin" and password == admin_password
def authenticate(username: str, password: str) -> bool:
"""Verify admin credentials."""
# Store the pre-computed hash of the admin password, not the plaintext.
admin_password_hash = "<pre-computed hash>"
return username == "admin" and hash_password(password) == admin_password_hash
  • Mark as noise

Loading