Skip to main content

Whitelist urls on ASGI applications allowing for cross origin requests

Project description

asgi-cors-middleware

Python package that allows whitelisting of urls on ASGI applications making it possible to perform cross origin requests from the browser.

CORS in a nutshell

Cross-Origin Resource Sharing (CORS) allows a server to define any origins other than its own that are safe for the browser to load resources from.

Mozilla does a good job of explaining CORS here.

Background

Assuming you have a web application that follows a client-server architecture, it's possible the frontend would be running on a server different from the API. If the frontend application made a request to the API, this kind of request would be blocked by the browser.

For security reasons, browsers block cross origin requests by default.

A cross origin request is a request made to a server with a different url/ origin. To mitigate around this, we could simply add the url of the frontend application as an allowed origin on the API server. Most web frameworks provide a way to do this or have third party libraries that achieve the same.

asgi-cors-middleware aims to provide a simple way to achieve the above for ASGI applications.

Features

  • Simple
  • Works with most ASGI frameworks (Django, Starlette, FastAPI, channels)
  • Works with Ariadne

Installation

Can be installed via pip

pip install asgi-cors-middleware

Usage

To use the middleware, just import it like so:

from asgi_cors_middleware import CorsASGIApp

To start whitelisting origins, just wrap your asgi application instance with CorsASGIApp.

app = CorsASGIApp(
    app=asgi_app_instance,
    origins=["www.example.com"]
)

Example

A simple HelloWorld application that whitelists the origins below:

Install an ASGI server

pip install uvicorn

Create a file called example.py and update it with the code below:

from asgi_cors_middleware import CorsASGIApp

class HelloWorld:
    def __init__(self, scope):
        pass

    async def __call__(self, receive, send):
        await send({
            'type': 'http.response.start',
            'status': 200,
            'headers': [
                [b'content-type', b'text/plain'],
            ]
        })
        await send({
            'type': 'http.response.body',
            'body': b'Hello, world!',
        })

app = CorsASGIApp(
    app=HelloWorld,
    origins=[
        "www.example.com",
        "localhost:9000"
    ]
)

That's it. For real, that's really it. Now your application is all set to allow requests from www.example.com and localhost:9000.

Run the app

uvicorn example:app

Contributing

For guidance and instructions, please see CONTRIBUTING.md

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

asgi-cors-middleware-0.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

asgi_cors_middleware-0.0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file asgi-cors-middleware-0.0.1.tar.gz.

File metadata

  • Download URL: asgi-cors-middleware-0.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.2

File hashes

Hashes for asgi-cors-middleware-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e4f769693b2ec11ebd031f347f927d6dae1f5423d84f7222e00bcb3c6ae7faa6
MD5 3c0a8924bfeebd71af652c6bdb2186d2
BLAKE2b-256 532ea97a99d57c0c0f1da220da4b7199d58e68b26b557c7cab1119346f029c58

See more details on using hashes here.

File details

Details for the file asgi_cors_middleware-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: asgi_cors_middleware-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.2

File hashes

Hashes for asgi_cors_middleware-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 998fbfc2f8c51df92eeb417bda692acbf9de1306f651e160a08444f891b313de
MD5 5f02bb2437d60c3f5b650fdafaf9479c
BLAKE2b-256 459536542aed679da736e7bf6ec4a406d5127ae2a8a9df0999aa8535439737c4

See more details on using hashes here.

Supported by

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