Skip to main content

aiohttp_csrf

The library provides csrf (xsrf) protection for aiohttp.web.

Breaking Change: New in 0.1.0 is Blake3 hashes are used by default. This means you must pass secret_phrase to aiohttp_csrf.storage.SessionStorage

note: The package aiohttp-csrf-fixed is aiohttp_csrf 0.0.2 + this commit. The maintainer didn't submit a PR so I just saw it by chance. I haven't had time to closely examine it but I think it's just removing the HTTP security error that happens if no CSRF is provided. Why do that? An HTTP error is good because it tells the client what happened and lets you handle it by middleware.

0.1.1: Converted @aiohttp_csrf.csrf_exempt decorator to a co-routine to make it compatible with latest aiohttp.

image

Basic usage

The library allows you to implement csrf (xsrf) protection for requests

Basic usage example:

import aiohttp_csrf
from aiohttp import web

FORM_FIELD_NAME = '_csrf_token'
COOKIE_NAME = 'csrf_token'


def make_app():
    csrf_policy = aiohttp_csrf.policy.FormPolicy(FORM_FIELD_NAME)

    csrf_storage = aiohttp_csrf.storage.CookieStorage(COOKIE_NAME)

    app = web.Application()

    aiohttp_csrf.setup(app, policy=csrf_policy, storage=csrf_storage)

    app.middlewares.append(aiohttp_csrf.csrf_middleware)

    async def handler_get_form_with_token(request):
        token = await aiohttp_csrf.generate_token(request)


        body = '''
            <html>
                <head><title>Form with csrf protection</title></head>
                <body>
                    <form method="POST" action="/">
                        <input type="hidden" name="{field_name}" value="{token}" />
                        <input type="text" name="name" />
                        <input type="submit" value="Say hello">
                    </form>
                </body>
            </html>
        '''  # noqa

        body = body.format(field_name=FORM_FIELD_NAME, token=token)

        return web.Response(
            body=body.encode('utf-8'),
            content_type='text/html',
        )

    async def handler_post_check(request):
        post = await request.post()

        body = 'Hello, {name}'.format(name=post['name'])

        return web.Response(
            body=body.encode('utf-8'),
            content_type='text/html',
        )

    app.router.add_route(
        'GET',
        '/',
        handler_get_form_with_token,
    )

    app.router.add_route(
        'POST',
        '/',
        handler_post_check,
    )

    return app


web.run_app(make_app())

Initialize

First of all, you need to initialize aiohttp_csrf in your application:

app = web.Application()

csrf_policy = aiohttp_csrf.policy.FormPolicy(FORM_FIELD_NAME)

csrf_storage = aiohttp_csrf.storage.CookieStorage(COOKIE_NAME)

aiohttp_csrf.setup(app, policy=csrf_policy, storage=csrf_storage)

Middleware and decorators

After initialize you can use @aiohttp_csrf.csrf_protect for handlers, that you want to protect. Or you can initialize aiohttp_csrf.csrf_middleware and do not disturb about using decorator (full middleware example here):

# ...
app.middlewares.append(aiohttp_csrf.csrf_middleware)
# ...

In this case all your handlers will be protected.

Note: we strongly recommend to use aiohttp_csrf.csrf_middleware and @aiohttp_csrf.csrf_exempt instead of manually managing with @aiohttp_csrf.csrf_protect. But if you prefer to use @aiohttp_csrf.csrf_protect, don't forget to use @aiohttp_csrf.csrf_protect for both methods: GET and POST (manual protection example)

If you want to use middleware, but need handlers without protection, you can use @aiohttp_csrf.csrf_exempt. Mark you handler with this decorator and this handler will not check the token:

@aiohttp_csrf.csrf_exempt
async def handler_post_not_check(request):
    ...

Generate token

For generate token you need to call aiohttp_csrf.generate_token in your handler:

@aiohttp_csrf.csrf_protect
async def handler_get(request):
    token = await aiohttp_csrf.generate_token(request)
    ...

