2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-11-22 19:06:07 +01:00
|
|
|
defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
|
|
|
use Pleroma.Web, :controller
|
2020-04-21 15:29:19 +02:00
|
|
|
|
2019-02-03 18:44:18 +01:00
|
|
|
alias Pleroma.ReverseProxy
|
|
|
|
alias Pleroma.Web.MediaProxy
|
2017-11-22 19:06:07 +01:00
|
|
|
|
2018-12-07 19:36:44 +01:00
|
|
|
@default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]]
|
2018-11-30 17:44:42 +01:00
|
|
|
|
2019-02-03 18:44:18 +01:00
|
|
|
def remote(conn, %{"sig" => sig64, "url" => url64} = params) do
|
2018-12-02 11:24:02 +01:00
|
|
|
with config <- Pleroma.Config.get([:media_proxy], []),
|
2018-11-23 17:40:45 +01:00
|
|
|
true <- Keyword.get(config, :enabled, false),
|
|
|
|
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
|
2020-06-17 20:13:55 +02:00
|
|
|
{_, false} <- {:in_banned_urls, MediaProxy.in_banned_urls(url)},
|
2019-07-14 23:01:32 +02:00
|
|
|
:ok <- filename_matches(params, conn.request_path, url) do
|
2018-12-02 11:24:02 +01:00
|
|
|
ReverseProxy.call(conn, url, Keyword.get(config, :proxy_opts, @default_proxy_opts))
|
2017-11-28 21:44:25 +01:00
|
|
|
else
|
2020-06-17 20:13:55 +02:00
|
|
|
error when error in [false, {:in_banned_urls, true}] ->
|
2018-11-23 17:40:45 +01:00
|
|
|
send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
|
2018-03-30 15:01:53 +02:00
|
|
|
|
|
|
|
{:error, :invalid_signature} ->
|
2018-11-23 17:40:45 +01:00
|
|
|
send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403))
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2018-11-23 17:40:45 +01:00
|
|
|
{:wrong_filename, filename} ->
|
|
|
|
redirect(conn, external: MediaProxy.build_url(sig64, url64, filename))
|
2017-11-22 19:06:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-14 23:01:32 +02:00
|
|
|
def filename_matches(%{"filename" => _} = _, path, url) do
|
|
|
|
filename = MediaProxy.filename(url)
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2019-07-15 17:45:56 +02:00
|
|
|
if filename && does_not_match(path, filename) do
|
2018-12-10 07:39:57 +01:00
|
|
|
{:wrong_filename, filename}
|
|
|
|
else
|
|
|
|
:ok
|
2017-12-11 02:31:37 +01:00
|
|
|
end
|
|
|
|
end
|
2019-07-12 18:34:30 +02:00
|
|
|
|
2019-07-14 23:01:32 +02:00
|
|
|
def filename_matches(_, _, _), do: :ok
|
2019-07-15 17:45:56 +02:00
|
|
|
|
2019-07-12 18:34:30 +02:00
|
|
|
defp does_not_match(path, filename) do
|
|
|
|
basename = Path.basename(path)
|
|
|
|
basename != filename and URI.decode(basename) != filename and URI.encode(basename) != filename
|
|
|
|
end
|
2017-11-22 19:06:07 +01:00
|
|
|
end
|