From 5d7c852283b1afd603f169e9ace80bf1714795d0 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 13 Nov 2021 23:07:13 +0100 Subject: Fix no link previews being generated for pages with invalid structured data (#16979) Fix #16955 --- app/lib/link_details_extractor.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/lib') diff --git a/app/lib/link_details_extractor.rb b/app/lib/link_details_extractor.rb index 3876173e0..8b38e8d0c 100644 --- a/app/lib/link_details_extractor.rb +++ b/app/lib/link_details_extractor.rb @@ -59,7 +59,7 @@ class LinkDetailsExtractor end def json - @json ||= Oj.load(@data) + @json ||= first_of_value(Oj.load(@data)) end end @@ -178,6 +178,8 @@ class LinkDetailsExtractor @structured_data ||= begin json_ld = document.xpath('//script[@type="application/ld+json"]').map(&:content).first json_ld.present? ? StructuredData.new(json_ld) : nil + rescue Oj::ParseError + nil end end -- cgit From 3517867b7656ff506a04e394fc8c1a855b7d504a Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 14 Nov 2021 21:55:40 +0100 Subject: Fix confusing error when webfinger request returns empty document (#16986) For some reason, some misconfigured servers return an empty document when queried over webfinger. Since an empty document does not lead to a parse error, the error is not caught properly and triggers uncaught exceptions later on. This PR fixes that by immediately erroring out with `Webfinger::Error` on getting an empty response. --- app/lib/webfinger.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/lib') diff --git a/app/lib/webfinger.rb b/app/lib/webfinger.rb index e0e022cea..1ffb5b4bf 100644 --- a/app/lib/webfinger.rb +++ b/app/lib/webfinger.rb @@ -46,7 +46,9 @@ class Webfinger def body_from_webfinger(url = standard_url, use_fallback = true) webfinger_request(url).perform do |res| if res.code == 200 - res.body_with_limit + body = res.body_with_limit + raise Webfinger::Error, "Request for #{@uri} returned empty response" if body.empty? + body elsif res.code == 404 && use_fallback body_from_host_meta elsif res.code == 410 -- cgit