Advanced usage

Policies

You can use different policies for check tokens. Library provides 3 types of policy:

  • FormPolicy. This policy will search token in the body of your POST request (Usually use for forms) or as a GET variable of the same name. You need to specify name of field that will be checked.
  • HeaderPolicy. This policy will search token in headers of your POST request (Usually use for AJAX requests). You need to specify name of header that will be checked.
  • FormAndHeaderPolicy. This policy combines behavior of FormPolicy and HeaderPolicy.

You can implement your custom policies if needed. But make sure that your custom policy implements aiohttp_csrf.policy.AbstractPolicy interface.

Storages

You can use different types of storages for storing token. Library provides 2 types of storage:

  • CookieStorage. Your token will be stored in cookie variable. You need to specify cookie name.
  • SessionStorage. Your token will be stored in session. You need to specify session variable name.

Important: If you want to use session storage, you need setup aiohttp_session in your application (session storage example)

You can implement your custom storages if needed. But make sure that your custom storage implements aiohttp_csrf.storage.AbstractStorage interface.

Token generators

You can use different token generator in your application. By default storages using aiohttp_csrf.token_generator.SimpleTokenGenerator

But if you need more secure token generator - you can use aiohttp_csrf.token_generator.HashedTokenGenerator

And you can implement your custom token generators if needed. But make sure that your custom token generator implements aiohttp_csrf.token_generator.AbstractTokenGenerator interface.

Invalid token behavior

By default, if token is invalid, aiohttp_csrf will raise aiohttp.web.HTTPForbidden exception.

You have ability to specify your custom error handler. It can be:

  • callable instance. Input parameter - aiohttp request.
def custom_error_handler(request):
    # do something
    return aiohttp.web.Response(status=403)

# or

async def custom_async_error_handler(request):
    # await do something
    return aiohttp.web.Response(status=403)

It will be called instead of protected handler.

  • sub class of Exception. In this case this Exception will be raised.
class CustomException(Exception):
    pass

You can specify custom error handler globally, when initialize aiohttp_csrf in your application:

...
class CustomException(Exception):
    pass

...
aiohttp_csrf.setup(app, policy=csrf_policy, storage=csrf_storage, error_renderer=CustomException)
...

In this case custom error handler will be applied to all protected handlers.

Or you can specify custom error handler locally, for specific handler:

...
class CustomException(Exception):
    pass

...
@aiohttp_csrf.csrf_protect(error_renderer=CustomException)
def handler_with_custom_csrf_error(request):
    ...

In this case custom error handler will be applied to this handler only. For all other handlers will be applied global error handler.

Metadata

Release files for aiohttp-csrf 0.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 aiohttp-csrf 0.1.1
File Size Uploaded Provenance Actions
aiohttp_csrf-0.1.1.tar.gz 7.3 kB Without provenance Details for aiohttp_csrf-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-csrf 0.1.1
File Interpreter ABI Platform Actions
aiohttp_csrf-0.1.1-py3-none-any.whl Python 3 none any Details for aiohttp_csrf-0.1.1-py3-none-any.whl

Total release size: 14.2 kB

Release files / aiohttp_csrf-0.1.1.tar.gz

Download URL aiohttp_csrf-0.1.1.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ddb8204abf45e9519b9a1166d0107c846b5e8588628f5086d2e9d5730e39d662
BLAKE2b-256 checksum
How to use checksums
641b86dc0a7668b8d64b51913590fe26e6dd9d94986fcfe34bdac63846358fff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

Release files / aiohttp_csrf-0.1.1-py3-none-any.whl

Download URL aiohttp_csrf-0.1.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb51ba706fcfcf73cce9b5a25f3c9c70ae32b4ba28e9e6dc59b0ebd71fb037ea
BLAKE2b-256 checksum
How to use checksums
95d3ce34c0f8b8b830da9ac892dc4d5b412ddc22a93e8fa55adaab381eaaae76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

Security for aiohttp-csrf 0.1.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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