Skip to main content

Zero-dependency security middleware for Python. Protects against XSS, SQL injection, CSRF, SSRF, HPP, rate limiting, bot detection, and 15+ more attack types. Works with Flask, FastAPI, and Django.

Project description

Arcis Python

PyPI version PyPI downloads GitHub stars Python 3.9+ License: MIT

One-line security for Python web applications + the canonical arcis CLI for scan / audit / supply-chain analysis.

Arcis is a cross-platform security library that provides drop-in protection against common web vulnerabilities. Part of the Arcis ecosystem with implementations for Node.js, Python, and Go.

What's new in v1.4.4

  • Detect-and-block middleware — opt in with app.add_middleware(ArcisMiddleware, block=True). Returns 403 + tags telemetry on attack-pattern match instead of silently sanitizing.
  • 6 new standalone detectorsdetect_xss / detect_sql / detect_nosql / detect_path_traversal / detect_command_injection / detect_prototype_pollution.
  • CLI overhaularcis --list discovery, live progress bars, summary blocks, arcis update command, optional interactive picker (pip install "arcis[interactive]").
  • End-to-end CLI → dashboard wiringarcis scan/audit/sca POST results to the dashboard when ARCIS_ENDPOINT is set.
  • See the full release history at gagancm.github.io/arcis/changelog.html.

Installation

# Core library (no dependencies)
pip install arcis

# With framework integrations
pip install arcis[flask]
pip install arcis[fastapi]
pip install arcis[django]

# All frameworks + dev tools
pip install arcis[dev]

Quick Start

Flask

from flask import Flask
from arcis import Arcis

app = Flask(__name__)
Arcis(app)  # That's it! Your app is now protected.

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

FastAPI

from fastapi import FastAPI
from arcis.fastapi import ArcisMiddleware

app = FastAPI()
app.add_middleware(ArcisMiddleware)

@app.get('/')
async def hello():
    return {'message': 'Hello, World!'}

Django

# settings.py
MIDDLEWARE = [
    'arcis.django.ArcisMiddleware',
    # ... other middleware
]

# Optional configuration
ARCIS_CONFIG = {
    'rate_limit_max': 100,
    'rate_limit_window_ms': 60000,
    'sanitize_xss': True,
    'sanitize_sql': True,
}

Features

Input Sanitization

Automatically sanitize user input to prevent:

  • XSS (Cross-Site Scripting)
  • SQL Injection
  • NoSQL Injection (MongoDB operators)
  • Path Traversal (../ attacks)
  • Prototype Pollution (__proto__, constructor)
  • HTTP Header Injection (CRLF, response splitting)
  • SSRF (private IPs, cloud metadata, dangerous protocols)
  • Open Redirect (absolute URLs, javascript:, protocol-relative)
from arcis import sanitize_string, sanitize_dict

# Sanitize a string
clean = sanitize_string("<script>alert('xss')</script>")
# Result: "&lt;script&gt;alert(&#x27;xss&#x27;)&lt;/script&gt;"

# Sanitize a dictionary (including nested objects)
data = {"name": "<script>xss</script>", "$gt": ""}
clean = sanitize_dict(data)
# Result: {"name": "&lt;script&gt;..."}  ($gt key removed)

Rate Limiting

Protect against brute force and DDoS attacks with fixed window, sliding window, or token bucket:

from arcis import RateLimiter
from arcis.middleware import SlidingWindowLimiter, TokenBucketLimiter

# Fixed window
limiter = RateLimiter(max_requests=100, window_ms=60000)

# Sliding window — smoother rate enforcement
sliding = SlidingWindowLimiter(max_requests=100, window_ms=60000)

# Token bucket — burst-friendly
bucket = TokenBucketLimiter(capacity=100, refill_rate=10)  # 10 tokens/sec

Bot Detection

Detect and categorize bots with 80+ patterns across 7 categories:

from arcis.middleware import BotDetector

detector = BotDetector()
result = detector.detect(user_agent, request_headers)
# result.is_bot, result.category, result.confidence

CSRF Protection

Double-submit cookie pattern with token generation and validation:

from arcis.middleware import CsrfProtection

csrf = CsrfProtection(secret="your-secret-key")

Security Headers

Automatically add security headers to all responses:

  • Content-Security-Policy
  • X-Content-Type-Options: nosniff
  • X-Frame-Options: DENY
  • Strict-Transport-Security
  • X-XSS-Protection: 0

Input Validation

from arcis import Validator, validate_email, validate_url

# Quick validation
if validate_email(user_input):
    print("Valid email!")

