excid3/revise_auth

Error with rate_limit when using Litestack

Closed this issue ยท 8 comments

After updating my revise_auth from 0.7 to 0.8 I ended up getting an error in production only, right after login:

2024-08-12T15:27:57.296 app[2874274c044068] syd [info] I, [2024-08-12T15:27:57.293811 #326] INFO -- : [1143bcd3-309a-4c6b-995b-36772a4446cc] Parameters: {"authenticity_token"=>"[FILTERED]", "email"=>"organiser@example.com", "password"=>"[FILTERED]", "button"=>""}

2024-08-12T15:27:57.296 app[2874274c044068] syd [info] I, [2024-08-12T15:27:57.295058 #326] INFO -- : [1143bcd3-309a-4c6b-995b-36772a4446cc] Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.0ms)

2024-08-12T15:27:57.296 app[2874274c044068] syd [info] E, [2024-08-12T15:27:57.296055 #326] ERROR -- : [1143bcd3-309a-4c6b-995b-36772a4446cc]

2024-08-12T15:27:57.296 app[2874274c044068] syd [info] [1143bcd3-309a-4c6b-995b-36772a4446cc] NoMethodError (undefined method `>' for true):

2024-08-12T15:27:57.296 app[2874274c044068] syd [info] [1143bcd3-309a-4c6b-995b-36772a4446cc]

2024-08-12T15:27:57.296 app[2874274c044068] syd [info] [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/rate_limiting.rb:55:in `rate_limiting'

2024-08-12T15:27:57.296 app[2874274c044068] syd [info] [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/rate_limiting.rb:48:in `block in rate_limit'

...

Looking at lines 53-55 of rate_limiting.rb:

def rate_limiting(to:, within:, by:, with:, store:)
        count = store.increment("rate-limit:#{controller_path}:#{instance_exec(&by)}", 1, expires_in: within)
        if count && count > to

My guess is that store.increment is returning true instead of a number and so it ends up being true > to, causing the error.

There was no error in development, only production, so my guess is that because I'm using Litestack in production as per this article, the store.increment isn't working as expected?

Reverting to revise_auth 0.7 (before the addition of rate_limit) has temporarily solved the issue -- have I found a bug in Rails or is this a bug in Litestack not working as Rails expects?

Yeah, isn't an issue with ReviseAuth. This would be an issue with your Rails cache store returning true instead of a number most likely.

Share the full stacktrace so I can point you in the right direction to fix it.

Thanks, appreciate any hints!

Log lines specific to the error:

  |   | 2024-08-12 23:27:57.296 | E, [2024-08-12T15:27:57.296055 #326] ERROR -- : [1143bcd3-309a-4c6b-995b-36772a4446cc]  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] NoMethodError (undefined method `>' for true): |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/rate_limiting.rb:55:in `rate_limiting' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/rate_limiting.rb:48:in `block in rate_limit' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:407:in `instance_exec' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:407:in `block in make_lambda' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:179:in `block in call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:180:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:559:in `block in invoke_before' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:559:in `each' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:559:in `invoke_before' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:119:in `block in run_callbacks' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] turbo-rails (2.0.6) lib/turbo-rails.rb:24:in `with_request_id' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] turbo-rails (2.0.6) app/controllers/concerns/turbo/request_id_tracking.rb:10:in `turbo_tracking_request_id' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:130:in `block in run_callbacks' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actiontext (7.2.0) lib/action_text/rendering.rb:25:in `with_renderer' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actiontext (7.2.0) lib/action_text/engine.rb:71:in `block (4 levels) in <class:Engine>' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:130:in `instance_exec' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:130:in `block in run_callbacks' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:141:in `run_callbacks' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/abstract_controller/callbacks.rb:260:in `process_action' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/rescue.rb:27:in `process_action' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/instrumentation.rb:77:in `block in process_action' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/notifications.rb:210:in `block in instrument' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/notifications/instrumenter.rb:58:in `instrument' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/notifications.rb:210:in `instrument' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/instrumentation.rb:76:in `process_action' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal/params_wrapper.rb:259:in `process_action' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activerecord (7.2.0) lib/active_record/railties/controller_runtime.rb:39:in `process_action' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/abstract_controller/base.rb:163:in `process' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionview (7.2.0) lib/action_view/rendering.rb:40:in `process' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal.rb:252:in `dispatch' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_controller/metal.rb:335:in `dispatch' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/routing/route_set.rb:67:in `dispatch' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/routing/route_set.rb:50:in `serve' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/journey/router.rb:53:in `block in serve' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/journey/router.rb:133:in `block in find_routes' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/journey/router.rb:126:in `each' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/journey/router.rb:126:in `find_routes' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/journey/router.rb:34:in `serve' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/routing/route_set.rb:896:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack (3.1.7) lib/rack/tempfile_reaper.rb:20:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack (3.1.7) lib/rack/etag.rb:29:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack (3.1.7) lib/rack/conditional_get.rb:43:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack (3.1.7) lib/rack/head.rb:15:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/http/permissions_policy.rb:38:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/http/content_security_policy.rb:36:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack-session (2.0.0) lib/rack/session/abstract/id.rb:272:in `context' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack-session (2.0.0) lib/rack/session/abstract/id.rb:266:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/cookies.rb:704:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/callbacks.rb:31:in `block in call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] activesupport (7.2.0) lib/active_support/callbacks.rb:101:in `run_callbacks' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/callbacks.rb:30:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:31:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/show_exceptions.rb:32:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] railties (7.2.0) lib/rails/rack/logger.rb:41:in `call_app' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] railties (7.2.0) lib/rails/rack/logger.rb:29:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/remote_ip.rb:96:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/request_id.rb:33:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack (3.1.7) lib/rack/method_override.rb:28:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack (3.1.7) lib/rack/runtime.rb:24:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/executor.rb:16:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/static.rb:27:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] rack (3.1.7) lib/rack/sendfile.rb:114:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/ssl.rb:82:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] actionpack (7.2.0) lib/action_dispatch/middleware/host_authorization.rb:143:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] railties (7.2.0) lib/rails/engine.rb:535:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] puma (6.4.2) lib/puma/configuration.rb:272:in `call' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] puma (6.4.2) lib/puma/request.rb:100:in `block in handle_request' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] puma (6.4.2) lib/puma/thread_pool.rb:378:in `with_force_shutdown' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] puma (6.4.2) lib/puma/request.rb:99:in `handle_request' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] puma (6.4.2) lib/puma/server.rb:464:in `process_client' |  
  |   | 2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] puma (6.4.2) lib/puma/server.rb:245:in `block in run' |  
-- | -- | --
2024-08-12 23:27:57.296 | [1143bcd3-309a-4c6b-995b-36772a4446cc] puma (6.4.2) lib/puma/thread_pool.rb:155:in `block in spawn_thread' |  

Ah, just found this oldmoe/litestack#122

Nice! Already fixed!

Probably have to use it directly from GitHub while he cuts a new release.

Yeah tried using the git/github gem for litestack but turns out the deploy for Fly doesn't like them for whatever reason, so for now just using revise_auth 0.7 and wrote myself a todo to update litestack/revise_auth later.

Hmm, what was the issue with Fly? I would expect that to be all handled by Bundler?

Turned out it was just because I didn't have git installed on my instance so I added it to the list of packages needed to build gems and was able to deploy and use litestack from github and revise_auth 0.8 without any errors.

Changed my todo to update litestack to 0.4.5 when it's released and remove git from the Dockerfile as I think it adds a little time onto the deploy having to install it.

Thanks for your diligence, helped me learn a thing or two :)

Ohh, that would do it. Don't normally need git, so it makes sense to not have it normally. ๐Ÿ‘