invalid_authenticity_token_rescue 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +45 -0
- data/Rakefile +34 -0
- data/lib/invalid_authenticity_token_rescue.rb +21 -0
- data/lib/invalid_authenticity_token_rescue/railtie.rb +9 -0
- data/lib/invalid_authenticity_token_rescue/version.rb +3 -0
- data/lib/tasks/invalid_authenticity_token_rescue_tasks.rake +4 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: abe9b291f63dcf0eb474c08570b21cc9541275a2
|
4
|
+
data.tar.gz: 96d82d018a762e629d87087b03bdaf550a1fbcc0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a31ee1e0cf77387d702748ad76ce217fb88736248a13e974c76d974334ca3e9a5f2a136351bd37f31f2e0d161048c72e2b494206b4e0c28e1486536c0600500
|
7
|
+
data.tar.gz: e7b03f83b556977162ec54c63676f1c4314d499401b4d0b035ab8d833baa939eae4e6a0557975fcf65f8a0ead5336bac4fe68d53472a617839ffda4638cd2da5
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Aaron Baldwin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# InvalidAuthenticityTokenRescue
|
2
|
+
Rails 5 default protect_from_forgery is to raise an exception. Some browsers trigger this exception by caching pages. This gem rescues the InvalidAuthenticityToken exception, triggers ExceptionNotifier, and redirects to the login page. For more details and steps to reprodcue the problem see this issue: https://github.com/rails/rails/issues/21948.
|
3
|
+
|
4
|
+
With the default Rails 5 settings users receive an error page when an InvalidAuthenticityToken is raised. For actual malicious requests this would be fine. However, with the way some broswers cache pages legitimate users are getting these error pages and sometimes make repreated unsuccessful attempts to sumbmit a form.
|
5
|
+
|
6
|
+
With this gem in place when an InvalidAuthenticityToken is raised it will be rescued. The error information is captured and notifications are sent to developers. The user will be redirected to new_session_path with flash warning set to *"Your session has expired, please log in again"*.
|
7
|
+
|
8
|
+
## ExceptionNotification Gem Required
|
9
|
+
Install and congiure the [exception_notifcation](https://github.com/smartinez87/exception_notification) gem first to receive notifications when InvalidAuthenticityToken exceptions are raised.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'invalid_authenticity_token_rescue'
|
16
|
+
```
|
17
|
+
|
18
|
+
Add **rescue_from_invalid_authenticity_token** to your ApplicationController:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class ApplicationController < ActionController::Base
|
22
|
+
protect_from_forgery with: :exception
|
23
|
+
rescue_from_invalid_authenticity_token
|
24
|
+
...
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
Add **skip_before_action** to public forms (optional):
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class SessionsController < ApplicationController
|
32
|
+
skip_before_action :verify_authenticity_token, on: :create
|
33
|
+
...
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Adding **skip_before_action** is optional but will improve user experience. Rails **protect_from_forgery** is intended to prevent a logged in user's credentials from being maliciously used to submit a form as that user. Publicly accessible forms, like a login page, that do not rely on a currently logged in user are not susceptible to forgery attacks.
|
38
|
+
|
39
|
+
Adding **skip_before_action** will allow the request to complete and the users session to be setup with the correct token. Subsequent forms submitted by the user will complete successfully. If **skip_before_action** is not added the user will be redirected to the login page and notifed that their session has expired and they need to login again.
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wwidea/invalid_authenticity_token_rescue.
|
43
|
+
|
44
|
+
## License
|
45
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
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
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'InvalidAuthenticityTokenRescue'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'invalid_authenticity_token_rescue/railtie'
|
2
|
+
|
3
|
+
module InvalidAuthenticityTokenRescue
|
4
|
+
module RescueFromInvalidAuthenticityToken
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def rescue_from_invalid_authenticity_token
|
9
|
+
rescue_from ActionController::InvalidAuthenticityToken, with: :invalid_authenticity_token
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def invalid_authenticity_token(exception)
|
16
|
+
ExceptionNotifier.notify_exception(exception, env: request.env)
|
17
|
+
flash[:warning] = 'Your session has expired, please log in again'
|
18
|
+
redirect_to new_session_path
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module InvalidAuthenticityTokenRescue
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer "invalid_authenticity_token_rescue" do
|
4
|
+
ActiveSupport.on_load :action_controller do
|
5
|
+
ActionController::Base.include InvalidAuthenticityTokenRescue::RescueFromInvalidAuthenticityToken
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: invalid_authenticity_token_rescue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Baldwin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.2
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.2'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 5.0.2
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.2'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: exception_notification
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 4.2.1
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 4.2.1
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
description: Rails 5 default protect_from_forgery is to raise an exception. Some browsers
|
62
|
+
trigger this exception by caching pages. This gem rescues the InvalidAuthenticityToken
|
63
|
+
exception, triggers ExceptionNotifier, and redirects to the login page.
|
64
|
+
email:
|
65
|
+
- baldwina@brightwayslearning.org
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- MIT-LICENSE
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- lib/invalid_authenticity_token_rescue.rb
|
74
|
+
- lib/invalid_authenticity_token_rescue/railtie.rb
|
75
|
+
- lib/invalid_authenticity_token_rescue/version.rb
|
76
|
+
- lib/tasks/invalid_authenticity_token_rescue_tasks.rake
|
77
|
+
homepage: https://github.com/wwidea/invalid_authenticity_token_rescue
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Rescues from Rails 5 InvalidAuthenticityToken exception
|
101
|
+
test_files: []
|