rack-cors 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rack-cors might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 509192f6673d85eb0b23b4169f5d392d1e380fdc
4
- data.tar.gz: 5b123af66d8580566d8012012f62f90e8f4043d8
3
+ metadata.gz: 7a4d5e6683440676f486ce61b3c16ff2e7c8f65e
4
+ data.tar.gz: be95c0c3dce56c965aff4b1cb398f2ad6fb4f6b3
5
5
  SHA512:
6
- metadata.gz: 274193cd4cc2bca5187e0fd7aacb928558cc5a0bddcd236c56b7791ccc8e7ceaffd53f96bae15992960d614de3bfd1ed12c0fa459ad25b7c5313ec8d166bbe1b
7
- data.tar.gz: ba69d913329a58954760c7acd2d0d574ca5620e90b98617c65cfc67136433b9b834c6364a018d01dd231632a44a2c5ee4a398bd5fe1d4c27af44ed50e8c0cfbd
6
+ metadata.gz: 8ae27dfd82bd822e700c476963118b15e68dee7850aaccb8b43a05670903f9f66217c8cd77dcf425f664581ecadf00eb43c1c1926648d97caf07c6d4a4dd0574
7
+ data.tar.gz: df278b2f1b3e6f0b01305d601fb58f2d41aef0579232d5ae27564be76483afdfb58b9219708d5a944c7db293025d7bacec4924dc63c06aefed2a7df4ad00e9ec
@@ -23,6 +23,10 @@ module Rack
23
23
 
24
24
  DEFAULT_VARY_HEADERS = ['Origin'].freeze
25
25
 
26
+ # All CORS routes need to accept CORS simple headers at all times
27
+ # {https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers}
28
+ CORS_SIMPLE_HEADERS = ['accept', 'accept-language', 'content-language', 'content-type'].freeze
29
+
26
30
  def initialize(app, opts={}, &block)
27
31
  @app = app
28
32
  @debug_mode = !!opts[:debug]
@@ -134,7 +138,7 @@ module Rack
134
138
  @logger_proc = nil
135
139
  logger_proc.call
136
140
 
137
- elsif defined?(Rails) && Rails.logger
141
+ elsif defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
138
142
  Rails.logger
139
143
 
140
144
  elsif env[RACK_LOGGER]
@@ -155,7 +159,7 @@ module Rack
155
159
  resource, error = match_resource(env)
156
160
  unless resource
157
161
  result.miss(error)
158
- return {}
162
+ return {}
159
163
  end
160
164
 
161
165
  return resource.process_preflight(env, result)
@@ -407,10 +411,14 @@ module Rack
407
411
  end
408
412
 
409
413
  def allow_headers?(request_headers)
410
- return false if headers.nil?
411
- headers == :any || begin
412
- request_headers = request_headers.split(/,\s*/) if request_headers.kind_of?(String)
413
- request_headers.all?{|h| headers.include?(h.downcase)}
414
+ headers = self.headers || []
415
+ if headers == :any
416
+ return true
417
+ end
418
+ request_headers = request_headers.split(/,\s*/) if request_headers.kind_of?(String)
419
+ request_headers.all? do |header|
420
+ header = header.downcase
421
+ CORS_SIMPLE_HEADERS.include?(header) || headers.include?(header)
414
422
  end
415
423
  end
416
424
 
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Cors
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
@@ -16,7 +16,7 @@ end
16
16
  Rack::Test::Methods.class_eval do
17
17
  def_delegator :current_session, :options
18
18
  end
19
-
19
+
20
20
  module MiniTest::Assertions
21
21
  def assert_cors_success(response)
22
22
  assert !response.headers['Access-Control-Allow-Origin'].nil?, "Expected a successful CORS response"
@@ -405,6 +405,54 @@ describe Rack::Cors do
405
405
  end
406
406
  end
407
407
 
408
+ describe 'with headers set to nil' do
409
+ let(:app) do
410
+ Rack::Builder.new do
411
+ use Rack::Cors do
412
+ allow do
413
+ origins '*'
414
+ resource '/', headers: nil
415
+ end
416
+ end
417
+ map('/') do
418
+ run ->(env) { [200, {'Content-Type' => 'text/html'}, ['hello']] }
419
+ end
420
+ end
421
+ end
422
+
423
+ it 'should succeed with CORS simple headers' do
424
+ preflight_request('http://localhost:3000', '/', :headers => 'Accept')
425
+ last_response.must_render_cors_success
426
+ end
427
+ end
428
+
429
+ describe 'with custom allowed headers' do
430
+ let(:app) do
431
+ Rack::Builder.new do
432
+ use Rack::Cors do
433
+ allow do
434
+ origins '*'
435
+ resource '/', headers: []
436
+ end
437
+ end
438
+ map('/') do
439
+ run ->(env) { [200, {'Content-Type' => 'text/html'}, ['hello']] }
440
+ end
441
+ end
442
+ end
443
+
444
+ it 'should succeed with CORS simple headers' do
445
+ preflight_request('http://localhost:3000', '/', :headers => 'Accept')
446
+ last_response.must_render_cors_success
447
+ preflight_request('http://localhost:3000', '/', :headers => 'Accept-Language')
448
+ last_response.must_render_cors_success
449
+ preflight_request('http://localhost:3000', '/', :headers => 'Content-Type')
450
+ last_response.must_render_cors_success
451
+ preflight_request('http://localhost:3000', '/', :headers => 'Content-Language')
452
+ last_response.must_render_cors_success
453
+ end
454
+ end
455
+
408
456
  protected
409
457
  def cors_request(*args)
410
458
  path = args.first.is_a?(String) ? args.first : '/'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-cors
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Calvin Yu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-18 00:00:00.000000000 Z
11
+ date: 2017-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler