URL encode remote emoji pack names (#362)

fix #246

Co-authored-by: Sol Fisher Romanoff <sol@solfisher.com>
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/362
Co-authored-by: sfr <sol@solfisher.com>
Co-committed-by: sfr <sol@solfisher.com>
This commit is contained in:
sfr 2023-01-15 18:14:04 +00:00 committed by floatingghost
parent 0fb2042f2c
commit 20cd8a0fc4
5 changed files with 52 additions and 8 deletions

View file

@ -252,7 +252,7 @@ defmodule Pleroma.Emoji.Pack do
with :ok <- validate_shareable_packs_available(uri), with :ok <- validate_shareable_packs_available(uri),
{:ok, remote_pack} <- {:ok, remote_pack} <-
uri |> URI.merge("/api/v1/pleroma/emoji/pack?name=#{name}") |> http_get(), uri |> URI.merge("/api/v1/pleroma/emoji/pack?name=#{URI.encode(name)}") |> http_get(),
{:ok, %{sha: sha, url: url} = pack_info} <- fetch_pack_info(remote_pack, uri, name), {:ok, %{sha: sha, url: url} = pack_info} <- fetch_pack_info(remote_pack, uri, name),
{:ok, archive} <- download_archive(url, sha), {:ok, archive} <- download_archive(url, sha),
pack <- copy_as(remote_pack, as || name), pack <- copy_as(remote_pack, as || name),
@ -593,7 +593,9 @@ defmodule Pleroma.Emoji.Pack do
{:ok, {:ok,
%{ %{
sha: sha, sha: sha,
url: URI.merge(uri, "/api/v1/pleroma/emoji/packs/archive?name=#{name}") |> to_string() url:
URI.merge(uri, "/api/v1/pleroma/emoji/packs/archive?name=#{URI.encode(name)}")
|> to_string()
}} }}
%{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) -> %{"fallback-src" => src, "fallback-src-sha256" => sha} when is_binary(src) ->

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 B

View file

@ -0,0 +1,12 @@
{
"files": {
"blank": "blank.png",
"blank2": "blank2.png"
},
"pack": {
"description": "Test description",
"homepage": "https://pleroma.social",
"license": "Test license",
"share-files": true
}
}

View file

@ -40,11 +40,11 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/v1/pleroma/emoji/packs") |> get("/api/v1/pleroma/emoji/packs")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 5
assert resp["packs"] assert resp["packs"]
|> Map.keys() |> Map.keys()
|> length() == 4 |> length() == 5
shared = resp["packs"]["test_pack"] shared = resp["packs"]["test_pack"]
assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"} assert shared["files"] == %{"blank" => "blank.png", "blank2" => "blank2.png"}
@ -61,7 +61,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/v1/pleroma/emoji/packs?page_size=1") |> get("/api/v1/pleroma/emoji/packs?page_size=1")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 5
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
@ -74,7 +74,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/v1/pleroma/emoji/packs?page_size=1&page=2") |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=2")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 5
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
assert length(packs) == 1 assert length(packs) == 1
[pack2] = packs [pack2] = packs
@ -84,7 +84,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/v1/pleroma/emoji/packs?page_size=1&page=3") |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=3")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 5
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
assert length(packs) == 1 assert length(packs) == 1
[pack3] = packs [pack3] = packs
@ -94,7 +94,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> get("/api/v1/pleroma/emoji/packs?page_size=1&page=4") |> get("/api/v1/pleroma/emoji/packs?page_size=1&page=4")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
assert resp["count"] == 4 assert resp["count"] == 5
packs = Map.keys(resp["packs"]) packs = Map.keys(resp["packs"])
assert length(packs) == 1 assert length(packs) == 1
[pack4] = packs [pack4] = packs
@ -221,6 +221,24 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
url: "https://nonshared-pack" url: "https://nonshared-pack"
} -> } ->
text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip")) text(File.read!("#{@emoji_path}/test_pack_nonshared/nonshared.zip"))
%{
method: :get,
url: "https://example.com/api/v1/pleroma/emoji/pack?name=test%20with%20spaces"
} ->
conn
|> get("/api/v1/pleroma/emoji/pack?name=test%20with%20spaces")
|> json_response_and_validate_schema(200)
|> json()
%{
method: :get,
url: "https://example.com/api/v1/pleroma/emoji/packs/archive?name=test%20with%20spaces"
} ->
conn
|> get("/api/v1/pleroma/emoji/packs/archive?name=test%20with%20spaces")
|> response(200)
|> text()
end) end)
assert admin_conn assert admin_conn
@ -261,6 +279,18 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do
|> json_response_and_validate_schema(200) == "ok" |> json_response_and_validate_schema(200) == "ok"
refute File.exists?("#{@emoji_path}/test_pack_nonshared2") refute File.exists?("#{@emoji_path}/test_pack_nonshared2")
assert admin_conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/pleroma/emoji/packs/download", %{
url: "https://example.com",
name: "test with spaces",
as: "test with spaces"
})
|> json_response_and_validate_schema(200) == "ok"
assert File.exists?("#{@emoji_path}/test with spaces/pack.json")
assert File.exists?("#{@emoji_path}/test with spaces/blank.png")
end end
test "nonshareable instance", %{admin_conn: admin_conn} do test "nonshareable instance", %{admin_conn: admin_conn} do