Object Storage Swift - Setting up CORS on Object Storage
Objective
CORS is a mechanism that allows code running in a browser (e.g. Javascript) to make requests to a domain other than the one from which it originates.
Swift supports CORS requests to containers and objects.
CORS metadata is only present in the container. The values specified apply to the container itself and all objects contained within it.
The purpose of this guide is to familiarise you with the concept of CORS.
Requirements
- Preparing the environment to use the OpenStack API by installing python-swiftclient
- Setting the OpenStack environment variables
Instructions
There are 3 metadata to manage the CORS on a container:
| Metadata | Description |
|---|---|
| X-Container-Meta-Access-Control-Allow-Origin | Origins allowed to make Cross Origin queries, separated by a space. |
| X-Container-Meta-Access-Control-Max-Age | Maximum time during which the origins can retain the results of the preliminary check. |
| X-Container-Meta-Access-Control-Expose-Headers | Headers exposed to the user agent (for example the browser) in the actual request response. Separated by a space. |
The background
CORS Metadata Definition
CORS specifies a wildcard character *, which permits access to all user agents, regardless of domain, protocol, or host. While there are valid use cases for this approach, it also permits a malicious actor to create a convincing facsimile of a user interface, and trick users into revealing authentication credentials. Please carefully evaluate your use case and the relevant documentation to avoid any risk for your organization.
The CORS specification does not support using this wildcard as a part of a URI. Setting allowed_origin to * would work, while *.example.com would not.
If the server is running on a non-standard port, it must be specified: http://example.com:8080
Demo page
Host the following html page on the web server corresponding to the CORS origin.
cors.html:
Request Headers
Response Headers
Before a browser issues an actual request, it can issue a screening request. The screening request is a call to verify that the source is authorised to make the request. The sequence of events is as follows:
- The browser sends an OPTIONS request to Swift.
- Swift returns
200/401to the browser depending on the authorisedorigins. - If the response is
200, the browser makes the "actual request" to Swift, i.e.PUT,POST,DELETE,HEAD,GET.
When a browser receives a response to an actual request, it exposes only the headers listed in the Access-Control-Expose-Headers header. By default, Swift returns the following values for this header:
- "simple-response-header" as listed on http://www.w3.org/TR/cors/#simple-response-header
- The
etag,x-timestamp,x-trans-id,x-openstack-request-idheaders. - All metadata headers (
X-Container-Meta-*for containers andX-Object-Meta-*for objects). - The headers listed in
X-Container-Meta-Access-Control-Expose-Headers. - Headers configured with the cors_expose_headers option in
proxy-server.conf.
Delete CORS Metadata
Go further
If you need training or technical assistance to implement our solutions, contact your sales representative or click on [this link]((https://www.ovhcloud.com/en-au/professional-services/) to get a quote and ask our Professional Services experts for assisting you on your specific use case of your project.
Join our community of users.
-
Secure Shell (SSH) : un protocole de réseau sécurisé utilisé pour établir des connexions entre un client et un serveur. Il permet d'exécuter des commandes à distance de manière sécurisée. ↩