Cross-Origin Resource Sharing (CORS): The Deep Dive
Cross-Origin Resource Sharing (CORS) is a browser mechanism that relaxes the Same-Origin Policy (SOP). While intended to allow safe data sharing, misconfigurations can allow attackers to bypass SOP entirely and steal sensitive user data like API keys, session tokens, and personal information.
đ¸ 1. The Core Mechanics
To understand the attacks, you must understand the âHandshake.â
The Request (Browser)
When a browser makes a cross-origin request (e.g., from evil.com to bank.com), it automatically adds the Origin header.
GET /api/data HTTP/1.1
Host: bank.com
Origin: [https://evil.com](https://evil.com)
Cookie: session=...
The Response (Server)
The server decides whether to allow the request using three key headers:
-
Access-Control-Allow-Origin(ACAO): The specific domain allowed to read the response. -
Access-Control-Allow-Credentials(ACAC): Must betrueto allow the browser to share cookies/auth headers. -
Access-Control-Allow-Methods: Allowed HTTP methods (GET, POST, etc.).
The Trap: If ACAC is true, the ACAO header cannot be a wildcard (*). It must match the requesting origin exactly. This forces developers to dynamically generate the ACAO header, leading to vulnerabilities.
𧨠2. Vulnerability Class 1: The âReflectionâ Flaws
These occur when the server blindly trusts the clientâs input to generate the ACAO header.
A. Basic Origin Reflection
The application reads the Origin header from the request and echoes it back in the Access-Control-Allow-Origin header.
Attack: The attacker hosts a script on evil.com.
Result: The server sees Origin: https://evil.com and responds with Access-Control-Allow-Origin: https://evil.com and Credentials: true. The browser allows the attacker to read the victimâs data.
B. Errors Parsing Origin Headers (Weak Regex)
Developers try to validate the origin but use flawed regular expressions.
Prefix Match: Allows normal-website.com.evil.com.
Suffix Match: Allows hackersnormal-website.com.
Protocol Blindness: Allows http://normal-website.com even if the site is HTTPS (see âBreaking TLSâ below).
𧨠3. Vulnerability Class 2: The null Origin
The value null is a special origin sent by the browser in specific scenarios. Developers often whitelist it for local testing, not realizing it can be triggered by attackers.
The Attack Vectors
If a server whitelists Access-Control-Allow-Origin: null, it can be exploited in three ways:
-
Sandboxed Iframes:
An attacker uses
<iframe sandbox="allow-scripts ...">. Requests from this iframe are sent with Origin: null. -
Local Files (file://):
If an attacker can trick a victim into downloading and opening a malicious HTML file locally, requests from that file send Origin: null.
-
Cross-Origin Redirects:
The attacker forces a redirect chain: evil.com -> vulnerable.com. In some redirect scenarios, the final request may have Origin: null. If whitelisted, the attacker captures the data.
𧨠4. Vulnerability Class 3: Trust Relationships & XSS
Even if CORS is configured âcorrectlyâ (only whitelisting specific trusted domains), the system is only as secure as its weakest trust.
The Scenario:
secure-bank.com allows CORS from partner-site.com.
partner-site.com has a Cross-Site Scripting (XSS) vulnerability.
The Attack:
-
The attacker injects a malicious script into
partner-site.com(via the XSS). -
The script makes a request to
secure-bank.com. -
The browser sends
Origin: https://partner-site.com. -
secure-bank.comtrusts this origin and returns the sensitive data. -
The attackerâs script exfiltrates the data.
Impact: XSS on a trusted partner domain equals full compromise of the main domain via CORS.
𧨠5. Vulnerability Class 4: Breaking TLS (The MITM)
This attack exploits the fact that some HTTPS sites allow CORS requests from their own insecure HTTP subdomains.
The Scenario:
https://secure.com trusts http://trusted.secure.com.
The Attack:
-
The attacker performs a Man-in-the-Middle (MITM) attack on the victim (e.g., public WiFi).
-
They intercept traffic to the insecure
http://trusted.secure.comand inject malicious JS. -
This script makes a CORS request to
https://secure.com. -
Since
secure.comtrusts the HTTP origin, it releases the data. -
Result: The attacker bypasses TLS protections entirely by leveraging the insecure trust relationship.
𧨠6. Vulnerability Class 5: Intranet Exploitation
Most internal networks (Intranets) are less hardened than public sites. They often rely on IP-based authentication or allow Access-Control-Allow-Origin: *.
The Attack:
-
Attacker convinces a victim (employee) to visit
evil.com. -
evil.comuses the victimâs browser to scan the internal network (192.168.x.x). -
If an internal app allows
ACAO: *(common in dev environments), the attackerâs script can read internal documents and exfiltrate them to the public internet.
Note: This works even without credentials if the internal app relies solely on âbeing on the networkâ for access.
đĄď¸ 7. Prevention Strategies
-
Strict Whitelisting: Never reflect the
Originheader. Check it against a hardcoded list of trusted domains. -
Block
null: Never whitelist thenullorigin. -
Protocol Parity: Only allow HTTPS origins for HTTPS sites. Never trust HTTP.
-
Vary Header: Always send
Vary: Origin. This tells proxies/caches that the response content (headers) changes based on the Origin, preventing cache poisoning. -
Avoid Wildcards: Do not use
*for internal resources or any resource handling credentials.
â 8. Interview Corner: 10 Advanced Questions
Q1: Can you exploit CORS if the server reflects the Origin but sets Access-Control-Allow-Credentials: false?
Answer:
Only if the data is accessible without authentication.
If the endpoint requires a session cookie, and ACAC is false, the browser will not send the cookie. The server will see an unauthenticated request and likely return an error or public data.
Exception: If the app uses IP-based auth (Intranet) and the victim is on the allowed network, the attack still works.
Q2: What is the âNull Originâ vulnerability and how is it exploited?
Answer:
It occurs when a server whitelists the string null in the ACAO header. It is exploited by using a sandboxed iframe (<iframe sandbox="allow-scripts ...">). Browsers send Origin: null for requests from such frames, matching the whitelist and allowing data theft.
Q3: How does XSS affect CORS security?
Answer:
If a Trusted Origin (Domain A) has an XSS vulnerability, it compromises Domain B (the target) if Domain B whitelists Domain A for CORS. The attacker uses the XSS on Domain A to launch authenticated CORS requests to Domain B, which are permitted because the Origin is trusted.
Q4: Explain the âBreaking TLSâ CORS attack.
Answer:
It happens when a secure HTTPS site trusts an insecure HTTP subdomain for CORS. An attacker with network positioning (MITM) injects code into the HTTP traffic. This code sends requests to the HTTPS site. Since the HTTPS site trusts the HTTP origin, it returns sensitive data, which the attacker steals, effectively bypassing encryption.
Q5: Why is Access-Control-Allow-Origin: * dangerous on an Intranet?
Answer:
Internal apps often lack strong auth, relying on network perimeter security. If an internal app allows , an external attacker can use a victim employeeâs browser as a proxy. The victim visits the attackerâs site, which JS-scans the internal network and reads the data because the internal server explicitly permits âeveryoneâ () to read it.
Q6: Can you bypass a regex that validates *.normal-website.com?
Answer:
Yes. If the regex isnât anchored correctly (^ and $), I can register hackersnormal-website.com (suffix match) or normal-website.com.evil.com (prefix/subdomain match). The server sees the trusted string inside the malicious domain and allows it.
Q7: What is the Preflight Request and when does it happen?
Answer:
A Preflight is an OPTIONS request sent by the browser before the actual request. It happens for âcomplexâ requestsâthose using methods other than GET/POST/HEAD, or custom headers (like Content-Type: application/json). It asks the server permission to send the actual request.
Q8: What does Vary: Origin do?
Answer:
It instructs caches (CDNs/Browsers) to key the cache based on the Origin header. This prevents a âpoisonedâ cache scenario where a response containing ACAO: malicious.com (generated for an attacker) is served to a legitimate user, or vice versa.
Q9: Can CORS be exploited without user interaction?
Answer:
Usually, no. The victim must visit the attackerâs site (or a compromised site) while logged in to the target application. It is a client-side attack dependent on the victimâs browser context.
Q10: Is it safe to dynamically reflect the Origin if I validate it first?
Answer:
Yes, this is the standard secure implementation. You read the Origin, check if it exists in your database of trusted domains (exact match), and only then reflect it. If it doesnât match, you return no CORS headers or a default error.
đ 9. Scenario-Based Questions
đ Scenario 1: The âNullâ Whitelist
Context: You see Access-Control-Allow-Origin: null in a response. The developer says itâs needed for their local file testing.
The Question: How do you prove this is a critical vulnerability?
The âHiredâ Answer:
âI will build a Proof of Concept using a sandboxed iframe.
-
I create
exploit.htmlwith<iframe sandbox='allow-scripts allow-forms' srcdoc='...malicious JS...'>. -
The JS inside attempts to read the endpoint.
-
The browser sends
Origin: null. -
The server accepts it and returns the data.
This proves that anyone can bypass their SOP protections, not just their local files.â
đ Scenario 2: The Trust Chain
Context: api.target.com is secure. It only allows CORS from help.target.com. You scanned help.target.com and found a Stored XSS.
The Question: What is the maximum impact?
The âHiredâ Answer:
âThe impact is full data compromise of api.target.com.
-
I use the Stored XSS on
helpto execute JS on all visitors. -
My JS makes XHR requests to
api.target.com. -
Since
apitrustshelp, the browser permits the read. -
I can steal all data visible to the
apiendpoint for every user who visits thehelppage.â
đ Scenario 3: The Intranet Scan
Context: You are on an external red team. You know the company uses an internal ticketing system at 10.0.0.5. You suspect it allows wildcard CORS.
The Question: How do you exploit this without VPN access?
The âHiredâ Answer:
âI will use a Drive-By CORS attack.
-
I host a malicious website (e.g., a watering hole or phishing link).
-
When an employee visits, my JS attempts to fetch
http://10.0.0.5/tickets. -
If the internal server sends
Access-Control-Allow-Origin: *, the browser allows my external script to read the internal response. -
My script sends the ticket data back to my external C2 server.â
đ Scenario 4: The Credentials Mismatch
Context: A developer sets Access-Control-Allow-Origin: * AND Access-Control-Allow-Credentials: true.
The Question: Is this exploitable?
The âHiredâ Answer:
âTechnically, no, because browsers block this configuration entirely. It violates the CORS spec. The browser will throw a console error and refuse to show the response to the JavaScript. The developer effectively broke their own API, but they didnât create a data leak vulnerability (unless they use a very old or non-compliant browser).â
đ Scenario 5: The âPublicâ API
Context: You found Access-Control-Allow-Origin: * on api.weather.com. It returns public weather data.
The Question: Is this a finding?
The âHiredâ Answer:
âNo. CORS protections are for private, user-specific data. Public data should be accessible by everyone. Unless the API also returns sensitive user info (like âMy Saved Locationsâ) mixed with the public data, ACAO: * is the correct configuration for a public API.â
đ Summary of Part 1
-
Concept: CORS relaxes SOP. Misconfigurations break isolation.
-
Vulnerabilities: Reflected Origins,
nullOrigin whitelisting, Weak Regex. -
Advanced Vectors: Exploiting Trust Relationships (XSS), Breaking TLS via HTTP trust, and Intranet data theft.
-
Defense: Strict, server-side validation of the Origin header against a hardcoded whitelist.