freshcrate
Home > Frameworks > aiohttp-cors

aiohttp-cors

CORS support for aiohttp

Description

======================== CORS support for aiohttp ======================== ``aiohttp_cors`` library implements `Cross Origin Resource Sharing (CORS) <cors_>`__ support for `aiohttp <aiohttp_>`__ asyncio-powered asynchronous HTTP server. Jump directly to `Usage`_ part to see how to use ``aiohttp_cors``. Same-origin policy ================== Web security model is tightly connected to `Same-origin policy (SOP) <sop_>`__. In short: web pages cannot *Read* resources which origin doesn't match origin of requested page, but can *Embed* (or *Execute*) resources and have limited ability to *Write* resources. Origin of a page is defined in the `Standard <cors_>`__ as tuple ``(schema, host, port)`` (there is a notable exception with Internet Explorer: it doesn't use port to define origin, but uses it's own `Security Zones <https://msdn.microsoft.com/en-us/library/ms537183.aspx>`__). Can *Embed* means that resource from other origin can be embedded into the page, e.g. by using ``<script src="...">``, ``<img src="...">``, ``<iframe src="...">``. Cannot *Read* means that resource from other origin *source* cannot be obtained by page (*source* — any information that would allow to reconstruct resource). E.g. the page can *Embed* image with ``<img src="...">``, but it can't get information about specific pixels, so page can't reconstruct original image (though some information from the other resource may still be leaked: e.g. the page can read embedded image dimensions). Limited ability to *Write* means, that the page can send POST requests to other origin with limited set of ``Content-Type`` values and headers. Restriction to *Read* resource from other origin is related to authentication mechanism that is used by browsers: when browser reads (downloads) resource he automatically sends all security credentials that user previously authorized for that resource (e.g. cookies, HTTP Basic Authentication). For example, if *Read* would be allowed and user is authenticated in some internet banking, malicious page would be able to embed internet banking page with ``iframe`` (since authentication is done by the browser it may be embedded as if user is directly navigated to internet banking page), then read user private information by reading *source* of the embedded page (which may be not only source code, but, for example, screenshot of the embedded internet banking page). Cross-origin resource sharing ============================= `Cross-origin Resource Sharing (CORS) <cors_>`__ allows to override SOP for specific resources. In short, CORS works in the following way. When page ``https://client.example.com`` request (*Read*) resource ``https://server.example.com/resource`` that have other origin, browser implicitly appends ``Origin: https://client.example.com`` header to the HTTP request, effectively requesting server to give read permission for the resource to the ``https://client.example.com`` page:: GET /resource HTTP/1.1 Origin: https://client.example.com Host: server.example.com If server allows access from the page to the resource, it responds with resource with ``Access-Control-Allow-Origin: https://client.example.com`` HTTP header (optionally allowing exposing custom server headers to the page and enabling use of the user credentials on the server resource):: Access-Control-Allow-Origin: https://client.example.com Access-Control-Allow-Credentials: true Access-Control-Expose-Headers: X-Server-Header Browser checks, if server responded with proper ``Access-Control-Allow-Origin`` header and accordingly allows or denies access for the obtained resource to the page. CORS specification designed in a way that servers that are not aware of CORS will not expose any additional information, except allowed by the SOP. To request resources with custom headers or using custom HTTP methods (e.g. ``PUT``, ``DELETE``) that are not allowed by SOP, CORS-enabled browser first send *preflight request* to the resource using ``OPTIONS`` method, in which he queries access to the resource with specific method and headers:: OPTIONS / HTTP/1.1 Origin: https://client.example.com Access-Control-Request-Method: PUT Access-Control-Request-Headers: X-Client-Header CORS-enabled server responds is requested method is allowed and which of the specified headers are allowed:: Access-Control-Allow-Origin: https://client.example.com Access-Control-Allow-Credentials: true Access-Control-Allow-Methods: PUT Access-Control-Allow-Headers: X-Client-Header Access-Control-Max-Age: 3600 Browser checks response to preflight request, and, if actual request allowed, does actual request. Installation ============ You can install ``aiohttp_cors`` as a typical Python library from PyPI or from git: .. code-block:: bash $ pip install aiohttp_cors Usage ===== To use ``aiohttp_cors`` you need to configure the application and enable CORS on `resources and routes <https://aiohttp.readthedo

Release History

VersionChangesUrgencyDate
0.8.1Imported from PyPI (0.8.1)Low4/21/2026
v0.8.1Release v0.8.1Low3/31/2025
v0.8.0Release v0.8.0Low3/16/2025
v0.7.0CHANGES ================== - Make web view check implicit and type based (#159) - Disable Python 3.4 support (#156) - Support aiohttp 3.0+ (#155) Low3/6/2018
v0.6.0## Changes - Support aiohttp views by ``CorsViewMixin`` (#145) Low12/21/2017
v0.5.3## Changes - Fix `typing` being installed on Python 3.6.Low4/21/2017
v0.5.2## Changes - Fix tests compatibility with ``aiohttp`` 2.0. This release and release v0.5.0 should work on ``aiohttp`` 2.0. Low3/28/2017
v0.5.1## Changes - Enforce `aiohttp` version to be less than 2.0. Newer `aiohttp` releases will be supported in the next release. Low3/22/2017
v0.5.0## Changes - Fixed compatibility issues with aiohttp 1.1 - Dropped support for aiohttp older than 1.1 Low11/18/2016
v0.4.0- Fixed support with new Resources objects introduced in `aiohttp` 0.21.0. Minimum supported version of `aiohttp` is 0.21.4 now. - New Resources objects are supported. You can specify default configuration for a Resource and use `allow_methods` to explicitly list allowed methods (or `*` for all HTTP methods): ``` python # Allow POST and PUT requests from "http://client.example.org" origin. hello_resource = cors.add(app.router.add_resource("/hello"), { Low4/4/2016
v0.3.0- Rename `UrlDistatcherRouterAdapter` to `UrlDispatcherRouterAdapter`. - Set maximum supported `aiohttp` version to `0.20.2`, see bug #30 for details. Low2/5/2016
v0.2.0- Move ABCs from `aiohttp_cors.router_adapter` to `aiohttp_cors.abc`. - Rename `RouterAdapter` to `AbstractRouterAdapter`. - Fix bug with configuring CORS for named routes. Low11/30/2015
v0.1.0Release v0.1.0Low11/5/2015

Dependencies & License Audit

Loading dependencies...

Similar Packages

pre-commitA framework for managing and maintaining multi-language pre-commit hooks.v4.6.0
azure-core-tracing-opentelemetryMicrosoft Azure Azure Core OpenTelemetry plugin Library for Pythonazure-template_0.1.0b6187637
spdx-toolsSPDX parser and tools.0.8.5
lacesDjango components that know how to render themselves.0.1.2
django-tasksA backport of Django's built in Tasks framework0.12.0