From fe176e81364a643d9c2579049f32c51e67351dd6 Mon Sep 17 00:00:00 2001 From: Milo Delgado Date: Thu, 28 May 2026 07:09:24 -0500 Subject: [PATCH 1/3] fix(rails): exclude MimeNegotiation::InvalidType by default Raised when a client sends a malformed Accept or Content-Type header (typically scanner/fuzzer traffic). Rails maps it to a 406 via ActionDispatch::ExceptionWrapper.rescue_responses, so it is not an application bug worth capturing. --- .../generators/posthog/templates/posthog.rb | 1 + .../lib/posthog/rails/configuration.rb | 1 + spec/posthog/rails/configuration_spec.rb | 42 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 spec/posthog/rails/configuration_spec.rb diff --git a/posthog-rails/lib/generators/posthog/templates/posthog.rb b/posthog-rails/lib/generators/posthog/templates/posthog.rb index 7a13aec..d5ac82d 100644 --- a/posthog-rails/lib/generators/posthog/templates/posthog.rb +++ b/posthog-rails/lib/generators/posthog/templates/posthog.rb @@ -130,6 +130,7 @@ # - ActionController::RoutingError # - ActionController::UnknownFormat # - ActionController::UnknownHttpMethod +# - ActionDispatch::Http::MimeNegotiation::InvalidType # - ActionDispatch::Http::Parameters::ParseError # - ActiveRecord::RecordNotFound # - ActiveRecord::RecordNotUnique diff --git a/posthog-rails/lib/posthog/rails/configuration.rb b/posthog-rails/lib/posthog/rails/configuration.rb index e755ebe..6f4b68e 100644 --- a/posthog-rails/lib/posthog/rails/configuration.rb +++ b/posthog-rails/lib/posthog/rails/configuration.rb @@ -56,6 +56,7 @@ def default_excluded_exceptions 'ActionController::RoutingError', 'ActionController::UnknownFormat', 'ActionController::UnknownHttpMethod', + 'ActionDispatch::Http::MimeNegotiation::InvalidType', 'ActionDispatch::Http::Parameters::ParseError', 'ActiveRecord::RecordNotFound', 'ActiveRecord::RecordNotUnique' diff --git a/spec/posthog/rails/configuration_spec.rb b/spec/posthog/rails/configuration_spec.rb new file mode 100644 index 0000000..1139a98 --- /dev/null +++ b/spec/posthog/rails/configuration_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'action_controller' +require 'action_dispatch' + +$LOAD_PATH.unshift File.expand_path('../../../posthog-rails/lib', __dir__) + +require 'posthog/rails/configuration' + +RSpec.describe PostHog::Rails::Configuration do + subject(:config) { described_class.new } + + describe '#should_capture_exception?' do + it 'excludes ActionController::RoutingError by default' do + exception = ActionController::RoutingError.new('No route matches') + expect(config.should_capture_exception?(exception)).to be false + end + + it 'excludes ActionDispatch::Http::MimeNegotiation::InvalidType by default' do + # Raised when a client sends a malformed Accept / Content-Type header + # (typically scanner traffic). Rails maps it to a 406 — see + # ActionDispatch::ExceptionWrapper.rescue_responses — so it is not a + # bug worth capturing. + exception = ActionDispatch::Http::MimeNegotiation::InvalidType.new('"foo" is not a valid MIME type') + expect(config.should_capture_exception?(exception)).to be false + end + + it 'captures application exceptions that are not in the excluded list' do + exception = StandardError.new('something broke') + expect(config.should_capture_exception?(exception)).to be true + end + + it 'honours user-supplied excluded_exceptions in addition to defaults' do + config.excluded_exceptions = ['MyApp::IgnorableError'] + stub_const('MyApp::IgnorableError', Class.new(StandardError)) + expect(config.should_capture_exception?(MyApp::IgnorableError.new)).to be false + # defaults still apply + expect(config.should_capture_exception?(ActionController::RoutingError.new('x'))).to be false + end + end +end From 6266a6ec05c55102e22e87bfaaf78944b6001c68 Mon Sep 17 00:00:00 2001 From: Milo Delgado Date: Thu, 28 May 2026 07:14:30 -0500 Subject: [PATCH 2/3] test(rails): parameterise default-exclusion examples Matches the iteration pattern used elsewhere in the spec suite (e.g. client_spec.rb:628). --- spec/posthog/rails/configuration_spec.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/spec/posthog/rails/configuration_spec.rb b/spec/posthog/rails/configuration_spec.rb index 1139a98..b46a110 100644 --- a/spec/posthog/rails/configuration_spec.rb +++ b/spec/posthog/rails/configuration_spec.rb @@ -12,18 +12,18 @@ subject(:config) { described_class.new } describe '#should_capture_exception?' do - it 'excludes ActionController::RoutingError by default' do - exception = ActionController::RoutingError.new('No route matches') - expect(config.should_capture_exception?(exception)).to be false - end - - it 'excludes ActionDispatch::Http::MimeNegotiation::InvalidType by default' do - # Raised when a client sends a malformed Accept / Content-Type header - # (typically scanner traffic). Rails maps it to a 406 — see - # ActionDispatch::ExceptionWrapper.rescue_responses — so it is not a - # bug worth capturing. - exception = ActionDispatch::Http::MimeNegotiation::InvalidType.new('"foo" is not a valid MIME type') - expect(config.should_capture_exception?(exception)).to be false + # MimeNegotiation::InvalidType is raised when a client sends a malformed + # Accept / Content-Type header (typically scanner traffic). Rails maps it + # to a 406 — see ActionDispatch::ExceptionWrapper.rescue_responses — so it + # is not a bug worth capturing. + { + 'ActionController::RoutingError' => ActionController::RoutingError.new('No route matches'), + 'ActionDispatch::Http::MimeNegotiation::InvalidType' => + ActionDispatch::Http::MimeNegotiation::InvalidType.new('"foo" is not a valid MIME type') + }.each do |name, exception| + it "excludes #{name} by default" do + expect(config.should_capture_exception?(exception)).to be false + end end it 'captures application exceptions that are not in the excluded list' do From c988585556deb0e113fcf1a9733b65adaa9cd3b5 Mon Sep 17 00:00:00 2001 From: Anna Garcia Date: Mon, 1 Jun 2026 10:40:35 -0400 Subject: [PATCH 3/3] chore: add changeset for MimeNegotiation::InvalidType exclusion Co-Authored-By: Claude Opus 4.8 (1M context) --- .changeset/exclude-mime-negotiation-invalidtype.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/exclude-mime-negotiation-invalidtype.md diff --git a/.changeset/exclude-mime-negotiation-invalidtype.md b/.changeset/exclude-mime-negotiation-invalidtype.md new file mode 100644 index 0000000..e0529c7 --- /dev/null +++ b/.changeset/exclude-mime-negotiation-invalidtype.md @@ -0,0 +1,5 @@ +--- +'posthog-ruby': patch +--- + +posthog-rails: exclude `ActionDispatch::Http::MimeNegotiation::InvalidType` from captured exceptions by default. It is raised on malformed `Accept`/`Content-Type` headers (almost always scanner traffic) and mapped to a 406 by Rails, so it is noise rather than an app error.