rack-simple_csrf 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc1560d053a3c6e140795a8ed52edbab6718888c
4
- data.tar.gz: c35998ea6d6e873baf8f98bc2fa9dfe9f53c0372
3
+ metadata.gz: 3bd1ac436f818ec61c32035bda83691a1e3c1be6
4
+ data.tar.gz: 590eb39f69fba11d6e669dc0855aab8254f70924
5
5
  SHA512:
6
- metadata.gz: 61ebfca10811016cf01de6267b27ba424efd4b56ef331ddae5a7a60a587d6d4c2fddb2aac581b25a6c4e4da1bdd5c9b7c020c69194e9e855e04deb80233e4e83
7
- data.tar.gz: 93d11241740bc891deb13389154fc1f64e24a36c7a94493b655cccb2dc4a9d1abdb16c0f56557155fa89fa296ce734375b4ddc88a68111cdec55b05b2af3051e
6
+ metadata.gz: 5ef169dc22529a218fd7da48788db481a4d62c62e27fad34862cb6bfe7476e07e4ff97d5f5b8d18879174ec88b013a1a049c8d0eba55c99eae1223af20f30a38
7
+ data.tar.gz: d12cd87a46cfa4c9cbc547af1d472d53af636b968a70acbdbbcd3bbeb904b5b0a11fd601ba098d14c8de3b4b7ebbb39ce914c919a24c7174574c61092894175b
data/Readme.md CHANGED
@@ -18,7 +18,8 @@ class MyApp < Sinatra::Base
18
18
 
19
19
  CSRF_SKIP_LIST = [
20
20
  "/my-path",
21
- "POST:/my-other-path"
21
+ "POST:/my-other-path",
22
+ "/regexp-path/.*"
22
23
  ]
23
24
 
24
25
  class << self
@@ -31,29 +31,47 @@ module Rack
31
31
 
32
32
  @render_with = opts[:render_with]
33
33
  @header = opts.fetch(:header, "HTTP_X_CSRF_TOKEN")
34
- @methods = (%w(POST PUT DELETE PATCH) + opts.fetch(:http_methods, [])).flatten.uniq
34
+ @methods = (%w(POST PUT DELETE PATCH) + \
35
+ opts.fetch(:http_methods, [])).flatten.uniq
35
36
  end
36
37
 
37
38
  def call(env, req = Rack::Request.new(env))
38
39
  raise_if_session_unavailable_for! req
39
40
  setup_csrf_for! req
41
+
40
42
  return @app.call(env) if continue?(req)
41
43
  @raise ? raise(CSRFFailedToValidateError) : render_error_for!(env)
42
44
  end
43
45
 
44
46
  private
45
47
  def continue?(req)
46
- req.params[@field] == req.env["rack.session"][@key] ||
47
- req.env[@header] == req.env["rack.session"][@key] ||
48
- ! @methods.include?(req.request_method) || any_skips?(req)
48
+ req.params[@field] == req.env["rack.session"][@key] || \
49
+ req.env[@header] == req.env["rack.session"][@key] || \
50
+ ! @methods.include?(req.request_method) || \
51
+ any_skips?(req)
49
52
  end
50
53
 
51
54
  private
52
55
  def any_skips?(req)
53
- (Array === @skip && @skip.any? do |url|
54
- meth, path = Regexp.escape(req.request_method), Regexp.escape(req.path)
55
- url =~ /^#{meth}:#{path}$/ || url =~ /^#{path}$/
56
- end)
56
+ return false if ! @skip.is_a?(Array) || @skip.empty?
57
+ method = Regexp.escape(req.request_method)
58
+ path = Regexp.escape(req.path)
59
+ matched = @skip.select do |p|
60
+ p = p.split ":"
61
+ if p.size > 1
62
+ if method !~ /\A#{p[0]}\Z/
63
+ return false
64
+ end
65
+
66
+ p = p[1..-1].join ":"
67
+ end
68
+
69
+ if path =~ /\A#{p}\Z/
70
+ return true
71
+ end
72
+ end
73
+
74
+ matched.size > 0
57
75
  end
58
76
 
59
77
  private
@@ -70,22 +88,26 @@ module Rack
70
88
 
71
89
  private
72
90
  def render_error_for!(env)
73
- Proc === @render_with ? @render_with.call(env) : [403, {}, ["Unauthorized"]]
91
+ @render_with.is_a?(Proc) ? @render_with.call(env) : \
92
+ [403, {}, ["Unauthorized"]]
74
93
  end
75
94
 
76
95
  module Helpers
77
96
  extend self
78
97
 
79
98
  def csrf_meta_tag(opts = {}, session = session)
80
- %Q{<meta name="#{opts[:field] || "auth"}" content="#{session[opts[:key] || "csrf"]}">}
99
+ %Q{<meta name="#{opts[:field] || "auth"}" content="#{ \
100
+ session[opts[:key] || "csrf"]}">}
81
101
  end
