A Rack middleware that provides Cross-Origin Resource Sharing (CORS) support for Rack-compatible web applications.
Rack::CORS is a Rack middleware that implements Cross-Origin Resource Sharing (CORS) for Rack-compatible web applications. It solves the problem of enabling secure cross-origin AJAX requests by adding the necessary HTTP headers to responses, eliminating the need for JSONP or other workarounds.
Ruby developers building Rack-based web applications or APIs, especially those using Rails, Sinatra, or other Rack-compatible frameworks that need to handle cross-origin requests.
Developers choose Rack::CORS because it's a lightweight, standards-compliant middleware that integrates seamlessly into the Rack stack, offers fine-grained configuration for security, and is widely adopted in the Ruby ecosystem with robust Rails support.
Rack Middleware for handling Cross-Origin Resource Sharing (CORS), which makes cross-origin AJAX possible.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Supports exact strings, regular expressions, wildcards, and dynamic blocks for defining allowed origins, as shown in the configuration examples with regex patterns like /\Ahttp:\/\/192\.168\.0\.\d{1,3}(:\d+)?\z/.
Allows per-resource settings for HTTP methods, headers, credentials, and max-age, enabling fine-tuned security policies, such as exposing custom headers or limiting methods to [:get, :post].
Automatically generates configuration for Rails API apps and integrates via initializers, making setup straightforward, as noted in the README for apps created with the --api option.
Includes debug logging and X-Rack-CORS headers for troubleshooting, which helps identify CORS issues quickly, as highlighted in the middleware options section.
In production, static files served by external web servers bypass the middleware, requiring additional server-level configuration for CORS headers, as admitted in the Common Gotchas section.
Must be placed early in the stack to work correctly with other middleware like Warden or Rack::Cache, which can be error-prone and require careful management, as detailed in positioning scenarios.
Versions before 2.0.0 have limited support for non-standard protocols, forcing workarounds like omitting the protocol in origin definitions, which adds complexity for older setups.