Skip to main content

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.
  • ⚙️ Configurable rules and whitelist.
  • 🛠️ Easy integration with Flask applications.
  • 📜 Logging for blocked requests with color-coded output.
  • 🧠 Advanced detection for SQL injection, XSS, path traversal, command injection, and more.

Installation 🛠️

From PyPI

Install FlaskGuard directly from PyPI:

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 init_app

app = Flask(__name__)
init_app(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",
    }
}

custom_whitelist = {
    "query_string": [r"safe_custom_param=value"],
}

load_user_config(user_rules=custom_rules, user_whitelist=custom_whitelist)

Rules 🛡️

FlaskGuard includes the following built-in rules:

  1. SQL Injection: Detects SQL injection patterns such as ' OR 1=1, UNION SELECT, SLEEP(), and more.
  2. XSS Attack: Detects Cross-Site Scripting (XSS) patterns in <script> tags, event handlers, and encoded payloads.
  3. Suspicious User-Agent: Blocks requests from tools commonly used in attacks, such as sqlmap, curl, wget, and more.
  4. Path Traversal: Detects attempts to access sensitive files using patterns like ../../etc/passwd.
  5. Remote and Local File Inclusion (RFI/LFI): Detects attempts to include remote or local files.
  6. Command Injection: Detects shell command injection patterns and common commands like ls, cat, and rm.
  7. Email Injection: Detects email header injection attempts using BCC, CC, and newline characters.
  8. HTTP Header Injection: Detects HTTP header injection attempts with patterns like Set-Cookie and Content-Length.
  9. CSRF Token Missing (Optional): Detects requests missing CSRF tokens. This rule is disabled by default to avoid false positives. Enable it only if your app uses CSRF tokens.

Enabling the csrf_token_missing Rule

If your app uses CSRF tokens, you can enable the csrf_token_missing rule by updating the configuration:

from flask_guard.rules import load_user_config

custom_rules = {
    "csrf_token_missing": {
        "enabled": True,
    }
}

load_user_config(user_rules=custom_rules)

Testing the Firewall 🧪

You can test the firewall using curl commands. Below are examples of malicious requests:

SQL Injection

curl "http://127.0.0.1:5000/malicious?query=%27%20OR%201%3D1%20--"

XSS Attack

curl "http://127.0.0.1:5000/malicious?query=%3Cscript%3Ealert%281%29%3C%2Fscript%3E"

Path Traversal

curl "http://127.0.0.1:5000/malicious?query=../../etc/passwd"

Command Injection

curl "http://127.0.0.1:5000/malicious?query=ls%20-al"

Email Injection

curl "http://127.0.0.1:5000/malicious?query=TO:%20victim@example.com%0ABCC:%20attacker@example.com"

HTTP Header Injection

curl "http://127.0.0.1:5000/malicious?query=%0D%0ASet-Cookie:%20malicious=true"

Legitimate Request

curl "http://127.0.0.1:5000/safe?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

safe_flask-1.0.3.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safe_flask-1.0.3-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file safe_flask-1.0.3.tar.gz.

File metadata

  • Download URL: safe_flask-1.0.3.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for safe_flask-1.0.3.tar.gz
Algorithm Hash digest
SHA256 7677a95a5f1a7cff2eaad6115c71da5f64d7d1975b4fae1276a539a2573c559c
MD5 e857c75193ef4cb7d047ec7f6a8e0ec0
BLAKE2b-256 bc3a8caac28194cc3bebd8245033aaa003560d51584950e46f2a4bdb146fb855

See more details on using hashes here.

File details

Details for the file safe_flask-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: safe_flask-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for safe_flask-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a7f0d8670b3df8e975a88cde801cd517b74c24983bf26e670804acf0bd2b67c2
MD5 9c1ffec838ade44967f7189e9ab435cb
BLAKE2b-256 7f4a6ae1d901305ef038ebc63397b33a1a18371c8c4f457cd21632ceb3dce5a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page