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:
- www.example.com
- localhost:9000
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
Metadata
Release files for asgi-cors-middleware 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asgi-cors-middleware-0.0.2.tar.gz | 7.5 kB | Without provenance | Details for asgi-cors-middleware-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asgi_cors_middleware-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for asgi_cors_middleware-0.0.2-py3-none-any.whl |
Total release size: 13.1 kB
Release files / asgi-cors-middleware-0.0.2.tar.gz
| Download URL | asgi-cors-middleware-0.0.2.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
054ee9409c9b6c8088e758719d38441cd2fdb7cf7675318c89a13f01b5bb4efe
|
|
BLAKE2b-256 checksum How to use checksums |
d053dadd41692da6fc8462bc6025c19a52ededa4d3833aff10fb80791e97d516
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.4
|
Release files / asgi_cors_middleware-0.0.2-py3-none-any.whl
| Download URL | asgi_cors_middleware-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
305c7e258d4b81a9e8bebc493163e88155daa0f3909c7190ce7d4d3a7aaf1436
|
|
BLAKE2b-256 checksum How to use checksums |
5abaf3f5ba3a1d65318bbcf3503b7647c5138b76c797a2fe681263b98af28c01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.4
|
Security for asgi-cors-middleware 0.0.2
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.