GithubHelp home page GithubHelp logo

It is not possible to handle "not a valid MIME type" error using the exceptions_app = self.routes when the value of CONTENT_TYPE header is invalid about rails HOT 4 CLOSED

ayuto-takasaki-LITALICO avatar ayuto-takasaki-LITALICO commented on June 26, 2024
It is not possible to handle "not a valid MIME type" error using the exceptions_app = self.routes when the value of CONTENT_TYPE header is invalid

from rails.

Comments (4)

Earlopain avatar Earlopain commented on June 26, 2024 3

Can you check if #51247 works for you?

from rails.

Earlopain avatar Earlopain commented on June 26, 2024 2

Reproducable test case

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"
end

require "action_controller/railtie"

class TestApp < Rails::Application
  config.root = __dir__
  config.hosts << "example.org"
  config.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  routes.draw do
    get "/about", to: "test#about"
    match "/404", to: "test#not_found", via: :all
  end

  config.exceptions_app = self.routes
end

class TestController < ActionController::Base
  include Rails.application.routes.url_helpers

  def about
    render "/about"
  end

  def not_found
    render json: { error: "some error message" }, status: :not_found
  end
end

require "minitest/autorun"
require "rack/test"

class BugTest < Minitest::Test
  include Rack::Test::Methods

  def test_returns_success
    post "/about", nil, { "CONTENT_TYPE" => "invalid" }
    refute_equal(500, last_response.status)
  end

  private

  def app
    Rails.application
  end
end

from rails.

ayuto-takasaki-LITALICO avatar ayuto-takasaki-LITALICO commented on June 26, 2024 1

Can you check if #51247 works for you?

It worked!

from rails.

ayuto-takasaki-LITALICO avatar ayuto-takasaki-LITALICO commented on June 26, 2024

Thank you for writing the reproducable test case.

I also have to fix request.formats to ActionDispatch::Request.new(request.env.clone).formats,
because following lines add the RACK_REQUEST_FORM_ERROR header when the request.formats method is called.
https://github.com/rack/rack/blob/a4bc5e0f41c750135969ceece8772ab112dc8f17/lib/rack/request.rb#L536-L538

I tried to write a PR, but it seems it takes time to setup development environment.

diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb
index 5f7368de56..deb07e6018 100644
--- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb
+++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb
@@ -67,9 +67,10 @@ def render_exception(request, wrapper)
       def fallback_to_html_format_if_invalid_mime_type(request)
         # If the MIME type for the request is invalid then the @exceptions_app may not
         # be able to handle it. To make it easier to handle, we switch to HTML.
-        request.formats
+        ActionDispatch::Request.new(request.env.clone).formats
       rescue ActionDispatch::Http::MimeNegotiation::InvalidType
         request.set_header "HTTP_ACCEPT", "text/html"
+        request.set_header "CONTENT_TYPE", "application/x-www-form-urlencoded"
       end
 
       def pass_response(status)

****/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/rack-3.0.9.1/lib/rack/request.rb:503:in `POST'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/request.rb:407:in `block (2 levels) in POST'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/parameters.rb:90:in `parse_formatted_parameters'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/request.rb:406:in `block in POST'
****/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/rack-3.0.9.1/lib/rack/request.rb:107:in `fetch'
****/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/rack-3.0.9.1/lib/rack/request.rb:107:in `fetch_header'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/request.rb:405:in `POST'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/parameters.rb:57:in `parameters'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/mime_negotiation.rb:165:in `params_readable?'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/mime_negotiation.rb:69:in `block in formats'
****/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/rack-3.0.9.1/lib/rack/request.rb:107:in `fetch'
****/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/rack-3.0.9.1/lib/rack/request.rb:107:in `fetch_header'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/http/mime_negotiation.rb:68:in `formats'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:70:in `fallback_to_html_format_if_invalid_mime_type'
****/Documents/repositories/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:52:in `render_exception'

from rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.