/
boundaries_mrf.ex
605 lines (530 loc) · 15.7 KB
/
boundaries_mrf.ex
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
defmodule Bonfire.Federate.ActivityPub.BoundariesMRF do
@moduledoc "Filter activities depending on their origin instance, actor, or other criteria"
use Bonfire.Common.Utils
use Arrows
require ActivityPub.Config
import Untangle
alias ActivityPub.MRF
alias Bonfire.Boundaries
alias Bonfire.Federate.ActivityPub.AdapterUtils
@behaviour MRF
@impl true
def filter(activity, is_local?) do
info(activity, "to filter")
authors =
AdapterUtils.all_actors(activity)
|> debug("authors")
local_author_ids =
authors
|> AdapterUtils.local_actor_ids()
|> debug("local_author_ids")
recipients = AdapterUtils.all_recipients(activity)
local_recipient_ids =
recipients
|> AdapterUtils.local_actor_ids()
|> debug("local_recipient_ids")
# num_local_authors = length(local_author_ids)
# = (num_local_authors && num_local_authors == length(authors))
debug(is_local?, "is_local?")
with {:ok, activity} <-
maybe_check_and_filter(
:ghost,
is_local?,
local_author_ids,
local_recipient_ids,
activity
),
{:ok, activity} <-
maybe_check_and_filter(
:silence,
is_local?,
local_author_ids,
local_recipient_ids,
activity
) do
{:ok, activity}
|> debug("Boundary check OK!")
else
{:reject, e} ->
warn(e, "Activity rejected by Boundaries")
{:reject, e}
{:error, e} ->
warn(e, "Activity rejected by Boundaries")
{:reject, e}
e ->
warn(e, "Activity rejected by Boundaries")
{:reject, e}
end
end
defp maybe_check_and_filter(
check_block_type,
is_local?,
local_author_ids,
local_recipient_ids,
activity
) do
block_types = Boundaries.Blocks.types_blocked(check_block_type)
is_follow? = AdapterUtils.is_follow?(activity)
rejects = rejects_regex(block_types)
cond do
is_follow? and :silence == check_block_type and is_local? ->
debug("do not follow silenced remote actors")
# block_or_filter_recipients(block_types, activity, local_author_ids, is_local?)
# activity_blocked?(block_types, local_author_ids, activity, rejects) ||
object_blocked?(
block_types,
local_author_ids,
rejects,
AdapterUtils.id_or_object_id(e(activity, "object", nil))
)
|> debug("object_blocked") || {:ok, activity}
is_follow? and :silence == check_block_type and !is_local? ->
debug("accept follows from silenced remote actors")
{:ok, activity}
is_follow? and :ghost == check_block_type and !is_local? ->
debug("reject follows from ghosted remote actors")
activity_blocked?(block_types, local_recipient_ids, activity, rejects) || {:ok, activity}
:silence == check_block_type and is_local? ->
debug("do nothing with silencing on outgoing local activities")
{:ok, activity}
:silence == check_block_type and !is_local? ->
debug(
"check for silenced actor/instances & filter recipients of incoming remote activities"
)
activity_blocked?(block_types, [], activity, rejects) ||
block_or_filter_recipients(
block_types,
activity,
local_author_ids,
local_recipient_ids,
is_local?
)
:ghost == check_block_type and is_local? ->
debug("filter ghosted recipients of outgoing local activities")
block_or_filter_recipients(
block_types,
activity,
local_author_ids,
local_recipient_ids,
is_local?
)
:ghost == check_block_type and !is_local? ->
debug("do nothing with ghosting on incoming remote activities")
{:ok, activity}
true ->
error("no filter cond matched")
end
end
defp activity_blocked?(block_types, local_character_ids, activity, rejects) do
object_blocked?(
block_types,
local_character_ids,
rejects,
AdapterUtils.id_or_object_id(e(activity, "actor", nil))
)
|> debug("activity's actor?") ||
object_blocked?(
block_types,
local_character_ids,
rejects,
AdapterUtils.id_or_object_id(e(activity, "object", "attributedTo", nil))
)
|> debug("object's actor?") ||
object_blocked?(
block_types,
local_character_ids,
rejects,
AdapterUtils.id_or_object_id(activity)
)
|> debug("activity's instance?") ||
object_blocked?(
block_types,
local_character_ids,
rejects,
AdapterUtils.id_or_object_id(e(activity, "object", nil))
)
|> debug("object or its instance?")
end
defp object_blocked?(block_types, local_author_ids, rejects, canonical_uri)
when is_binary(canonical_uri) do
uri = URI.parse(canonical_uri)
# |> debug("uri")
instance_blocked_in_config?(uri, rejects) ||
actor_or_instance_blocked?(block_types, local_author_ids, uri, nil, rejects)
end
defp object_blocked?(block_types, local_author_ids, rejects, %{} = object) do
canonical_uri = URIs.canonical_url(object)
uri = URI.parse(canonical_uri)
instance_blocked_in_config?(uri, rejects) ||
actor_or_instance_blocked?(block_types, local_author_ids, uri, object, rejects)
end
defp object_blocked?(_, _, _, canonical_uri) do
warn(canonical_uri, "no valid URI")
# raise "no valid URI"
nil
end
defp instance_blocked_in_config?(%{host: actor_host} = _actor_uri, rejects) do
MRF.subdomain_match?(rejects, actor_host)
# || Bonfire.Federate.ActivityPub.Instances.is_blocked?(actor_uri, :any, :instance_wide)
# ^ NOTE: no need to check the instance block in DB here because that's being handled by Peered.is_blocked? triggered by Actors.is_blocked? via actor_or_instance_blocked?
end
defp actor_or_instance_blocked?(
block_types,
local_author_ids,
%URI{} = actor_uri,
actor,
rejects
) do
clean_url = "#{actor_uri.host}#{actor_uri.path}"
# debug(block_types, "block_types")
# debug(actor_uri, "actor_uri")
local_author_ids =
local_author_ids
|> Enum.map(&(elem(&1, 1) |> ulid()))
|> debug("local_author_ids")
# || Bonfire.Federate.ActivityPub.Peered.is_blocked?(actor_uri, :any, :instance_wide) # NOTE: no need to check the instance-wide block here because that's being handled by Boundaries.is_blocked?
# |> debug()
MRF.subdomain_match?(rejects, clean_url) ||
Bonfire.Federate.ActivityPub.Peered.is_blocked?(actor || actor_uri, block_types,
user_ids: local_author_ids
)
end
defp block_or_filter_recipients(
block_types,
activity,
local_author_ids,
local_recipient_ids,
is_local?
) do
case filter_recipients(
block_types,
activity,
local_author_ids,
local_recipient_ids,
is_local?
) do
%{type: "Create"} = filtered ->
apply_filtered_recipients(filtered, activity, is_local?)
%{"type" => "Create"} = filtered ->
apply_filtered_recipients(filtered, activity, is_local?)
%{data: %{"type" => "Create"}} = filtered ->
apply_filtered_recipients(filtered, activity, is_local?)
filtered ->
debug(filtered, "accept non-create activity (even with no recipients)")
{:ok, filtered}
end
end
defp apply_filtered_recipients(filtered, activity, is_local?) do
if filtered != activity,
do: debug(filtered, "activity has been filtered"),
else: debug("no blocks apply")
if e(filtered, :to, nil) || e(filtered, :cc, nil) || e(filtered, :bto, nil) ||
e(filtered, :bcc, nil) || e(filtered, :audience, nil) ||
e(activity, "publishedDate", nil) || e(activity, "object", "publishedDate", nil) ||
e(activity, "object", "type", nil) == "Tombstone" do
# ^ `publishedDate` here is intended as an exception for bookwyrm which doesn't put audience info
{:ok, filtered}
else
if is_local? do
debug(
activity,
"do not federate local activity because it has no recipients or they were all filtered"
)
:ignore
else
debug(
activity,
"reject remote activity because it has no recipients or they were all filtered"
)
{:reject,
"Do not accept incoming federated activity because it has no recipients or they were all filtered"}
end
end
end
defp filter_recipients(block_types, activity, local_author_ids, local_recipient_ids, is_local?) do
rejects = rejects_regex(block_types)
activity
|> filter_recipients_field(
:to,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
|> filter_recipients_field(
:cc,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
|> filter_recipients_field(
:bto,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
|> filter_recipients_field(
:bcc,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
|> filter_recipients_field(
:audience,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
end
defp filter_recipients_field(
activity,
field,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
) do
case activity[field] do
recipients when is_list(recipients) and recipients != [] ->
Map.put(
activity,
field,
filter_actors(
activity,
recipients,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
)
recipient when is_binary(recipient) ->
Map.put(
activity,
field,
filter_actors(
activity,
[recipient],
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
)
_ ->
if is_atom(field),
do:
filter_recipients_field(
activity,
to_string(field),
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
),
else: activity
end
end
defp filter_actors(
activity,
actors,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
) do
(actors || [])
|> Enum.reject(&is_nil/1)
|> Enum.uniq()
|> debug("before filter")
|> Enum.reject(
&filter_actor?(
activity,
&1,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
)
)
end
defp filter_actor?(
_activity,
uri,
_block_types,
_rejects,
_local_author_ids,
_local_recipient_ids,
_is_local?
)
when ActivityPub.Config.is_in(uri, :public_uris) do
false
end
# defp filter_actor?(
# activity,
# %{ap_id: actor},
# block_types,
# rejects,
# local_author_ids,
# local_recipient_ids,
# is_local?
# )
# when is_binary(actor) do
# filter_actor?(
# activity,
# actor,
# block_types,
# rejects,
# local_author_ids,
# local_recipient_ids,
# is_local?
# )
# end
# defp filter_actor?(
# activity,
# %{"id" => actor},
# block_types,
# rejects,
# local_author_ids,
# local_recipient_ids,
# is_local?
# )
# when is_binary(actor) do
# filter_actor?(
# activity,
# actor,
# block_types,
# rejects,
# local_author_ids,
# local_recipient_ids,
# is_local?
# )
# end
defp filter_actor?(
activity,
recipient,
block_types,
rejects,
local_author_ids,
local_recipient_ids,
is_local?
) do
recipient_actor =
e(recipient, :ap_id, nil) || e(recipient, "id", nil) || e(recipient, :data, "id", nil) ||
recipient
# |> debug("uri")
debug(is_local?, "is_local???")
if is_binary(recipient_actor) do
recipient_actor_uri = URI.parse(recipient_actor)
clean_recipient_actor_uri = "#{recipient_actor_uri.host}#{recipient_actor_uri.path}"
author_ids =
local_author_ids
|> Enum.map(&(elem(&1, 1) |> ulid()))
{by_characters, actor_to_check} =
if is_local? do
debug(
"local activity - need to check if local author blocks the (maybe remote) recipient"
)
local_recipient = e(local_recipient_ids, recipient_actor, nil) || recipient_actor
{author_ids, local_recipient}
else
debug("remote activity - need to check if the local recipient blocks the remote author")
local_recipient = e(local_recipient_ids, recipient_actor, nil) || recipient_actor
{local_recipient, e(author_ids, nil) || AdapterUtils.all_actors(activity)}
end
|> debug("by_characters & actor_to_check")
# || Bonfire.Federate.ActivityPub.Peered.is_blocked?(recipient_actor_uri, :any, :instance_wide) # NOTE: no need to check the instance-wide block here because that's being handled by Boundaries.is_blocked?
MRF.subdomain_match?(rejects, recipient_actor_uri.host)
|> debug(
"filter '#{recipient_actor_uri.host}' blocked #{inspect(block_types)} instance in config?"
) ||
MRF.subdomain_match?(rejects, clean_recipient_actor_uri)
|> debug(
"filter '#{clean_recipient_actor_uri}' blocked #{inspect(block_types)} actor in config?"
) ||
Bonfire.Federate.ActivityPub.Peered.is_blocked?(actor_to_check, block_types,
user_ids: by_characters
)
|> debug(
"filter '#{id(actor_to_check) || inspect(actor_to_check)}' blocked (#{inspect(block_types)}) by #{inspect(by_characters)} in DB, either instance-wide or by specified local_actor_ids?"
)
end
end
defp rejects_regex(block_types) do
(filter_empty(block_types, []) ++ [:block])
# |> debug()
|> Enum.map(&ActivityPub.Config.get([:boundaries, &1]))
|> filter_empty([])
# |> debug()
|> MRF.subdomains_regex()
|> debug("MRF instance_wide blocks from config")
end
def actor_blocked?(
actor,
direction \\ nil,
by_user \\ nil
)
def actor_blocked?(
actor,
:in,
by_user
) do
actor_blocked?(
actor,
:silence,
by_user
)
end
def actor_blocked?(
actor,
:out,
by_user
) do
actor_blocked?(
actor,
:ghost,
by_user
)
end
def actor_blocked?(
actor,
nil,
by_user
) do
actor_blocked?(
actor,
[:ghost, :silence],
by_user
)
end
def actor_blocked?(
actor,
check_block_types,
by_user
) do
block_types = Boundaries.Blocks.types_blocked(check_block_types)
rejects = rejects_regex(block_types)
object_blocked?(
block_types,
by_user || [],
rejects,
AdapterUtils.id_or_object_id(actor) || actor
)
|> debug()
end
end