Flask

Flask is a straightforward framework to build web apps more quickly than ever.

Set up Flask

Install Flask :

pip install Flask

Lunch the app :

python3 application.py

Basic application structure :

Basic router file :

from flask import Flask, render_template

app = Flask(name)

@app.route('/') def welcome(): return render_template('hello.html')

@app.route('/login') def login(): return render_template('login.html')

if name == 'main': app.run(debug=True)

Connect MySQL Database to Flask project :

Install depedencies :

pip install flask flask-mysqldb mysql-connector-python

Set up router file with right db configuration :

Our current form :

Verify the database is correctly connected :

Sanitizing form input

Sanitizing form on client side :

Bleach is a python library which handle input and sanitize them before it's sent to the backend.

pip install bleach

Implementation of bleach in our router :

Prevent SQL injection by sanitizing server-side input data :

Instead of directly concatenating user inputs into SQL queries, parameterized queries use placeholders (%s in this case) to separate SQL code from user-supplied data. This method ensures that user inputs are treated as data rather than executable SQL code, thus preventing SQL injection attacks.

The MySQL library I'm using (pymysqld) automatically handles escaping special characters in the parameters passed through %s. This means that even if a user enters something like ' OR '1'='1', it will be safely treated as data rather than an attempt to modify the query structure.

Last updated