What Is HTTP CORS?

Today we take it for granted that all the resources contained in a website, such as images or videos, are displayed immediately when the site is opened. Often, however, these resources come from a different source. Many images and, above all, videos come from third-party providers or from CDNs (content delivery networks). The most prominent examples are image services such as Adobe Stock, video sources such as YouTube, and service providers such as Google or Amazon that deliver fonts or scripts. When it comes to third-party resources, however, modern browsers follow an overarching rule or restriction known as the SOP (same-origin policy).

This means that a browser may only load resources contained in a web page from the same origin as the web page itself. The effect can often be observed when a web page initially shows only placeholders in place of image or video resources. With HTTP CORS (Cross-Origin Resource Sharing), this restriction can be lifted selectively. A targeted CORS definition tells a client for which resources access from other origins is to be allowed.

How Resource Requests and the SOP Interact

All resource restrictions imposed by the SOP can prevent images, videos, fonts, and scripts from being loaded. The SOP only permits the use of resources from the own origin of the requested domain. The following fictitious example shows how this works in more detail.

  • You open the following web page in a browser.

https://www.dataliquid.com/home.html

The essential security concept of the SOP thus actively prevents a website from loading harmful resources or injecting malicious scripts into the browser. Without the SOP, malicious code could, for example, access all session data in a browser without restriction. With HTTP CORS, this safeguard can be controlled selectively and opened for permitted resources.

How Does CORS Work in Practice?

In the previous example, the web page could not load the image files because of the existing SOP. With an appropriate instruction, however, loading can be enabled explicitly. To do this, CORS must be defined for one or more targets from which the resources are to be loaded. The browser asks the target whether a cross-origin request should be allowed. This is usually controlled either through a specially parameterized HTTP header or through a separate request.

Below is a sample request header for what is known as a simple resource request.

GET /typo3conf/ext/sitepackage/Resources/Public/Images/Favicon/favicon.ico
HTTP/2.0
Accept: text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: de,en-US;q=0.7,en;q=0.3
Cache-Control: max-age=0
Connection: keep-alive
Cookie: cb-enabled=enabled; _ga=GA1.2.…9; _gat_gtag_UA_112275217_1=1
DNT: 1
Host: www.dataliquid.com
If-Modified-Since: Sun, 05 Mar 2017 16:14:06 GMT
If-None-Match: "58bc394e-3aee"
TE: Trailers
Referer: https://www.dataliquid.com/Images/Favicon/favicon.ico
Origin: https://www.dataliquid.com/
User-Agent: Mozilla/5.0 (Windows NT 6.3; W…) Gecko/20100101 Firefox/63.0

In this server request, the most important parameter, “Origin,” specifies the source (domain) from which the call is made. The server can then respond accordingly and permit an exception to the SOP. The response header carries a corresponding instruction, as follows.

HTTP / 2.0 200 OK
access-control-allow-origin: *
age: 2314637
alt-svc: quic=":443"; ma=2592000; v="44,43,39,35"
cache-control: no-cache, no-store, must-revalidate
content-length: 35
content-type: image/gif
date: Wed, 14 Nov 2018 15:28:16 GMT
expires: Mon, 01 Jan 1990 00:00:00 GMT
last-modified: Sun, 17 May 1998 03:00:00 GMT

In the response, the most important parameter, “Access-Control-Allow-Origin,” defines the permissions granted. In this case, the asterisk * means that the resource is released generally, across sites, for all domains. This parameter in particular should therefore be handled with care. Ideally, you enter exactly the domains that are required.

In addition to the simple resource request, CORS also allows “preflight requests” for more complex requests. A preflight request of this kind is sent to the target in advance with a CORS header. This additional request determines whether a planned cross-origin request may be allowed or not. The corresponding CORS header is sent with the HTTP method OPTIONS. Preflight requests offer extensive further options that go beyond the scope of this article.

Conclusion

Network security remains a top priority. Alongside the human factors, network technology provides equally important aspects and methods for securing the transfer of content and data between servers and clients. The SOP directive is a first step. To offer and use resources across origins in a secured browser environment, HTTP CORS should be used. It lets you configure and permit access to third-party resources and domains in great detail.

An important aspect of CORS, however, is handling the permission options correctly. A rule that is not restrictive enough is granted all too quickly and opens the door to malware and data theft. Targeted permissions, strict checks of REST APIs, for example, and above all extensive penetration tests should be carried out carefully before release. A general recommendation is also to implement CORS only when third-party resources and sources have to be accessed for specific reasons. In that case, strict care in the implementation comes first.

Share LinkedIn X Email