Skip to main content

Flask-WAF

Flask-WAF is an advanced Web Application Firewall (WAF) extension for Flask applications. It provides comprehensive protection against various web application threats, enhancing the security of your Flask-based web applications.

Table of Contents

  1. Features
  2. Installation
  3. Quick Start
  4. Configuration
  5. Advanced Usage
  6. API Reference
  7. Contributing
  8. License

Features

  • Advanced rule engine for detecting and blocking malicious requests
  • Session protection to prevent session hijacking and fixation attacks
  • Content Security Policy (CSP) implementation
  • Threat intelligence integration
  • Anomaly detection to identify unusual patterns
  • Rate limiting to prevent abuse
  • Comprehensive logging
  • Customizable security rules and policies

Installation

You can install Flask-WAF using pip:

pip install flask-waf

Alternatively, you can install from the source:

git clone https://github.com/yourusername/flask-waf.git
cd flask-waf
pip install -e .

Quick Start

Here's a simple example of how to use Flask-WAF:

from flask import Flask
from flask_waf import WAF

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'  # Required for session handling
waf = WAF(app)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(debug=True)

This basic setup will apply default WAF protection to your Flask application.

Configuration

Flask-WAF can be configured using a JSON file or by passing a dictionary to the WAF constructor. Here's an example configuration:

waf_config = {
    "max_request_size": 1048576,  # 1MB
    "allowed_content_types": [
        "application/x-www-form-urlencoded",
        "application/json",
        "multipart/form-data"
    ],
    "max_url_length": 2083,
    "max_query_params": 100,
    "max_headers": 100,
    "required_headers": ["Host", "User-Agent"],
    "rate_limit": 100,  # requests per minute
    "session_protection": True,
    "content_security_policy": {
        "default-src": ["'self'"],
        "script-src": ["'self'", "'unsafe-inline'"],
        "style-src": ["'self'", "'unsafe-inline'"],
    },
    "anomaly_detection": {
        "request_threshold": 10,
        "time_window": 60
    }
}

waf = WAF(app, config=waf_config)

You can also load the configuration from a JSON file:

waf = WAF(app, config_file='waf_config.json')

Advanced Usage

Custom Rules

You can add custom rules to the WAF's rule engine:

from flask_waf import WAF, Rule

waf = WAF(app)

custom_rule = Rule(
    name='Custom SQL Injection Check',
    pattern=r'UNION\s+SELECT',
    locations=['params', 'form', 'json'],
    severity='high',
    description='Detected potential SQL injection attempt'
)

waf.rule_engine.add_rule(custom_rule)

Threat Intelligence Integration

You can update the threat intelligence module with custom malicious patterns:

waf.threat_intel.add_malicious_pattern(r'malware\.com')
waf.threat_intel.add_malicious_ip_range('192.0.2.0', '192.0.2.255')

Logging

Flask-WAF provides comprehensive logging. You can customize the log file location:

waf.logger.set_log_file('/path/to/waf.log')

API Reference

WAF Class

The main class for initializing the Web Application Firewall.

class WAF:
    def __init__(self, app=None, config=None, config_file=None):
        ...

    def init_app(self, app):
        ...

    def check_request(self):
        ...

    def add_security_headers(self, response):
        ...

Rule Class

Used for defining custom security rules.

class Rule:
    def __init__(self, name, pattern, locations, severity='medium', description=''):
        ...

    def check(self, data):
        ...

RuleEngine Class

Manages and applies security rules.

class RuleEngine:
    def add_rule(self, rule):
        ...

    def remove_rule(self, rule_name):
        ...

    def check_request(self, request):
        ...

For a complete API reference, please refer to the API documentation.

Contributing

We welcome contributions! Please see our contributing guide for more details.

License

Flask-WAF is released under the MIT License. See the LICENSE file for more details.

Metadata

Release files for flask-waf 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-waf 2.1.1
File Size Uploaded
flask_waf-2.1.1.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-waf 2.1.1
File Interpreter ABI Platform
flask_waf-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.5 kB

Release files / flask_waf-2.1.1.tar.gz

Download URL flask_waf-2.1.1.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
755641186a43aa0b20c4da1b6297c3c63358db6650333f771bf187c765eed018
BLAKE2b-256 checksum
How to use checksums
f5628b359cdb6cab61ecdd363d71eb3959488e0301d465926699fa514d8e4948
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release files / flask_waf-2.1.1-py3-none-any.whl

Download URL flask_waf-2.1.1-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
096bc87e2c049c4ca8abf65fcd64b2cbc87e989f6c203eabf5b3463bdc87bd01
BLAKE2b-256 checksum
How to use checksums
3c0a9d38b55cd602aaa174e240f863ff3ac0906cfb1cbc9caccc50c2faa233a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

2.1.0

2 release files

2.0.9

2 release files

2.0.8

1 release file

2.0.7

1 release file

2.0.6

1 release file

2.0.5

1 release file

2.0.4

1 release file

2.0.3

1 release file

2.0.2

1 release file

2.0.1

1 release file

2.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page