question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rack::Cors misses the OPTIONS request

See original GitHub issue

I have rack-cors 0.4.0 installed with this config on applicaiton.rb

config.middleware.insert_before 0, 'Rack::Cors', debug: true, logger: (-> { Rails.logger }) do
  allow do
    origins '*'

    resource '/api/*',
      headers: :any,
      methods: [:get, :post, :delete, :put, :options, :head],
      max_age: 0
  end

and I keep getting OPTIONS requests handled by the Rails routes directly, instead of Rack::Cors:

2015-09-17T03:06:14.925486+00:00 app[web.1]: Started OPTIONS "/api/v2/posts/1136" for 103.252.202.20 at 2015-09-17 03:06:14 +0000
..... SKIPPED ....
2015-09-17T03:06:14.930356+00:00 app[web.1]:
2015-09-17T03:06:14.930360+00:00 app[web.1]: ActionController::RoutingError (No route matches [OPTIONS] "/api/v2/posts/1136"):
2015-09-17T03:13:33.005456+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2015-09-17T03:13:33.005457+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rollbar-2.1.2/lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
2015-09-17T03:13:33.005459+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-09-17T03:13:33.005460+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:38:in `call_app'
2015-09-17T03:13:33.005461+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:20:in `block in call'
2015-09-17T03:13:33.005463+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-09-17T03:13:33.005464+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-09-17T03:13:33.005465+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-09-17T03:13:33.005466+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:20:in `call'
2015-09-17T03:13:33.005467+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/request_store-1.2.0/lib/request_store/middleware.rb:8:in `call'
2015-09-17T03:13:33.005468+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-09-17T03:13:33.005470+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-09-17T03:13:33.005471+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-09-17T03:13:33.005472+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/static.rb:116:in `call'
2015-09-17T03:13:33.005473+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-cors-0.4.0/lib/rack/cors.rb:80:in `call'
2015-09-17T03:13:33.005475+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/engine.rb:518:in `call'
2015-09-17T03:13:33.005476+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application.rb:165:in `call'
2015-09-17T03:13:33.005477+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:580:in `process_client'
2015-09-17T03:13:33.005478+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:674:in `worker_loop'
2015-09-17T03:13:33.005479+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:529:in `spawn_missing_workers'
2015-09-17T03:13:33.005480+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:140:in `start'
2015-09-17T03:13:33.005481+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/bin/unicorn:126:in `<top (required)>'
2015-09-17T03:13:33.005483+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/bin/unicorn:23:in `load'
2015-09-17T03:13:33.005484+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/bin/unicorn:23:in `<main>'

It’s as if Rack::Cors misses the request. This only happens on Production. Here’s the middleware on Production environment:

use Rack::Cors
use ActionDispatch::Static
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007f55b9ce4288>
use Rack::Runtime
use ActionDispatch::RequestId
use RequestStore::Middleware
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use Rollbar::Middleware::Rails::RollbarMiddleware
use ActionDispatch::RemoteIp
use ActionDispatch::Callbacks
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Warden::Manager
use Apipie::Extractor::Recorder::Middleware
use Apipie::StaticDispatcher
run ClientsApi::Application.routes

Did I miss something? Any idea how to debug this? Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
e0dacommented, Feb 9, 2018

@fguillen I wish you’d included the critical change to your routes, but we in the future appreciate that you solved your problem. https://xkcd.com/979/

1reaction
cyucommented, Jul 15, 2017

Fixed with #106. Closing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rails Responds with 404 on CORS Preflight Options Request
Since the GET requests are working, I'm assuming that what I'm missing is the correct route for the OPTIONS request. However, I've tried...
Read more >
Rails CORS Guide: What It Is and How to Enable It - StackHawk
In the most simple scenario, CORS will block all requests from a different origin than ... rack-cors also supports a few additional options....
Read more >
Setting headers when using the error! method? - Google Groups
I am going to guess that the order matters, and that you simply don't hit the middleware *after* the route has been matched...
Read more >
Setting Up CORS On A Rails App - DEV Community ‍ ‍
Solution · 1. Add rack-cors Gem · 2. Bundle install · 3. Add the configuration in the Application.rb · 4. Add the configuration...
Read more >
Ember+Rails devs: what do you do for CORS/OPTIONS ...
cross origin policy rack middleware gem 'rack-cors', require: 'rack/cors'. and here is what my cors initializer looks like
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found