# Full validator
assert Validator.email("test@example.com")  # True
assert Validator.url("https://example.com")  # True
assert Validator.uuid("550e8400-e29b-41d4-a716-446655440000")  # True
assert Validator.length("hello", min_len=3, max_len=10)  # True

Safe Logging

Log safely without exposing secrets:

from arcis import SafeLogger

logger = SafeLogger()

# Automatically redacts sensitive fields
logger.info("User login", {"email": "user@test.com", "password": "secret"})
# Output: {"email": "user@test.com", "password": "[REDACTED]"}

# Prevents log injection (removes newlines/control characters)
logger.info("User: attacker\nAdmin: true")  # Newlines stripped

Configuration

All frameworks support the same configuration options:

# Flask
Arcis(
    app,
    sanitize=True,
    sanitize_xss=True,
    sanitize_sql=True,
    sanitize_nosql=True,
    sanitize_path=True,
    rate_limit=True,
    rate_limit_max=100,
    rate_limit_window_ms=60000,
    headers=True,
    csp="default-src 'self'",
)

# FastAPI
app.add_middleware(
    ArcisMiddleware,
    rate_limit_max=50,
    sanitize_sql=False,
)

# Django (settings.py)
ARCIS_CONFIG = {
    'rate_limit_max': 50,
    'sanitize_sql': False,
}

Standalone Middleware (Django)

Use individual components if you only need specific protection:

MIDDLEWARE = [
    'arcis.django.ArcisSanitizeMiddleware',   # Only sanitization
    'arcis.django.ArcisRateLimitMiddleware',  # Only rate limiting
    'arcis.django.ArcisHeadersMiddleware',    # Only security headers
]

Testing

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/ -v

# With coverage
pytest tests/ --cov=arcis --cov-report=html

API Reference

Core Classes

Class Description
Arcis Main class - configures all protections
Sanitizer Input sanitization
RateLimiter Fixed window rate limiting
SlidingWindowLimiter Sliding window rate limiting
TokenBucketLimiter Token bucket rate limiting
BotDetector Bot detection with 80+ patterns
CsrfProtection CSRF double-submit cookie protection
SecurityHeaders Security headers
Validator Input validation
SafeLogger Safe logging with redaction

Exceptions

Exception Description
RateLimitExceeded Raised when rate limit is exceeded
ValidationError Raised when validation fails

Convenience Functions

Function Description
sanitize_string(value) Sanitize a single string
sanitize_dict(data) Sanitize a dictionary
sanitize_xss(value) XSS sanitization only
sanitize_sql(value) SQL injection sanitization only
sanitize_nosql(data) NoSQL injection sanitization only
sanitize_path(value) Path traversal sanitization only
validate_email(value) Email validation with disposable blocklist, typo suggestions, MX verify
validate_url(value) Validate URL format
validate_url_ssrf(value) URL validation with SSRF protection
validate_redirect(value) Open redirect prevention
validate_uuid(value) Validate UUID format

Utilities

Function Description
parse_duration(value) Parse duration strings ("5m", "1h") to milliseconds
get_client_ip(request) Platform-aware IP detection (proxy headers, etc.)
fingerprint_request(request) Generate request fingerprint for tracking

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repo and create your branch from nwl (the active development branch)
  2. All PRs target nwlmain is release-only
  3. All changes must pass existing tests
  4. New features require test cases aligned with spec/TEST_VECTORS.json

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

arcis-1.4.4.tar.gz (114.0 kB view details)

Uploaded Source

Built Distribution

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

arcis-1.4.4-py3-none-any.whl (129.1 kB view details)

Uploaded Python 3

File details

Details for the file arcis-1.4.4.tar.gz.

File metadata

  • Download URL: arcis-1.4.4.tar.gz
  • Upload date:
  • Size: 114.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for arcis-1.4.4.tar.gz
Algorithm Hash digest
SHA256 66fba7f04081dda9fc6d4bd7d207d6b92728a6a8758aa6fc8537c2e2aac9e80b
MD5 3f21cc006b38fb5feaa3e6b4ef33f55e
BLAKE2b-256 26940d8538cf8b4ae4247c5940696b1638f6214e9e4348832d2e025ec4958c93

See more details on using hashes here.

File details

Details for the file arcis-1.4.4-py3-none-any.whl.

File metadata

  • Download URL: arcis-1.4.4-py3-none-any.whl
  • Upload date:
  • Size: 129.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for arcis-1.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f465cb8be56f35fa7b5deecc9c578b92307f3e61b39f5d3765ab7012f9fde08f
MD5 d8f2a5288a105fc67046d0bc3ab9f78c
BLAKE2b-256 ed2a59a8da8f7beb79e5c22176f11d86c0c8d8f2b837e06d2b36c77e2cd09dfc

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