Add comprehensive CORS support with Cross-Origin-Resource-Policy for all API endpoints#98
Add comprehensive CORS support with Cross-Origin-Resource-Policy for all API endpoints#98
Conversation
Co-authored-by: RogerPodacter <59190+RogerPodacter@users.noreply.github.com>
|
@copilot hey Copilot, i think it should also have |
|
@copilot , do we need cross-origin-resource-policy: cross-origin too? Also just make every API endpoint allow all cross origin |
…ll API endpoints Co-authored-by: RogerPodacter <59190+RogerPodacter@users.noreply.github.com>
I've added both the Cross-Origin-Resource-Policy header and global CORS support for all API endpoints. Changes made in commit 49015c0:
Now all API endpoints will automatically include CORS headers when cross-origin requests are made, and the Cross-Origin-Resource-Policy header ensures resources can be accessed cross-origin. |
Problem
The API was not properly handling CORS (Cross-Origin Resource Sharing) requests across all endpoints, preventing web applications from making cross-origin requests. While a global CORS configuration existed in
config/initializers/cors.rb, it wasn't being applied correctly to endpoints that usesend_data()to return binary content, and the Cross-Origin-Resource-Policy header was missing.Root Cause
The issue occurred because:
send_data()to return binary contentSolution
This PR adds comprehensive CORS handling for all API endpoints with minimal changes:
Global CORS Support
ApplicationControllerviabefore_actionto ensure all endpoints support CORS when anOriginheader is presentcross-originfor all responses to allow cross-origin resource accessEndpoint-Specific Changes
Cross-Origin-Resource-Policy: cross-originTesting
Example Usage
After this change, web applications can make cross-origin requests to any API endpoint:
Backward Compatibility
These changes are fully backward compatible - existing functionality is preserved while adding comprehensive CORS support for all cross-origin requests.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.