2026-10-01 11:12:34 +02:00
using PrivaPub.Federation.Actors ;
2026-10-01 11:25:05 +02:00
using PrivaPub.Federation.Moderation ;
2026-10-01 11:12:34 +02:00
using PrivaPub.Federation.Objects ;
using PrivaPub.Federation.Signing ;
2026-10-03 11:10:42 +02:00
using PrivaPub.Infrastructure.Http ;
2026-10-01 11:12:34 +02:00
using PrivaPub.Infrastructure.Jobs ;
2026-10-03 10:59:55 +02:00
using PrivaPub.Infrastructure.Statistics ;
2026-10-01 11:12:34 +02:00
using PrivaPub.Models.Federation ;
using PrivaPub.Models.Jobs ;
2026-10-03 10:59:55 +02:00
using PrivaPub.Models.Statistics ;
2026-10-01 11:12:34 +02:00
using System.Text.Json ;
using System.Text.Json.Nodes ;
using static PrivaPub . Federation . Objects . ActivityJson ;
namespace PrivaPub.Federation.Inbox
{
2026-10-03 10:59:55 +02:00
public sealed record InboxResult ( int StatusCode , string Error = default , int? RetryAfterSeconds = default , string Reason = default );
2026-10-01 11:12:34 +02:00
2026-10-05 09:34:01 +02:00
// ForwardedBy: the server that passed the activity on, signing with its own key (Forwarded)
2026-10-01 17:49:22 +02:00
public sealed record InboxPayload ( string ActorURI , string Activity , string Inbox = default , string KeyId = default , string Algorithm = default ,
2026-10-05 09:34:01 +02:00
string [] SignedHeaders = default , DateTime ? ReceivedAt = default , string ForwardedBy = default );
2026-10-01 11:12:34 +02:00
public interface IInboxReceiver
{
Task < InboxResult > Receive ( HttpRequest request , LocalActor recipient , CancellationToken token );
2026-10-03 10:59:55 +02:00
InboxResult NoSuchRecipient ( HttpRequest request );
2026-10-01 11:12:34 +02:00
}
2026-11-10 01:24:43 +01:00
public class InboxReceiver (
ILocalActorService localActors ,
IRemoteActorService remoteActors ,
IJobQueue queue ,
IDomainBlocks domainBlocks ,
ILogger < InboxReceiver > logger ,
IInteractionLedger ledger = default ) : IInboxReceiver
2026-10-01 11:12:34 +02:00
{
2026-10-01 18:25:18 +02:00
const int KeyRetrySeconds = 300 ;
2026-10-01 11:12:34 +02:00
const int MaxBodyBytes = 1024 * 1024 ;
2026-10-03 10:59:55 +02:00
sealed class Receipt
{
public string Type ;
public string ObjectType ;
public string ClaimedHost ;
public string VerifiedActor ;
public string Inbox ;
public string Signature = "none" ;
public long? Bytes ;
public bool Blocked ;
}
public InboxResult NoSuchRecipient ( HttpRequest request )
{
var result = new InboxResult ( StatusCodes . Status404NotFound , "no such local actor" , Reason : "unknown-recipient" );
2026-10-05 06:48:22 +02:00
var keyId = RequestSignature . Of ( request )?. KeyId ;
2026-10-03 10:59:55 +02:00
Record ( new Receipt { ClaimedHost = HostOf ( keyId ), Inbox = "personal" , Bytes = request . ContentLength }, result , 0 );
return result ;
2026-10-01 11:12:34 +02:00
}
public async Task < InboxResult > Receive ( HttpRequest request , LocalActor recipient , CancellationToken token )
2026-10-03 10:59:55 +02:00
{
2026-10-03 11:10:42 +02:00
using var scope = HttpScope . Triggered ( "verify" );
2026-10-03 10:59:55 +02:00
var started = System . Diagnostics . Stopwatch . GetTimestamp ();
var receipt = new Receipt { Inbox = recipient == default ? "shared" : "personal" , Bytes = request . ContentLength };
InboxResult result = default ;
try
{
result = await Receive ( request , recipient , receipt , token );
return result ;
}
finally
{
Record ( receipt , result , ( int ) System . Diagnostics . Stopwatch . GetElapsedTime ( started ). TotalMilliseconds );
}
}
void Record ( Receipt receipt , InboxResult result , int latencyMs )
{
2026-11-10 01:24:43 +01:00
if ( ledger == default || result == default )
2026-10-03 10:59:55 +02:00
return ;
var verified = receipt . VerifiedActor != default ;
2026-11-10 01:24:43 +01:00
ledger . Record ( new InteractionEvent
2026-10-03 10:59:55 +02:00
{
Channel = Interactions . Receive ,
Host = verified ? Interactions . HostOf ( receipt . VerifiedActor ) : receipt . ClaimedHost ,
Activity = receipt . Type ,
Object = receipt . ObjectType ,
Status = result . StatusCode ,
2026-10-05 09:34:01 +02:00
Outcome = result . StatusCode != StatusCodes . Status202Accepted ? Interactions . Refused
: result . Reason == "forwarded-ignored" ? Interactions . Dropped : Interactions . Queued ,
2026-10-03 10:59:55 +02:00
Reason = result . Reason ,
LatencyMs = latencyMs ,
Bytes = receipt . Bytes ,
Inbox = receipt . Inbox ,
Signature = receipt . Signature
}, verified ? receipt . VerifiedActor : default , hostClaimed : ! verified && ! receipt . Blocked );
}
2026-11-10 01:24:43 +01:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0007", Justification = "ActivityPub servers retry a 503 with Retry-After when the signing key cannot be fetched yet; a 4XX would make them drop the activity.")]
2026-10-03 10:59:55 +02:00
async Task < InboxResult > Receive ( HttpRequest request , LocalActor recipient , Receipt receipt , CancellationToken token )
2026-10-01 11:12:34 +02:00
{
if ( request . ContentLength > MaxBodyBytes )
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status413PayloadTooLarge , Reason : "too-large" );
2026-10-01 11:12:34 +02:00
using var buffer = new MemoryStream ();
await request . Body . CopyToAsync ( buffer , token );
if ( buffer . Length > MaxBodyBytes )
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status413PayloadTooLarge , Reason : "too-large" );
2026-10-01 11:12:34 +02:00
var body = buffer . ToArray ();
2026-10-03 10:59:55 +02:00
receipt . Bytes = body . Length ;
2026-10-01 11:12:34 +02:00
2026-11-10 01:24:43 +01:00
var activity = default ( JsonNode );
2026-10-01 11:12:34 +02:00
try
{
activity = JsonNode . Parse ( body );
}
catch ( JsonException )
{
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status400BadRequest , "the body is not JSON" , Reason : "not-json" );
2026-10-01 11:12:34 +02:00
}
if ( activity is not JsonObject )
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status400BadRequest , "the body is not an activity" , Reason : "not-activity" );
2026-10-01 11:12:34 +02:00
var type = Value ( activity , "type" );
var actorUri = Id ( activity [ "actor" ]);
2026-10-03 10:59:55 +02:00
receipt . Type = type ;
receipt . ObjectType = activity [ "object" ] is JsonObject embedded ? Value ( embedded , "type" ) : default ;
receipt . ClaimedHost = HostOf ( actorUri );
2026-10-01 11:12:34 +02:00
if ( string . IsNullOrEmpty ( type ) || string . IsNullOrEmpty ( actorUri ))
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status400BadRequest , "type and actor are required" , Reason : "missing-type-or-actor" );
2026-10-01 11:12:34 +02:00
2026-10-05 06:48:22 +02:00
// draft-cavage's Signature, or RFC 9421's Signature-Input and Signature
var signature = RequestSignature . Of ( request );
if ( signature == default )
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status401Unauthorized , "missing or unreadable Signature header" , Reason : "no-signature" );
2026-10-05 06:48:22 +02:00
receipt . Signature = signature . Scheme ;
receipt . ClaimedHost ??= HostOf ( signature . KeyId );
2026-11-10 01:24:43 +01:00
if ( domainBlocks . IsSuspended ( HostOf ( signature . KeyId )) || domainBlocks . IsSuspended ( HostOf ( actorUri )))
2026-10-03 10:59:55 +02:00
{
receipt . Blocked = true ;
2026-11-10 01:24:43 +01:00
receipt . ClaimedHost = domainBlocks . IsSuspended ( HostOf ( actorUri )) ? HostOf ( actorUri ) : HostOf ( signature . KeyId );
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status202Accepted , Reason : "suspended" );
}
2026-10-01 11:12:34 +02:00
2026-10-05 06:48:22 +02:00
var requestProblem = signature . Problem ( request , body );
2026-10-01 11:12:34 +02:00
if ( requestProblem != default )
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status401Unauthorized , requestProblem , Reason : HttpSignatures . ProblemCode ( requestProblem ));
2026-10-01 11:12:34 +02:00
2026-11-10 01:24:43 +01:00
var signed = signature . Signed ( request , localActors . BaseAddress );
var keyOwner = await remoteActors . GetActorByKeyId ( signature . KeyId , refresh : false , token );
2026-10-01 11:12:34 +02:00
if ( keyOwner == default && type == "Delete" && Id ( activity [ "object" ]) == actorUri )
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status202Accepted , Reason : "self-delete-unknown-key" );
2026-10-05 06:48:22 +02:00
if ( keyOwner == default || ! signature . VerifiedBy ( keyOwner . PublicKey , signed ))
2026-10-01 11:12:34 +02:00
{
2026-11-10 01:24:43 +01:00
keyOwner = await remoteActors . GetActorByKeyId ( signature . KeyId , refresh : true , token );
if ( keyOwner == default && remoteActors . KeyTemporarilyUnavailable ( signature . KeyId ))
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status503ServiceUnavailable , "the signing key could not be fetched; try again later" , KeyRetrySeconds , "key-unavailable" );
2026-10-05 06:48:22 +02:00
if ( keyOwner == default || ! signature . VerifiedBy ( keyOwner . PublicKey , signed ))
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status401Unauthorized , "the signature does not verify" , Reason : "signature-invalid" );
2026-10-01 11:12:34 +02:00
}
2026-10-05 09:34:01 +02:00
// signed by someone else: passed on by a server that holds the thread, its signature good (a 401 would tell it
// otherwise, and make a double-knocking sender try its other scheme)
receipt . VerifiedActor = keyOwner . ActorURI ;
var forwardedBy = string . Equals ( keyOwner . ActorURI , actorUri , StringComparison . Ordinal ) ? default : keyOwner . ActorURI ;
// (ours come back too, Friendica forwarding our comments in its threads: we know them already)
2026-11-10 01:24:43 +01:00
if ( forwardedBy != default && (! Forwarded . Takeable ( type , activity , actorUri ) || Origin . Same ( actorUri , localActors . BaseAddress )))
2026-10-05 09:34:01 +02:00
return new ( StatusCodes . Status202Accepted , Reason : "forwarded-ignored" );
2026-10-01 11:12:34 +02:00
var shapeProblem = await ShapeProblem ( type , activity , actorUri , token );
if ( shapeProblem != default )
return shapeProblem ;
var activityId = Id ( activity );
2026-10-05 06:48:22 +02:00
var payload = new InboxPayload ( actorUri , activity . ToJsonString (), recipient == default ? "shared" : "personal" , signature . KeyId ,
2026-10-05 09:34:01 +02:00
signature . Algorithm , signature . Covered , DateTime . UtcNow , forwardedBy );
// a forwarded copy never stands in for the author's own delivery, which may carry what the copy could not
// (a followers-only reply our instance actor cannot read): each is kept once, apart
var dedupe = activityId == default ? default : ( forwardedBy == default ? "inbox|" : "inbox|forwarded|" ) + activityId ;
2026-10-05 09:52:02 +02:00
var queuedPayload = JsonSerializer . Serialize ( payload );
var host = new Uri ( actorUri ). Host . ToLowerInvariant ();
2026-11-10 01:24:43 +01:00
var queued = await queue . Enqueue ( JobKind . ProcessInbox , queuedPayload , host , dedupe , token );
2026-10-05 09:52:02 +02:00
// one id its sender gave two activities is no duplicate (Friendica's ids are uniqid(), a prefix and the
// microsecond, which two of its processes answering at once share): what each carries tells them apart, and a
// true copy carries the same
2026-11-10 01:24:43 +01:00
if (! queued && dedupe != default && CarriesOther ( await queue . Payload ( dedupe , token ), activity ))
queued = await queue . Enqueue ( JobKind . ProcessInbox , queuedPayload , host , $"{dedupe}|{Digest(activity)}" , token );
2026-10-06 07:37:03 +02:00
// an Undo of a Follow that comes again while the follow it ends exists again is meant for that follow: Mastodon
// gives every Undo it sends after reading a roll-call (FEP-8fcf) one id, `{actor}#follows//undo`. It is kept
// once per follow, so a retry still counts as a copy
if (! queued && dedupe != default && type == "Undo" && await FollowAgain ( activity , actorUri , token ) is { } follow )
2026-11-10 01:24:43 +01:00
queued = await queue . Enqueue ( JobKind . ProcessInbox , queuedPayload , host , $"{dedupe}|{Digest(activity)}|{follow}" , token );
2026-10-06 07:46:46 +02:00
// FEP-8fcf: what the sender says of its followers here (FollowingSynchronization)
if ( queued && forwardedBy == default && FollowersSynchronization . Claim ( actorUri , request . Headers [ FollowersSynchronization . Header ]. ToString ()) is { } claim )
2026-11-10 01:24:43 +01:00
await queue . Enqueue ( JobKind . SynchronizeFollowing , JsonSerializer . Serialize ( claim ), host , FollowingSynchronization . DedupeKey ( claim , activityId ), token );
logger . LogInformation ( "Inbox {Recipient}: {Type} from {Actor} queued" , recipient ?. Handle ?? "shared" , type , actorUri );
2026-10-05 09:34:01 +02:00
return new ( StatusCodes . Status202Accepted , Reason : ! queued ? "duplicate" : forwardedBy != default ? "forwarded" : "queued" );
2026-10-01 11:12:34 +02:00
}
2026-10-06 07:37:03 +02:00
// the follow an Undo of a Follow would end, when there is one: its record's id
async Task < string > FollowAgain ( JsonNode activity , string actorUri , CancellationToken token )
{
if ( activity [ "object" ] is not JsonObject inner || Value ( inner , "type" ) != "Follow" || Id ( inner [ "object" ]) is not { } targetUri
2026-11-10 01:24:43 +01:00
|| await localActors . FindByUri ( targetUri , token ) is not { } target )
2026-10-06 07:37:03 +02:00
return default ;
var follower = await MongoDB . Entities . DB . Default . Find < Follower >()
. Match ( f => f . ActorURI == actorUri && f . LocalActorId == target . Id && f . LocalActorKind == target . Kind )
. ExecuteFirstAsync ( token );
return follower ?. ID ;
}
2026-10-03 10:59:55 +02:00
static string HostOf ( string uri ) => Uri . TryCreate ( uri , UriKind . Absolute , out var parsed ) ? parsed . Host . ToLowerInvariant () : default ;
2026-10-01 11:25:05 +02:00
2026-10-06 10:58:45 +02:00
public static bool CarriesOther ( string earlierPayload , JsonNode activity )
2026-10-05 09:52:02 +02:00
{
if ( earlierPayload == default )
return false ;
var earlier = JsonNode . Parse ( JsonSerializer . Deserialize < InboxPayload >( earlierPayload ). Activity );
return Value ( earlier , "type" ) != Value ( activity , "type" ) || Id ( earlier [ "actor" ]) != Id ( activity [ "actor" ])
|| earlier [ "object" ]?. ToJsonString () != activity [ "object" ]?. ToJsonString ();
}
// what an activity carries, short: its type, actor and object
2026-10-06 10:58:45 +02:00
public static string Digest ( JsonNode activity ) =>
2026-10-05 09:52:02 +02:00
Convert . ToHexString ( System . Security . Cryptography . SHA256 . HashData ( System . Text . Encoding . UTF8 . GetBytes (
$"{Value(activity, " type ")}\n{Id(activity[" actor "])}\n{activity[" object "]?.ToJsonString()}" )))[.. 16 ]. ToLowerInvariant ();
2026-10-01 11:12:34 +02:00
async Task < InboxResult > ShapeProblem ( string type , JsonNode activity , string actorUri , CancellationToken token )
{
var activityId = Id ( activity );
2026-10-05 12:02:25 +02:00
// (Funkwhale names its answer after the activity it answers: `<our Follow's id>/accept`)
if ( activityId != default && ! Origin . Same ( activityId , actorUri )
2026-11-10 01:24:43 +01:00
&& !( type is "Accept" or "Reject" && Id ( activity [ "object" ]) is { } answered && Origin . Same ( answered , localActors . BaseAddress )
2026-10-05 12:02:25 +02:00
&& activityId . StartsWith ( answered + "/" , StringComparison . Ordinal )))
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status400BadRequest , "the activity's id is not on its actor's origin" , Reason : "id-cross-origin" );
2026-10-01 11:12:34 +02:00
var inner = activity [ "object" ];
switch ( type )
{
case "Follow" :
2026-10-06 10:58:45 +02:00
// (by its actor's id, or the profile page Forte names instead)
2026-11-10 01:24:43 +01:00
var target = await localActors . FindByAddress ( Id ( inner ), token );
2026-10-06 19:23:17 +02:00
if ( target is not { IsFederated : true } || target . IsServerActor )
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status404NotFound , "no such local actor" , Reason : "unknown-recipient" );
2026-10-01 11:12:34 +02:00
break ;
case "Undo" when inner is JsonObject && Id ( inner [ "actor" ]) != actorUri :
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status400BadRequest , "an actor can only undo its own activities" , Reason : "undo-foreign" );
2026-10-05 10:54:32 +02:00
// (attributed to another account of the actor's own server, as Mobilizon's organiser creates a group's
// event: the handlers read it from that server, Origin.Same)
2026-10-01 11:12:34 +02:00
case "Create" or "Update" when inner is JsonObject && Origin . Same ( Id ( inner ), actorUri )
2026-10-05 10:54:32 +02:00
&& inner [ "attributedTo" ] != default && ! Origin . Same ( Id ( inner [ "attributedTo" ]), actorUri )
2026-10-01 11:12:34 +02:00
&& Value ( inner , "type" ) is not ( "Person" or "Service" or "Application" or "Group" or "Organization" ):
2026-10-03 10:59:55 +02:00
return new ( StatusCodes . Status400BadRequest , "the object is not attributed to the actor" , Reason : "misattributed" );
2026-10-01 11:12:34 +02:00
}
return default ;
}
}
}