actionpack-per_form_csrf_tokens 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3dbf92626e5ce7f75cef9db6b0b50a6ce6bac99
4
+ data.tar.gz: d26e97ae9a24d33fdbc50d1cc38d7953867a423b
5
+ SHA512:
6
+ metadata.gz: 2c3a4c98ce2955c2d8b8e2e3b52f22e28c4c1791d6e5813a3f9e64641ecd049fa9eff183f5fce76dd9d6fccc73c74c0f4ebca8db64c1e6588374fe2a0da3f32a
7
+ data.tar.gz: 7db96a3b05f5bb9ab91c1f4ddc19dc42927aa5b38430777e3ceaa6a1dbf80434ba4b4e751c3b574912f8d18b7a349a79f7f748b5fe5ca5971d02c5fe0b5a7dce
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## [0.1.0](https://github.com/yasaichi/actionpack-per_form_csrf_tokens/releases/tag/v0.1.0) (September 8, 2021)
4
+
5
+ - The initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Yuichi Goto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Actionpack::PerFormCsrfTokens
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/actionpack/per_form_csrf_tokens`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'actionpack-per_form_csrf_tokens'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install actionpack-per_form_csrf_tokens
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/actionpack-per_form_csrf_tokens. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the ActionPack::PerFormCsrfTokens project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/actionpack-per_form_csrf_tokens/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require "rake/testtask"
10
+
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << "lib"
13
+ t.libs << "test"
14
+ t.pattern = "test/**/*_test.rb"
15
+ t.verbose = false
16
+ end
17
+
18
+ task default: :test
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/security_utils"
5
+ require "openssl"
6
+ require "uri"
7
+
8
+ module ActionPack
9
+ module PerFormCsrfTokens
10
+ module RequestForgeryProtection
11
+ GLOBAL_CSRF_TOKEN_IDENTIFIER = "!real_csrf_token"
12
+ private_constant :GLOBAL_CSRF_TOKEN_IDENTIFIER
13
+
14
+ private
15
+
16
+ def compare_with_global_token(token, session)
17
+ ::ActiveSupport::SecurityUtils.secure_compare(token, global_csrf_token(session))
18
+ end
19
+
20
+ def csrf_token_hmac(session, identifier)
21
+ ::OpenSSL::HMAC.digest(
22
+ ::OpenSSL::Digest.new("SHA256"),
23
+ real_csrf_token(session),
24
+ identifier
25
+ )
26
+ end
27
+
28
+ def decode_csrf_token(encoded_csrf_token)
29
+ ::Base64.strict_decode64(encoded_csrf_token)
30
+ rescue ::ArgumentError
31
+ ::Base64.urlsafe_decode64(encoded_csrf_token)
32
+ end
33
+
34
+ def global_csrf_token(session)
35
+ csrf_token_hmac(session, GLOBAL_CSRF_TOKEN_IDENTIFIER)
36
+ end
37
+
38
+ def per_form_csrf_token(session, action_path, method)
39
+ csrf_token_hmac(session, [action_path, method.downcase].join("#"))
40
+ end
41
+
42
+ # Override
43
+ def real_csrf_token(session)
44
+ session[:_csrf_token] ||= ::SecureRandom.base64(self.class::AUTHENTICITY_TOKEN_LENGTH)
45
+ decode_csrf_token(session[:_csrf_token])
46
+ end
47
+
48
+ def unmask_token(masked_token)
49
+ # Split the token into the one-time pad and the encrypted
50
+ # value and decrypt it.
51
+ one_time_pad = masked_token[0...self.class::AUTHENTICITY_TOKEN_LENGTH]
52
+ encrypted_csrf_token = masked_token[self.class::AUTHENTICITY_TOKEN_LENGTH..-1]
53
+ xor_byte_strings(one_time_pad, encrypted_csrf_token)
54
+ end
55
+
56
+ # Override
57
+ def valid_authenticity_token?(session, encoded_masked_token)
58
+ return false if encoded_masked_token.nil? || encoded_masked_token.empty? || !encoded_masked_token.is_a?(String)
59
+
60
+ begin
61
+ masked_token = decode_csrf_token(encoded_masked_token)
62
+ rescue ::ArgumentError # encoded_masked_token is invalid Base64
63
+ return false
64
+ end
65
+
66
+ # See if it's actually a masked token or not. In order to
67
+ # deploy this code, we should be able to handle any unmasked
68
+ # tokens that we've issued without error.
69
+
70
+ case masked_token.length
71
+ when self.class::AUTHENTICITY_TOKEN_LENGTH
72
+ # This is actually an unmasked token. This is expected if
73
+ # you have just upgraded to masked tokens, but should stop
74
+ # happening shortly after installing this gem.
75
+ compare_with_real_token(masked_token, session)
76
+
77
+ when self.class::AUTHENTICITY_TOKEN_LENGTH * 2
78
+ csrf_token = unmask_token(masked_token)
79
+
80
+ compare_with_global_token(csrf_token, session) ||
81
+ compare_with_real_token(csrf_token, session) ||
82
+ valid_per_form_csrf_token?(csrf_token, session)
83
+ else
84
+ false # Token is malformed.
85
+ end
86
+ end
87
+
88
+ def valid_per_form_csrf_token?(token, session)
89
+ correct_token = per_form_csrf_token(
90
+ session,
91
+ request.path.chomp("/"),
92
+ request.request_method
93
+ )
94
+
95
+ ::ActiveSupport::SecurityUtils.secure_compare(token, correct_token)
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionPack
4
+ module PerFormCsrfTokens
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require "active_support"
2
+ require "active_support/lazy_load_hooks"
3
+ require "actionpack/per_form_csrf_tokens/request_forgery_protection"
4
+ require "actionpack/per_form_csrf_tokens/version"
5
+
6
+ ActiveSupport.on_load(:action_controller) do
7
+ include(ActionPack::PerFormCsrfTokens::RequestForgeryProtection)
8
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actionpack-per_form_csrf_tokens
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuichi Goto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.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.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "<"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "<"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.0
83
+ description: Backport of per-form CSRF tokens into Rails 4
84
+ email:
85
+ - yasaichi@users.noreply.github.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - CHANGELOG.md
91
+ - LICENSE.txt
92
+ - README.md
93
+ - Rakefile
94
+ - lib/actionpack/per_form_csrf_tokens.rb
95
+ - lib/actionpack/per_form_csrf_tokens/request_forgery_protection.rb
96
+ - lib/actionpack/per_form_csrf_tokens/version.rb
97
+ homepage: https://github.com/yasaichi/actionpack-per_form_csrf_tokens
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.14.4
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Backport of per-form CSRF tokens
121
+ test_files: []