The Cross-Origin-Resource-Policy
(CORP) header is an HTTP response header that allows server administrators to control which origins can access specific resources on their server. This header provides an additional layer of security by preventing unauthorized cross-origin requests from accessing sensitive resources, thus mitigating potential security risks such as cross-origin data leaks and attacks.
Purpose
The primary purpose of the Cross-Origin-Resource-Policy
header is to protect sensitive resources from being accessed by unauthorized origins. By specifying a policy, server administrators can control which origins are allowed to access specific resources, thereby limiting the potential for cross-origin data leaks and attacks.
Syntax
The syntax of the Cross-Origin-Resource-Policy
header is as follows:
Cross-Origin-Resource-Policy: <policy>
Where <policy>
can be one of the following values:
same-origin
: The resource can only be accessed by the same origin that served the resource.same-site
: The resource can be accessed by any origin within the same site, as determined by the site concept.cross-origin
: The resource can be accessed by any origin, effectively allowing cross-origin requests.
Usage
To use the Cross-Origin-Resource-Policy
header, include it in the HTTP response for the specific resource you want to protect. Depending on the policy value, different origins will be allowed or denied access to the resource.
Example 1: same-origin policy
In this example, the server sets the Cross-Origin-Resource-Policy
header to same-origin
, allowing only requests from the same origin to access the resource.
Request:
GET /sensitive-data HTTP/1.1
Host: example.com
Response:
HTTP/1.1 200 OK
Content-Type: application/json
Cross-Origin-Resource-Policy: same-origin
{
"data": "sensitive information"
}
In this case, only requests from the example.com
origin will be allowed to access the sensitive data. Requests from other origins will be blocked.
Example 2: same-site policy
In this example, the server sets the Cross-Origin-Resource-Policy
header to same-site
, allowing any origin within the same site to access the resource.
Request:
GET /shared-data HTTP/1.1
Host: subdomain.example.com
Response:
HTTP/1.1 200 OK
Content-Type: application/json
Cross-Origin-Resource-Policy: same-site
{
"data": "shared information"
}
In this case, requests from any origin within the example.com
site, including its subdomains, will be allowed to access the shared data. Requests from other sites will be blocked.
Example 3: cross-origin policy
In this example, the server sets the Cross-Origin-Resource-Policy
header to cross-origin
, allowing any origin to access the resource.
Request:
GET /public-data HTTP/1.1
Host: example.com
Response:
HTTP/1.1 200 OK
Content-Type: application/json
Cross-Origin-Resource-Policy: cross-origin
{
"data": "public information"
}
In this case, requests from any origin will be allowed to access the public data.
Summary
The Cross-Origin-Resource-Policy
header is a valuable security feature that allows server administrators to control which origins can access specific resources on their server. By setting appropriate policies, you can protect sensitive resources from unauthorized cross-origin requests and mitigate potential security risks.
Remember to choose the appropriate policy value depending on your use case:
same-origin
: Restrict access to the same origin that served the resource.same-site
: Allow access to any origin within the same site.cross-origin
: Allow access to any origin.
By understanding and implementing the Cross-Origin-Resource-Policy
header, you can enhance the security of your web applications and protect sensitive data from potential threats.