82
102
 
83
103
  def csrf_form_tag(opts = {}, session = session)
84
104
  session_key = session[opts[:key] || "csrf"]
85
105
  tag = opts[:tag] || "div"
106
+
86
107
  <<-HTML.strip_heredoc(opts[:offset])
87
108
  <#{tag} class="hidden">
88
- <input type="hidden" name="#{opts[:field] || "auth"}" value="#{session_key}">
109
+ <input type="hidden" name="#{ \
110
+ opts[:field] || "auth"}" value="#{session_key}">
89
111
  </#{tag}>
90
112
  HTML
91
113
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class SimpleCsrf
3
- VERSION = "1.0.0"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,85 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-simple_csrf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordon Bedwell
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-03 00:00:00.000000000 Z
11
+ date: 2014-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ~>
18
- - !ruby/object:Gem::Version
19
- version: '1.5'
20
- type: :runtime
21
- prerelease: false
22
15
  version_requirements: !ruby/object:Gem::Requirement
23
16
  requirements:
24
17
  - - ~>
25
18
  - !ruby/object:Gem::Version
26
19
  version: '1.5'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
20
  requirement: !ruby/object:Gem::Requirement
30
21
  requirements:
31
22
  - - ~>
32
23
  - !ruby/object:Gem::Version
33
- version: '2.14'
34
- type: :development
24
+ version: '1.5'
35
25
  prerelease: false
26
+ type: :runtime
27
+ - !ruby/object:Gem::Dependency
28
+ name: envygeeks-coveralls
36
29
  version_requirements: !ruby/object:Gem::Requirement
37
30
  requirements:
38
31
  - - ~>
39
32
  - !ruby/object:Gem::Version
40
- version: '2.14'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec-expect_error
33
+ version: '0.2'
43
34
  requirement: !ruby/object:Gem::Requirement
44
35
  requirements:
45
36
  - - ~>
46
37
  - !ruby/object:Gem::Version
47
- version: '0.0'
48
- type: :development
38
+ version: '0.2'
49
39
  prerelease: false
40
+ type: :development
41
+ - !ruby/object:Gem::Dependency
42
+ name: luna-rspec-formatters
50
43
  version_requirements: !ruby/object:Gem::Requirement
51
44
  requirements:
52
45
  - - ~>
53
46
  - !ruby/object:Gem::Version
54
- version: '0.0'
55
- - !ruby/object:Gem::Dependency
56
- name: envygeeks-coveralls
47
+ version: '1.2'
57
48
  requirement: !ruby/object:Gem::Requirement
58
49
  requirements:
59
50
  - - ~>
60
51
  - !ruby/object:Gem::Version
61
- version: '0.0'
62
- type: :development
52
+ version: '1.2'
63
53
  prerelease: false
54
+ type: :development
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
64
57
  version_requirements: !ruby/object:Gem::Requirement
65
58
  requirements:
66
59
  - - ~>
67
60
  - !ruby/object:Gem::Version
68
- version: '0.0'
69
- - !ruby/object:Gem::Dependency
70
- name: luna-rspec-formatters
61
+ version: '3.0'
71
62
  requirement: !ruby/object:Gem::Requirement
72
63
  requirements:
73
64
  - - ~>
74
65
  - !ruby/object:Gem::Version
75
- version: '0.0'
76
- type: :development
66
+ version: '3.0'
77
67
  prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ~>
81
- - !ruby/object:Gem::Version
82
- version: '0.0'
68
+ type: :development
83
69
  description: A simpler CSRF middleware for Rack.
84
70
  email:
85
71
  - envygeeks@gmail.com
@@ -87,17 +73,17 @@ executables: []
87
73
  extensions: []
88
74
  extra_rdoc_files: []
89
75
  files:
90
- - Gemfile
91
- - License
92
- - Rakefile
93
76
  - Readme.md
77
+ - Rakefile
78
+ - License
79
+ - Gemfile
94
80
  - lib/rack/csrf.rb
95
81
  - lib/rack/simple_csrf.rb
96
82
  - lib/rack/simple_csrf/version.rb
97
83
  homepage: https://envygeeks.com/projects/rack-csrf
98
84
  licenses: []
99
85
  metadata: {}
100
- post_install_message:
86
+ post_install_message:
101
87
  rdoc_options: []
102
88
  require_paths:
103
89
  - lib
@@ -112,9 +98,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
98
  - !ruby/object:Gem::Version
113
99
  version: '0'
114
100
  requirements: []
115
- rubyforge_project:
116
- rubygems_version: 2.2.1
117
- signing_key:
101
+ rubyforge_project:
102
+ rubygems_version: 2.1.9
103
+ signing_key:
118
104
  specification_version: 4
119
105
  summary: A simpler CSRF middleware for Rack.
120
106
  test_files: []