Merge branch 'bugfix/favicon-too-long' into 'develop'
instance: Drop favicon URLs longer than 255 characters Closes #2116 See merge request pleroma/pleroma!2949
This commit is contained in:
commit
d5c286b802
2 changed files with 45 additions and 1 deletions
|
@ -14,6 +14,8 @@ defmodule Pleroma.Instances.Instance do
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
schema "instances" do
|
schema "instances" do
|
||||||
field(:host, :string)
|
field(:host, :string)
|
||||||
field(:unreachable_since, :naive_datetime_usec)
|
field(:unreachable_since, :naive_datetime_usec)
|
||||||
|
@ -145,6 +147,10 @@ def get_or_update_favicon(%URI{host: host} = instance_uri) do
|
||||||
|
|
||||||
favicon
|
favicon
|
||||||
end
|
end
|
||||||
|
rescue
|
||||||
|
e ->
|
||||||
|
Logger.warn("Instance.get_or_update_favicon(\"#{host}\") error: #{inspect(e)}")
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
defp scrape_favicon(%URI{} = instance_uri) do
|
defp scrape_favicon(%URI{} = instance_uri) do
|
||||||
|
@ -165,7 +171,12 @@ defp scrape_favicon(%URI{} = instance_uri) do
|
||||||
_ -> nil
|
_ -> nil
|
||||||
end
|
end
|
||||||
rescue
|
rescue
|
||||||
_ -> nil
|
e ->
|
||||||
|
Logger.warn(
|
||||||
|
"Instance.scrape_favicon(\"#{to_string(instance_uri)}\") error: #{inspect(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,6 +8,7 @@ defmodule Pleroma.Instances.InstanceTest do
|
||||||
|
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
import ExUnit.CaptureLog
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
|
|
||||||
setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
|
setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
|
||||||
|
@ -97,4 +98,36 @@ test "does NOT modify `unreachable_since` value of existing record in case it's
|
||||||
assert initial_value == instance.unreachable_since
|
assert initial_value == instance.unreachable_since
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "Scrapes favicon URLs" do
|
||||||
|
Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
|
||||||
|
%Tesla.Env{
|
||||||
|
status: 200,
|
||||||
|
body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert "https://favicon.example.org/favicon.png" ==
|
||||||
|
Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
|
||||||
|
end
|
||||||
|
|
||||||
|
test "Returns nil on too long favicon URLs" do
|
||||||
|
long_favicon_url =
|
||||||
|
"https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
|
||||||
|
|
||||||
|
Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
|
||||||
|
%Tesla.Env{
|
||||||
|
status: 200,
|
||||||
|
body: ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert capture_log(fn ->
|
||||||
|
assert nil ==
|
||||||
|
Instance.get_or_update_favicon(
|
||||||
|
URI.parse("https://long-favicon.example.org/")
|
||||||
|
)
|
||||||
|
end) =~
|
||||||
|
"Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue