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.
This commit is contained in:
Basti 2020-05-26 11:30:21 +02:00
parent ae5c02b7ed
commit 7c89900c70
No known key found for this signature in database
GPG Key ID: BE4634D632D39B67
3 changed files with 60 additions and 4 deletions

View File

@ -103,7 +103,7 @@ module Manifique
icon_links.each do |link| icon_links.each do |link|
icon = {} icon = {}
icon["src"] = link.attributes["href"].value rescue nil 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["sizes"] = link.attributes["sizes"].value rescue nil
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"]) icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
@metadata.icons.push icon @metadata.icons.push icon
@ -117,7 +117,7 @@ module Manifique
icon_links.each do |link| icon_links.each do |link|
icon = { "purpose" => "apple-touch-icon" } icon = { "purpose" => "apple-touch-icon" }
icon["src"] = link.attributes["href"].value rescue nil 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["sizes"] = link.attributes["sizes"].value rescue nil
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"]) icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
@metadata.icons.push icon @metadata.icons.push icon
@ -130,7 +130,7 @@ module Manifique
if mask_icon_link = @html.at_css("link[rel=mask-icon]") if mask_icon_link = @html.at_css("link[rel=mask-icon]")
icon = { "purpose" => "mask-icon" } icon = { "purpose" => "mask-icon" }
icon["src"] = mask_icon_link.attributes["href"].value rescue nil 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["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
icon["color"] = mask_icon_link.attributes["color"].value rescue nil icon["color"] = mask_icon_link.attributes["color"].value rescue nil
@metadata.icons.push icon @metadata.icons.push icon
@ -138,6 +138,14 @@ module Manifique
end end
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) def get_icon_type(src)
extension = src.match(/\.([a-zA-Z]+)$/)[1] extension = src.match(/\.([a-zA-Z]+)$/)[1]
return "image/#{extension}" return "image/#{extension}"
@ -161,6 +169,5 @@ module Manifique
rescue rescue
false false
end end
end end
end end

17
spec/fixtures/kommit.html vendored Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Kommit</title>
<meta name="description" content="Augment your memory" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<link rel="canonical" href="/" />
<link rel="alternate" hreflang="en" href="/en/" />
<link rel="apple-touch-icon" href="https://abcdefg.s3.amazonaws.com/public/icon.jpg">
</head>
<head>
<body>
</body>
</html>

View File

@ -211,6 +211,38 @@ RSpec.describe Manifique::WebClient do
end end
end 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
end end