summaryrefslogtreecommitdiff
path: root/test/media_proxy_test.exs
blob: 69e9bc9f467805906a1ce656118bc58ec9f2b1e5 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.MediaProxyTest do
  use ExUnit.Case
  import Pleroma.Web.MediaProxy
  alias Pleroma.Web.MediaProxy.MediaProxyController

  setup do
    enabled = Pleroma.Config.get([:media_proxy, :enabled])
    on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end)
    :ok
  end

  describe "when enabled" do
    setup do
      Pleroma.Config.put([:media_proxy, :enabled], true)
      :ok
    end

    test "ignores invalid url" do
      assert url(nil) == nil
      assert url("") == nil
    end

    test "ignores relative url" do
      assert url("/local") == "/local"
      assert url("/") == "/"
    end

    test "ignores local url" do
      local_url = Pleroma.Web.Endpoint.url() <> "/hello"
      local_root = Pleroma.Web.Endpoint.url()
      assert url(local_url) == local_url
      assert url(local_root) == local_root
    end

    test "encodes and decodes URL" do
      url = "https://pleroma.soykaf.com/static/logo.png"
      encoded = url(url)

      assert String.starts_with?(
               encoded,
               Pleroma.Config.get([:media_proxy, :base_url], Pleroma.Web.base_url())
             )

      assert String.ends_with?(encoded, "/logo.png")

      assert decode_result(encoded) == url
    end

    test "encodes and decodes URL without a path" do
      url = "https://pleroma.soykaf.com"
      encoded = url(url)
      assert decode_result(encoded) == url
    end

    test "encodes and decodes URL without an extension" do
      url = "https://pleroma.soykaf.com/path/"
      encoded = url(url)
      assert String.ends_with?(encoded, "/path")
      assert decode_result(encoded) == url
    end

    test "encodes and decodes URL and ignores query params for the path" do
      url = "https://pleroma.soykaf.com/static/logo.png?93939393939&bunny=true"
      encoded = url(url)
      assert String.ends_with?(encoded, "/logo.png")
      assert decode_result(encoded) == url
    end

    test "ensures urls are url-encoded" do
      assert decode_result(url("https://pleroma.social/Hello world.jpg")) ==
               "https://pleroma.social/Hello%20world.jpg"

      assert decode_result(url("https://pleroma.social/Hello%20world.jpg")) ==
               "https://pleroma.social/Hello%20world.jpg"
    end

    test "validates signature" do
      secret_key_base = Pleroma.Config.get([Pleroma.Web.Endpoint, :secret_key_base])

      on_exit(fn ->
        Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], secret_key_base)
      end)

      encoded = url("https://pleroma.social")

      Pleroma.Config.put(
        [Pleroma.Web.Endpoint, :secret_key_base],
        "00000000000000000000000000000000000000000000000"
      )

      [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
      assert decode_url(sig, base64) == {:error, :invalid_signature}
    end

    test "filename_matches preserves the encoded or decoded path" do
      assert MediaProxyController.filename_matches(
               true,
               "/Hello world.jpg",
               "http://pleroma.social/Hello world.jpg"
             ) == :ok

      assert MediaProxyController.filename_matches(
               true,
               "/Hello%20world.jpg",
               "http://pleroma.social/Hello%20world.jpg"
             ) == :ok

      assert MediaProxyController.filename_matches(
               true,
               "/my%2Flong%2Furl%2F2019%2F07%2FS.jpg",
               "http://pleroma.social/my%2Flong%2Furl%2F2019%2F07%2FS.jpg"
             ) == :ok
    end

    test "encoded url are tried to match for proxy as `conn.request_path` encodes the url" do
      # conn.request_path will return encoded url
      request_path = "/ANALYSE-DAI-_-LE-STABLECOIN-100-D%C3%89CENTRALIS%C3%89-BQ.jpg"

      assert MediaProxyController.filename_matches(
               true,
               request_path,
               "https://mydomain.com/uploads/2019/07/ANALYSE-DAI-_-LE-STABLECOIN-100-DÉCENTRALISÉ-BQ.jpg"
             ) == :ok
    end

    test "uses the configured base_url" do
      base_url = Pleroma.Config.get([:media_proxy, :base_url])

      if base_url do
        on_exit(fn ->
          Pleroma.Config.put([:media_proxy, :base_url], base_url)
        end)
      end

      Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")

      url = "https://pleroma.soykaf.com/static/logo.png"
      encoded = url(url)

      assert String.starts_with?(encoded, Pleroma.Config.get([:media_proxy, :base_url]))
    end

    # https://git.pleroma.social/pleroma/pleroma/issues/580
    test "encoding S3 links (must preserve `%2F`)" do
      url =
        "https://s3.amazonaws.com/example/test.png?X-Amz-Credential=your-access-key-id%2F20130721%2Fus-east-1%2Fs3%2Faws4_request"

      encoded = url(url)
      assert decode_result(encoded) == url
    end

    test "does not change whitelisted urls" do
      upload_config = Pleroma.Config.get([Pleroma.Upload])
      media_url = "https://media.pleroma.social"
      Pleroma.Config.put([Pleroma.Upload, :base_url], media_url)
      Pleroma.Config.put([:media_proxy, :whitelist], ["media.pleroma.social"])
      Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")

      url = "#{media_url}/static/logo.png"
      encoded = url(url)

      assert String.starts_with?(encoded, media_url)

      Pleroma.Config.put([Pleroma.Upload], upload_config)
    end
  end

  describe "when disabled" do
    setup do
      enabled = Pleroma.Config.get([:media_proxy, :enabled])

      if enabled do
        Pleroma.Config.put([:media_proxy, :enabled], false)

        on_exit(fn ->
          Pleroma.Config.put([:media_proxy, :enabled], enabled)
          :ok
        end)
      end

      :ok
    end

    test "does not encode remote urls" do
      assert url("https://google.fr") == "https://google.fr"
    end
  end

  defp decode_result(encoded) do
    [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
    {:ok, decoded} = decode_url(sig, base64)
    decoded
  end

  test "mediaproxy whitelist" do
    Pleroma.Config.put([:media_proxy, :enabled], true)
    Pleroma.Config.put([:media_proxy, :whitelist], ["google.com", "feld.me"])
    url = "https://feld.me/foo.png"

    unencoded = url(url)
    assert unencoded == url
  end
end