From 7c89900c7086ccf719c183f580848809f2a1c9e3 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 26 May 2020 11:30:21 +0200 Subject: [PATCH] Ignore inadequate icon sources Icons with data URLs as source are throwing exceptions when trying to parse their type via the file extension. This fix ignores all icons with data URLs to begin with. --- lib/manifique/web_client.rb | 15 +++++++++++---- spec/fixtures/kommit.html | 17 ++++++++++++++++ spec/manifique/web_client_spec.rb | 32 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/kommit.html diff --git a/lib/manifique/web_client.rb b/lib/manifique/web_client.rb index 6702de1..4d4eefb 100644 --- a/lib/manifique/web_client.rb +++ b/lib/manifique/web_client.rb @@ -103,7 +103,7 @@ module Manifique icon_links.each do |link| icon = {} icon["src"] = link.attributes["href"].value rescue nil - next if icon["src"].to_s.empty? + next unless is_adequate_src(icon["src"]) icon["sizes"] = link.attributes["sizes"].value rescue nil icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"]) @metadata.icons.push icon @@ -117,7 +117,7 @@ module Manifique icon_links.each do |link| icon = { "purpose" => "apple-touch-icon" } icon["src"] = link.attributes["href"].value rescue nil - next if icon["src"].to_s.empty? + next unless is_adequate_src(icon["src"]) icon["sizes"] = link.attributes["sizes"].value rescue nil icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"]) @metadata.icons.push icon @@ -130,7 +130,7 @@ module Manifique if mask_icon_link = @html.at_css("link[rel=mask-icon]") icon = { "purpose" => "mask-icon" } icon["src"] = mask_icon_link.attributes["href"].value rescue nil - return if icon["src"].to_s.empty? + return unless is_adequate_src(icon["src"]) icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"]) icon["color"] = mask_icon_link.attributes["color"].value rescue nil @metadata.icons.push icon @@ -138,6 +138,14 @@ module Manifique end end + def is_data_url?(src) + !!src.match(/^data:/) + end + + def is_adequate_src(src) + !src.nil? && !is_data_url?(src) + end + def get_icon_type(src) extension = src.match(/\.([a-zA-Z]+)$/)[1] return "image/#{extension}" @@ -161,6 +169,5 @@ module Manifique rescue false end - end end diff --git a/spec/fixtures/kommit.html b/spec/fixtures/kommit.html new file mode 100644 index 0000000..c0111fe --- /dev/null +++ b/spec/fixtures/kommit.html @@ -0,0 +1,17 @@ + + + + + Kommit + + + + + + + + + + + + diff --git a/spec/manifique/web_client_spec.rb b/spec/manifique/web_client_spec.rb index 5b903c1..95fe715 100644 --- a/spec/manifique/web_client_spec.rb +++ b/spec/manifique/web_client_spec.rb @@ -211,6 +211,38 @@ RSpec.describe Manifique::WebClient do end end end + + context "with data URL icons" do + before do + index_html = File.read(File.join(__dir__, "..", "fixtures", "kommit.html")); + stub_request(:get, "https://kosmos.social/"). + to_return(body: index_html, status: 200, headers: { + "Content-Type": "text/html; charset=utf-8" + }) + end + + subject { web_client.fetch_metadata } + + it "returns a metadata object" do + expect(subject).to be_kind_of(Manifique::Metadata) + end + + it "loads properties from parsed HTML" do + expect(subject.name).to eq("Kommit") + expect(subject.description).to eq("Augment your memory") + end + + it "ignores data URL icons" do + expect(subject.icons.length).to eq(1) + end + + it "loads icons from link[rel=apple-touch-icon] elements" do + apple_touch_icons = subject.icons.select{|i| i["purpose"] == "apple-touch-icon"} + expect(apple_touch_icons.length).to eq(1) + expect(apple_touch_icons.first["type"]).to eq("image/jpg") + expect(apple_touch_icons.first["sizes"]).to be_nil + end + end end end