@oxy.so/federation 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +16 -0
- package/dist/cjs/.tsbuildinfo +1 -0
- package/dist/cjs/actorObject.js +216 -0
- package/dist/cjs/apContext.js +48 -0
- package/dist/cjs/apUri.js +132 -0
- package/dist/cjs/httpSignature.js +187 -0
- package/dist/cjs/index.js +99 -0
- package/dist/cjs/networkIdentity.js +487 -0
- package/dist/cjs/node/actorResolver.js +625 -0
- package/dist/cjs/node/actorRouter.js +307 -0
- package/dist/cjs/node/delivery.js +415 -0
- package/dist/cjs/node/identityBridge.js +133 -0
- package/dist/cjs/node/inboundDispatch.js +268 -0
- package/dist/cjs/node/index.js +63 -0
- package/dist/cjs/node/signedFetch.js +122 -0
- package/dist/cjs/node/webfingerRouter.js +166 -0
- package/dist/cjs/urls.js +55 -0
- package/dist/esm/.tsbuildinfo +1 -0
- package/dist/esm/actorObject.js +210 -0
- package/dist/esm/apContext.js +45 -0
- package/dist/esm/apUri.js +126 -0
- package/dist/esm/httpSignature.js +179 -0
- package/dist/esm/index.js +65 -0
- package/dist/esm/networkIdentity.js +472 -0
- package/dist/esm/node/actorResolver.js +620 -0
- package/dist/esm/node/actorRouter.js +304 -0
- package/dist/esm/node/delivery.js +412 -0
- package/dist/esm/node/identityBridge.js +130 -0
- package/dist/esm/node/inboundDispatch.js +263 -0
- package/dist/esm/node/index.js +51 -0
- package/dist/esm/node/signedFetch.js +119 -0
- package/dist/esm/node/webfingerRouter.js +163 -0
- package/dist/esm/urls.js +50 -0
- package/dist/types/.tsbuildinfo +1 -0
- package/dist/types/actorObject.d.ts +182 -0
- package/dist/types/apContext.d.ts +35 -0
- package/dist/types/apUri.d.ts +107 -0
- package/dist/types/httpSignature.d.ts +113 -0
- package/dist/types/index.d.ts +336 -0
- package/dist/types/networkIdentity.d.ts +509 -0
- package/dist/types/node/actorResolver.d.ts +287 -0
- package/dist/types/node/actorRouter.d.ts +108 -0
- package/dist/types/node/delivery.d.ts +248 -0
- package/dist/types/node/identityBridge.d.ts +84 -0
- package/dist/types/node/inboundDispatch.d.ts +156 -0
- package/dist/types/node/index.d.ts +51 -0
- package/dist/types/node/signedFetch.d.ts +74 -0
- package/dist/types/node/webfingerRouter.d.ts +62 -0
- package/dist/types/urls.d.ts +55 -0
- package/package.json +119 -0
- package/src/__tests__/actorObject.test.ts +258 -0
- package/src/__tests__/actorResolver.test.ts +252 -0
- package/src/__tests__/actorResolverNetworkIdentity.test.ts +297 -0
- package/src/__tests__/apUri.test.ts +53 -0
- package/src/__tests__/delivery.test.ts +432 -0
- package/src/__tests__/federationHost.test.ts +281 -0
- package/src/__tests__/httpSignature.test.ts +343 -0
- package/src/__tests__/inboundDispatch.test.ts +381 -0
- package/src/__tests__/index.test.ts +8 -0
- package/src/__tests__/networkIdentity.test.ts +525 -0
- package/src/__tests__/routers.test.ts +460 -0
- package/src/__tests__/urls.test.ts +26 -0
- package/src/actorObject.ts +313 -0
- package/src/apContext.ts +45 -0
- package/src/apUri.ts +161 -0
- package/src/httpSignature.ts +282 -0
- package/src/index.ts +419 -0
- package/src/networkIdentity.ts +731 -0
- package/src/node/actorResolver.ts +839 -0
- package/src/node/actorRouter.ts +438 -0
- package/src/node/delivery.ts +729 -0
- package/src/node/identityBridge.ts +230 -0
- package/src/node/inboundDispatch.ts +420 -0
- package/src/node/index.ts +136 -0
- package/src/node/signedFetch.ts +177 -0
- package/src/node/webfingerRouter.ts +226 -0
- package/src/urls.ts +71 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inbound ActivityPub dispatch + the follow-protocol handlers.
|
|
3
|
+
*
|
|
4
|
+
* The engine owns the DISPATCHER (validate the untrusted activity, switch on its
|
|
5
|
+
* type) and the FOLLOW-PROTOCOL verbs — Follow / Accept / Undo(Follow) / Reject —
|
|
6
|
+
* because those are identical across every Oxy app: they bridge a federated follow
|
|
7
|
+
* edge into the Oxy graph (via the identity adapter), record the AP-side follow
|
|
8
|
+
* row (via the store adapter), and send the Accept back (via the delivery
|
|
9
|
+
* service). Every CONTENT verb (Create / Announce / Like / Delete / Update, and a
|
|
10
|
+
* non-follow Undo) is handed to the app-registered
|
|
11
|
+
* {@link InboundDispatcherConfig.onContentActivity} callback, where the app's own
|
|
12
|
+
* post/engagement handlers live. The consent gate + notification side effects are
|
|
13
|
+
* injected so the engine holds no app knowledge.
|
|
14
|
+
*
|
|
15
|
+
* Extracted behaviour-identically from Mention's former `InboxProcessingService`
|
|
16
|
+
* dispatcher + `handleIncomingFollow` / `handleUndo(Follow)` / `handleAccept` /
|
|
17
|
+
* `handleReject`.
|
|
18
|
+
*/
|
|
19
|
+
import { normalizeActorUsername } from '../urls.js';
|
|
20
|
+
/**
|
|
21
|
+
* Thrown when a federated follow is about to be bridged but the FOLLOWER actor
|
|
22
|
+
* has not yet resolved to an Oxy user (`oxyUserId` missing) — e.g. Oxy was
|
|
23
|
+
* unreachable when the actor was fetched. A federated follow MUST become a real
|
|
24
|
+
* Oxy edge, never a ghost, so the whole inbound activity is DEFERRED rather than
|
|
25
|
+
* bridged half-way:
|
|
26
|
+
*
|
|
27
|
+
* - in the BullMQ inbox worker, throwing fails the job, which retries with
|
|
28
|
+
* bounded exponential backoff; a later attempt (Oxy reachable) resolves the
|
|
29
|
+
* actor and bridges the follow. A permanently-unresolvable actor exhausts the
|
|
30
|
+
* attempts and the activity is dropped — never a ghost edge.
|
|
31
|
+
* - in the inline (no-Redis) fallback, it surfaces as a 500 from the inbox
|
|
32
|
+
* endpoint, so the remote re-delivers per ActivityPub.
|
|
33
|
+
*/
|
|
34
|
+
export class ActorResolutionPendingError extends Error {
|
|
35
|
+
constructor(actorUri, context) {
|
|
36
|
+
super(`Actor ${actorUri} is not yet resolved to an Oxy user${context ? ` (${context})` : ''}; deferring inbound activity`);
|
|
37
|
+
this.name = 'ActorResolutionPendingError';
|
|
38
|
+
this.actorUri = actorUri;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The lowercased host of an actor URI, or null when it is not a parseable absolute
|
|
43
|
+
* URL. Callers treat null as blocked: an origin whose host cannot be determined
|
|
44
|
+
* cannot be checked against the domain policy, so it fails closed.
|
|
45
|
+
*/
|
|
46
|
+
function actorUriHost(actorUri) {
|
|
47
|
+
try {
|
|
48
|
+
return new URL(actorUri).hostname.toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Read the `object`'s referenced actor/target uri (a string, or an embedded `{ id }`). */
|
|
55
|
+
function objectTargetUri(object) {
|
|
56
|
+
if (typeof object === 'string')
|
|
57
|
+
return object;
|
|
58
|
+
if (object && typeof object === 'object') {
|
|
59
|
+
const id = object.id;
|
|
60
|
+
if (typeof id === 'string')
|
|
61
|
+
return id;
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
/** Build the inbound-activity dispatcher from an app's adapters + content handlers. */
|
|
66
|
+
export function createInboundDispatcher(config) {
|
|
67
|
+
const { logger } = config;
|
|
68
|
+
async function handleIncomingFollow(activity, actorUri) {
|
|
69
|
+
const targetActorUri = objectTargetUri(activity.object);
|
|
70
|
+
if (!targetActorUri)
|
|
71
|
+
return;
|
|
72
|
+
// Extract username from our actor URL
|
|
73
|
+
const match = targetActorUri.match(/\/ap\/users\/([^/]+)$/);
|
|
74
|
+
if (!match)
|
|
75
|
+
return;
|
|
76
|
+
const username = normalizeActorUsername(match[1]);
|
|
77
|
+
// Resolve the Oxy user to get a real user ID
|
|
78
|
+
const user = await config.identity.resolveUserByUsername(username);
|
|
79
|
+
if (!user) {
|
|
80
|
+
logger.warn(`Incoming follow for unknown user ${username} from ${actorUri}`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const localUserId = String(user._id || user.id);
|
|
84
|
+
// The target user may have turned fediverse sharing off — drop the Follow
|
|
85
|
+
// silently (no bridge, no Accept, no Reject). A Reject is unverifiable
|
|
86
|
+
// against a 404'd actor and would reveal the account exists, so this must
|
|
87
|
+
// look identical to a Follow sent to an unknown user. Gated here, BEFORE
|
|
88
|
+
// the follower actor is fetched/resolved, so an OFF user never triggers any
|
|
89
|
+
// of the bridge/Accept/notification side effects below.
|
|
90
|
+
if (!config.consent.isSharingEnabledFromUser(user)) {
|
|
91
|
+
logger.debug(`[Federation] inbound follow for ${username} dropped — sharing off`);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// Resolve the follower actor and REQUIRE its Oxy user id: a fediverse
|
|
95
|
+
// follower must become a real Oxy edge, never a ghost. When the actor is
|
|
96
|
+
// missing or not yet resolved to an Oxy user (Oxy was unreachable when it was
|
|
97
|
+
// fetched), throw `ActorResolutionPendingError` so the BullMQ inbox job
|
|
98
|
+
// retries with backoff and bridges the follow on a later attempt.
|
|
99
|
+
const actor = await config.actorResolver.getOrFetchActor(actorUri);
|
|
100
|
+
const followerOxyUserId = actor?.oxyUserId;
|
|
101
|
+
if (!followerOxyUserId) {
|
|
102
|
+
throw new ActorResolutionPendingError(actorUri, `Follow ${String(activity.id)}`);
|
|
103
|
+
}
|
|
104
|
+
// A self-follow (the follower resolves to the same local user) is meaningless
|
|
105
|
+
// in the Oxy graph — skip before touching any state or delivering an Accept.
|
|
106
|
+
if (followerOxyUserId === localUserId) {
|
|
107
|
+
logger.debug(`[Federation] ignoring self-follow from ${actorUri} to ${username}`);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
// Create the Oxy follow edge BEFORE sending Accept so a retry never spams
|
|
111
|
+
// Accepts: the bridge is idempotent (safe to re-run), but an Accept delivered
|
|
112
|
+
// before the edge was committed could be re-sent on every retry. On failure
|
|
113
|
+
// the bridge throws, failing the job so the whole sequence retries.
|
|
114
|
+
await config.identity.bridgeFollow(followerOxyUserId, localUserId);
|
|
115
|
+
await config.follows.upsertInboundAccepted(localUserId, actorUri, String(activity.id));
|
|
116
|
+
// Send Accept back so the remote server knows the follow succeeded
|
|
117
|
+
await config.delivery.sendAccept(localUserId, username, String(activity.id), actorUri);
|
|
118
|
+
// Fail-soft: the Oxy edge is already committed, so a notification failure must
|
|
119
|
+
// never fail (and thus retry) the follow.
|
|
120
|
+
if (config.onInboundFollowAccepted) {
|
|
121
|
+
await config.onInboundFollowAccepted(localUserId, followerOxyUserId, actorUri);
|
|
122
|
+
}
|
|
123
|
+
logger.info(`Accepted follow from ${actorUri} to ${username}`);
|
|
124
|
+
}
|
|
125
|
+
async function handleUndoFollow(object, actorUri) {
|
|
126
|
+
const targetActorUri = objectTargetUri(object.object);
|
|
127
|
+
const match = targetActorUri?.match(/\/ap\/users\/([^/]+)$/);
|
|
128
|
+
let localUserId;
|
|
129
|
+
if (match) {
|
|
130
|
+
const user = await config.identity.resolveUserByUsername(normalizeActorUsername(match[1]));
|
|
131
|
+
if (user)
|
|
132
|
+
localUserId = String(user._id || user.id);
|
|
133
|
+
}
|
|
134
|
+
// Idempotency: locate the follow row FIRST. Absent → this Undo was already
|
|
135
|
+
// processed (a redelivery), so there is nothing to tear down — return.
|
|
136
|
+
const follow = await config.follows.findInboundFollow(actorUri, localUserId);
|
|
137
|
+
if (!follow) {
|
|
138
|
+
logger.debug(`Undo follow from ${actorUri}: no matching row (already processed)`);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Remove the Oxy follow edge BEFORE deleting the local row, so a transient
|
|
142
|
+
// bridge failure retries with the row still present. The edge can only exist
|
|
143
|
+
// when the follower actor resolved to an Oxy user; without an `oxyUserId` no
|
|
144
|
+
// edge was ever created, so there is nothing to remove. THROW on transient
|
|
145
|
+
// bridge failure (job retry); the bridge is idempotent.
|
|
146
|
+
const followerOxyUserId = await config.follows.findActorOxyUserId(actorUri);
|
|
147
|
+
if (followerOxyUserId) {
|
|
148
|
+
await config.identity.bridgeUnfollow(followerOxyUserId, follow.localUserId);
|
|
149
|
+
}
|
|
150
|
+
await config.follows.deleteFollowById(follow._id);
|
|
151
|
+
logger.debug(`Undo follow from ${actorUri}`);
|
|
152
|
+
}
|
|
153
|
+
async function handleUndo(activity, actorUri) {
|
|
154
|
+
const object = activity.object;
|
|
155
|
+
if (!object)
|
|
156
|
+
return;
|
|
157
|
+
const objectType = typeof object === 'string' ? null : object.type;
|
|
158
|
+
if (objectType === 'Follow') {
|
|
159
|
+
await handleUndoFollow(object, actorUri);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
// Undo(Like) / Undo(Announce) — content teardown. Hand the WHOLE Undo
|
|
163
|
+
// activity to the app (it re-inspects the embedded object type).
|
|
164
|
+
await config.onContentActivity(activity, actorUri);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async function handleAccept(activity, actorUri) {
|
|
168
|
+
const object = activity.object;
|
|
169
|
+
if (!object)
|
|
170
|
+
return;
|
|
171
|
+
let updated = false;
|
|
172
|
+
if (typeof object === 'string') {
|
|
173
|
+
// Remote sent Accept with a string reference (the Follow activity ID).
|
|
174
|
+
// Try matching by activityId first, fall back to any pending follow.
|
|
175
|
+
updated = await config.follows.markOutboundAcceptedByActivityId(actorUri, object);
|
|
176
|
+
if (!updated) {
|
|
177
|
+
updated = await config.follows.markOutboundAcceptedAnyPending(actorUri);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else if (object.type === 'Follow') {
|
|
181
|
+
const followActivityId = object.id;
|
|
182
|
+
updated = typeof followActivityId === 'string' && followActivityId.length > 0
|
|
183
|
+
? await config.follows.markOutboundAcceptedByActivityId(actorUri, followActivityId)
|
|
184
|
+
: await config.follows.markOutboundAcceptedAnyPending(actorUri);
|
|
185
|
+
}
|
|
186
|
+
if (updated) {
|
|
187
|
+
logger.debug(`Follow accepted by ${actorUri}`);
|
|
188
|
+
// Fire-and-forget: backfill the newly followed actor's recent posts.
|
|
189
|
+
if (config.onOutboundFollowAccepted) {
|
|
190
|
+
await config.onOutboundFollowAccepted(actorUri);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async function handleReject(activity, actorUri) {
|
|
195
|
+
const object = activity.object;
|
|
196
|
+
if (!object)
|
|
197
|
+
return;
|
|
198
|
+
const objectType = typeof object === 'string' ? null : object.type;
|
|
199
|
+
if (objectType === 'Follow') {
|
|
200
|
+
const followActivityId = typeof object === 'object' ? object.id : undefined;
|
|
201
|
+
await config.follows.markOutboundRejected(actorUri, typeof followActivityId === 'string' ? followActivityId : undefined);
|
|
202
|
+
logger.debug(`Follow rejected by ${actorUri}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async function processInboxActivity(activity, verifiedActorUri) {
|
|
206
|
+
// Instance domain policy, FIRST — before the payload is even parsed.
|
|
207
|
+
//
|
|
208
|
+
// This is the single chokepoint for inbound federation: every transport (the
|
|
209
|
+
// inbox route's inline path, the BullMQ inbox worker replaying a queued job,
|
|
210
|
+
// and any direct connector call) converges here, and every verb — Follow,
|
|
211
|
+
// Accept, Undo, Reject, and the app's content verbs — is dispatched below it.
|
|
212
|
+
// So one check here suspends an instance completely: no posts, no actors, no
|
|
213
|
+
// follows, no boosts, no notifications.
|
|
214
|
+
//
|
|
215
|
+
// It is keyed on `verifiedActorUri`, the origin the HTTP signature actually
|
|
216
|
+
// proved, which is also the identity every handler downstream trusts — so
|
|
217
|
+
// there is no second, weaker identity a hostile payload could be routed under.
|
|
218
|
+
const originHost = actorUriHost(verifiedActorUri);
|
|
219
|
+
if (originHost === null || config.isBlockedDomain(originHost)) {
|
|
220
|
+
logger.warn(`[Federation] dropping inbound activity from blocked origin ${verifiedActorUri} (host=${originHost ?? 'unparseable'})`);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
// Inbound JSON arrives from arbitrary, UNTRUSTED remote servers. Validate the
|
|
224
|
+
// whole activity BEFORE any handler reads it. The validation never throws; a
|
|
225
|
+
// malformed or hostile payload is rejected cleanly here.
|
|
226
|
+
const validation = config.validateActivity(activity);
|
|
227
|
+
if (!validation.ok) {
|
|
228
|
+
const rawType = typeof activity?.type === 'string'
|
|
229
|
+
? activity.type
|
|
230
|
+
: Array.isArray(activity?.type)
|
|
231
|
+
? activity.type.join(',')
|
|
232
|
+
: 'unknown';
|
|
233
|
+
const rawId = typeof activity?.id === 'string' ? activity.id : 'unknown';
|
|
234
|
+
logger.warn(`[Federation] dropping invalid inbound activity from ${verifiedActorUri} (type=${rawType}, id=${rawId}): ${validation.summary}`);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
switch (validation.type) {
|
|
238
|
+
case 'Follow':
|
|
239
|
+
await handleIncomingFollow(activity, verifiedActorUri);
|
|
240
|
+
break;
|
|
241
|
+
case 'Undo':
|
|
242
|
+
await handleUndo(activity, verifiedActorUri);
|
|
243
|
+
break;
|
|
244
|
+
case 'Accept':
|
|
245
|
+
await handleAccept(activity, verifiedActorUri);
|
|
246
|
+
break;
|
|
247
|
+
case 'Reject':
|
|
248
|
+
await handleReject(activity, verifiedActorUri);
|
|
249
|
+
break;
|
|
250
|
+
// Content verbs — the app owns these (posts, engagement, actor profile edits).
|
|
251
|
+
case 'Create':
|
|
252
|
+
case 'Delete':
|
|
253
|
+
case 'Like':
|
|
254
|
+
case 'Announce':
|
|
255
|
+
case 'Update':
|
|
256
|
+
await config.onContentActivity(activity, verifiedActorUri);
|
|
257
|
+
break;
|
|
258
|
+
default:
|
|
259
|
+
logger.debug(`Unhandled activity type: ${validation.type}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return { processInboxActivity };
|
|
263
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@oxy.so/federation/node` — the runnable Node/Express federation engine.
|
|
3
|
+
*
|
|
4
|
+
* A SEPARATE subpath from the package root so this Node-only code never enters
|
|
5
|
+
* isomorphic bundles that import `@oxy.so/federation`.
|
|
6
|
+
*
|
|
7
|
+
* Phase 2 (HTTP signatures): the signed-fetch transport — a signed ActivityPub
|
|
8
|
+
* GET with per-hop HTTP-signature re-signing, built over an app-injected
|
|
9
|
+
* SSRF-safe single-hop transport. The pure sign/verify crypto it drives lives in
|
|
10
|
+
* the isomorphic `.` entry.
|
|
11
|
+
*
|
|
12
|
+
* Phase 3 (actor model + resolution): the identity bridge + remote-actor resolver.
|
|
13
|
+
*
|
|
14
|
+
* Phase 4 (delivery + follow lifecycle + routers + inbound dispatch): the outbound
|
|
15
|
+
* delivery transport + follow protocol, the inbound dispatcher (Follow/Accept/
|
|
16
|
+
* Undo(Follow)/Reject, delegating content verbs to the app), and the webfinger +
|
|
17
|
+
* actor + inbox + follow-graph Express routers.
|
|
18
|
+
*/
|
|
19
|
+
export { createSignedFetch, } from './signedFetch.js';
|
|
20
|
+
/**
|
|
21
|
+
* The actor↔Oxy-user identity bridge — the default implementation of the
|
|
22
|
+
* `PUT /users/resolve` + actor-gone archive/delete seam over an injected
|
|
23
|
+
* service-request transport.
|
|
24
|
+
*/
|
|
25
|
+
export { createIdentityBridge, } from './identityBridge.js';
|
|
26
|
+
/**
|
|
27
|
+
* Remote-actor resolution/caching/refresh (webfinger, signed actor fetch,
|
|
28
|
+
* 410-Gone tombstone) over a bring-your-own-store adapter, the identity bridge,
|
|
29
|
+
* and injected transports + text normalization.
|
|
30
|
+
*/
|
|
31
|
+
export { createActorResolver, ActorResolver, } from './actorResolver.js';
|
|
32
|
+
/**
|
|
33
|
+
* Outbound activity delivery + the follow lifecycle (Follow / Undo(Follow) /
|
|
34
|
+
* Accept(Follow)) + the `Update(Person)` actor rebroadcast, over injected key
|
|
35
|
+
* custody, an SSRF-safe delivery transport, and bring-your-own-store adapters.
|
|
36
|
+
*/
|
|
37
|
+
export { createDeliveryService, } from './delivery.js';
|
|
38
|
+
/**
|
|
39
|
+
* Inbound ActivityPub dispatch — the untrusted-activity validator + switch, the
|
|
40
|
+
* follow-protocol handlers (Follow / Accept / Undo(Follow) / Reject) over the
|
|
41
|
+
* identity + store adapters, and the `onContentActivity` seam every content verb
|
|
42
|
+
* (Create / Announce / Like / Delete / Update, non-follow Undo) is handed to.
|
|
43
|
+
*/
|
|
44
|
+
export { createInboundDispatcher, ActorResolutionPendingError, } from './inboundDispatch.js';
|
|
45
|
+
/** The WebFinger + host-meta discovery router (domain-parameterized, consent-gated). */
|
|
46
|
+
export { createWebfingerRouter, } from './webfingerRouter.js';
|
|
47
|
+
/**
|
|
48
|
+
* The ActivityPub actor + inbox + follow-graph router (actor GET incl. the
|
|
49
|
+
* `instance` actor, inbox POST with HTTP-sig verify, followers/following pages).
|
|
50
|
+
*/
|
|
51
|
+
export { createActorRouter, } from './actorRouter.js';
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `signedFetch` — a signed ActivityPub GET with per-hop HTTP-signature
|
|
3
|
+
* re-signing, built over an injected SSRF-safe single-hop transport.
|
|
4
|
+
*
|
|
5
|
+
* WHY A FACTORY OVER AN INJECTED TRANSPORT (not core `safeFetch` directly)
|
|
6
|
+
* -----------------------------------------------------------------------
|
|
7
|
+
* An HTTP signature is bound to the `(request-target)`/`host` of ONE specific
|
|
8
|
+
* URL, so on a redirect the signature MUST be recomputed for the new target.
|
|
9
|
+
* `@oxy.so/core/server`'s `safeFetch` follows redirects internally and re-sends
|
|
10
|
+
* the ORIGINAL headers on each hop (it never re-signs, and it destroys redirect
|
|
11
|
+
* bodies), so it cannot back per-hop re-signing. Instead — mirroring how
|
|
12
|
+
* `@oxy.so/protocol/node` injects its `NodeFetch` adapter over `safeFetch` — this
|
|
13
|
+
* factory takes a single-hop transport that validates + IP-pins ONE request and
|
|
14
|
+
* returns the response WITHOUT following redirects. The engine owns the
|
|
15
|
+
* federation policy (signing, the bounded redirect loop that re-signs each hop,
|
|
16
|
+
* the unsigned 5xx fallback); the app supplies the SSRF transport (Mention adapts
|
|
17
|
+
* its `@oxy.so/core/server`-based single-hop fetch), keeping the SSRF/DNS-pin
|
|
18
|
+
* policy in ONE place.
|
|
19
|
+
*/
|
|
20
|
+
import { signRequest } from '../httpSignature.js';
|
|
21
|
+
/** Total time budget for a single signed hop (connect + response headers). */
|
|
22
|
+
const SIGNED_FETCH_TIMEOUT_MS = 10000;
|
|
23
|
+
/** Bounded redirect budget for signed AP GETs; each hop is re-validated and re-signed. */
|
|
24
|
+
const SIGNED_FETCH_MAX_REDIRECTS = 3;
|
|
25
|
+
const REDIRECT_STATUS_CODES = new Set([301, 302, 303, 307, 308]);
|
|
26
|
+
function requestInitHeaders(init) {
|
|
27
|
+
if (!init.headers)
|
|
28
|
+
return {};
|
|
29
|
+
if (init.headers instanceof Headers) {
|
|
30
|
+
// `Headers.forEach` is typed on the base `DOM` lib, whereas
|
|
31
|
+
// `Headers.entries()` requires `DOM.Iterable` — which this package's build
|
|
32
|
+
// tsconfig deliberately omits (isomorphic node/web split). `forEach` keeps
|
|
33
|
+
// the flatten build-safe under every lib config (Docker + local).
|
|
34
|
+
const flattened = {};
|
|
35
|
+
init.headers.forEach((value, key) => {
|
|
36
|
+
flattened[key] = value;
|
|
37
|
+
});
|
|
38
|
+
return flattened;
|
|
39
|
+
}
|
|
40
|
+
if (Array.isArray(init.headers))
|
|
41
|
+
return Object.fromEntries(init.headers);
|
|
42
|
+
return init.headers;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Build a `signedFetch(url, accept, init?)`:
|
|
46
|
+
*
|
|
47
|
+
* Signs a GET request using the instance actor key (via the injected signer) and
|
|
48
|
+
* performs it under the SSRF-safe contract (the injected single-hop transport
|
|
49
|
+
* validates the URL AND pins the TCP connection to the validated IP).
|
|
50
|
+
*
|
|
51
|
+
* Redirects are followed manually (bounded by {@link SIGNED_FETCH_MAX_REDIRECTS}),
|
|
52
|
+
* re-validating AND re-signing each hop — an HTTP signature is bound to the
|
|
53
|
+
* `(request-target)`/`host` of a specific URL. When the caller passes
|
|
54
|
+
* `init.redirect === 'manual'`, the redirect `Response` is returned directly so
|
|
55
|
+
* the caller can apply its own stricter redirect policy.
|
|
56
|
+
*
|
|
57
|
+
* Signed for servers that enforce authorized fetch (e.g. Threads). On a 5xx the
|
|
58
|
+
* request is retried unsigned (same SSRF-safe path) as a fallback for public
|
|
59
|
+
* resources.
|
|
60
|
+
*/
|
|
61
|
+
export function createSignedFetch(config) {
|
|
62
|
+
return async function signedFetch(url, accept, init = {}) {
|
|
63
|
+
const acceptHeader = `${accept}, application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
|
|
64
|
+
const keyId = await config.getInstanceKeyId();
|
|
65
|
+
const extraHeaders = requestInitHeaders(init);
|
|
66
|
+
const manualRedirect = init.redirect === 'manual';
|
|
67
|
+
const fetchOnce = async (targetUrl, signed) => {
|
|
68
|
+
const sigHeaders = signed ? await signRequest(config.sign, keyId, 'GET', targetUrl) : {};
|
|
69
|
+
return config.fetchSingleHop(targetUrl, {
|
|
70
|
+
headers: {
|
|
71
|
+
Accept: acceptHeader,
|
|
72
|
+
'User-Agent': config.userAgent,
|
|
73
|
+
...sigHeaders,
|
|
74
|
+
...extraHeaders,
|
|
75
|
+
},
|
|
76
|
+
signal: init.signal ?? AbortSignal.timeout(SIGNED_FETCH_TIMEOUT_MS),
|
|
77
|
+
headersTimeoutMs: SIGNED_FETCH_TIMEOUT_MS,
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const fetchFollowingRedirects = async (initialUrl, signed) => {
|
|
81
|
+
let currentUrl = initialUrl;
|
|
82
|
+
for (let hop = 0; hop <= SIGNED_FETCH_MAX_REDIRECTS; hop++) {
|
|
83
|
+
const res = await fetchOnce(currentUrl, signed);
|
|
84
|
+
if (!REDIRECT_STATUS_CODES.has(res.status)) {
|
|
85
|
+
return { res, finalUrl: currentUrl };
|
|
86
|
+
}
|
|
87
|
+
// The caller asked to handle redirects itself (stricter per-hop policy).
|
|
88
|
+
if (manualRedirect) {
|
|
89
|
+
return { res, finalUrl: currentUrl };
|
|
90
|
+
}
|
|
91
|
+
const location = res.headers.get('location');
|
|
92
|
+
if (hop === SIGNED_FETCH_MAX_REDIRECTS || !location) {
|
|
93
|
+
return { res, finalUrl: currentUrl };
|
|
94
|
+
}
|
|
95
|
+
currentUrl = new URL(location, currentUrl).toString();
|
|
96
|
+
}
|
|
97
|
+
throw new Error('redirect loop exhausted');
|
|
98
|
+
};
|
|
99
|
+
const { res, finalUrl } = await fetchFollowingRedirects(url, true);
|
|
100
|
+
// If the remote server returns a 5xx (e.g. it can't resolve our keyId to
|
|
101
|
+
// verify the signature), retry without the signature as a fallback for public
|
|
102
|
+
// resources. Retry from the post-redirect URL so we don't restart a chain that
|
|
103
|
+
// already landed on the failing hop.
|
|
104
|
+
if (res.status >= 500) {
|
|
105
|
+
config.logger?.info(`[FedSync] signedFetch got ${res.status} for ${finalUrl}, retrying unsigned`);
|
|
106
|
+
return fetchFollowingRedirects(finalUrl, false).then(({ res: unsignedRes }) => unsignedRes);
|
|
107
|
+
}
|
|
108
|
+
// A 401/403 on a signed request means the remote rejected OUR signature (e.g.
|
|
109
|
+
// it could not resolve/verify our keyId, or our instance key pair is
|
|
110
|
+
// missing/invalid because the service token could not be acquired). Without a
|
|
111
|
+
// log this silently yields zero results — surface it so the failure mode is
|
|
112
|
+
// observable in production. The caller still receives the response and decides
|
|
113
|
+
// how to proceed; we do not change control flow here.
|
|
114
|
+
if (res.status === 401 || res.status === 403) {
|
|
115
|
+
config.logger?.warn(`[FedSync] signedFetch got ${res.status} ${res.statusText} for ${url} — remote rejected our HTTP signature (check instance key pair / service token); returning the failed response so no posts are imported from this source`);
|
|
116
|
+
}
|
|
117
|
+
return res;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WebFinger + host-meta discovery router.
|
|
3
|
+
*
|
|
4
|
+
* `/.well-known/webfinger` resolves `acct:<user>@<domain>` to the local actor URL;
|
|
5
|
+
* `/.well-known/host-meta(.json)` advertises the WebFinger LRDD template. Both are
|
|
6
|
+
* domain-parameterized (each app answers for its OWN `domain`) and both enforce
|
|
7
|
+
* the fediverse-sharing consent gate — a disabled/unknown user 404s
|
|
8
|
+
* indistinguishably. The JRD cache (Mention: Redis) is injected so the caching
|
|
9
|
+
* strategy stays app-side; the response bytes + the 404-when-off semantics live
|
|
10
|
+
* here so every Oxy app discovers identically.
|
|
11
|
+
*
|
|
12
|
+
* Extracted behaviour-identically from Mention's `wellKnown.routes.ts`.
|
|
13
|
+
*/
|
|
14
|
+
import { Router } from 'express';
|
|
15
|
+
import { isSameFederationHost } from '../apUri.js';
|
|
16
|
+
import { INSTANCE_ACTOR_USERNAME, normalizeActorUsername } from '../urls.js';
|
|
17
|
+
/** 1 hour, in seconds — the WebFinger JRD cache TTL + response `max-age`. */
|
|
18
|
+
const WEBFINGER_CACHE_TTL = 3600;
|
|
19
|
+
/** 24h — host-meta is effectively static. */
|
|
20
|
+
const HOST_META_CACHE_CONTROL = `max-age=${60 * 60 * 24}`;
|
|
21
|
+
/** Build the WebFinger + host-meta discovery router for an app's domain. */
|
|
22
|
+
export function createWebfingerRouter(config) {
|
|
23
|
+
const router = Router();
|
|
24
|
+
const { domain } = config;
|
|
25
|
+
const webfingerTemplate = `https://${domain}/.well-known/webfinger?resource={uri}`;
|
|
26
|
+
router.get('/webfinger', async (req, res) => {
|
|
27
|
+
if (!config.federationEnabled) {
|
|
28
|
+
return res.status(404).json({ error: 'Federation is disabled' });
|
|
29
|
+
}
|
|
30
|
+
const resource = typeof req.query.resource === 'string' ? req.query.resource : undefined;
|
|
31
|
+
if (!resource || !resource.startsWith('acct:')) {
|
|
32
|
+
return res.status(400).json({ error: 'Resource must start with acct:' });
|
|
33
|
+
}
|
|
34
|
+
const acct = resource.replace('acct:', '');
|
|
35
|
+
const atIndex = acct.indexOf('@');
|
|
36
|
+
if (atIndex === -1) {
|
|
37
|
+
return res.status(400).json({ error: 'Invalid acct format' });
|
|
38
|
+
}
|
|
39
|
+
const username = normalizeActorUsername(acct.substring(0, atIndex));
|
|
40
|
+
const acctDomain = acct.substring(atIndex + 1).trim();
|
|
41
|
+
if (!isSameFederationHost(acctDomain, domain)) {
|
|
42
|
+
return res.status(404).json({ error: 'Unknown domain' });
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
// The instance actor is NOT an Oxy user: it has no profile, no consent
|
|
46
|
+
// record, and `resolveUser` will never find it — so it must be answered
|
|
47
|
+
// here, ahead of both the resolve and the sharing-consent gate, exactly as
|
|
48
|
+
// the actor route answers ahead of them. Without this branch the server
|
|
49
|
+
// actor is served as an actor but is not WebFinger-resolvable, and every
|
|
50
|
+
// secure-mode instance then refuses our signed GETs: Mastodon's
|
|
51
|
+
// `FetchRemoteKeyService#find_actor` calls `FetchRemoteActorService`
|
|
52
|
+
// WITHOUT `only_key:`, which runs `check_webfinger!` unconditionally, so a
|
|
53
|
+
// 404 here raises `Webfinger::Error` and the signed fetch 401s.
|
|
54
|
+
//
|
|
55
|
+
// Deliberately NOT cached: the document is static (no I/O to amortize) and
|
|
56
|
+
// routing it through the app's JRD cache would let one stale or evicted
|
|
57
|
+
// entry make the server actor undiscoverable for a full TTL — which is the
|
|
58
|
+
// outage this branch exists to prevent.
|
|
59
|
+
if (username === INSTANCE_ACTOR_USERNAME) {
|
|
60
|
+
const instanceJrd = {
|
|
61
|
+
subject: `acct:${INSTANCE_ACTOR_USERNAME}@${domain}`,
|
|
62
|
+
links: [
|
|
63
|
+
{
|
|
64
|
+
rel: 'self',
|
|
65
|
+
type: 'application/activity+json',
|
|
66
|
+
// The SAME builder call the actor route uses for the actor `id`.
|
|
67
|
+
// Mastodon compares `webfinger.self_link_href` against the actor
|
|
68
|
+
// uri and rejects a mismatch, so these must not be built twice.
|
|
69
|
+
href: config.urls.actor(INSTANCE_ACTOR_USERNAME),
|
|
70
|
+
},
|
|
71
|
+
// No `profile-page` rel: the server actor has no human-facing page
|
|
72
|
+
// (`/@instance` is not a profile), and the file's existing policy is
|
|
73
|
+
// that a dangling link is worse than an absent one.
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
res.set('Content-Type', 'application/jrd+json; charset=utf-8');
|
|
77
|
+
res.set('Cache-Control', `max-age=${WEBFINGER_CACHE_TTL}`);
|
|
78
|
+
return res.json(instanceJrd);
|
|
79
|
+
}
|
|
80
|
+
// Check the JRD cache first.
|
|
81
|
+
const cached = await config.cache.get(username);
|
|
82
|
+
if (cached) {
|
|
83
|
+
res.set('Content-Type', 'application/jrd+json; charset=utf-8');
|
|
84
|
+
res.set('Cache-Control', `max-age=${WEBFINGER_CACHE_TTL}`);
|
|
85
|
+
return res.json(cached);
|
|
86
|
+
}
|
|
87
|
+
const user = await config.resolveUser(username);
|
|
88
|
+
if (!user)
|
|
89
|
+
return res.status(404).json({ error: 'User not found' });
|
|
90
|
+
// Sharing OFF must be indistinguishable from a nonexistent user — same 404
|
|
91
|
+
// body, no separate error code. UNLIKE the other user-scoped surfaces,
|
|
92
|
+
// webfinger does a SECOND, uncached consent read here rather than reusing
|
|
93
|
+
// the already-resolved `user`: this response is ALSO cached for a full hour
|
|
94
|
+
// below, so a stale-DTO false positive would lock the actor (un)discoverable
|
|
95
|
+
// for up to an hour. An Oxy OUTAGE ('unavailable') on that fresh read falls
|
|
96
|
+
// back to the already-resolved `user` instead of 404ing, so a transient
|
|
97
|
+
// hiccup never makes a real account momentarily undiscoverable.
|
|
98
|
+
const sharingState = await config.consent.getSharingStateByUsername(username);
|
|
99
|
+
if (sharingState === 'disabled' || sharingState === 'unknown-user') {
|
|
100
|
+
return res.status(404).json({ error: 'User not found' });
|
|
101
|
+
}
|
|
102
|
+
if (sharingState === 'unavailable' && !config.consent.isSharingEnabledFromUser(user)) {
|
|
103
|
+
return res.status(404).json({ error: 'User not found' });
|
|
104
|
+
}
|
|
105
|
+
const response = {
|
|
106
|
+
subject: `acct:${username}@${domain}`,
|
|
107
|
+
links: [
|
|
108
|
+
{
|
|
109
|
+
rel: 'self',
|
|
110
|
+
type: 'application/activity+json',
|
|
111
|
+
href: config.urls.actor(username),
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
rel: 'http://webfinger.net/rel/profile-page',
|
|
115
|
+
type: 'text/html',
|
|
116
|
+
href: `https://${domain}/@${username}`,
|
|
117
|
+
},
|
|
118
|
+
// NOTE: the `http://ostatus.org/schema/1.0/subscribe` (remote-follow) rel
|
|
119
|
+
// is intentionally omitted — there is no authorize-interaction endpoint
|
|
120
|
+
// to point it at, and a dangling template would be worse than its absence.
|
|
121
|
+
],
|
|
122
|
+
};
|
|
123
|
+
config.cache.set(username, response);
|
|
124
|
+
res.set('Content-Type', 'application/jrd+json; charset=utf-8');
|
|
125
|
+
res.set('Cache-Control', `max-age=${WEBFINGER_CACHE_TTL}`);
|
|
126
|
+
return res.json(response);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
config.logger.error('WebFinger error:', err);
|
|
130
|
+
return res.status(500).json({ error: 'Internal server error' });
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
router.get('/host-meta', (_req, res) => {
|
|
134
|
+
if (!config.federationEnabled) {
|
|
135
|
+
return res.status(404).json({ error: 'Federation is disabled' });
|
|
136
|
+
}
|
|
137
|
+
const xrd = `<?xml version="1.0" encoding="UTF-8"?>
|
|
138
|
+
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
|
139
|
+
<Link rel="lrdd" type="application/jrd+json" template="${webfingerTemplate}"/>
|
|
140
|
+
</XRD>
|
|
141
|
+
`;
|
|
142
|
+
res.set('Content-Type', 'application/xrd+xml; charset=utf-8');
|
|
143
|
+
res.set('Cache-Control', HOST_META_CACHE_CONTROL);
|
|
144
|
+
return res.send(xrd);
|
|
145
|
+
});
|
|
146
|
+
router.get('/host-meta.json', (_req, res) => {
|
|
147
|
+
if (!config.federationEnabled) {
|
|
148
|
+
return res.status(404).json({ error: 'Federation is disabled' });
|
|
149
|
+
}
|
|
150
|
+
res.set('Content-Type', 'application/jrd+json; charset=utf-8');
|
|
151
|
+
res.set('Cache-Control', HOST_META_CACHE_CONTROL);
|
|
152
|
+
return res.json({
|
|
153
|
+
links: [
|
|
154
|
+
{
|
|
155
|
+
rel: 'lrdd',
|
|
156
|
+
type: 'application/jrd+json',
|
|
157
|
+
template: webfingerTemplate,
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
return router;
|
|
163
|
+
}
|
package/dist/esm/urls.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domain-parameterized ActivityPub URL builders.
|
|
3
|
+
*
|
|
4
|
+
* Every Oxy app federates under its OWN domain (`@user@mention.earth`,
|
|
5
|
+
* `@user@homiio.com`, `@user@oxy.so`), so the actor/inbox/outbox/collection URLs
|
|
6
|
+
* an app mints must be scoped to that app's instance — never a module-level
|
|
7
|
+
* constant. {@link createUrlBuilders} is the factory each app instantiates once
|
|
8
|
+
* with its `FEDERATION_DOMAIN` (and, optionally, a distinct `ACTOR_DOMAIN`); the
|
|
9
|
+
* returned builders produce the exact URL shapes Mastodon and the rest of the
|
|
10
|
+
* fediverse expect, byte-for-byte identical to the strings the actor document
|
|
11
|
+
* advertises.
|
|
12
|
+
*
|
|
13
|
+
* `actor()` is scoped to `actorDomain` (the host in the actor `id` / `publicKey`
|
|
14
|
+
* owner) while every other builder is scoped to `domain`; in the common case both
|
|
15
|
+
* are the same host. The two are kept separate so a deployment that serves the
|
|
16
|
+
* actor namespace from a different host than the webfinger/inbox host can still
|
|
17
|
+
* advertise a self-consistent actor.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Build the ActivityPub URL builders for an app instance.
|
|
21
|
+
*
|
|
22
|
+
* @param domain the app's federation domain (webfinger / inbox / collections host).
|
|
23
|
+
* @param actorDomain the host that owns the actor `id`; defaults to `domain`.
|
|
24
|
+
*/
|
|
25
|
+
export function createUrlBuilders(domain, actorDomain = domain) {
|
|
26
|
+
return {
|
|
27
|
+
actor: (username) => `https://${actorDomain}/ap/users/${username}`,
|
|
28
|
+
inbox: (username) => `https://${domain}/ap/users/${username}/inbox`,
|
|
29
|
+
outbox: (username) => `https://${domain}/ap/users/${username}/outbox`,
|
|
30
|
+
featured: (username) => `https://${domain}/ap/users/${username}/collections/featured`,
|
|
31
|
+
followers: (username) => `https://${domain}/ap/users/${username}/followers`,
|
|
32
|
+
following: (username) => `https://${domain}/ap/users/${username}/following`,
|
|
33
|
+
sharedInbox: () => `https://${domain}/ap/inbox`,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The reserved local-part of the instance-wide server actor (`Application`).
|
|
38
|
+
*
|
|
39
|
+
* This actor signs the engine's own outbound GETs; it is NOT an Oxy user, has no
|
|
40
|
+
* profile page and no fediverse-sharing consent record. Both the actor route and
|
|
41
|
+
* the WebFinger route must answer for it, and Mastodon compares the WebFinger
|
|
42
|
+
* `self` href against the actor `id` byte-for-byte before it will trust a signed
|
|
43
|
+
* fetch — so the two routers derive that URL from THIS constant through the same
|
|
44
|
+
* {@link UrlBuilders.actor} builder rather than each spelling the name themselves.
|
|
45
|
+
*/
|
|
46
|
+
export const INSTANCE_ACTOR_USERNAME = 'instance';
|
|
47
|
+
/** Normalize a local actor username from a path segment or WebFinger acct local-part. */
|
|
48
|
+
export function normalizeActorUsername(username) {
|
|
49
|
+
return username.trim().toLowerCase();
|
|
50
|
+
}
|