From ff08d99d4dceebd720e16e4a40118cbd6941b0ca Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Thu, 27 Jun 2024 16:41:03 +0200 Subject: [PATCH] Catch encoding errors when creating link previews. (#30853) --- app/services/fetch_link_card_service.rb | 2 +- spec/fixtures/requests/redirect_with_utf8_url.txt | 5 +++++ spec/services/fetch_link_card_service_spec.rb | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/requests/redirect_with_utf8_url.txt diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 9692dd21b..8bc9f912c 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -32,7 +32,7 @@ class FetchLinkCardService < BaseService end attach_card if @card&.persisted? - rescue HTTP::Error, OpenSSL::SSL::SSLError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError => e + rescue HTTP::Error, OpenSSL::SSL::SSLError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError, Encoding::UndefinedConversionError => e Rails.logger.debug { "Error fetching link #{@original_url}: #{e}" } nil end diff --git a/spec/fixtures/requests/redirect_with_utf8_url.txt b/spec/fixtures/requests/redirect_with_utf8_url.txt new file mode 100644 index 000000000..08f99ee2a --- /dev/null +++ b/spec/fixtures/requests/redirect_with_utf8_url.txt @@ -0,0 +1,5 @@ +HTTP/1.1 301 Moved Permanently +server: nginx +date: Thu, 27 Jun 2024 11:04:53 GMT +content-type: text/html; charset=UTF-8 +location: http://example.com/ärgerliche-umlaute.html diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb index 4f02aba54..d83a52751 100644 --- a/spec/services/fetch_link_card_service_spec.rb +++ b/spec/services/fetch_link_card_service_spec.rb @@ -27,6 +27,7 @@ RSpec.describe FetchLinkCardService do stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt')) stub_request(:get, 'http://example.com/windows-1251').to_return(request_fixture('windows-1251.txt')) stub_request(:get, 'http://example.com/low_confidence_latin1').to_return(request_fixture('low_confidence_latin1.txt')) + stub_request(:get, 'http://example.com/aergerliche-umlaute').to_return(request_fixture('redirect_with_utf8_url.txt')) Rails.cache.write('oembed_endpoint:example.com', oembed_cache) if oembed_cache @@ -101,6 +102,14 @@ RSpec.describe FetchLinkCardService do end end + context 'with a redirect URL with faulty encoding' do + let(:status) { Fabricate(:status, text: 'http://example.com/aergerliche-umlaute') } + + it 'does not create a preview card' do + expect(status.preview_card).to be_nil + end + end + context 'with a 404 URL' do let(:status) { Fabricate(:status, text: 'http://example.com/not-found') }