Resolve relative icon URLs when attaching web app icons
AppCatalogManager::UpdateMetadata built remote icon download URLs with naive string concatenation, so a relative src such as "./apple-touch-icon.png" produced a URL with a dot segment (https://host/./apple-touch-icon.png). Servers that do not normalise dot segments (e.g. 5apps/openresty) answer those with 404, so the icon was never attached. Resolve icon URLs against the web app URL with URI.join (RFC 3986), which collapses dot segments and handles bare and absolute URLs correctly. Bump manifique to ~> 1.1.1, which fixes the same class of bug for relative web app manifest URLs ("./manifest.webmanifest"), and add service specs covering manifest metadata loading and icon URL resolution.
This commit is contained in:
4 files changed
+82
-8
No files matched your search
@@ -62,7 +62,7 @@ gem "sentry-rails"
|
||||
# Services
|
||||
gem 'discourse_api'
|
||||
gem "lnurl"
|
||||
gem 'manifique', '~> 1.1.0'
|
||||
gem 'manifique', '~> 1.1.1'
|
||||
gem 'nostr', '~> 0.6.0'
|
||||
gem "redis", "~> 5.4"
|
||||
|
||||
|
||||
+2
-2
@@ -262,7 +262,7 @@ GEM
|
||||
net-imap
|
||||
net-pop
|
||||
net-smtp
|
||||
manifique (1.1.0)
|
||||
manifique (1.1.1)
|
||||
faraday (~> 2.9.0)
|
||||
faraday-follow_redirects (= 0.3.0)
|
||||
nokogiri (~> 1.16.0)
|
||||
@@ -566,7 +566,7 @@ DEPENDENCIES
|
||||
letter_opener_web
|
||||
listen (~> 3.2)
|
||||
lnurl
|
||||
manifique (~> 1.1.0)
|
||||
manifique (~> 1.1.1)
|
||||
mission_control-jobs
|
||||
net-ldap
|
||||
nostr (~> 0.6.0)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
require "manifique"
|
||||
require "down"
|
||||
require "uri"
|
||||
|
||||
module AppCatalogManager
|
||||
class UpdateMetadata < AppCatalogManagerService
|
||||
@@ -39,11 +40,7 @@ module AppCatalogManager
|
||||
end
|
||||
|
||||
def attach_remote_image(attachment_name, icon)
|
||||
if icon['src'].start_with?("http")
|
||||
download_url = icon['src']
|
||||
else
|
||||
download_url = "#{@app.url}/#{icon["src"].gsub(/^\//,'')}"
|
||||
end
|
||||
download_url = URI.join(@app.url, icon["src"]).to_s
|
||||
filename = "#{attachment_name}-#{Time.now.to_i}.png"
|
||||
key = "web_apps/#{@app.id}/icons/#{filename}"
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
require 'rails_helper'
|
||||
require 'webmock/rspec'
|
||||
|
||||
RSpec.describe AppCatalogManager::UpdateMetadata, type: :model do
|
||||
let(:web_app) { AppCatalog::WebApp.create!(url: "https://apps.example.com", metadata: {}) }
|
||||
let(:downloaded_urls) { [] }
|
||||
|
||||
let(:index_html) do
|
||||
<<~HTML
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Example App</title>
|
||||
<link rel="manifest" href="./manifest.webmanifest">
|
||||
<link rel="apple-touch-icon" href="./apple-touch-icon.png">
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
||||
HTML
|
||||
end
|
||||
|
||||
let(:manifest) do
|
||||
{
|
||||
"name" => "Example App",
|
||||
"short_name" => "Example",
|
||||
"start_url" => "./",
|
||||
"icons" => [
|
||||
{ "src" => "icon-192.png", "sizes" => "192x192", "type" => "image/png" }
|
||||
]
|
||||
}.to_json
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:get, %r{\Ahttps://apps\.example\.com/?\z}).
|
||||
to_return(body: index_html, status: 200,
|
||||
headers: { "Content-Type" => "text/html; charset=utf-8" })
|
||||
|
||||
stub_request(:get, "https://apps.example.com/manifest.webmanifest").
|
||||
to_return(body: manifest, status: 200,
|
||||
headers: { "Content-Type" => "application/manifest+json" })
|
||||
|
||||
allow(Down).to receive(:download) do |url|
|
||||
downloaded_urls << url
|
||||
file = Tempfile.new(["icon", ".png"])
|
||||
file.binmode
|
||||
file.write(File.binread(Rails.root.join("spec/fixtures/files/bender.png")))
|
||||
file.rewind
|
||||
file
|
||||
end
|
||||
end
|
||||
|
||||
it "loads name and metadata from the web app manifest" do
|
||||
described_class.call(app: web_app)
|
||||
|
||||
web_app.reload
|
||||
expect(web_app.name).to eq("Example App")
|
||||
expect(web_app.metadata["short_name"]).to eq("Example")
|
||||
expect(web_app.metadata["start_url"]).to eq("./")
|
||||
end
|
||||
|
||||
it "downloads icons from URLs resolved against the app URL" do
|
||||
described_class.call(app: web_app)
|
||||
|
||||
expect(downloaded_urls).to eq([
|
||||
"https://apps.example.com/icon-192.png",
|
||||
"https://apps.example.com/apple-touch-icon.png"
|
||||
])
|
||||
end
|
||||
|
||||
it "attaches the downloaded icons" do
|
||||
described_class.call(app: web_app)
|
||||
|
||||
web_app.reload
|
||||
expect(web_app.icon).to be_attached
|
||||
expect(web_app.apple_touch_icon).to be_attached
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user