Rate-limit login attempts and password reset emails #13
Loading…
Reference in a new issue
No description provided.
Delete branch "armin-bot/impress:claude/codebase-security-audit-3e2fxx"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This comes out of a security audit looking for practical, likely-exploitable issues. The main finding: nothing limited password guessing.
POST /users/sign_inwould check passwords as fast as we could answer, so an attacker could brute-force weak passwords, or replay credentials leaked from other sites ("credential stuffing"). Similarly,POST /users/passwordwould send unlimited reset emails to anyone's inbox.Companion PR for Impress 2020, whose
loginmutation checks the same passwords: OpenNeo/impress-2020#3What this changes
Adds Rack::Attack throttles, next to the existing pets one:
logins/iplogins/accountpassword_resets/ippassword_resets/accountThrottled browser requests get redirected back to the form, with a friendly flash message. JSON requests get a
429withRetry-After.Screenshots: throttled login and password reset
A few details that matter for these to actually hold up:
utf8mb3_general_cicollation treatsvictim,VICTIM,Victim, andVíctimas the same name. If we keyed by the raw text, an attacker would get a fresh budget for every spelling.req.paramsignores JSON, which would let{"auth_user": …}requests skip the per-account limit.to_son whatever it gets, so{"name": 12345}logs into the account named "12345". The per-account limit has to count that too. (The review subagent caught this one.)//users//sign_in/the same way the router does. We also match the optional format suffix the router accepts (/users/sign_in.json,.html, …).Bug fix along the way: the existing pets throttle's HTML response crashed with
NoMethodError(so users saw a 500 page), because it wrote toenv['action_dispatch.request.flash_hash'], which isn't loaded yet when Rack::Attack runs. All the throttles now share a helper that loads the flash, sets the message, and commits it to the session.Lockouts and the "known device" cookie
Usernames are public, so a per-account limit could let someone lock a user out on purpose, just by spamming bad passwords. To prevent that, a browser that has logged into an account before is exempt from that account's limits (the per-IP limits still apply). This is OWASP's "device cookie" approach. Every successful login (password, remember-me, or NeoPass) adds the account's ID to a signed, HttpOnly
known_login_devicecookie, which remembers up to 5 accounts.So a griefer can only block password login for that account from browsers it's never been used on. Even then, it's for 10 minutes at a time, and NeoPass and password reset (which signs you in directly) still work.
Tradeoffs and notes
Rails.cache, which in production is the defaultFileStore, and that never deletes expired entries on its own. This was already true for the pets throttle (a file per IP per 30 seconds!), and these throttles add a few more files per attacker. Iftmp/cacheisn't already reset by each deploy, a periodicRails.cache.cleanup(or another cache store) would be worth adding.Testing
spec/requests/rate_limits_spec.rb(17 examples) covers:authenticated_at_speconly avoids this by accident, because it calls a path helper first.)bundle exec rspec(406 examples), plus the Test::Unit tests. I also reloaded the test DB, which had a strayItemleft in it that broke homepage rendering.Audit notes: what else I checked
No other likely-exploitable issues found in this app. Here's what I covered, so it's clear what "clean" means:
current_user.ClosetHanger#list_belongs_to_userblocks moving items into someone else's list. Staff-only actions are gated. Shadowban and list-visibility rules are applied consistently.protect_from_forgeryuses:null_session, and it runs before our other callbacks, so forged requests run logged-out. OmniAuth's request phase is POST-only with CSRF protection (omniauth-rails_csrf_protection), and the OIDCstateis checked.return_toandoriginparams flow intoredirect_toin several places. Rails 8.1'saction_on_open_redirect = :raiseblocks external hosts, so these can only cause a 500, not a redirect.html_safe/rawsites only wrap escaped content (content_tag,_htmltranslations). List descriptions go throughSanitize::Config::BASIC. The petpage output is escaped inside its<textarea>.sanitize_sql_like. The few interpolations only use constants.images.neopets.comorigin. The public data dump only covers modeling tables (no user data).force_ssl, cookie sessions,Forwardedstripped (from the earlier advisory fix), MiniProfiler restricted in production, no secrets committed.Worth knowing, not fixed here (lower risk, or needs a bigger decision):
config/initializers/devise.rb). That's fast to brute-force if theopenneo_idDB ever leaks. Moving to bcrypt (re-hashing on login) is the real fix, but Impress 2020's login verifies these same hashes, so it needs to be coordinated with that, or with retiring Impress 2020's login.X-Forwarded-Forintocurrent_sign_in_ip, which our shadowban check (User#likely_is?) trusts. The only effect: a shadowbanned user could make their own listings visible to one IP of their choosing.🤖 Generated with Claude Code
https://claude.ai/code/session_014PWWd37NNMoRcoGZrnWKEs
Yet another useful note that clearing out Impress 2020 is not only simplifying for maintenance, but practical for users! It would unblock (and mostly even resolve) everything straggling.