summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/media_proxy/controller.ex
blob: a711b54e944633c4032a10c6bd5dc6d1abf777e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MediaProxy.MediaProxyController do
  use Pleroma.Web, :controller
  alias Pleroma.ReverseProxy
  alias Pleroma.Web.MediaProxy

  @default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]]

  def remote(conn, %{"sig" => sig64, "url" => url64} = params) do
    with config <- Pleroma.Config.get([:media_proxy], []),
         true <- Keyword.get(config, :enabled, false),
         {:ok, url} <- MediaProxy.decode_url(sig64, url64),
         :ok <- filename_matches(Map.has_key?(params, "filename"), conn.request_path, url) do
      ReverseProxy.call(conn, url, Keyword.get(config, :proxy_opts, @default_proxy_opts))
    else
      false ->
        send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))

      {:error, :invalid_signature} ->
        send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403))

      {:wrong_filename, filename} ->
        redirect(conn, external: MediaProxy.build_url(sig64, url64, filename))
    end
  end

  def filename_matches(has_filename, path, url) do
    filename = url |> MediaProxy.filename()

    if has_filename && filename && does_not_match(path, filename) do
      {:wrong_filename, filename}
    else
      :ok
    end
  end

  defp does_not_match(path, filename) do
    basename = Path.basename(path)
    basename != filename and URI.decode(basename) != filename and URI.encode(basename) != filename
  end
end