https://github.com/cyu/rack-cors
Rack Middleware for handling Cross-Origin Resource Sharing (CORS), which makes cross-origin AJAX possible.
https://github.com/cyu/rack-cors
Keywords from Contributors
activerecord rubygems activejob mvc crash-reporting rack rspec sinatra rubocop feature-flag
Last synced: about 17 hours ago
JSON representation
Repository metadata
Rack Middleware for handling Cross-Origin Resource Sharing (CORS), which makes cross-origin AJAX possible.
- Host: GitHub
- URL: https://github.com/cyu/rack-cors
- Owner: cyu
- License: mit
- Created: 2010-06-02T10:14:29.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2025-05-16T17:59:04.000Z (7 months ago)
- Last Synced: 2025-12-05T14:22:21.721Z (9 days ago)
- Language: JavaScript
- Homepage:
- Size: 537 KB
- Stars: 3,291
- Watchers: 38
- Forks: 258
- Open Issues: 33
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
README.md
Rack CORS Middleware 
Rack::Cors provides support for Cross-Origin Resource Sharing (CORS) for Rack compatible web applications.
The CORS spec allows web applications to make cross domain AJAX calls without using workarounds such as JSONP. See further explanations on MDN
Installation
Install the gem:
gem install rack-cors
Or in your Gemfile:
gem 'rack-cors'
Configuration
Rails Configuration
For Rails, you'll need to add this middleware on application startup. A practical way to do this is with an initializer file. For example, the following will allow GET, POST, PATCH, or PUT requests from any origin on any resource:
# config/initializers/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :put]
end
end
NOTE: If you create application with --api option, configuration is automatically generated in config/initializers/cors.rb.
We use insert_before to make sure Rack::Cors runs at the beginning of the stack to make sure it isn't interfered with by other middleware (see Rack::Cache note in Common Gotchas section). Basic setup examples for Rails 5 & Rails 6 can be found in the examples/ directory.
See The Rails Guide to Rack for more details on rack middlewares or watch the railscast.
Read more about it here in the Rails Guides
Rack Configuration
NOTE: If you're running Rails, adding config/initializers/cors.rb should be enough. There is no need to update config.ru as well.
In config.ru, configure Rack::Cors by passing a block to the use command:
use Rack::Cors do
allow do
origins 'localhost:3000', '127.0.0.1:3000',
/\Ahttp:\/\/192\.168\.0\.\d{1,3}(:\d+)?\z/
# regular expressions can be used here
resource '/file/list_all/', :headers => 'x-domain-token'
resource '/file/at/*',
methods: [:get, :post, :delete, :put, :patch, :options, :head],
headers: 'x-domain-token',
expose: ['Some-Custom-Response-Header'],
max_age: 600
# headers to expose
end
allow do
origins '*'
resource '/public/*', headers: :any, methods: :get
# Only allow a request for a specific host
resource '/api/v1/*',
headers: :any,
methods: :get,
if: proc { |env| env['HTTP_HOST'] == 'api.example.com' }
end
end
Configuration Reference
Middleware Options
- debug (boolean): Enables debug logging and
X-Rack-CORSHTTP headers for debugging. - logger (Object or Proc): Specify the logger to log to. If a proc is provided, it will be called when a logger is needed. This is helpful in cases where the logger is initialized after
Rack::Corsis initially configured, likeRails.logger.
Origin
Origins can be specified as a string, a regular expression, or as '*' to allow all origins.
*SECURITY NOTE: Be careful when using regular expressions to not accidentally be too inclusive. For example, the expression /https:\/\/example\.com/ will match the domain example.com.randomdomainname.co.uk. It is recommended that any regular expression be enclosed with start & end string anchors, like \Ahttps:\/\/example\.com\z.
Additionally, origins can be specified dynamically via a block of the following form:
origins { |source, env| true || false }
A Resource path can be specified as exact string match (/path/to/file.txt) or with a '*' wildcard (/all/files/in/*). A resource can take the following options:
- methods (string or array or
:any): The HTTP methods allowed for the resource. - headers (string or array or
:any): The HTTP headers that will be allowed in the CORS resource request. Use:anyto allow for any headers in the actual request. - expose (string or array): The HTTP headers in the resource response can be exposed to the client.
- credentials (boolean, default:
false): Sets theAccess-Control-Allow-Credentialsresponse header. Note: If a wildcard (*) origin is specified, this option cannot be set totrue. Read this security article for more information. - max_age (number): Sets the
Access-Control-Max-Ageresponse header. - if (Proc): If the result of the proc is true, will process the request as a valid CORS request.
- vary (string or array): A list of HTTP headers to add to the 'Vary' header.
Common Gotchas
Origin Matching
-
When specifying an origin, make sure that it does not have a trailing slash.
-
When specifying an HTTP origin that uses the scheme's default port (e.g.
http://example.test:80), some clients may not strip the port which could result in unexpected blocked requests (additional context here).
Testing Postman and/or cURL
- Make sure you're passing in an
Origin:header. That header is required to trigger a CORS response. Here's a good SO post about using cURL for testing CORS. - Make sure your origin does not have a trailing slash.
Positioning in the Middleware Stack
Positioning of Rack::Cors in the middleware stack is very important. In the Rails example above we put it above all other middleware which, in our experience, provides the most consistent results.
Here are some scenarios where incorrect positioning have created issues:
-
Serving static files. Insert before
ActionDispatch::Staticso that static files are served with the proper CORS headers. NOTE: this might not work in production as static files are usually served from the web server (Nginx, Apache) and not the Rails container. -
Caching in the middleware. Insert before
Rack::Cacheso that the proper CORS headers are written and not cached ones. -
Authentication via Warden Warden will return immediately if a resource that requires authentication is accessed without authentication. If
Warden::Manageris in the stack beforeRack::Cors, it will return without the correct CORS headers being applied, resulting in a failed CORS request.
You can run the following command to see what the middleware stack looks like:
bundle exec rails middleware
Note that the middleware stack is different in production. For example, the ActionDispatch::Static middleware will not be part of the stack if config.serve_static_assets = false. You can run this to see what your middleware stack looks like in production:
RAILS_ENV=production bundle exec rails middleware
Serving static files
If you trying to serve CORS headers on static assets (like CSS, JS, Font files), keep in mind that static files are usually served directly from web servers and never runs through the Rails container (including the middleware stack where Rack::Cors resides).
In Heroku, you can serve static assets through the Rails container by setting config.serve_static_assets = true in production.rb.
Custom Protocols (chrome-extension://, ionic://, etc.)
Prior to 2.0.0, http://, https://, and file:// are the only protocols supported in the origins list. If you wish to specify an origin that
has a custom protocol (chrome-extension://, ionic://, etc.) simply exclude the protocol. See issue.
For example, instead of specifying chrome-extension://aomjjhallfgjeglblehebfpbcfeobpga specify aomjjhallfgjeglblehebfpbcfeobpga in origins.
As of 2.0.0 (currently in RC1), you can specify origins with a custom protocol.
Rails 6 Host Matching
Rails 6 will block requests from unauthorized hosts, and this issue can be confused as a CORS related error. So in development, if you're making requests using something other than localhost or 127.0.0.1, make sure the server host has been authorized. More info here
Owner metadata
- Name: Calvin Yu
- Login: cyu
- Email:
- Kind: user
- Description: Rails, JavaScript, Serverless Partner @ R&Y Labs
- Website: https://rylabs.io
- Location: Atlanta, GA
- Twitter:
- Company: @RYLabs
- Icon url: https://avatars.githubusercontent.com/u/2431?v=4
- Repositories: 92
- Last ynced at: 2024-04-14T15:24:02.724Z
- Profile URL: https://github.com/cyu
GitHub Events
Total
- Issues event: 2
- Watch event: 35
- Issue comment event: 8
- Push event: 9
- Pull request review event: 3
- Pull request event: 13
- Fork event: 3
- Create event: 1
Last Year
- Issues event: 2
- Watch event: 30
- Issue comment event: 8
- Push event: 9
- Pull request event: 11
- Pull request review event: 3
- Fork event: 1
- Create event: 1
Committers metadata
Last synced: 8 days ago
Total Commits: 267
Total Committers: 77
Avg Commits per committer: 3.468
Development Distribution Score (DDS): 0.367
Commits in past year: 10
Committers in past year: 6
Avg Commits per committer in past year: 1.667
Development Distribution Score (DDS) in past year: 0.6
| Name | Commits | |
|---|---|---|
| Calvin Yu | me@s****m | 169 |
| Martyn Cross | m****n@o****o | 9 |
| Pablo Brasero | p****o@n****k | 5 |
| y-yagi | y****a@g****m | 3 |
| dependabot[bot] | 4****] | 3 |
| Riley | b****b@t****t | 2 |
| Richard Doe | r****e@r****t | 2 |
| Adam Bozanich | a****z@g****m | 2 |
| Daniel Dyba | d****a@g****m | 2 |
| Masato Ohba | o****e@g****m | 2 |
| Olle Jonsson | o****n@g****m | 2 |
| André Aubin | a****e@a****z | 1 |
| Andy Rossmeissl | a****y@r****t | 1 |
| Ben Standefer | b****r@g****m | 1 |
| Brad Gessler | b****d@p****m | 1 |
| Brad Lindsay | l****y@p****m | 1 |
| Brandon Turner | bt@b****t | 1 |
| Dmitry Prokhorov | d****b@g****m | 1 |
| Dmytro Stepaniuk | s****k@m****m | 1 |
| Eoin Kelly | e****n@e****o | 1 |
| Esquith Allen | 6****n | 1 |
| Dan Allison | d****n@d****g | 1 |
| Gabo | g****9@g****m | 1 |
| Felix Borzik | b****2@g****m | 1 |
| Everton J. Carpes | e****s@g****m | 1 |
| Andrew Nesbitt | a****z@g****m | 1 |
| Alexander [Cheba] Mankuta | c****a@p****g | 1 |
| Aldric Giacomoni | t****e@g****m | 1 |
| Aithscel | 7****l | 1 |
| Adam Butler | a****m@l****o | 1 |
| and 47 more... | ||
Committer domains:
- polleverywhere.com: 2
- neonroots.com: 1
- gocardless.com: 1
- abril.com.br: 1
- injixo.com: 1
- livingsocial.com: 1
- zendesk.com: 1
- lab.io: 1
- pointlessone.org: 1
- danallison.org: 1
- eoinkelly.info: 1
- mlsdev.com: 1
- brandonturner.net: 1
- rossmeissl.net: 1
- andral.xyz: 1
- rwdit.net: 1
- teleological.net: 1
- new-bamboo.co.uk: 1
- omise.co: 1
- ktdreyer.com: 1
- julik.nl: 1
- brgh.net: 1
- jjb.cc: 1
- joelvanhorn.com: 1
- jaredbeck.com: 1
- cookiejunkie.com: 1
- yandex.ru: 1
- lendesk.com: 1
- ucla.edu: 1
- oboxodo.com: 1
- doma.io: 1
- blogmutt.com: 1
- desire.sh: 1
- tiscali.cz: 1
- truffles.me.uk: 1
- detailedbalance.net: 1
- list.ru: 1
- ndbroadbent.com: 1
- sourcebender.com: 1
Issue and Pull Request metadata
Last synced: 18 days ago
Total issues: 90
Total pull requests: 44
Average time to close issues: about 1 year
Average time to close pull requests: 10 months
Total issue authors: 86
Total pull request authors: 33
Average comments per issue: 4.54
Average comments per pull request: 1.41
Merged pull request: 32
Bot issues: 0
Bot pull requests: 0
Past year issues: 2
Past year pull requests: 10
Past year average time to close issues: about 12 hours
Past year average time to close pull requests: 4 months
Past year issue authors: 2
Past year pull request authors: 4
Past year average comments per issue: 0.5
Past year average comments per pull request: 0.6
Past year merged pull request: 7
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- johnknapp (3)
- mices (2)
- sandstrom (2)
- vlbgomes (1)
- GermanDZ (1)
- MXfive (1)
- xiaopow (1)
- robinjdkim (1)
- bessey (1)
- llaine (1)
- meliborn (1)
- neryb (1)
- grncdr (1)
- scottbarrow (1)
- eregon (1)
Top Pull Request Authors
- y-yagi (4)
- cyu (3)
- nbr (2)
- ohbarye (2)
- m-nakamura145 (2)
- andrew (2)
- fynsta (2)
- fsanggang (2)
- wiktor-k (2)
- sato11 (2)
- mark-young-atg (2)
- akaryu0206 (1)
- julik (1)
- pevzzz (1)
- gabo-cs (1)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 3
-
Total downloads:
- rubygems: 473,547,252 total
- Total docker downloads: 985,913,020
- Total dependent packages: 82 (may contain duplicates)
- Total dependent repositories: 44,919 (may contain duplicates)
- Total versions: 82
- Total maintainers: 1
- Total advisories: 3
gem.coop: rack-cors
Middleware that will make Rack-based apps CORS compatible. Fork the project here: https://github.com/cyu/rack-cors
- Homepage: https://github.com/cyu/rack-cors
- Documentation: http://www.rubydoc.info/gems/rack-cors/
- Licenses: MIT
- Latest release: 3.0.0 (published 7 months ago)
- Last Synced: 2025-12-11T07:04:17.533Z (3 days ago)
- Versions: 27
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 236,688,406 Total
- Docker Downloads: 492,956,510
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.078%
- Downloads: 0.108%
- Docker downloads count: 0.203%
- Maintainers (1)
-
Funding:
- https://github.com/sponsors/cyu
rubygems.org: rack-cors
Middleware that will make Rack-based apps CORS compatible. Fork the project here: https://github.com/cyu/rack-cors
- Homepage: https://github.com/cyu/rack-cors
- Documentation: http://www.rubydoc.info/gems/rack-cors/
- Licenses: MIT
- Latest release: 3.0.0 (published 7 months ago)
- Last Synced: 2025-12-12T11:58:43.573Z (2 days ago)
- Versions: 27
- Dependent Packages: 82
- Dependent Repositories: 44,919
- Downloads: 236,858,846 Total
- Docker Downloads: 492,956,510
-
Rankings:
- Downloads: 0.109%
- Dependent repos count: 0.159%
- Docker downloads count: 0.25%
- Dependent packages count: 0.374%
- Average: 0.523%
- Stargazers count: 0.634%
- Forks count: 1.609%
- Maintainers (1)
-
Funding:
- https://github.com/sponsors/cyu
- Advisories:
proxy.golang.org: github.com/cyu/rack-cors
- Homepage:
- Documentation: https://pkg.go.dev/github.com/cyu/rack-cors#section-documentation
- Licenses: mit
- Latest release: v3.0.0+incompatible (published 7 months ago)
- Last Synced: 2025-12-12T11:58:44.801Z (2 days ago)
- Versions: 28
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 6.45%
- Average: 6.668%
- Dependent repos count: 6.885%
Dependencies
- webpack-dev-server ^3.11.0 development
- @rails/actioncable ^6.0.0
- @rails/activestorage ^6.0.0
- @rails/ujs ^6.0.0
- @rails/webpacker 5.2.1
- turbolinks ^5.2.0
- 1016 dependencies
- pry-byebug ~> 3.6.0
- rack >= 0
- rack-cors >= 1.1.0
- rack 2.2.3
- rack-cors 1.1.1
- byebug >= 0 development
- capybara >= 2.15 development
- chromedriver-helper >= 0 development
- listen >= 3.0.5, < 3.2 development
- selenium-webdriver >= 0 development
- spring >= 0 development
- spring-watcher-listen ~> 2.0.0 development
- web-console >= 3.3.0 development
- bootsnap >= 1.1.0
- coffee-rails ~> 4.2
- jbuilder ~> 2.5
- puma ~> 3.12
- rack-cors >= 1.1.0
- rails ~> 5.2.3
- sass-rails ~> 5.0
- sqlite3 >= 0
- turbolinks ~> 5
- tzinfo-data >= 0
- uglifier >= 1.3.0
- actioncable 5.2.4.4
- actionmailer 5.2.4.4
- actionpack 5.2.4.4
- actionview 5.2.4.4
- activejob 5.2.4.4
- activemodel 5.2.4.4
- activerecord 5.2.4.4
- activestorage 5.2.4.4
- activesupport 5.2.4.4
- addressable 2.7.0
- archive-zip 0.12.0
- arel 9.0.0
- bindex 0.8.1
- bootsnap 1.5.1
- builder 3.2.4
- byebug 11.1.3
- capybara 3.34.0
- childprocess 3.0.0
- chromedriver-helper 2.1.1
- coffee-rails 4.2.2
- coffee-script 2.4.1
- coffee-script-source 1.12.2
- concurrent-ruby 1.1.7
- crass 1.0.6
- erubi 1.10.0
- execjs 2.7.0
- ffi 1.14.0
- globalid 0.4.2
- i18n 1.8.5
- io-like 0.3.1
- jbuilder 2.10.1
- listen 3.1.5
- loofah 2.8.0
- mail 2.7.1
- marcel 0.3.3
- method_source 1.0.0
- mimemagic 0.3.5
- mini_mime 1.0.2
- mini_portile2 2.4.0
- minitest 5.14.2
- msgpack 1.3.3
- nio4r 2.5.4
- nokogiri 1.10.10
- public_suffix 4.0.6
- puma 3.12.6
- rack 2.2.3
- rack-cors 1.1.1
- rack-test 1.1.0
- rails 5.2.4.4
- rails-dom-testing 2.0.3
- rails-html-sanitizer 1.3.0
- railties 5.2.4.4
- rake 13.0.1
- rb-fsevent 0.10.4
- rb-inotify 0.10.1
- regexp_parser 1.8.2
- ruby_dep 1.5.0
- rubyzip 2.3.0
- sass 3.7.4
- sass-listen 4.0.0
- sass-rails 5.1.0
- selenium-webdriver 3.142.7
- spring 2.1.1
- spring-watcher-listen 2.0.1
- sprockets 3.7.2
- sprockets-rails 3.2.2
- sqlite3 1.4.2
- thor 1.0.1
- thread_safe 0.3.6
- tilt 2.0.10
- turbolinks 5.2.1
- turbolinks-source 5.2.0
- tzinfo 1.2.9
- uglifier 4.2.0
- web-console 3.7.0
- websocket-driver 0.7.3
- websocket-extensions 0.1.5
- xpath 3.2.0
- byebug >= 0 development
- capybara >= 3.26 development
- listen ~> 3.3 development
- rack-mini-profiler ~> 2.0 development
- selenium-webdriver >= 0 development
- spring >= 0 development
- web-console >= 4.1.0 development
- webdrivers >= 0 development
- bootsnap >= 1.4.4
- jbuilder ~> 2.7
- puma ~> 5.0
- rack-cors >= 1.1.0
- rails ~> 6.1.0
- sass-rails >= 6
- sqlite3 ~> 1.4
- turbolinks ~> 5
- tzinfo-data >= 0
- webpacker ~> 5.0
- actioncable 6.1.0
- actionmailbox 6.1.0
- actionmailer 6.1.0
- actionpack 6.1.0
- actiontext 6.1.0
- actionview 6.1.0
- activejob 6.1.0
- activemodel 6.1.0
- activerecord 6.1.0
- activestorage 6.1.0
- activesupport 6.1.0
- addressable 2.7.0
- bindex 0.8.1
- bootsnap 1.5.1
- builder 3.2.4
- byebug 11.1.3
- capybara 3.34.0
- childprocess 3.0.0
- concurrent-ruby 1.1.7
- crass 1.0.6
- erubi 1.10.0
- ffi 1.14.0
- globalid 0.4.2
- i18n 1.8.5
- jbuilder 2.10.1
- listen 3.3.3
- loofah 2.8.0
- mail 2.7.1
- marcel 0.3.3
- method_source 1.0.0
- mimemagic 0.3.5
- mini_mime 1.0.2
- mini_portile2 2.4.0
- minitest 5.14.2
- msgpack 1.3.3
- nio4r 2.5.4
- nokogiri 1.10.10
- public_suffix 4.0.6
- puma 5.1.1
- rack 2.2.3
- rack-cors 1.1.1
- rack-mini-profiler 2.2.0
- rack-proxy 0.6.5
- rack-test 1.1.0
- rails 6.1.0
- rails-dom-testing 2.0.3
- rails-html-sanitizer 1.3.0
- railties 6.1.0
- rake 13.0.2
- rb-fsevent 0.10.4
- rb-inotify 0.10.1
- regexp_parser 1.8.2
- rubyzip 2.3.0
- sass-rails 6.0.0
- sassc 2.4.0
- sassc-rails 2.1.2
- selenium-webdriver 3.142.7
- semantic_range 2.3.1
- spring 2.1.1
- sprockets 4.0.2
- sprockets-rails 3.2.2
- sqlite3 1.4.2
- thor 1.0.1
- tilt 2.0.10
- turbolinks 5.2.1
- turbolinks-source 5.2.0
- tzinfo 2.0.4
- web-console 4.1.0
- webdrivers 4.4.1
- webpacker 5.2.1
- websocket-driver 0.7.3
- websocket-extensions 0.1.5
- xpath 3.2.0
- zeitwerk 2.4.2
- bundler >= 1.16.0, < 3 development
- minitest ~> 5.11.0 development
- mocha ~> 1.6.0 development
- pry ~> 0.12 development
- rack-test ~> 1.1.0 development
- rake ~> 12.3.0 development
- rubocop ~> 0.80.1 development
- rack >= 2.0.0
- actions/checkout v3 composite
- ruby/setup-ruby v1 composite
Score: 33.55409348366704