S1 and S2 of the roadmap. RemoteActorService: - FetchObject accepts a document only when its id is the address it was served from; a same-origin document naming another address is asked for at that address once (how GoToSocial serves its key URIs), anything else is dropped; - GetActorByKeyId accepts a key only when the actor lists it, its owner is the actor and it lives on the actor's origin, whether the keyId points at the actor or at a key document; - a refetch for a key or an actor happens at most once per five minutes, so a bad signature cannot make us hammer a host; - the cache row is written by one atomic upsert on ActorURI; - every fetch is signed by the instance actor, never by the persona that happened to receive the activity. The inbox refuses an activity whose id is not on its actor's origin, and an Undo of someone else's activity; a Create's object, an Update and a Delete must be on the actor's origin too, and a cross-origin object is refetched from its own origin before it is trusted. Tests: a fake peer on two origins serves forged actors, foreign-owned keys, cross-origin key documents, aliases and a GoToSocial-style key address (integration, PRIVAPUB_TEST_MONGOD=1). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
198 lines
6.9 KiB
C#
198 lines
6.9 KiB
C#
using MongoDB.Entities;
|
|
|
|
using PrivaPub.Models.User;
|
|
using PrivaPub.StaticServices;
|
|
|
|
using System.Text.Json;
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
using PrivaPub.Federation.Objects;
|
|
using PrivaPub.Federation.Signing;
|
|
using PrivaPub.Infrastructure.Http;
|
|
|
|
namespace PrivaPub.Federation.Actors
|
|
{
|
|
public interface IRemoteActorService
|
|
{
|
|
Task<FetchedJson> FetchObject(string uri, CancellationToken token);
|
|
Task<ForeignAvatar> GetActor(string actorUri, bool refresh, CancellationToken token);
|
|
Task<ForeignAvatar> GetActorByKeyId(string keyId, bool refresh, CancellationToken token);
|
|
Task<string> ResolveHandle(string handle, CancellationToken token);
|
|
}
|
|
|
|
public class RemoteActorService : IRemoteActorService
|
|
{
|
|
public const string ActivityJson = "application/activity+json";
|
|
const string Accept = "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
|
|
static readonly TimeSpan CacheLifetime = TimeSpan.FromDays(1);
|
|
static readonly TimeSpan RefetchInterval = TimeSpan.FromMinutes(5);
|
|
|
|
readonly IFederationHttp _http;
|
|
readonly ILocalActorService _localActors;
|
|
readonly IMemoryCache _cache;
|
|
readonly DbEntities _dbEntities;
|
|
|
|
public RemoteActorService(IFederationHttp http, ILocalActorService localActors, IMemoryCache cache, DbEntities dbEntities)
|
|
{
|
|
_http = http;
|
|
_localActors = localActors;
|
|
_cache = cache;
|
|
_dbEntities = dbEntities;
|
|
}
|
|
|
|
public async Task<FetchedJson> FetchObject(string uri, CancellationToken token)
|
|
{
|
|
var signer = await _localActors.GetInstanceActor(token);
|
|
var fetched = await Get(uri, signer, token);
|
|
if (fetched == default)
|
|
return default;
|
|
|
|
var id = Text(fetched.Root, "id");
|
|
if (Origin.IsDocumentAt(id, fetched.FinalUri))
|
|
return fetched;
|
|
|
|
var finalUri = fetched.FinalUri;
|
|
fetched.Dispose();
|
|
if (!Origin.Same(id, finalUri.AbsoluteUri))
|
|
return default;
|
|
|
|
var named = await Get(id, signer, token);
|
|
if (named != default && Origin.IsDocumentAt(Text(named.Root, "id"), named.FinalUri))
|
|
return named;
|
|
named?.Dispose();
|
|
return default;
|
|
}
|
|
|
|
public async Task<ForeignAvatar> GetActor(string actorUri, bool refresh, CancellationToken token)
|
|
{
|
|
if (string.IsNullOrEmpty(actorUri))
|
|
return default;
|
|
actorUri = StripFragment(actorUri);
|
|
|
|
var cached = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == actorUri).ExecuteFirstAsync(token);
|
|
if (cached != default && !refresh && DateTime.UtcNow - cached.UpdatedAt < CacheLifetime)
|
|
return cached;
|
|
if (!MayFetch(actorUri))
|
|
return cached;
|
|
|
|
using var fetched = await FetchObject(actorUri, token);
|
|
var actor = fetched == default ? default : ActorDocument.Parse(fetched.Root);
|
|
if (actor == default)
|
|
return cached;
|
|
|
|
var key = cached == default ? default : actor.Key(cached.PublicKeyId);
|
|
return await Upsert(actor, key ?? actor.Keys.FirstOrDefault(), token);
|
|
}
|
|
|
|
public async Task<ForeignAvatar> GetActorByKeyId(string keyId, bool refresh, CancellationToken token)
|
|
{
|
|
if (Origin.Of(keyId) == default)
|
|
return default;
|
|
|
|
var cached = await _dbEntities.ForeignAvatars.Match(a => a.PublicKeyId == keyId).ExecuteFirstAsync(token);
|
|
if (cached != default && !string.IsNullOrEmpty(cached.PublicKey) && !refresh)
|
|
return cached;
|
|
if (!MayFetch(keyId))
|
|
return cached;
|
|
|
|
using var fetched = await FetchObject(StripFragment(keyId), token);
|
|
if (fetched == default)
|
|
return cached;
|
|
|
|
var actor = ActorDocument.Parse(fetched.Root);
|
|
if (actor == default)
|
|
{
|
|
var owner = Text(fetched.Root, "owner");
|
|
if (Text(fetched.Root, "id") != keyId || !Origin.Same(owner, keyId))
|
|
return default;
|
|
using var ownerDocument = await FetchObject(owner, token);
|
|
actor = ownerDocument == default ? default : ActorDocument.Parse(ownerDocument.Root);
|
|
}
|
|
|
|
var key = actor?.Key(keyId);
|
|
if (key == default)
|
|
return default;
|
|
return await Upsert(actor, key, token);
|
|
}
|
|
|
|
public async Task<string> ResolveHandle(string handle, CancellationToken token)
|
|
{
|
|
var parts = handle?.TrimStart('@').Split('@');
|
|
if (parts is not { Length: 2 } || string.IsNullOrEmpty(parts[0]) || string.IsNullOrEmpty(parts[1]))
|
|
return default;
|
|
|
|
var url = $"https://{parts[1]}/.well-known/webfinger?resource={Uri.EscapeDataString($"acct:{parts[0]}@{parts[1]}")}";
|
|
using var fetched = await _http.GetJson(url, "application/jrd+json, application/json", sign: default, token);
|
|
if (fetched == default)
|
|
return default;
|
|
var document = fetched.Document;
|
|
if (!document.RootElement.TryGetProperty("links", out var links) || links.ValueKind != JsonValueKind.Array)
|
|
return default;
|
|
|
|
foreach (var link in links.EnumerateArray())
|
|
{
|
|
var type = Text(link, "type") ?? string.Empty;
|
|
if (Text(link, "rel") == "self" && (type.Contains("activity+json") || type.Contains("ld+json")))
|
|
return Text(link, "href");
|
|
}
|
|
return default;
|
|
}
|
|
|
|
async Task<FetchedJson> Get(string uri, LocalActor signer, CancellationToken token) =>
|
|
await _http.GetJson(uri, Accept, request => HttpSignatures.Sign(request, signer, body: null), token);
|
|
|
|
bool MayFetch(string uri)
|
|
{
|
|
var key = "remote-actor:fetched:" + uri;
|
|
if (_cache.TryGetValue(key, out _))
|
|
return false;
|
|
_cache.Set(key, true, RefetchInterval);
|
|
return true;
|
|
}
|
|
|
|
static async Task<ForeignAvatar> Upsert(ActorDocument actor, ActorKey key, CancellationToken token)
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
return await DB.Default.UpdateAndGet<ForeignAvatar>()
|
|
.Match(a => a.ActorURI == actor.Id)
|
|
.Modify(a => a.UserName, actor.PreferredUsername)
|
|
.Modify(a => a.Name, actor.Name)
|
|
.Modify(a => a.Biography, actor.Summary)
|
|
.Modify(a => a.Url, actor.Url)
|
|
.Modify(a => a.Domain, new Uri(actor.Id).Authority)
|
|
.Modify(a => a.InboxURL, actor.Inbox)
|
|
.Modify(a => a.OutboxURL, actor.Outbox)
|
|
.Modify(a => a.SharedInboxURL, actor.SharedInbox)
|
|
.Modify(a => a.PictureURL, actor.Icon)
|
|
.Modify(a => a.IsDiscoverable, actor.Discoverable)
|
|
.Modify(a => a.AvatarType, Enum.TryParse<AvatarType>(actor.Type, out var type) ? type : AvatarType.Person)
|
|
.Modify(a => a.PublicKeyId, key?.Id)
|
|
.Modify(a => a.PublicKey, key?.Pem)
|
|
.Modify(a => a.UpdatedAt, now)
|
|
.Modify(b => b.SetOnInsert(a => a.CreatedAt, now))
|
|
.Option(o => o.IsUpsert = true)
|
|
.ExecuteAsync(token);
|
|
}
|
|
|
|
public static string StripFragment(string uri)
|
|
{
|
|
var hash = uri.IndexOf('#');
|
|
return hash < 0 ? uri : uri[..hash];
|
|
}
|
|
|
|
public static string Text(JsonElement element, string property)
|
|
{
|
|
if (element.ValueKind != JsonValueKind.Object || !element.TryGetProperty(property, out var value))
|
|
return default;
|
|
return value.ValueKind switch
|
|
{
|
|
JsonValueKind.String => value.GetString(),
|
|
JsonValueKind.Object => Text(value, "id") ?? Text(value, "href"),
|
|
JsonValueKind.Array => value.EnumerateArray().Select(v => v.ValueKind == JsonValueKind.String ? v.GetString() : Text(v, "id")).FirstOrDefault(v => v != null),
|
|
_ => default
|
|
};
|
|
}
|
|
}
|
|
}
|