2022-07-03 18:20:59 +00:00
|
|
|
defmodule Akkoma.Collections.FetcherTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
use Oban.Testing, repo: Pleroma.Repo
|
|
|
|
|
|
|
|
alias Akkoma.Collections.Fetcher
|
|
|
|
|
|
|
|
import Tesla.Mock
|
|
|
|
|
|
|
|
setup do
|
|
|
|
mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it should extract items from an embedded array in a Collection" do
|
Only allow exact id matches
This protects us from falling for obvious spoofs as from the current
upload exploit (unfortunately we can’t reasonably do anything about
spoofs with exact matches as was possible via emoji and proxy).
Such objects being invalid is supported by the spec, sepcifically
sections 3.1 and 3.2: https://www.w3.org/TR/activitypub/#obj-id
Anonymous objects are not relevant here (they can only exists within
parent objects iiuc) and neither is client-to-server or transient objects
(as those cannot be fetched in the first place).
This leaves us with the requirement for `id` to (a) exist and
(b) be a publicly dereferencable URI from the originating server.
This alone does not yet demand strict equivalence, but the spec then
further explains objects ought to be fetchable _via their ID_.
Meaning an object not retrievable via its ID, is invalid.
This reading is supported by the fact, e.g. GoToSocial (recently) and
Mastodon (for 6+ years) do already implement such strict ID checks,
additionally proving this doesn’t cause federation issues in practice.
However, apart from canonical IDs there can also be additional display
URLs. *omas first redirect those to their canonical location, but *keys
and Mastodon directly serve the AP representation without redirects.
Mastodon and GTS deal with this in two different ways,
but both constitute an effective countermeasure:
- Mastodon:
Unless it already is a known AP id, two fetches occur.
The first fetch just reads the `id` property and then refetches from
the id. The last fetch requires the returned id to exactly match the
URL the content was fetched from. (This can be optimised by skipping
the second fetch if it already matches)
https://github.com/mastodon/mastodon/blob/05eda8d19330a9c27c0cf07de19a87edff269057/app/helpers/jsonld_helper.rb#L168
https://github.com/mastodon/mastodon/commit/63f097979990bf5ba9db848b8a253056bad781af
- GTS:
Only does a single fetch and then checks if _either_ the id
_or_ url property (which can be an object) match the original fetch
URL. This relies on implementations always including their display URL
as "url" if differing from the id. For actors this is true for all
investigated implementations, for posts only Mastodon includes an
"url", but it is also the only one with a differing display URL.
https://github.com/superseriousbusiness/gotosocial/commit/2bafd7daf542d985ee76d9079a30a602cb7be827#diff-943bbb02c8ac74ac5dc5d20807e561dcdfaebdc3b62b10730f643a20ac23c24fR222
Albeit Mastodon’s refetch offers higher compatibility with theoretical
implmentations using either multiple different display URL or not
denoting any of them as "url" at all, for now we chose to adopt a
GTS-like refetch-free approach to avoid additional implementation
concerns wrt to whether redirects should be allowed when fetching a
canonical AP id and potential for accidentally loosening some checks
(e.g. cross-domain refetches) for one of the fetches.
This may be reconsidered in the future.
2024-03-16 00:00:19 +00:00
|
|
|
ap_id = "https://example.com/collection/ordered_array"
|
|
|
|
|
2022-07-03 18:20:59 +00:00
|
|
|
unordered_collection =
|
|
|
|
"test/fixtures/collections/unordered_array.json"
|
|
|
|
|> File.read!()
|
Only allow exact id matches
This protects us from falling for obvious spoofs as from the current
upload exploit (unfortunately we can’t reasonably do anything about
spoofs with exact matches as was possible via emoji and proxy).
Such objects being invalid is supported by the spec, sepcifically
sections 3.1 and 3.2: https://www.w3.org/TR/activitypub/#obj-id
Anonymous objects are not relevant here (they can only exists within
parent objects iiuc) and neither is client-to-server or transient objects
(as those cannot be fetched in the first place).
This leaves us with the requirement for `id` to (a) exist and
(b) be a publicly dereferencable URI from the originating server.
This alone does not yet demand strict equivalence, but the spec then
further explains objects ought to be fetchable _via their ID_.
Meaning an object not retrievable via its ID, is invalid.
This reading is supported by the fact, e.g. GoToSocial (recently) and
Mastodon (for 6+ years) do already implement such strict ID checks,
additionally proving this doesn’t cause federation issues in practice.
However, apart from canonical IDs there can also be additional display
URLs. *omas first redirect those to their canonical location, but *keys
and Mastodon directly serve the AP representation without redirects.
Mastodon and GTS deal with this in two different ways,
but both constitute an effective countermeasure:
- Mastodon:
Unless it already is a known AP id, two fetches occur.
The first fetch just reads the `id` property and then refetches from
the id. The last fetch requires the returned id to exactly match the
URL the content was fetched from. (This can be optimised by skipping
the second fetch if it already matches)
https://github.com/mastodon/mastodon/blob/05eda8d19330a9c27c0cf07de19a87edff269057/app/helpers/jsonld_helper.rb#L168
https://github.com/mastodon/mastodon/commit/63f097979990bf5ba9db848b8a253056bad781af
- GTS:
Only does a single fetch and then checks if _either_ the id
_or_ url property (which can be an object) match the original fetch
URL. This relies on implementations always including their display URL
as "url" if differing from the id. For actors this is true for all
investigated implementations, for posts only Mastodon includes an
"url", but it is also the only one with a differing display URL.
https://github.com/superseriousbusiness/gotosocial/commit/2bafd7daf542d985ee76d9079a30a602cb7be827#diff-943bbb02c8ac74ac5dc5d20807e561dcdfaebdc3b62b10730f643a20ac23c24fR222
Albeit Mastodon’s refetch offers higher compatibility with theoretical
implmentations using either multiple different display URL or not
denoting any of them as "url" at all, for now we chose to adopt a
GTS-like refetch-free approach to avoid additional implementation
concerns wrt to whether redirects should be allowed when fetching a
canonical AP id and potential for accidentally loosening some checks
(e.g. cross-domain refetches) for one of the fetches.
This may be reconsidered in the future.
2024-03-16 00:00:19 +00:00
|
|
|
|> Jason.decode!()
|
|
|
|
|> Map.put("id", ap_id)
|
|
|
|
|> Jason.encode!(pretty: true)
|
2022-07-03 18:20:59 +00:00
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^ap_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: unordered_collection,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2022-08-27 18:05:48 +00:00
|
|
|
{:ok, objects} = Fetcher.fetch_collection(ap_id)
|
2022-07-03 18:20:59 +00:00
|
|
|
assert [%{"type" => "Create"}, %{"type" => "Like"}] = objects
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it should extract items from an embedded array in an OrderedCollection" do
|
|
|
|
ordered_collection =
|
|
|
|
"test/fixtures/collections/ordered_array.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
ap_id = "https://example.com/collection/ordered_array"
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^ap_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: ordered_collection,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2022-08-27 18:05:48 +00:00
|
|
|
{:ok, objects} = Fetcher.fetch_collection(ap_id)
|
2022-07-03 18:20:59 +00:00
|
|
|
assert [%{"type" => "Create"}, %{"type" => "Like"}] = objects
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it should extract items from an referenced first page in a Collection" do
|
|
|
|
unordered_collection =
|
|
|
|
"test/fixtures/collections/unordered_page_reference.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
first_page =
|
|
|
|
"test/fixtures/collections/unordered_page_first.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
second_page =
|
|
|
|
"test/fixtures/collections/unordered_page_second.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
ap_id = "https://example.com/collection/unordered_page_reference"
|
|
|
|
first_page_id = "https://example.com/collection/unordered_page_reference?page=1"
|
|
|
|
second_page_id = "https://example.com/collection/unordered_page_reference?page=2"
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^ap_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: unordered_collection,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^first_page_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: first_page,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^second_page_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: second_page,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2022-08-27 18:05:48 +00:00
|
|
|
{:ok, objects} = Fetcher.fetch_collection(ap_id)
|
2022-07-03 18:20:59 +00:00
|
|
|
assert [%{"type" => "Create"}, %{"type" => "Like"}] = objects
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it should stop fetching when we hit :max_collection_objects" do
|
|
|
|
clear_config([:activitypub, :max_collection_objects], 1)
|
|
|
|
|
|
|
|
unordered_collection =
|
|
|
|
"test/fixtures/collections/unordered_page_reference.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
first_page =
|
|
|
|
"test/fixtures/collections/unordered_page_first.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
second_page =
|
|
|
|
"test/fixtures/collections/unordered_page_second.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
ap_id = "https://example.com/collection/unordered_page_reference"
|
|
|
|
first_page_id = "https://example.com/collection/unordered_page_reference?page=1"
|
|
|
|
second_page_id = "https://example.com/collection/unordered_page_reference?page=2"
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^ap_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: unordered_collection,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^first_page_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: first_page,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^second_page_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: second_page,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2022-08-27 18:05:48 +00:00
|
|
|
{:ok, objects} = Fetcher.fetch_collection(ap_id)
|
|
|
|
assert [%{"type" => "Create"}] = objects
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it should stop fetching when we hit a 404" do
|
|
|
|
clear_config([:activitypub, :max_collection_objects], 1)
|
|
|
|
|
|
|
|
unordered_collection =
|
|
|
|
"test/fixtures/collections/unordered_page_reference.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
first_page =
|
|
|
|
"test/fixtures/collections/unordered_page_first.json"
|
|
|
|
|> File.read!()
|
|
|
|
|
|
|
|
ap_id = "https://example.com/collection/unordered_page_reference"
|
|
|
|
first_page_id = "https://example.com/collection/unordered_page_reference?page=1"
|
|
|
|
second_page_id = "https://example.com/collection/unordered_page_reference?page=2"
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^ap_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: unordered_collection,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^first_page_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body: first_page,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
|
|
|
|
%{
|
|
|
|
method: :get,
|
|
|
|
url: ^second_page_id
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 404,
|
|
|
|
body: nil,
|
|
|
|
headers: [{"content-type", "application/activity+json"}]
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, objects} = Fetcher.fetch_collection(ap_id)
|
2022-07-03 18:20:59 +00:00
|
|
|
assert [%{"type" => "Create"}] = objects
|
|
|
|
end
|
|
|
|
end
|