@@ -29,11 +29,12 @@ module ExceptionHelpers
2929
3030 def self . included ( includer ) #:nodoc:
3131 includer . class_eval do
32- rescue_from StandardError , :with => :generic_exception
33- rescue_from ActiveRecord ::RecordNotFound , :with => :record_not_found
34- rescue_from ::AbstractController ::ActionNotFound , :with => :unknown_action
35- rescue_from CbrainException , :with => :cb_exception
36- rescue_from ActionController ::UnknownFormat , :with => :unknown_format
32+ rescue_from StandardError , :with => :generic_exception
33+ rescue_from ActiveRecord ::RecordNotFound , :with => :record_not_found
34+ rescue_from ::AbstractController ::ActionNotFound , :with => :unknown_action
35+ rescue_from CbrainException , :with => :cb_exception
36+ rescue_from ActionController ::UnknownFormat , :with => :unknown_format
37+ rescue_from ActionController ::InvalidAuthenticityToken , :with => :invalid_auth_token
3738 end
3839 end
3940
@@ -111,6 +112,32 @@ def cb_exception(exception)
111112 end
112113 end
113114
115+ # The authenticity token is maintained in the cookie session
116+ # for web pages; sometimes users open several tabs or keep them open
117+ # so long that it becomes out of sync. We just redirect.
118+ # There are also cases where bots try to POST a lot, so we are more
119+ # strict here and return unauthorized.
120+ def invalid_auth_token ( exception )
121+ raise if Rails . env == 'development' #Want to see stack trace in dev. Also will log it in exception logger
122+ respond_to do |format |
123+ format . html do
124+ controller = params [ :controller ] . to_s
125+ action = params [ :action ] . to_s
126+ if current_user . present? # some browser shenanigans, but legit user
127+ redirect_to default_redirect
128+ elsif controller == 'sessions' && action == 'create' # browser shenanigans but trying to log in
129+ redirect_to default_redirect # will be the sign in page
130+ else # POST to other forms; hackers?
131+ head :unauthorized
132+ end
133+ end
134+ format . js { head :unauthorized }
135+ format . xml { head :unauthorized }
136+ format . json { head :unauthorized }
137+ format . any { head :unauthorized }
138+ end
139+ end
140+
114141 # Anything else is serious.
115142 def generic_exception ( exception )
116143 raise if Rails . env == 'development' #Want to see stack trace in dev. Also will log it in exception logger
0 commit comments