CORS (Cross-Origin Resource Sharing)
CORS (Cross-Origin Resource Sharing) is a security mechanism that allows servers to specify which origins are permitted to access their resources. It is implemented through HTTP headers and ensures that only authorized clients from allowed origins can interact with the server.
1. Why Is CORS Needed?
By default, the Same-Origin Policy (SOP) prevents web browsers from making requests across origins for security reasons. However, modern web applications often require legitimate cross-origin requests, such as calling APIs hosted on different domains. CORS provides a secure way to enable such interactions while maintaining control over which origins are allowed.
2. How CORS Works
When a web browser initiates a cross-origin request, it follows this process:
a. Simple Requests
- Directly sent to the server if they meet these conditions:
- Allowed methods: GET, POST, HEAD.
- Allowed headers: Standard headers like Accept, Content-Type (with values such as text/plain).
- Server responds with appropriate CORS headers to allow or deny the request.
b. Preflight Requests
- If the request is more complex (e.g., uses custom headers or methods like PUT or DELETE), the browser sends a preflight request to verify if the server allows the action.
- The preflight request uses the HTTP OPTIONS method and includes:
- Origin: The requesting domain.
- Access-Control-Request-Method: The HTTP method intended for the actual request.
- Access-Control-Request-Headers: Custom headers being sent.
c. Server Response
- If the server approves the preflight request, it responds with appropriate CORS headers:
- Access-Control-Allow-Origin: Specifies the allowed origin(s).
- Access-Control-Allow-Methods: Lists allowed HTTP methods.
- Access-Control-Allow-Headers: Lists allowed headers.
- Access-Control-Allow-Credentials: Indicates if the request can include credentials (e.g., cookies, HTTP authentication).
d. Actual Request
- If the preflight check succeeds, the browser sends the actual request.
- The server responds with data and, optionally, CORS headers to confirm the action.
3. Example Flow
Preflight Request
- Client Request:
OPTIONS /api/data HTTP/1.1 Origin: https://example-client.com Access-Control-Request-Method: POST Access-Control-Request-Headers: Authorization
- Server Response:
HTTP/1.1 200 OK Access-Control-Allow-Origin: https://example-client.com Access-Control-Allow-Methods: POST, GET Access-Control-Allow-Headers: Authorization Access-Control-Allow-Credentials: true
Actual Request
Client Request:
POST /api/data HTTP/1.1 Origin: https://example-client.com Authorization: Bearer token123
Server Response:
HTTP/1.1 200 OK Access-Control-Allow-Origin: https://example-client.com Content-Type: application/json
4. Key CORS HTTP Headers
Header | Purpose |
---|---|
Access-Control-Allow-Origin | Specifies which origins can access the resource (e.g., * for all origins or specific domains). |
Access-Control-Allow-Methods | Lists allowed HTTP methods (e.g., GET, POST, PUT). |
Access-Control-Allow-Headers | Lists allowed custom headers (e.g., Authorization, Content-Type). |
Access-Control-Allow-Credentials | Indicates if credentials like cookies are allowed (true or not included). |
Access-Control-Expose-Headers | Specifies which headers are accessible to the client (e.g., X-Custom-Header). |
Access-Control-Max-Age | Specifies how long the results of a preflight request can be cached (in seconds). |
5. Use Cases
- Single-Page Applications (SPAs)
- Allow a client (e.g., https://example.com) to interact with APIs on a different origin (e.g., https://api.example.com).
- Third-Party APIs
- Provide public APIs while restricting access to specific trusted domains.
- Cross-Domain Authentication
- Allow clients to send cookies or tokens to authenticate with a server on another origin.
6. Common CORS Issues and Fixes
a. Common Issues
- Blocked by Browser
- The server doesn’t send appropriate CORS headers.
- Preflight Fails
- The server doesn’t allow the method or headers specified in the preflight request.
- Credentials Not Sent
- The Access-Control-Allow-Credentials header is missing or improperly set.
b. Fixes
Server-Side Configuration
Add proper CORS headers.
Example for an Express.js server:
const cors = require('cors'); app.use(cors({ origin: 'https://example.com', credentials: true }));
Avoid Wildcards with Credentials
Use specific origins when Access-Control-Allow-Credentials is true.
Example:
Access-Control-Allow-Origin: https://example.com
Preflight Optimization
- Use Access-Control-Max-Age to reduce preflight request frequency.
7. Security Considerations
- Misconfigured CORS Headers
- Allowing all origins (Access-Control-Allow-Origin: *) can expose sensitive resources to attackers.
- Credential Leakage
- Be cautious when enabling Access-Control-Allow-Credentials, as it allows cookies or tokens to be sent.
- Content Security Policy (CSP)
- Use CSP to mitigate risks by restricting the sources of scripts and resources.
8. Summary
Aspect | Details |
---|---|
What is CORS? | Mechanism to enable controlled cross-origin resource sharing. |
Preflight Request | An OPTIONS request to check permissions for complex requests. |
Key Headers | Access-Control-Allow-Origin, Access-Control-Allow-Methods, etc. |
Use Cases | SPAs, third-party APIs, cross-domain authentication. |
Common Issues | Missing headers, incorrect configurations, preflight failures. |
Best Practices | Use specific origins, restrict allowed methods/headers, and secure credentials. |
CORS enables secure and controlled communication between different origins, facilitating modern web application workflows. Proper server configuration and understanding of CORS headers are critical for ensuring functionality and security. Misconfigurations can lead to significant vulnerabilities, so always adhere to best practices when enabling CORS in your applications.
'Security Terms > Web Application' 카테고리의 다른 글
Cookies (0) | 2025.01.22 |
---|---|
HTTP Public Key Pinning (HPKP) (0) | 2025.01.22 |
Certificate Transparency (CT) (0) | 2025.01.22 |
HSTS (HTTP Strict Transport Security) (0) | 2025.01.21 |
Same Origin Policy (SOP) (0) | 2025.01.21 |