Rails Disable Route Format

- 2 mins read
rails webhookdump security

Today, while checking my APM (Application Performance Monitoring) tools, I found some strange errors in the reports. After looking closer, I realized these errors were coming from people trying to access my webhook URLs by adding format extensions like /blog.xml or /blog.json. Even though these endpoints don’t exist in my app, these requests were creating unnecessary error alerts.

In this post, I’ll show you how to disable these format extensions in Rails to avoid similar issues.

What’s Happening?

By default, Rails allows URLs to end with format extensions (like .xml, .json, .csv). This means when someone tries to access:

GET /webhooks.xml
GET /webhooks.json

Rails will try to respond to these requests. If you haven’t set up your app to handle these formats, it returns a 404 error, which then shows up in your error monitoring tools.

How to Fix This

There are three ways to solve this problem. Choose the one that fits your needs best:

Option 1: Turn Off Formats for All Routes

This is the simplest solution. In your config/routes.rb file, add:

Rails.application.routes.draw do
  default_format :html

  # Your routes here
  resources :webhooks, format: false
end

Option 2: Turn Off Formats for Specific Routes Only

If you want to keep formats for some routes (like an API) but disable them for others:

Rails.application.routes.draw do
  # API routes that can still use formats
  namespace :api do
    resources :webhooks
  end

  # Regular routes without formats
  scope format: false do
    resources :webhooks
  end
end

Option 3: Use Format Constraints

For more control over which routes accept formats:

Rails.application.routes.draw do
  constraints format: false do
    get '/blog', to: 'blog#index'
    resources :posts
  end

  # Routes outside this block can still use formats
end

source constraint: https://guides.rubyonrails.org/routing.html#specifying-constraints

What to Check After Making Changes

  1. Run rails routes to see how your changes affected your routes
  2. Watch your logs to make sure real requests aren’t being blocked
  3. Test your changes in development before pushing to production

Results From My Experience

After I made these changes: