Skip to content

APPROVED: Add bot protection: rescue MissingTemplate, robots.txt#1339

Open
maebeale wants to merge 5 commits intomainfrom
maebeale/bot-protection
Open

APPROVED: Add bot protection: rescue MissingTemplate, robots.txt#1339
maebeale wants to merge 5 commits intomainfrom
maebeale/bot-protection

Conversation

@maebeale
Copy link
Collaborator

@maebeale maebeale commented Mar 4, 2026

What is the goal of this PR and why is this important?

  • Bots/crawlers requesting non-HTML formats (e.g. Accept: text/plain) cause 500 errors (ActionView::MissingTemplate) across all public-facing controllers
  • The app had an empty robots.txt, leaving it open to unnecessary crawler traffic on internal pages

How did you approach the change?

  • Global rescue_from ActionView::MissingTemplate in ApplicationController returns 406 Not Acceptable instead of 500 — covers all controllers at once
  • Allowlist-based robots.txt — blocks everything by default (Disallow: /), explicitly allows only public pages (workshops, resources, stories, etc.). Avoids leaking internal app structure

Anything else to add?

  • Considered adding global rate limiting (rack-attack / Rails 8 rate_limit) but deferred — a blanket limit risks false positives on legitimate use (e.g. search fields firing per-keystroke), and Devise account locking already handles login brute-force
  • Targeted rate limits on specific endpoints (login, password reset, contact form) can be added in a follow-up

🤖 Generated with Claude Code

maebeale and others added 2 commits March 4, 2026 08:07
…page

Bots/crawlers requesting text/plain or other non-HTML formats caused a
500 error because no text template exists. Wrapping in respond_to
returns 406 Not Acceptable for unsupported formats instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ttack

- Rescue ActionView::MissingTemplate globally in ApplicationController
  to return 406 instead of 500 when bots request non-HTML formats
- Revert per-controller respond_to in HomeController (global rescue covers it)
- Add allowlist-based robots.txt that only permits crawling public pages
- Add rack-attack gem with login throttle and global rate limit

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@maebeale maebeale requested a review from jmilljr24 March 4, 2026 14:02
Copy link
Collaborator

@jmilljr24 jmilljr24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we wait to merge this to staging at a separate time from the bug fixes? I don't think it will break any but it's an app wise change.

Also was rail built in rate limit considered? Not sure exactly what we need but if something included with rails works it's worth avoiding a dependency.

maebeale and others added 2 commits March 4, 2026 09:42
Rails 8.1 provides native rate_limit in controllers, so no need for
an external gem. Added global rate limit (300 req / 5 min per IP) in
ApplicationController. Devise already handles login-specific protection
via account locking after 10 failed attempts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
A blanket rate limit risks blocking legitimate users (e.g. search fields
firing per-keystroke). Devise account locking already handles login
brute-force. Targeted rate limits can be added later where needed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@maebeale
Copy link
Collaborator Author

maebeale commented Mar 4, 2026

@jmilljr24 yes ofc re waiting! we just might get some more of those index template errors in honeybadger.

rack-attack is a very common gem and i've used it before. TIL that rails 8.1 built in some rate-limiting options! still, i'll remove the rate limiting stuff from this pr to keep it focused on the error we saw and updating robots.txt.

@maebeale maebeale changed the title Add bot protection: rescue MissingTemplate, robots.txt, rack-attack Add bot protection: rescue MissingTemplate, robots.txt Mar 4, 2026
@maebeale maebeale changed the title Add bot protection: rescue MissingTemplate, robots.txt APPROVED: Add bot protection: rescue MissingTemplate, robots.txt Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants