What is Cross-Origin Resource Sharing (CORS)?
Cross-Origin Resource Sharing (CORS) is an HTTP-header-based mechanism that allows a server to indicate which origins (domains, schemes, or ports) are permitted to access its resources. CORS overcomes the browser’s same-origin policy, which restricts web applications from accessing resources hosted on a different origin for security reasons. For example, a frontend app hosted onhttps://domain-a.com
may need to fetch resources from an API hosted at https://domain-b.com
. With CORS enabled, the browser permits this interaction if the API server provides the appropriate CORS headers.
Functional Overview of CORS
CORS works by adding new HTTP headers that let servers describe the origins and methods permitted for resource access. For requests with potential side effects on server data (e.g., POST or DELETE), the browser performs a preflight request using theOPTIONS
method to ensure the server approves the actual request.
How CORS Works with Coinbase API v2
The Coinbase App API v2 uses CORS to allow secure, client-side access to its endpoints directly from browsers, enabling various use cases like fetching live cryptocurrency prices. Here’s how it simplifies integration:- Ease of Use: JavaScript can directly fetch API data without needing server-side intermediaries.
- Support for Modern Applications: Suitable for building real-time dashboards or widgets.
Key HTTP Headers for CORS
-
Access-Control-Allow-Origin: Specifies which origin(s) are permitted access. Example:
A value of
*
allows access from all origins but is unsuitable for credentialed requests. -
Access-Control-Allow-Methods: Lists the HTTP methods permitted for cross-origin requests:
-
Access-Control-Allow-Headers: Specifies the custom headers allowed in the request:
-
Access-Control-Allow-Credentials: Indicates whether credentials (cookies or authentication tokens) can be included:
-
Access-Control-Max-Age: Indicates how long the preflight response can be cached:
Security Considerations
-
Never Expose Private API Keys: Storing private keys in client-side code risks unauthorized access.
- Use server-side authentication for sensitive operations.
- Leverage OAuth2 for user authorization.
-
Understand the Risks of Wildcards (
*
):- Using
*
forAccess-Control-Allow-Origin
can lead to unintended exposure. - For credentialed requests, explicitly specify the origin.
- Using
-
Use Preflight Requests for Sensitive Actions:
- Preflight ensures the server explicitly approves actions like modifying data (e.g., POST or DELETE).
Common CORS Scenarios
-
Simple Requests:
- Simple requests (GET, POST with safe headers) do not require preflight.
- Example:
-
Preflighted Requests:
- Required when using custom headers, non-simple methods, or credentials.
- Example:
-
Requests with Credentials:
- To send cookies or authentication tokens:
- To send cookies or authentication tokens:
Common Errors and Troubleshooting
-
Error:
"No 'Access-Control-Allow-Origin' header is present"
- Solution: Verify the API server includes the correct CORS headers.
-
Error:
"CORS preflight did not succeed"
- Solution: Ensure the server handles
OPTIONS
requests correctly.
- Solution: Ensure the server handles
-
Error:
"The request was redirected but preflight does not allow redirects"
- Solution: Avoid redirects or rewrite the client request to point to the final URL.
Real-World Use Cases with Coinbase API
- Cryptocurrency Dashboard: Build an app that fetches live Bitcoin prices for a portfolio tracker.
- Interactive Charts: Use API data for real-time price visualizations.
- Secure Transactions: Implement OAuth2 workflows for client-side apps.