This project is a simple, database-driven blog application built with Python and the Flask web framework. It was developed as a portfolio piece to demonstrate secure coding practices and the remediation of a critical web vulnerability.
The application was intentionally built with an SQL Injection (SQLi) vulnerability for educational purposes. It was then patched to demonstrate the correct, secure way to handle database queries.
This application is the target for its companion project, the SQL Injection Vulnerability Scanner.
The goal of this project is to showcase a full security lifecycle:
- Building a functional web application.
- Identifying a critical security flaw (SQLi).
- Remediating the flaw using industry-standard best practices.
- Verifying the fix.
-
Vulnerability: The initial version of the application passed user input from a search form directly into an SQL query using an f-string. This allowed an attacker to inject malicious SQL syntax (like a single quote
'), causing the database to error and proving the vulnerability. -
Remediation: The vulnerability was patched by implementing Parameterized Queries (Prepared Statements). Instead of formatting the user input into the query string, the input is passed as a separate parameter to the database driver. The driver then safely handles the input, preventing any malicious characters from affecting the SQL command's structure.
Vulnerable Code:
query = f"SELECT * FROM post WHERE title LIKE '%{query_param}%'" cursor.execute(query)
Secure Code:
query = "SELECT * FROM post WHERE title LIKE ?" cursor.execute(query, (f'%{query_param}%',))
- Clone the repository and
cdinto it. - Create and activate a virtual environment (
venv). - Install dependencies:
pip install -r requirements.txt(Note: You will need to create arequirements.txtfile with the contentFlaskandFlask-SQLAlchemy.) - Initialize the database:
flask shellthendb.create_all()andexit(). - Run the application:
flask run. The app will be available athttp://127.0.0.1:5000.
This project is licensed under the MIT License.