A plug-and-play firewall library for Flask applications.
Project description
FlaskGuard 🚀
FlaskGuard is a plug-and-play firewall library for Flask applications. It protects your application from common web vulnerabilities such as SQL injection, XSS, path traversal, and more.
Features ✨
- 🔒 Detects and blocks malicious requests in both query strings and POST request bodies.
- ⚙️ Configurable multi-target rules and whitelist.
- 🛠️ Easy integration with Flask applications.
- 📜 Logging for blocked requests with color-coded output, including details about the source of malicious input.
- 🧠 Advanced detection for SQL injection, XSS, path traversal, command injection, and more.
- 🛡️ Customizable blocked responses for enhanced flexibility.
- 🚀 Now with POST request support: Analyze and block malicious patterns in POST request bodies.
- 🧪 Experimental JSON Injection Protection: Disabled by default, can be enabled for testing.
Installation 🛠️
From PyPI
Install FlaskGuard directly with Pip:
pip install safe-flask
From GitHub
Install FlaskGuard from the GitHub repository:
pip install git+https://github.com/CodeGuardianSOF/FlaskGuard.git
From Source
Clone the repository and install FlaskGuard locally:
git clone https://github.com/CodeGuardianSOF/FlaskGuard.git
cd FlaskGuard
pip install .
Usage 🚀
Basic Integration
from flask import Flask
from flask_guard import FlaskGuard
app = Flask(__name__)
FlaskGuard(app)
@app.route("/")
def home():
return "Welcome to FlaskGuard-protected app!"
if __name__ == "__main__":
app.run()
Custom Rules and Whitelist
from flask_guard.rules import load_user_config
custom_rules = {
"custom_rule": {
"enabled": True,
"pattern": r"custom_pattern",
"target": ["query_string", "post_body"], # Apply to both query strings and POST bodies
}
}
custom_whitelist = {
"query_string": [r"safe_custom_param=value"],
"post_body": [r"safe_post_param=value"],
}
load_user_config(user_rules=custom_rules, user_whitelist=custom_whitelist)
Custom Blocked Response
You can customize the response returned when a request or response is blocked by FlaskGuard:
def custom_blocked_response():
return '403 Forbidden', [b"Custom Firewall Blocked Message: Access Denied."]
FlaskGuard(app, custom_blocked_response=custom_blocked_response)
Rules 🛡️
FlaskGuard includes the following built-in rules:
- SQL Injection: Detects SQL injection patterns such as
' OR 1=1,UNION SELECT,SLEEP(), and more. Applies to both query strings and POST bodies. - XSS Attack: Detects Cross-Site Scripting (XSS) patterns in
<script>tags, event handlers, and encoded payloads. Applies to both query strings and POST bodies. - Suspicious User-Agent: Blocks requests from tools commonly used in attacks, such as
sqlmap,curl,wget,python-requests, and more. - Path Traversal: Detects attempts to access sensitive files using patterns like
../../etc/passwd. Applies to both query strings and POST bodies. - Remote and Local File Inclusion (RFI/LFI): Detects attempts to include remote or local files. Applies to both query strings and POST bodies.
- Command Injection: Detects shell command injection patterns and common commands like
ls,cat,rm,bash, andpowershell. Applies to both query strings and POST bodies. - Email Injection: Detects email header injection attempts using
BCC,CC, and newline characters. Applies to both query strings and POST bodies. - HTTP Header Injection: Detects HTTP header injection attempts with patterns like
Set-CookieandContent-Length. Applies to both query strings and POST bodies. - Directory Listing: Detects responses exposing directory listings, such as
Index of /. Applies to response bodies. - Open Redirect: Detects potential open redirect vulnerabilities in query strings.
- JSON Injection (Experimental): Detects malicious patterns in JSON payloads, such as
__proto__,constructor, and<script>. Disabled by default and applies only to POST bodies.
Testing the Firewall 🧪
You can test the firewall using curl commands. Below are examples of malicious requests:
SQL Injection
curl -X POST "http://127.0.0.1:5000/malicious" -d "query=' OR 1=1 --"
XSS Attack
curl -X POST "http://127.0.0.1:5000/malicious" -d "query=<script>alert(1)</script>"
Path Traversal
curl -X POST "http://127.0.0.1:5000/malicious" -d "query=../../etc/passwd"
Command Injection
curl -X POST "http://127.0.0.1:5000/malicious" -d "query=ls -al"
Email Injection
curl -X POST "http://127.0.0.1:5000/malicious" -d "query=TO: victim@example.com\nBCC: attacker@example.com"
HTTP Header Injection
curl -X POST "http://127.0.0.1:5000/malicious" -d "query=\r\nSet-Cookie: malicious=true"
Directory Listing
curl -X GET "http://127.0.0.1:5000/malicious"
Open Redirect
curl -X POST "http://127.0.0.1:5000/malicious" -d "query=http://malicious-site.com"
JSON Injection (Experimental)
curl -X POST "http://127.0.0.1:5000/malicious" -H "Content-Type: application/json" -d '{"__proto__": "malicious"}'
Legitimate Request
curl -X POST "http://127.0.0.1:5000/safe" -d "query=safe_param=value"
Contributing 🤝
See CONTRIBUTING.md for details.
License 📄
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safe_flask-2.1.0.tar.gz.
File metadata
- Download URL: safe_flask-2.1.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ae0251f12b1f6763854124d7c6a98975116bac16a41aa587df92aebb623bbc1
|
|
| MD5 |
9b727d4919b39e2f88e2c5ea91a40b8b
|
|
| BLAKE2b-256 |
6fc0f87101855fe244ac084759244a2945a25e510c208cf3686514aac7724459
|
File details
Details for the file safe_flask-2.1.0-py3-none-any.whl.
File metadata
- Download URL: safe_flask-2.1.0-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa27d0ae8ee831eb54a51ae841ef29823f00111cc404b7825722e22c70230524
|
|
| MD5 |
8b51c299568098ecda9be0cf644a1703
|
|
| BLAKE2b-256 |
6e5984dfc5da21043c7838d1d1e9916913524f708f09c22aa22c67235e6b2032
|