roda-rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3665d0f667c4ff1848e1cd9165f79e8d1af48161
4
+ data.tar.gz: 947dcb654cef59987556f8a702b55104f4149b93
5
+ SHA512:
6
+ metadata.gz: 01473a39994fdfc89246dd71c2528cda834916bd09dfbd84e698a07b00ada1214efcc51ae78f4732e10820a79c3f9cd02ab9b7909faced661ce90deeeb5bad46
7
+ data.tar.gz: 84f7a70f815e31a297dbf93b598662f22a2c5f83b847a3f8e635add30c379c9e8befac5688e848c3fa369f130646ea7e974a7f0dbe5c8d0e2ddb183a1f0e15d4
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2016 Jeremy Evans
2
+ Copyright (c) 2004-2016 David Heinemeier Hansson
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to
6
+ deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
+ sell copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ = roda-rails
2
+
3
+ roda-rails offers integration for Roda when used as Rack middleware in a Rails
4
+ application. It allows the Roda middleware to use Rails flash handling as well
5
+ as Rails' CSRF support. Roda by default integrates with Rails' session support,
6
+ since that just uses the standard Rack session protocol.
7
+
8
+ Currently, only support for Rails 4.2 is included. Pull requests for other
9
+ Rails versions will be considered, in the form of a separate plugin per Rails
10
+ minor version.
11
+
12
+ Currently, there are no automated tests. If you decide to use this in production,
13
+ it may be a good idea to write automated tests for it and submit a pull request.
14
+
15
+ = Installation
16
+
17
+ gem install roda-rails
18
+
19
+ = Source Code
20
+
21
+ Source code is available on GitHub at https://github.com/jeremyevans/roda-rails
22
+
23
+ = Usage
24
+
25
+ roda-rails currently includes a rails42 plugin, which you can load just like
26
+ any other Roda plugin:
27
+
28
+ plugin :rails42
29
+
30
+ The following plugin options are supported:
31
+
32
+ :check_csrf :: A callable object that accepts the RodaRequest instance and returns
33
+ whether the CSRF token should be checked. The default is to check
34
+ all non-GET requests.
35
+ :invalid_csrf :: A callable object for the action to taken on an invalid CSRF token.
36
+ The default is to set a 400 response status and halt.
37
+ :csrf_key :: A callable object for how to get the submitted CSRF token. The default
38
+ is to get it from the +authenticity_token+ request parameter.
39
+
40
+ Methods offered:
41
+
42
+ csrf_tag :: Return a string containing a hidden input tag containing the CSRF token.
43
+ flash :: The Rails flash object associated with the current session.
44
+
45
+ = License
46
+
47
+ MIT
48
+
49
+ = Author
50
+
51
+ Jeremy Evans <code@jeremyevans.net>
@@ -0,0 +1,113 @@
1
+ class Roda
2
+ module RodaPlugins
3
+ module Rails42
4
+ Flash = ActionDispatch::Flash
5
+ AUTHENTICITY_TOKEN_LENGTH = 32
6
+ DEFAULT_CHECK_CSRF = lambda{|_| !r.is_get?}
7
+ DEFAULT_INVALID_CSRF = lambda do |r|
8
+ r.response.status = 400
9
+ r.halt
10
+ end
11
+ DEFAULT_CSRF_TOKEN = lambda{|r| r['authenticity_token']}
12
+
13
+ def self.configure(app, opts={})
14
+ opts = opts.dup
15
+ opts[:check_csrf] ||= DEFAULT_CHECK_CSRF
16
+ opts[:invalid_csrf] ||= DEFAULT_INVALID_CSRF
17
+ opts[:csrf_token] ||= DEFAULT_CSRF_TOKEN
18
+ app.opts[:rails] = opts
19
+ end
20
+
21
+ ### Rails 4.2 integration code, most code from Rails
22
+ module InstanceMethods
23
+ def call
24
+ catch(:halt) do
25
+ rails = self.class.opts[:rails]
26
+ r = request
27
+ if instance_exec(r, &rails[:check_csrf])
28
+ unless valid_authenticity_token?(session, instance_exec(r, &rails[:csrf_token]))
29
+ instance_exec(r, &rails[:invalid_csrf])
30
+ end
31
+ end
32
+
33
+ super
34
+ end
35
+ end
36
+
37
+ def flash
38
+ env[Flash::KEY] ||= Flash::FlashHash.from_session_value(session["flash"])
39
+ end
40
+
41
+ def csrf_tag
42
+ "<input type='hidden' name='authenticity_token' value=\"#{masked_authenticity_token(session)}\" />".html_safe
43
+ end
44
+
45
+ private
46
+
47
+ # Creates a masked version of the authenticity token that varies
48
+ # on each request. The masking is used to mitigate SSL attacks
49
+ # like BREACH.
50
+ def masked_authenticity_token(session)
51
+ one_time_pad = SecureRandom.random_bytes(AUTHENTICITY_TOKEN_LENGTH)
52
+ encrypted_csrf_token = xor_byte_strings(one_time_pad, real_csrf_token(session))
53
+ masked_token = one_time_pad + encrypted_csrf_token
54
+ Base64.strict_encode64(masked_token)
55
+ end
56
+
57
+ # Checks the client's masked token to see if it matches the
58
+ # session token. Essentially the inverse of
59
+ # +masked_authenticity_token+.
60
+ def valid_authenticity_token?(session, encoded_masked_token)
61
+ if encoded_masked_token.nil? || encoded_masked_token.empty? || !encoded_masked_token.is_a?(String)
62
+ return false
63
+ end
64
+
65
+ begin
66
+ masked_token = Base64.strict_decode64(encoded_masked_token)
67
+ rescue ArgumentError # encoded_masked_token is invalid Base64
68
+ return false
69
+ end
70
+
71
+ # See if it's actually a masked token or not. In order to
72
+ # deploy this code, we should be able to handle any unmasked
73
+ # tokens that we've issued without error.
74
+
75
+ if masked_token.length == AUTHENTICITY_TOKEN_LENGTH
76
+ # This is actually an unmasked token. This is expected if
77
+ # you have just upgraded to masked tokens, but should stop
78
+ # happening shortly after installing this gem
79
+ compare_with_real_token masked_token, session
80
+
81
+ elsif masked_token.length == AUTHENTICITY_TOKEN_LENGTH * 2
82
+ # Split the token into the one-time pad and the encrypted
83
+ # value and decrypt it
84
+ one_time_pad = masked_token[0...AUTHENTICITY_TOKEN_LENGTH]
85
+ encrypted_csrf_token = masked_token[AUTHENTICITY_TOKEN_LENGTH..-1]
86
+ csrf_token = xor_byte_strings(one_time_pad, encrypted_csrf_token)
87
+
88
+ compare_with_real_token csrf_token, session
89
+
90
+ else
91
+ false # Token is malformed
92
+ end
93
+ end
94
+
95
+ def compare_with_real_token(token, session)
96
+ ActiveSupport::SecurityUtils.secure_compare(token, real_csrf_token(session))
97
+ end
98
+
99
+ def real_csrf_token(session)
100
+ session[:_csrf_token] ||= SecureRandom.base64(AUTHENTICITY_TOKEN_LENGTH)
101
+ Base64.strict_decode64(session[:_csrf_token])
102
+ end
103
+
104
+ def xor_byte_strings(s1, s2)
105
+ s1.bytes.zip(s2.bytes).map { |(c1,c2)| c1 ^ c2 }.pack('c*')
106
+ end
107
+ end
108
+ end
109
+
110
+ register_plugin(:rails42, Rails42)
111
+ end
112
+ end
113
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roda-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: roda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.0
41
+ description: |
42
+ roda-rails offers integration for Roda when used as Rack middleware in a Rails
43
+ application. It allows the Roda middleware to use Rails flash handling as well
44
+ as Rails' CSRF support.
45
+ email:
46
+ - code@jeremyevans.net
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - MIT-LICENSE
52
+ - README.rdoc
53
+ - lib/roda/plugins/rails42.rb
54
+ homepage: https://github.com/jeremyevans/roda-rails
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.5.1
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Integration for using Roda as Rack middleware in a Rails app
78
+ test_files: []