Skip to main content

TornadoAPI Guard


tornadoapi-guard is a security library for Tornado that provides middleware to control IPs, log requests, detect penetration attempts and more. It integrates seamlessly with Tornado to offer robust protection against various security threats. Powered by guard-core.

License CI Release CodeQL Docs last-commit

Python Tornado Redis

Website · Docs · Playground · Dashboard · Discord


Ecosystem

TornadoAPI Guard is the Tornado adapter for the Guard security ecosystem. Powered by guard-core and compatible with the shared telemetry agent. Parallel implementations exist for TypeScript (on npm) and Rust (on crates.io).

Python

Package Role PyPI
guard-core Framework-agnostic security engine PyPI
guard-agent Telemetry agent PyPI
fastapi-guard FastAPI / Starlette adapter PyPI
flaskapi-guard Flask adapter PyPI
djapi-guard Django adapter PyPI
tornadoapi-guard Tornado adapter (this package) PyPI

TypeScript / JavaScript

Published under the @guardcore npm scope. Source in the guard-core-ts monorepo. Production-ready.

Package Role npm
@guardcore/core Core engine npm
@guardcore/express Express adapter npm
@guardcore/nestjs NestJS adapter npm
@guardcore/fastify Fastify adapter npm
@guardcore/hono Hono adapter npm

Rust

Published on crates.io. 🚧 Placeholder crates — implementation in progress.

Package Role crates.io
guard-core Core engine crates.io
actix-guard-rs Actix adapter crates.io
axum-guard-rs Axum adapter crates.io
rocket-guard-rs Rocket adapter crates.io
tower-guard-rs Tower adapter crates.io

Features

  • IP Whitelisting and Blacklisting: Control access based on IP addresses.
  • User Agent Filtering: Block requests from specific user agents.
  • Rate Limiting: Limit the number of requests from a single IP.
  • Automatic IP Banning: Automatically ban IPs after a certain number of suspicious requests.
  • Penetration Attempt Detection: Detect and log potential penetration attempts.
  • HTTP Security Headers: Comprehensive security headers management (CSP, HSTS, X-Frame-Options, etc.)
  • Custom Logging: Log security events to a custom file.
  • Cloud Provider IP Blocking: Block requests from cloud provider IPs (AWS, GCP, Azure).
  • IP Geolocation: Use a service like IPInfo.io API to determine the country of an IP address.
  • Distributed State Management: (Optional) Redis integration for shared security state across instances
  • Flexible Storage: Redis-enabled distributed storage or in-memory storage for single instance deployments

Installation

To install tornadoapi-guard, use pip:

pip install tornadoapi-guard

v1.0.0 release notes

tornadoapi-guard 1.0.0 ships against guard-core >= 3.0.0 and exposes two adapter-side surfaces alongside the upstream fail-secure default.

Upstream fail-secure default

SecurityConfig.fail_secure defaults to True (inherited from guard-core 3.0.0). If any security check raises an unhandled exception, the request is blocked with HTTP 500 instead of falling through. Opt out on deployments that need fail-open behavior:

from tornadoapi_guard import SecurityConfig, SecurityMiddleware

config = SecurityConfig(
    fail_secure=False,
)
middleware = SecurityMiddleware(config=config)

The recommended migration is to keep the new default, surface check exceptions in your monitoring, and fix them at the root.

Reading agent buffer state

SecurityMiddleware.agent_stats exposes the agent's live buffer drop counters and circuit-breaker state without reaching into the agent directly. Wire it into a Tornado-style health-check handler:

import tornado.web
from tornadoapi_guard import SecurityMiddleware


class AgentHealthHandler(tornado.web.RequestHandler):
    async def get(self) -> None:
        middleware: SecurityMiddleware = self.application.settings["security_middleware"]
        self.write(middleware.agent_stats)
# {"enabled": True,
#  "buffer_stats": {"events_dropped": 0, "metrics_dropped": 0, ...},
#  "transport_stats": {"circuit_breaker_state": "CLOSED", ...}}

When the agent is disabled or failed to initialize, the property returns {"enabled": False}. Read it on each scrape — it reflects live counters and is not cached.

Wiring the package version through to the agent

from tornadoapi_guard import SecurityConfig, __version__

config = SecurityConfig(
    enable_agent=True,
    agent_api_key="...",
    agent_guard_version=__version__,
)

__version__ resolves via importlib.metadata.version("tornadoapi_guard") and falls back to "0.0.0+unknown" when the package is not installed (development from source). Pairs with guard-core >= 3.0.0's SecurityConfig.agent_guard_version for SaaS-side telemetry attribution.


Quick Start

import asyncio
import tornado.web
from tornadoapi_guard import SecurityConfig, SecurityHandler, SecurityMiddleware

config = SecurityConfig(
    rate_limit=100,
    auto_ban_threshold=5,
    enable_penetration_detection=True,
)
middleware = SecurityMiddleware(config=config)


class HelloHandler(SecurityHandler):
    async def get(self) -> None:
        self.write({"message": "hello"})


async def main() -> None:
    await middleware.initialize()
    app = tornado.web.Application(
        [(r"/", HelloHandler)],
        security_middleware=middleware,
    )
    app.listen(8000)
    await asyncio.Event().wait()


if __name__ == "__main__":
    asyncio.run(main())

See examples/simple_app/main.py for a full reference application exercising every decorator and security feature.


Documentation

Full documentation is published at https://guard-core.github.io/tornadoapi-guard/latest/, or build locally with make serve-docs.


Development

# Install dependencies
make install-dev

# Run tests locally
make local-test

# Run linters
make lint

# Fix formatting
make fix

# Run all checks
make check-all

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Author

Renzo Franceschini - contact@guard-core.com .


Acknowledgements

Metadata

Release files for tornadoapi-guard 1.2.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 tornadoapi-guard 1.2.1
File Size Uploaded
tornadoapi_guard-1.2.1.tar.gz 30.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tornadoapi-guard 1.2.1
File Interpreter ABI Platform
tornadoapi_guard-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 46.2 kB

Release files / tornadoapi_guard-1.2.1.tar.gz

Download URL tornadoapi_guard-1.2.1.tar.gz
Size 30.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7e91541b36f2b5bd831bcbe955ccc57d783b179db27e28a636f5cba98233e35e
BLAKE2b-256 checksum
How to use checksums
36c066684283daed98ee80f63fe204be9856b42ac5596d816725c06c2f92fb05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 8, 2026.

Transparency log

Release files / tornadoapi_guard-1.2.1-py3-none-any.whl

Download URL tornadoapi_guard-1.2.1-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
125909599a4e49835d92d51c243545311c6e53894aae5e0aa5826ee7e55b707f
BLAKE2b-256 checksum
How to use checksums
1904c7273463768f65483e14e70ab243e17f54eecb19efed2bae4d240991c8a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 8, 2026.

Transparency log

Release history Release notifications | RSS feed

2.0.0

2 release files

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.0.0

2 release files

0.0.1

2 release files

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