cyu / rack-cors

Rack Middleware for handling Cross-Origin Resource Sharing (CORS), which makes cross-origin AJAX possible.
MIT License
3.27k stars 262 forks source link

"headers" is nil - crash on request #169

Open wesleysmith12 opened 6 years ago

wesleysmith12 commented 6 years ago

Gemfil:

source 'https://rubygems.org'
gem 'grape', "~> 1.0"
gem 'grape-entity', "~> 0.6"
gem 'grape-swagger', "~> 0.27"
gem 'grape-swagger-entity', "~> 0.2"
gem 'json', "~> 2.1"
gem 'otr-activerecord', "~> 1.2"
gem 'rack-cors',  require: 'rack/cors'
gem 'jwt', "~> 2.1"
gem 'rake', "~> 12.3"
gem 'bcrypt'
gem 'active_record-acts_as'
gem 'kaminari'
gem 'kaminari-grape'
gem 'grape-kaminari'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
gem 'mailgun-ruby', '~>1.1.6'
gem 'whenever', require: false
gem 'mysql2', '~> 0.4.10'

group :development do
        gem 'sqlite3', "~> 1.3"
        gem 'dotenv'
        gem 'byebug'
end

Output (not error happens only after request)

[2018-07-16 19:01:04] INFO  WEBrick 1.3.1
[2018-07-16 19:01:04] INFO  ruby 2.4.1 (2017-03-22) [x86_64-linux]
[2018-07-16 19:01:04] INFO  WEBrick::HTTPServer#start: pid=21927 port=9292

NoMethodError: undefined method `[]' for nil:NilClass
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-cors-1.0.2/lib/rack/cors.rb:114:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-cors-1.0.2/lib/rack/cors.rb:97:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/otr-activerecord-1.2.5/lib/otr-activerecord/middleware/connection_management.rb:14:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/tempfile_reaper.rb:15:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/lint.rb:49:in `_call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/lint.rb:37:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/show_exceptions.rb:23:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/common_logger.rb:33:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/chunked.rb:54:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/content_length.rb:15:in `call'
        /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-2.0.3/lib/rack/handler/webrick.rb:86:in `service'
        /usr/share/rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/webrick/httpserver.rb:140:in `service'
        /usr/share/rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/webrick/httpserver.rb:96:in `run'
        /usr/share/rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/webrick/server.rb:290:in `block in start_thread'

Because I was curious, I looked at the file /home/ubuntu/.rvm/gems/ruby-2.4.1@MYAPP/gems/rack-cors-1.0.2/lib/rack/cors.rb:114:in `call':

      if vary_resource
        vary = headers[VARY]
        cors_vary_headers = if vary_resource.vary_headers && vary_resource.vary_headers.any?
          vary_resource.vary_headers
        else
          DEFAULT_VARY_HEADERS
        end
        headers[VARY] = ((vary ? vary.split(/,\s*/) : []) + cors_vary_headers).uniq.join(', ')
      end

Line 114 is: vary = headers[VARY]

Not sure why that wouldn't be set, but I'm getting an error cause of it.

simonhildebrandt commented 5 years ago

I'm having a similar error, for this minimal Rack app:

  app = Rack::Builder.new do
    use Rack::Cors do
      allow do
        origins '*'
        resource '*', headers: :any, methods: [:put, :get, :post, :delete, :options]
      end
    end

    run lambda { |env|
      ['200', {'Content-Type' => 'text/html'}, ['response accepted']]
    }
  end

  Rack::Handler::WEBrick.run app, {Port: 3000}
simonhildebrandt commented 5 years ago

I strongly suspect that my problem was caused by bailing out without returning anything, further down in the stack (there's an if inside that lambda that I edited out for clarity.) I made sure to return something for all cases, and I haven't seen the problem since.

@wesleysmith12 - I reckon the gem could handle this case more gracefully, but maybe this will help setup a work around for you?

sandstrom commented 2 years ago

I'm doing some issue gardening 🌱🌿 🌷 and came upon this issue. Since it's quite old I just wanted to ask if this is still relevant? If it isn't, maybe we can close this issue?

By closing some old issues we reduce the list of open issues to a more manageable set.