Change references from "deleted_urls" to "banned_urls" as nothing is handled via media deletions anymore; all actions are manual operations by an admin to ban the url
This commit is contained in:
parent
c08c9db0c1
commit
2731ea1334
@ -149,7 +149,7 @@ defp cachex_children do
|
|||||||
build_cachex("web_resp", limit: 2500),
|
build_cachex("web_resp", limit: 2500),
|
||||||
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
|
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
|
||||||
build_cachex("failed_proxy_url", limit: 2500),
|
build_cachex("failed_proxy_url", limit: 2500),
|
||||||
build_cachex("deleted_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000)
|
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000)
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
|
|||||||
with uploader <- Keyword.fetch!(config, :uploader),
|
with uploader <- Keyword.fetch!(config, :uploader),
|
||||||
proxy_remote = Keyword.get(config, :proxy_remote, false),
|
proxy_remote = Keyword.get(config, :proxy_remote, false),
|
||||||
{:ok, get_method} <- uploader.get_file(file),
|
{:ok, get_method} <- uploader.get_file(file),
|
||||||
false <- media_is_deleted(conn, get_method) do
|
false <- media_is_banned(conn, get_method) do
|
||||||
get_media(conn, get_method, proxy_remote, opts)
|
get_media(conn, get_method, proxy_remote, opts)
|
||||||
else
|
else
|
||||||
_ ->
|
_ ->
|
||||||
@ -61,13 +61,13 @@ def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
|
|||||||
|
|
||||||
def call(conn, _opts), do: conn
|
def call(conn, _opts), do: conn
|
||||||
|
|
||||||
defp media_is_deleted(%{request_path: path} = _conn, {:static_dir, _}) do
|
defp media_is_banned(%{request_path: path} = _conn, {:static_dir, _}) do
|
||||||
MediaProxy.in_deleted_urls(Pleroma.Web.base_url() <> path)
|
MediaProxy.in_banned_urls(Pleroma.Web.base_url() <> path)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp media_is_deleted(_, {:url, url}), do: MediaProxy.in_deleted_urls(url)
|
defp media_is_banned(_, {:url, url}), do: MediaProxy.in_banned_urls(url)
|
||||||
|
|
||||||
defp media_is_deleted(_, _), do: false
|
defp media_is_banned(_, _), do: false
|
||||||
|
|
||||||
defp get_media(conn, {:static_dir, directory}, _, opts) do
|
defp get_media(conn, {:static_dir, directory}, _, opts) do
|
||||||
static_opts =
|
static_opts =
|
||||||
|
@ -27,7 +27,7 @@ defmodule Pleroma.Web.AdminAPI.MediaProxyCacheController do
|
|||||||
|
|
||||||
def index(%{assigns: %{user: _}} = conn, params) do
|
def index(%{assigns: %{user: _}} = conn, params) do
|
||||||
cursor =
|
cursor =
|
||||||
:deleted_urls_cache
|
:banned_urls_cache
|
||||||
|> :ets.table([{:traverse, {:select, Cachex.Query.create(true, :key)}}])
|
|> :ets.table([{:traverse, {:select, Cachex.Query.create(true, :key)}}])
|
||||||
|> :qlc.cursor()
|
|> :qlc.cursor()
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ def index(%{assigns: %{user: _}} = conn, params) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def delete(%{assigns: %{user: _}, body_params: %{urls: urls}} = conn, _) do
|
def delete(%{assigns: %{user: _}, body_params: %{urls: urls}} = conn, _) do
|
||||||
MediaProxy.remove_from_deleted_urls(urls)
|
MediaProxy.remove_from_banned_urls(urls)
|
||||||
render(conn, "index.json", urls: urls)
|
render(conn, "index.json", urls: urls)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ def purge(%{assigns: %{user: _}, body_params: %{urls: urls, ban: ban}} = conn, _
|
|||||||
MediaProxy.Invalidation.purge(urls)
|
MediaProxy.Invalidation.purge(urls)
|
||||||
|
|
||||||
if ban do
|
if ban do
|
||||||
MediaProxy.put_in_deleted_urls(urls)
|
MediaProxy.put_in_banned_urls(urls)
|
||||||
end
|
end
|
||||||
|
|
||||||
render(conn, "index.json", urls: urls)
|
render(conn, "index.json", urls: urls)
|
||||||
|
@ -10,27 +10,27 @@ defmodule Pleroma.Web.MediaProxy do
|
|||||||
|
|
||||||
@base64_opts [padding: false]
|
@base64_opts [padding: false]
|
||||||
|
|
||||||
@spec in_deleted_urls(String.t()) :: boolean()
|
@spec in_banned_urls(String.t()) :: boolean()
|
||||||
def in_deleted_urls(url), do: elem(Cachex.exists?(:deleted_urls_cache, url(url)), 1)
|
def in_banned_urls(url), do: elem(Cachex.exists?(:banned_urls_cache, url(url)), 1)
|
||||||
|
|
||||||
def remove_from_deleted_urls(urls) when is_list(urls) do
|
def remove_from_banned_urls(urls) when is_list(urls) do
|
||||||
Cachex.execute!(:deleted_urls_cache, fn cache ->
|
Cachex.execute!(:banned_urls_cache, fn cache ->
|
||||||
Enum.each(Invalidation.prepare_urls(urls), &Cachex.del(cache, &1))
|
Enum.each(Invalidation.prepare_urls(urls), &Cachex.del(cache, &1))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_from_deleted_urls(url) when is_binary(url) do
|
def remove_from_banned_urls(url) when is_binary(url) do
|
||||||
Cachex.del(:deleted_urls_cache, url(url))
|
Cachex.del(:banned_urls_cache, url(url))
|
||||||
end
|
end
|
||||||
|
|
||||||
def put_in_deleted_urls(urls) when is_list(urls) do
|
def put_in_banned_urls(urls) when is_list(urls) do
|
||||||
Cachex.execute!(:deleted_urls_cache, fn cache ->
|
Cachex.execute!(:banned_urls_cache, fn cache ->
|
||||||
Enum.each(Invalidation.prepare_urls(urls), &Cachex.put(cache, &1, true))
|
Enum.each(Invalidation.prepare_urls(urls), &Cachex.put(cache, &1, true))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def put_in_deleted_urls(url) when is_binary(url) do
|
def put_in_banned_urls(url) when is_binary(url) do
|
||||||
Cachex.put(:deleted_urls_cache, url(url), true)
|
Cachex.put(:banned_urls_cache, url(url), true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def url(url) when is_nil(url) or url == "", do: nil
|
def url(url) when is_nil(url) or url == "", do: nil
|
||||||
|
@ -14,11 +14,11 @@ def remote(conn, %{"sig" => sig64, "url" => url64} = params) do
|
|||||||
with config <- Pleroma.Config.get([:media_proxy], []),
|
with config <- Pleroma.Config.get([:media_proxy], []),
|
||||||
true <- Keyword.get(config, :enabled, false),
|
true <- Keyword.get(config, :enabled, false),
|
||||||
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
|
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
|
||||||
{_, false} <- {:in_deleted_urls, MediaProxy.in_deleted_urls(url)},
|
{_, false} <- {:in_banned_urls, MediaProxy.in_banned_urls(url)},
|
||||||
:ok <- filename_matches(params, conn.request_path, url) do
|
:ok <- filename_matches(params, conn.request_path, url) do
|
||||||
ReverseProxy.call(conn, url, Keyword.get(config, :proxy_opts, @default_proxy_opts))
|
ReverseProxy.call(conn, url, Keyword.get(config, :proxy_opts, @default_proxy_opts))
|
||||||
else
|
else
|
||||||
error when error in [false, {:in_deleted_urls, true}] ->
|
error when error in [false, {:in_banned_urls, true}] ->
|
||||||
send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
|
send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
|
||||||
|
|
||||||
{:error, :invalid_signature} ->
|
{:error, :invalid_signature} ->
|
||||||
|
@ -13,7 +13,7 @@ defmodule Pleroma.Web.AdminAPI.MediaProxyCacheControllerTest do
|
|||||||
setup do: clear_config([:media_proxy])
|
setup do: clear_config([:media_proxy])
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
on_exit(fn -> Cachex.clear(:deleted_urls_cache) end)
|
on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
@ -34,14 +34,14 @@ defmodule Pleroma.Web.AdminAPI.MediaProxyCacheControllerTest do
|
|||||||
|
|
||||||
describe "GET /api/pleroma/admin/media_proxy_caches" do
|
describe "GET /api/pleroma/admin/media_proxy_caches" do
|
||||||
test "shows banned MediaProxy URLs", %{conn: conn} do
|
test "shows banned MediaProxy URLs", %{conn: conn} do
|
||||||
MediaProxy.put_in_deleted_urls([
|
MediaProxy.put_in_banned_urls([
|
||||||
"http://localhost:4001/media/a688346.jpg",
|
"http://localhost:4001/media/a688346.jpg",
|
||||||
"http://localhost:4001/media/fb1f4d.jpg"
|
"http://localhost:4001/media/fb1f4d.jpg"
|
||||||
])
|
])
|
||||||
|
|
||||||
MediaProxy.put_in_deleted_urls("http://localhost:4001/media/gb1f44.jpg")
|
MediaProxy.put_in_banned_urls("http://localhost:4001/media/gb1f44.jpg")
|
||||||
MediaProxy.put_in_deleted_urls("http://localhost:4001/media/tb13f47.jpg")
|
MediaProxy.put_in_banned_urls("http://localhost:4001/media/tb13f47.jpg")
|
||||||
MediaProxy.put_in_deleted_urls("http://localhost:4001/media/wb1f46.jpg")
|
MediaProxy.put_in_banned_urls("http://localhost:4001/media/wb1f46.jpg")
|
||||||
|
|
||||||
response =
|
response =
|
||||||
conn
|
conn
|
||||||
@ -74,7 +74,7 @@ test "shows banned MediaProxy URLs", %{conn: conn} do
|
|||||||
|
|
||||||
describe "POST /api/pleroma/admin/media_proxy_caches/delete" do
|
describe "POST /api/pleroma/admin/media_proxy_caches/delete" do
|
||||||
test "deleted MediaProxy URLs from banned", %{conn: conn} do
|
test "deleted MediaProxy URLs from banned", %{conn: conn} do
|
||||||
MediaProxy.put_in_deleted_urls([
|
MediaProxy.put_in_banned_urls([
|
||||||
"http://localhost:4001/media/a688346.jpg",
|
"http://localhost:4001/media/a688346.jpg",
|
||||||
"http://localhost:4001/media/fb1f4d.jpg"
|
"http://localhost:4001/media/fb1f4d.jpg"
|
||||||
])
|
])
|
||||||
@ -88,8 +88,8 @@ test "deleted MediaProxy URLs from banned", %{conn: conn} do
|
|||||||
|> json_response_and_validate_schema(200)
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
assert response["urls"] == ["http://localhost:4001/media/a688346.jpg"]
|
assert response["urls"] == ["http://localhost:4001/media/a688346.jpg"]
|
||||||
refute MediaProxy.in_deleted_urls("http://localhost:4001/media/a688346.jpg")
|
refute MediaProxy.in_banned_urls("http://localhost:4001/media/a688346.jpg")
|
||||||
assert MediaProxy.in_deleted_urls("http://localhost:4001/media/fb1f4d.jpg")
|
assert MediaProxy.in_banned_urls("http://localhost:4001/media/fb1f4d.jpg")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -114,8 +114,8 @@ test "perform invalidates cache of MediaProxy", %{conn: conn} do
|
|||||||
|
|
||||||
assert response["urls"] == urls
|
assert response["urls"] == urls
|
||||||
|
|
||||||
refute MediaProxy.in_deleted_urls("http://example.com/media/a688346.jpg")
|
refute MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
|
||||||
refute MediaProxy.in_deleted_urls("http://example.com/media/fb1f4d.jpg")
|
refute MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -137,8 +137,8 @@ test "perform invalidates cache of MediaProxy and adds url to banned", %{conn: c
|
|||||||
|
|
||||||
assert response["urls"] == urls
|
assert response["urls"] == urls
|
||||||
|
|
||||||
assert MediaProxy.in_deleted_urls("http://example.com/media/a688346.jpg")
|
assert MediaProxy.in_banned_urls("http://example.com/media/a688346.jpg")
|
||||||
assert MediaProxy.in_deleted_urls("http://example.com/media/fb1f4d.jpg")
|
assert MediaProxy.in_banned_urls("http://example.com/media/fb1f4d.jpg")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -12,7 +12,7 @@ defmodule Pleroma.Web.MediaProxy.InvalidationTest do
|
|||||||
setup do: clear_config([:media_proxy])
|
setup do: clear_config([:media_proxy])
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
on_exit(fn -> Cachex.clear(:deleted_urls_cache) end)
|
on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "Invalidation.Http" do
|
describe "Invalidation.Http" do
|
||||||
@ -23,7 +23,7 @@ test "perform request to clear cache" do
|
|||||||
|
|
||||||
Config.put([Invalidation.Http], method: :purge, headers: [{"x-refresh", 1}])
|
Config.put([Invalidation.Http], method: :purge, headers: [{"x-refresh", 1}])
|
||||||
image_url = "http://example.com/media/example.jpg"
|
image_url = "http://example.com/media/example.jpg"
|
||||||
Pleroma.Web.MediaProxy.put_in_deleted_urls(image_url)
|
Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
|
||||||
|
|
||||||
mock(fn
|
mock(fn
|
||||||
%{
|
%{
|
||||||
@ -35,9 +35,9 @@ test "perform request to clear cache" do
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
assert capture_log(fn ->
|
assert capture_log(fn ->
|
||||||
assert Pleroma.Web.MediaProxy.in_deleted_urls(image_url)
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
||||||
assert Invalidation.purge([image_url]) == {:ok, [image_url]}
|
assert Invalidation.purge([image_url]) == {:ok, [image_url]}
|
||||||
assert Pleroma.Web.MediaProxy.in_deleted_urls(image_url)
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
||||||
end) =~ "Running cache purge: [\"#{image_url}\"]"
|
end) =~ "Running cache purge: [\"#{image_url}\"]"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -50,13 +50,13 @@ test "run script to clear cache" do
|
|||||||
Config.put([Invalidation.Script], script_path: "purge-nginx")
|
Config.put([Invalidation.Script], script_path: "purge-nginx")
|
||||||
|
|
||||||
image_url = "http://example.com/media/example.jpg"
|
image_url = "http://example.com/media/example.jpg"
|
||||||
Pleroma.Web.MediaProxy.put_in_deleted_urls(image_url)
|
Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
|
||||||
|
|
||||||
with_mocks [{System, [], [cmd: fn _, _ -> {"ok", 0} end]}] do
|
with_mocks [{System, [], [cmd: fn _, _ -> {"ok", 0} end]}] do
|
||||||
assert capture_log(fn ->
|
assert capture_log(fn ->
|
||||||
assert Pleroma.Web.MediaProxy.in_deleted_urls(image_url)
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
||||||
assert Invalidation.purge([image_url]) == {:ok, [image_url]}
|
assert Invalidation.purge([image_url]) == {:ok, [image_url]}
|
||||||
assert Pleroma.Web.MediaProxy.in_deleted_urls(image_url)
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
||||||
end) =~ "Running cache purge: [\"#{image_url}\"]"
|
end) =~ "Running cache purge: [\"#{image_url}\"]"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -11,7 +11,7 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do
|
|||||||
setup do: clear_config([Pleroma.Web.Endpoint, :secret_key_base])
|
setup do: clear_config([Pleroma.Web.Endpoint, :secret_key_base])
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
on_exit(fn -> Cachex.clear(:deleted_urls_cache) end)
|
on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it returns 404 when MediaProxy disabled", %{conn: conn} do
|
test "it returns 404 when MediaProxy disabled", %{conn: conn} do
|
||||||
@ -71,11 +71,11 @@ test "it performs ReverseProxy.call when signature valid", %{conn: conn} do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it returns 404 when url contains in deleted_urls cache", %{conn: conn} do
|
test "it returns 404 when url contains in banned_urls cache", %{conn: conn} do
|
||||||
Config.put([:media_proxy, :enabled], true)
|
Config.put([:media_proxy, :enabled], true)
|
||||||
Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
|
Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000")
|
||||||
url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
|
url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png")
|
||||||
Pleroma.Web.MediaProxy.put_in_deleted_urls("https://google.fn/test.png")
|
Pleroma.Web.MediaProxy.put_in_banned_urls("https://google.fn/test.png")
|
||||||
|
|
||||||
with_mock Pleroma.ReverseProxy,
|
with_mock Pleroma.ReverseProxy,
|
||||||
call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
|
call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do
|
||||||
|
Loading…
Reference in New Issue
Block a user