As an industry-leading web security specialist with over two decades of experience, I've worked extensively with enterprises seeking to protect their applications from cross-origin threats. One powerful yet often overlooked mechanism is the Cross-Origin Resource Policy (CORP) HTTP header. This header can thwart hotlinking, cross-site script inclusion (XSSI), and speculative side-channel attacks such as Spectre—ultimately enhancing a website's defensive posture. Below, we'll explore how CORP functions, the risks of not using it, and practical steps for configuring it in Apache, Nginx, and IIS.
Cross-Origin Resource Policy is a security measure communicated via the Cross-Origin-Resource-Policy response header. It offers websites and applications the ability to control which external origins can embed certain resources—especially images, scripts, and other media. Although the same-origin policy blocks many cross-origin requests (particularly JavaScript-based fetch calls), it doesn't prevent embedded resources from being accessed cross-origin by default. CORP helps fill that gap.
Key Insight: CORP specifically focuses on resources embedded without CORS checks, such as images, scripts, and other media loaded through HTML elements like <img>
, <script>
, or <video>
.
Vulnerabilities like Meltdown and Spectre showed how malicious parties could extract sensitive information by exploiting the browser's speculative execution. CORP adds an extra line of defense, preventing loaded resources from leaking data to a hostile website.
Attackers can sometimes load your scripts in their own pages to glean user data or internal application details. CORP can instruct browsers to block such attempts.
CORP can help deter unauthorized embedding of your site's images or other media, minimizing bandwidth theft and plagiarism.
The Cross-Origin-Resource-Policy header accepts three possible values, each specifying different levels of restrictiveness:
Only resources loaded from the exact same scheme, host, and port are allowed.
Example: If set on https://example.com
, resources can only be embedded within pages from the same domain, using the same protocol and port.
Resources may be loaded from the same site, regardless of subdomain or port number.
Example: If set on https://api.example.com
, resources can be embedded by pages from https://example.com
or https://shop.example.com
.
Any origin can embed the resource. This is the least restrictive policy and may be needed if you want your assets to be widely embeddable (e.g., for public-facing APIs, CDNs, or widgets).
Example: If set on https://cdn.example.com/image.jpg
, any website can embed this image.
CORS (Cross-Origin Resource Sharing) | CORP (Cross-Origin Resource Policy) |
---|---|
Loosens restrictions by allowing cross-origin requests if explicitly permitted. | Tightens restrictions for embedded resources that would typically bypass same-origin policy protections. |
Uses Access-Control-Allow-Origin header to grant access. |
Uses Cross-Origin-Resource-Policy header to restrict access. |
Primarily affects JavaScript API fetch/XHR requests. | Primarily affects embedded resources (images, scripts, etc.). |
Opens communication channels that would otherwise be blocked. | Closes channels that would otherwise be open by default. |
While CORS focuses on enabling controlled cross-origin access (usually via Access-Control-Allow-Origin), CORP restricts or denies embedding of resources unless certain conditions are met. Both can coexist but serve different purposes in managing cross-origin requests.
Omitting CORP can leave your website exposed to:
Below are step-by-step instructions for setting up the Cross-Origin-Resource-Policy header on Apache, Nginx, and IIS. Choose the header value that best meets your security needs (same-origin, same-site, or cross-origin).
a2enmod headers
In your server configuration or .htaccess file, add:
<IfModule mod_headers.c> Header set Cross-Origin-Resource-Policy "same-origin" </IfModule>
Replace "same-origin" with "same-site" or "cross-origin" as needed.
systemctl restart apache2
or
service apache2 restart
Cross-Origin-Embedder-Policy (COEP) is closely related to CORP. If you set a COEP header (e.g., Cross-Origin-Embedder-Policy: require-corp), it will demand that embedded resources explicitly opt-in to cross-origin sharing by including the CORP header or appropriate CORS headers. This helps achieve a higher level of isolation, which browsers may require for powerful features like high-resolution timers or SharedArrayBuffer.
CDN Tip: If you maintain a CDN or any publicly consumable assets, consider explicitly setting:
Cross-Origin-Resource-Policy: cross-origin
so you're not accidentally blocking legitimate embedding requests from external sites—or from your own subdomains and ports, if your infrastructure is spread across multiple endpoints.
Learn more about Cross-Origin Resource Policy from these authoritative sources:
Learn about other HTTP security headers to strengthen your website's security: