@oxy.so/federation 2.14.0 → 2.16.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/dist/cjs/apUri.js +16 -0
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/node/actorResolver.js +11 -11
- package/dist/cjs/node/signedFetch.js +10 -4
- package/dist/esm/apUri.js +15 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/node/actorResolver.js +12 -12
- package/dist/esm/node/signedFetch.js +10 -4
- package/dist/types/apUri.d.ts +7 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/node/index.d.ts +1 -1
- package/dist/types/node/signedFetch.d.ts +18 -4
- package/package.json +1 -1
- package/src/__tests__/actorCollectionCounts.test.ts +29 -0
- package/src/__tests__/signedFetch.test.ts +59 -0
- package/src/apUri.ts +15 -0
- package/src/index.ts +1 -0
- package/src/node/actorResolver.ts +12 -12
- package/src/node/index.ts +1 -0
- package/src/node/signedFetch.ts +30 -6
package/dist/cjs/apUri.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
14
|
exports.canonicalFederationHost = canonicalFederationHost;
|
|
15
15
|
exports.isSameFederationHost = isSameFederationHost;
|
|
16
|
+
exports.apIri = apIri;
|
|
16
17
|
exports.extractActorUriFromActivityId = extractActorUriFromActivityId;
|
|
17
18
|
exports.createDomainPolicy = createDomainPolicy;
|
|
18
19
|
/** Path segments that typically separate an actor path from a post ID in ActivityPub URIs. */
|
|
@@ -75,6 +76,21 @@ function isSameFederationHost(a, b) {
|
|
|
75
76
|
return false;
|
|
76
77
|
return canonicalA === canonicalFederationHost(b);
|
|
77
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* The IRI a JSON-LD node reference names: the string itself, or the `id` of an
|
|
81
|
+
* embedded object. ActivityStreams lets a server embed a referenced node (an
|
|
82
|
+
* actor's `followers` collection with its `totalItems`, an `endpoints` value)
|
|
83
|
+
* instead of linking it, and both mean the same node. Anything else names none.
|
|
84
|
+
*/
|
|
85
|
+
function apIri(value) {
|
|
86
|
+
if (typeof value === 'string')
|
|
87
|
+
return value || undefined;
|
|
88
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
89
|
+
const id = value.id;
|
|
90
|
+
return typeof id === 'string' && id ? id : undefined;
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
78
94
|
/**
|
|
79
95
|
* Given an ActivityPub activity/object ID (URL), extract the actor URI by
|
|
80
96
|
* trimming everything from the first recognised post-path segment onward.
|
package/dist/cjs/index.js
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* knowledge of any app's post shape.
|
|
23
23
|
*/
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.groupFollowersCollection = exports.groupOutboxCollection = exports.isGroupActorKind = exports.GROUP_OUTBOX_SIZE = exports.LOCAL_ACTOR_TYPE_BY_ACCOUNT_KIND = exports.AP_ACTOR_TYPES = exports.isApActorType = exports.accountKindForActor = exports.localActorTypeForAccountKind = exports.normalizeAlsoKnownAs = exports.createLocalActorBuilder = exports.createDomainPolicy = exports.extractActorUriFromActivityId = exports.isSameFederationHost = exports.canonicalFederationHost = exports.AP_CONTEXT = exports.readProxyDeclarations = exports.upstreamHandleFromProxyOf = exports.upstreamHandleFromPreferredUsername = exports.upstreamHandleFromAutomatedActor = exports.upstreamHandleFromAlsoKnownAs = exports.upstreamHandleFromProfileField = exports.federatedUsernameFromUpstreamUrl = exports.parseUpstreamProfileUrl = exports.upstreamProfileUrl = exports.stripBridgeBoilerplate = exports.createBridgeRelabeller = exports.atprotoIdentityHandle = exports.isUnresolvedAtprotoHandle = exports.UNRESOLVED_ATPROTO_HANDLE = exports.blueskyHandleFromUsername = exports.blueskyUsernameFromHandle = exports.BSKY_NETWORK_DOMAIN = exports.FEDERATION_NETWORKS = exports.INSTANCE_ACTOR_USERNAME = exports.normalizeActorUsername = exports.createUrlBuilders = exports.DEFAULT_SIGNED_CONTENT_TYPE = exports.HTTP_SIGNATURE_ALGORITHM = exports.verifyHttpSignature = exports.signRequest = void 0;
|
|
25
|
+
exports.groupFollowersCollection = exports.groupOutboxCollection = exports.isGroupActorKind = exports.GROUP_OUTBOX_SIZE = exports.LOCAL_ACTOR_TYPE_BY_ACCOUNT_KIND = exports.AP_ACTOR_TYPES = exports.isApActorType = exports.accountKindForActor = exports.localActorTypeForAccountKind = exports.normalizeAlsoKnownAs = exports.createLocalActorBuilder = exports.createDomainPolicy = exports.apIri = exports.extractActorUriFromActivityId = exports.isSameFederationHost = exports.canonicalFederationHost = exports.AP_CONTEXT = exports.readProxyDeclarations = exports.upstreamHandleFromProxyOf = exports.upstreamHandleFromPreferredUsername = exports.upstreamHandleFromAutomatedActor = exports.upstreamHandleFromAlsoKnownAs = exports.upstreamHandleFromProfileField = exports.federatedUsernameFromUpstreamUrl = exports.parseUpstreamProfileUrl = exports.upstreamProfileUrl = exports.stripBridgeBoilerplate = exports.createBridgeRelabeller = exports.atprotoIdentityHandle = exports.isUnresolvedAtprotoHandle = exports.UNRESOLVED_ATPROTO_HANDLE = exports.blueskyHandleFromUsername = exports.blueskyUsernameFromHandle = exports.BSKY_NETWORK_DOMAIN = exports.FEDERATION_NETWORKS = exports.INSTANCE_ACTOR_USERNAME = exports.normalizeActorUsername = exports.createUrlBuilders = exports.DEFAULT_SIGNED_CONTENT_TYPE = exports.HTTP_SIGNATURE_ALGORITHM = exports.verifyHttpSignature = exports.signRequest = void 0;
|
|
26
26
|
/**
|
|
27
27
|
* HTTP Signatures (draft-cavage) — the pure sign/verify crypto every Oxy app's
|
|
28
28
|
* ActivityPub federation shares. Private-key custody is injected; the key never
|
|
@@ -89,6 +89,7 @@ var apUri_1 = require("./apUri");
|
|
|
89
89
|
Object.defineProperty(exports, "canonicalFederationHost", { enumerable: true, get: function () { return apUri_1.canonicalFederationHost; } });
|
|
90
90
|
Object.defineProperty(exports, "isSameFederationHost", { enumerable: true, get: function () { return apUri_1.isSameFederationHost; } });
|
|
91
91
|
Object.defineProperty(exports, "extractActorUriFromActivityId", { enumerable: true, get: function () { return apUri_1.extractActorUriFromActivityId; } });
|
|
92
|
+
Object.defineProperty(exports, "apIri", { enumerable: true, get: function () { return apUri_1.apIri; } });
|
|
92
93
|
Object.defineProperty(exports, "createDomainPolicy", { enumerable: true, get: function () { return apUri_1.createDomainPolicy; } });
|
|
93
94
|
/**
|
|
94
95
|
* The single builder of a LOCAL user's ActivityPub actor document —
|
|
@@ -265,7 +265,7 @@ class ActorResolver {
|
|
|
265
265
|
}
|
|
266
266
|
const actor = await readBoundedJson(res, ACTOR_BODY_MAX_BYTES);
|
|
267
267
|
const actorId = asString(actor.id);
|
|
268
|
-
const actorInbox =
|
|
268
|
+
const actorInbox = (0, apUri_1.apIri)(actor.inbox);
|
|
269
269
|
if (!actorId || !actorInbox) {
|
|
270
270
|
this.config.logger.info(`[FedSync] fetchRemoteActor missing fields for ${currentUri}: id=${!!actor.id} inbox=${!!actor.inbox} type=${String(actor.type)} keys=${Object.keys(actor).join(',')}`);
|
|
271
271
|
return null;
|
|
@@ -306,9 +306,9 @@ class ActorResolver {
|
|
|
306
306
|
const actorPublicKey = asRecord(actor.publicKey);
|
|
307
307
|
// Fetch collection counts (followers, following, posts) in parallel
|
|
308
308
|
const [followersCount, followingCount, postsCount] = await Promise.all([
|
|
309
|
-
this.fetchCollectionCount(
|
|
310
|
-
this.fetchCollectionCount(
|
|
311
|
-
this.fetchCollectionCount(
|
|
309
|
+
this.fetchCollectionCount((0, apUri_1.apIri)(actor.followers)),
|
|
310
|
+
this.fetchCollectionCount((0, apUri_1.apIri)(actor.following)),
|
|
311
|
+
this.fetchCollectionCount((0, apUri_1.apIri)(actor.outbox)),
|
|
312
312
|
]);
|
|
313
313
|
// Extract profile fields (PropertyValue attachments). Sanitize BEFORE
|
|
314
314
|
// normalizing: the canonical normalizer collapses whitespace, it never
|
|
@@ -345,7 +345,7 @@ class ActorResolver {
|
|
|
345
345
|
? actor.alsoKnownAs.filter((v) => typeof v === 'string')
|
|
346
346
|
: undefined;
|
|
347
347
|
// A move target is an actor URL; anything else is not one.
|
|
348
|
-
const movedToValue =
|
|
348
|
+
const movedToValue = (0, apUri_1.apIri)(actor.movedTo);
|
|
349
349
|
const movedTo = movedToValue && /^https?:\/\//.test(movedToValue) ? movedToValue : undefined;
|
|
350
350
|
// The IDENTITY the actor is stored under in Oxy, which is not necessarily the
|
|
351
351
|
// host it was fetched from — see `DeriveNetworkIdentity`. Everything below
|
|
@@ -385,10 +385,10 @@ class ActorResolver {
|
|
|
385
385
|
avatarUrl,
|
|
386
386
|
headerUrl,
|
|
387
387
|
inboxUrl: actorInbox,
|
|
388
|
-
outboxUrl:
|
|
389
|
-
sharedInboxUrl:
|
|
390
|
-
followersUrl:
|
|
391
|
-
followingUrl:
|
|
388
|
+
outboxUrl: (0, apUri_1.apIri)(actor.outbox),
|
|
389
|
+
sharedInboxUrl: (0, apUri_1.apIri)(actorEndpoints?.sharedInbox),
|
|
390
|
+
followersUrl: (0, apUri_1.apIri)(actor.followers),
|
|
391
|
+
followingUrl: (0, apUri_1.apIri)(actor.following),
|
|
392
392
|
publicKeyPem: asString(actorPublicKey?.publicKeyPem) || undefined,
|
|
393
393
|
publicKeyId: asString(actorPublicKey?.id) || undefined,
|
|
394
394
|
type: asString(actor.type) || 'Person',
|
|
@@ -398,8 +398,8 @@ class ActorResolver {
|
|
|
398
398
|
memorial: actor.memorial === true,
|
|
399
399
|
suspended: actor.suspended === true,
|
|
400
400
|
fields,
|
|
401
|
-
featuredUrl:
|
|
402
|
-
featuredTagsUrl:
|
|
401
|
+
featuredUrl: (0, apUri_1.apIri)(actor.featured),
|
|
402
|
+
featuredTagsUrl: (0, apUri_1.apIri)(actor.featuredTags),
|
|
403
403
|
alsoKnownAs,
|
|
404
404
|
...(movedTo && { movedTo }),
|
|
405
405
|
networkAcct: networkIdentity?.federatedUsername,
|
|
@@ -57,14 +57,16 @@ function requestInitHeaders(init) {
|
|
|
57
57
|
* `init.redirect === 'manual'`, the redirect `Response` is returned directly so
|
|
58
58
|
* the caller can apply its own stricter redirect policy.
|
|
59
59
|
*
|
|
60
|
-
* Signed for servers that enforce authorized fetch (e.g. Threads).
|
|
61
|
-
* request is retried unsigned (same SSRF-safe
|
|
62
|
-
* resources.
|
|
60
|
+
* Signed for servers that enforce authorized fetch (e.g. Threads). An
|
|
61
|
+
* instance-signed request that gets a 5xx is retried unsigned (same SSRF-safe
|
|
62
|
+
* path) as a fallback for public resources. `init.keyId` signs as another actor
|
|
63
|
+
* (see {@link SignedFetchInit.keyId}); such a request is never retried unsigned.
|
|
63
64
|
*/
|
|
64
65
|
function createSignedFetch(config) {
|
|
65
66
|
return async function signedFetch(url, accept, init = {}) {
|
|
66
67
|
const acceptHeader = `${accept}, application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
|
|
67
|
-
const
|
|
68
|
+
const actorSigned = init.keyId !== undefined;
|
|
69
|
+
const keyId = init.keyId ?? (await config.getInstanceKeyId());
|
|
68
70
|
const extraHeaders = requestInitHeaders(init);
|
|
69
71
|
const manualRedirect = init.redirect === 'manual';
|
|
70
72
|
const fetchOnce = async (targetUrl, signed) => {
|
|
@@ -100,6 +102,10 @@ function createSignedFetch(config) {
|
|
|
100
102
|
throw new Error('redirect loop exhausted');
|
|
101
103
|
};
|
|
102
104
|
const { res, finalUrl } = await fetchFollowingRedirects(url, true);
|
|
105
|
+
// An actor-signed answer is the remote's verdict for THAT actor (a follower or
|
|
106
|
+
// recipient): an unsigned retry asks a different question, so it never runs.
|
|
107
|
+
if (actorSigned)
|
|
108
|
+
return res;
|
|
103
109
|
// If the remote server returns a 5xx (e.g. it can't resolve our keyId to
|
|
104
110
|
// verify the signature), retry without the signature as a fallback for public
|
|
105
111
|
// resources. Retry from the post-redirect URL so we don't restart a chain that
|
package/dist/esm/apUri.js
CHANGED
|
@@ -69,6 +69,21 @@ export function isSameFederationHost(a, b) {
|
|
|
69
69
|
return false;
|
|
70
70
|
return canonicalA === canonicalFederationHost(b);
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* The IRI a JSON-LD node reference names: the string itself, or the `id` of an
|
|
74
|
+
* embedded object. ActivityStreams lets a server embed a referenced node (an
|
|
75
|
+
* actor's `followers` collection with its `totalItems`, an `endpoints` value)
|
|
76
|
+
* instead of linking it, and both mean the same node. Anything else names none.
|
|
77
|
+
*/
|
|
78
|
+
export function apIri(value) {
|
|
79
|
+
if (typeof value === 'string')
|
|
80
|
+
return value || undefined;
|
|
81
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
82
|
+
const id = value.id;
|
|
83
|
+
return typeof id === 'string' && id ? id : undefined;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
72
87
|
/**
|
|
73
88
|
* Given an ActivityPub activity/object ID (URL), extract the actor URI by
|
|
74
89
|
* trimming everything from the first recognised post-path segment onward.
|
package/dist/esm/index.js
CHANGED
|
@@ -56,7 +56,7 @@ export { FEDERATION_NETWORKS, BSKY_NETWORK_DOMAIN, blueskyUsernameFromHandle, bl
|
|
|
56
56
|
* built from — not a parallel implementation that agrees today.
|
|
57
57
|
*/
|
|
58
58
|
export { AP_CONTEXT } from './apContext.js';
|
|
59
|
-
export { canonicalFederationHost, isSameFederationHost, extractActorUriFromActivityId, createDomainPolicy, } from './apUri.js';
|
|
59
|
+
export { canonicalFederationHost, isSameFederationHost, extractActorUriFromActivityId, apIri, createDomainPolicy, } from './apUri.js';
|
|
60
60
|
/**
|
|
61
61
|
* The single builder of a LOCAL user's ActivityPub actor document —
|
|
62
62
|
* byte-identical across apps, with media resolution injected. The actor `type`
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* e.g. Mention's `IFederatedActor`) so callers keep full typing on the returned
|
|
19
19
|
* document.
|
|
20
20
|
*/
|
|
21
|
-
import { isSameFederationHost } from '../apUri.js';
|
|
21
|
+
import { apIri, isSameFederationHost } from '../apUri.js';
|
|
22
22
|
import { readProxyDeclarations, } from '../networkIdentity.js';
|
|
23
23
|
import { readBodyChunks, ResponseBodyTooLargeError } from './boundedBody.js';
|
|
24
24
|
/**
|
|
@@ -261,7 +261,7 @@ export class ActorResolver {
|
|
|
261
261
|
}
|
|
262
262
|
const actor = await readBoundedJson(res, ACTOR_BODY_MAX_BYTES);
|
|
263
263
|
const actorId = asString(actor.id);
|
|
264
|
-
const actorInbox =
|
|
264
|
+
const actorInbox = apIri(actor.inbox);
|
|
265
265
|
if (!actorId || !actorInbox) {
|
|
266
266
|
this.config.logger.info(`[FedSync] fetchRemoteActor missing fields for ${currentUri}: id=${!!actor.id} inbox=${!!actor.inbox} type=${String(actor.type)} keys=${Object.keys(actor).join(',')}`);
|
|
267
267
|
return null;
|
|
@@ -302,9 +302,9 @@ export class ActorResolver {
|
|
|
302
302
|
const actorPublicKey = asRecord(actor.publicKey);
|
|
303
303
|
// Fetch collection counts (followers, following, posts) in parallel
|
|
304
304
|
const [followersCount, followingCount, postsCount] = await Promise.all([
|
|
305
|
-
this.fetchCollectionCount(
|
|
306
|
-
this.fetchCollectionCount(
|
|
307
|
-
this.fetchCollectionCount(
|
|
305
|
+
this.fetchCollectionCount(apIri(actor.followers)),
|
|
306
|
+
this.fetchCollectionCount(apIri(actor.following)),
|
|
307
|
+
this.fetchCollectionCount(apIri(actor.outbox)),
|
|
308
308
|
]);
|
|
309
309
|
// Extract profile fields (PropertyValue attachments). Sanitize BEFORE
|
|
310
310
|
// normalizing: the canonical normalizer collapses whitespace, it never
|
|
@@ -341,7 +341,7 @@ export class ActorResolver {
|
|
|
341
341
|
? actor.alsoKnownAs.filter((v) => typeof v === 'string')
|
|
342
342
|
: undefined;
|
|
343
343
|
// A move target is an actor URL; anything else is not one.
|
|
344
|
-
const movedToValue =
|
|
344
|
+
const movedToValue = apIri(actor.movedTo);
|
|
345
345
|
const movedTo = movedToValue && /^https?:\/\//.test(movedToValue) ? movedToValue : undefined;
|
|
346
346
|
// The IDENTITY the actor is stored under in Oxy, which is not necessarily the
|
|
347
347
|
// host it was fetched from — see `DeriveNetworkIdentity`. Everything below
|
|
@@ -381,10 +381,10 @@ export class ActorResolver {
|
|
|
381
381
|
avatarUrl,
|
|
382
382
|
headerUrl,
|
|
383
383
|
inboxUrl: actorInbox,
|
|
384
|
-
outboxUrl:
|
|
385
|
-
sharedInboxUrl:
|
|
386
|
-
followersUrl:
|
|
387
|
-
followingUrl:
|
|
384
|
+
outboxUrl: apIri(actor.outbox),
|
|
385
|
+
sharedInboxUrl: apIri(actorEndpoints?.sharedInbox),
|
|
386
|
+
followersUrl: apIri(actor.followers),
|
|
387
|
+
followingUrl: apIri(actor.following),
|
|
388
388
|
publicKeyPem: asString(actorPublicKey?.publicKeyPem) || undefined,
|
|
389
389
|
publicKeyId: asString(actorPublicKey?.id) || undefined,
|
|
390
390
|
type: asString(actor.type) || 'Person',
|
|
@@ -394,8 +394,8 @@ export class ActorResolver {
|
|
|
394
394
|
memorial: actor.memorial === true,
|
|
395
395
|
suspended: actor.suspended === true,
|
|
396
396
|
fields,
|
|
397
|
-
featuredUrl:
|
|
398
|
-
featuredTagsUrl:
|
|
397
|
+
featuredUrl: apIri(actor.featured),
|
|
398
|
+
featuredTagsUrl: apIri(actor.featuredTags),
|
|
399
399
|
alsoKnownAs,
|
|
400
400
|
...(movedTo && { movedTo }),
|
|
401
401
|
networkAcct: networkIdentity?.federatedUsername,
|
|
@@ -54,14 +54,16 @@ function requestInitHeaders(init) {
|
|
|
54
54
|
* `init.redirect === 'manual'`, the redirect `Response` is returned directly so
|
|
55
55
|
* the caller can apply its own stricter redirect policy.
|
|
56
56
|
*
|
|
57
|
-
* Signed for servers that enforce authorized fetch (e.g. Threads).
|
|
58
|
-
* request is retried unsigned (same SSRF-safe
|
|
59
|
-
* resources.
|
|
57
|
+
* Signed for servers that enforce authorized fetch (e.g. Threads). An
|
|
58
|
+
* instance-signed request that gets a 5xx is retried unsigned (same SSRF-safe
|
|
59
|
+
* path) as a fallback for public resources. `init.keyId` signs as another actor
|
|
60
|
+
* (see {@link SignedFetchInit.keyId}); such a request is never retried unsigned.
|
|
60
61
|
*/
|
|
61
62
|
export function createSignedFetch(config) {
|
|
62
63
|
return async function signedFetch(url, accept, init = {}) {
|
|
63
64
|
const acceptHeader = `${accept}, application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
|
|
64
|
-
const
|
|
65
|
+
const actorSigned = init.keyId !== undefined;
|
|
66
|
+
const keyId = init.keyId ?? (await config.getInstanceKeyId());
|
|
65
67
|
const extraHeaders = requestInitHeaders(init);
|
|
66
68
|
const manualRedirect = init.redirect === 'manual';
|
|
67
69
|
const fetchOnce = async (targetUrl, signed) => {
|
|
@@ -97,6 +99,10 @@ export function createSignedFetch(config) {
|
|
|
97
99
|
throw new Error('redirect loop exhausted');
|
|
98
100
|
};
|
|
99
101
|
const { res, finalUrl } = await fetchFollowingRedirects(url, true);
|
|
102
|
+
// An actor-signed answer is the remote's verdict for THAT actor (a follower or
|
|
103
|
+
// recipient): an unsigned retry asks a different question, so it never runs.
|
|
104
|
+
if (actorSigned)
|
|
105
|
+
return res;
|
|
100
106
|
// If the remote server returns a 5xx (e.g. it can't resolve our keyId to
|
|
101
107
|
// verify the signature), retry without the signature as a fallback for public
|
|
102
108
|
// resources. Retry from the post-redirect URL so we don't restart a chain that
|
package/dist/types/apUri.d.ts
CHANGED
|
@@ -59,6 +59,13 @@ export declare function canonicalFederationHost(host: string): string;
|
|
|
59
59
|
* that is not named is not in any set.
|
|
60
60
|
*/
|
|
61
61
|
export declare function isSameFederationHost(a: string, b: string): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* The IRI a JSON-LD node reference names: the string itself, or the `id` of an
|
|
64
|
+
* embedded object. ActivityStreams lets a server embed a referenced node (an
|
|
65
|
+
* actor's `followers` collection with its `totalItems`, an `endpoints` value)
|
|
66
|
+
* instead of linking it, and both mean the same node. Anything else names none.
|
|
67
|
+
*/
|
|
68
|
+
export declare function apIri(value: unknown): string | undefined;
|
|
62
69
|
/**
|
|
63
70
|
* Given an ActivityPub activity/object ID (URL), extract the actor URI by
|
|
64
71
|
* trimming everything from the first recognised post-path segment onward.
|
package/dist/types/index.d.ts
CHANGED
|
@@ -56,7 +56,7 @@ export { FEDERATION_NETWORKS, BSKY_NETWORK_DOMAIN, blueskyUsernameFromHandle, bl
|
|
|
56
56
|
* built from — not a parallel implementation that agrees today.
|
|
57
57
|
*/
|
|
58
58
|
export { AP_CONTEXT } from './apContext';
|
|
59
|
-
export { canonicalFederationHost, isSameFederationHost, extractActorUriFromActivityId, createDomainPolicy, type DomainPolicy, type DomainPolicyConfig, } from './apUri';
|
|
59
|
+
export { canonicalFederationHost, isSameFederationHost, extractActorUriFromActivityId, apIri, createDomainPolicy, type DomainPolicy, type DomainPolicyConfig, } from './apUri';
|
|
60
60
|
/**
|
|
61
61
|
* The single builder of a LOCAL user's ActivityPub actor document —
|
|
62
62
|
* byte-identical across apps, with media resolution injected. The actor `type`
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* Undo(Follow)/Reject, delegating content verbs to the app), and the webfinger +
|
|
17
17
|
* actor + inbox + follow-graph Express routers.
|
|
18
18
|
*/
|
|
19
|
-
export { createSignedFetch, type SignedFetch, type CreateSignedFetchConfig, type SingleHopFetch, type SingleHopFetchInit, type SignedFetchLogger, } from './signedFetch';
|
|
19
|
+
export { createSignedFetch, type SignedFetch, type SignedFetchInit, type CreateSignedFetchConfig, type SingleHopFetch, type SingleHopFetchInit, type SignedFetchLogger, } from './signedFetch';
|
|
20
20
|
/**
|
|
21
21
|
* X's `t.co` wrapper unwrapped into the link the author posted, over the same
|
|
22
22
|
* injected single-hop transport — for bridged X bios and posts.
|
|
@@ -52,8 +52,21 @@ export interface CreateSignedFetchConfig {
|
|
|
52
52
|
/** Optional diagnostics sink (5xx unsigned retry, 401/403 rejection). */
|
|
53
53
|
logger?: SignedFetchLogger;
|
|
54
54
|
}
|
|
55
|
+
/** Request options for one {@link SignedFetch} call. */
|
|
56
|
+
export interface SignedFetchInit extends RequestInit {
|
|
57
|
+
/**
|
|
58
|
+
* Sign this request (and every re-signed redirect hop) with this actor key
|
|
59
|
+
* instead of the instance actor's — e.g. a local user's `…#main-key`, so a
|
|
60
|
+
* server that gates a followers-only or direct object on the requesting actor
|
|
61
|
+
* serves it to a follower or recipient. The signer must hold that key.
|
|
62
|
+
*
|
|
63
|
+
* An actor-signed request is never retried unsigned: its answer is the remote's
|
|
64
|
+
* verdict for that actor, so the response is returned as-is on any status.
|
|
65
|
+
*/
|
|
66
|
+
keyId?: string;
|
|
67
|
+
}
|
|
55
68
|
/** A signed ActivityPub GET, returning the standard WHATWG {@link Response}. */
|
|
56
|
-
export type SignedFetch = (url: string, accept: string, init?:
|
|
69
|
+
export type SignedFetch = (url: string, accept: string, init?: SignedFetchInit) => Promise<Response>;
|
|
57
70
|
/**
|
|
58
71
|
* Build a `signedFetch(url, accept, init?)`:
|
|
59
72
|
*
|
|
@@ -67,8 +80,9 @@ export type SignedFetch = (url: string, accept: string, init?: RequestInit) => P
|
|
|
67
80
|
* `init.redirect === 'manual'`, the redirect `Response` is returned directly so
|
|
68
81
|
* the caller can apply its own stricter redirect policy.
|
|
69
82
|
*
|
|
70
|
-
* Signed for servers that enforce authorized fetch (e.g. Threads).
|
|
71
|
-
* request is retried unsigned (same SSRF-safe
|
|
72
|
-
* resources.
|
|
83
|
+
* Signed for servers that enforce authorized fetch (e.g. Threads). An
|
|
84
|
+
* instance-signed request that gets a 5xx is retried unsigned (same SSRF-safe
|
|
85
|
+
* path) as a fallback for public resources. `init.keyId` signs as another actor
|
|
86
|
+
* (see {@link SignedFetchInit.keyId}); such a request is never retried unsigned.
|
|
73
87
|
*/
|
|
74
88
|
export declare function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxy.so/federation",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.16.0",
|
|
4
4
|
"description": "Oxy Federation — the app-agnostic ActivityPub identity + follow engine substrate: the network-connector contract, normalized cross-network DTOs, HTTP signatures, actor resolution, the outbound delivery transport + follow lifecycle, the inbound dispatcher, and the webfinger/actor/inbox Express routers. Domain-parameterized so every Oxy app backend federates under its own domain.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -118,6 +118,35 @@ describe('fetchRemoteActor — search opt-in and moves', () => {
|
|
|
118
118
|
});
|
|
119
119
|
});
|
|
120
120
|
|
|
121
|
+
describe('fetchRemoteActor — embedded node references', () => {
|
|
122
|
+
it('reads an embedded collection or endpoint by its id, as it reads a linked one', async () => {
|
|
123
|
+
const embed = (id: string) => ({ id, type: 'OrderedCollection', totalItems: 9 });
|
|
124
|
+
const { upserts } = await resolveWith({
|
|
125
|
+
actor: actorDocument({
|
|
126
|
+
inbox: { id: 'https://remote.example/users/bob/inbox' },
|
|
127
|
+
followers: embed(FOLLOWERS),
|
|
128
|
+
following: embed(FOLLOWING),
|
|
129
|
+
outbox: embed(OUTBOX),
|
|
130
|
+
featured: embed('https://remote.example/users/bob/featured'),
|
|
131
|
+
featuredTags: embed('https://remote.example/users/bob/tags'),
|
|
132
|
+
endpoints: { sharedInbox: { id: 'https://remote.example/inbox' } },
|
|
133
|
+
}),
|
|
134
|
+
followers: collection(12),
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
expect(upserts[0]).toMatchObject({
|
|
138
|
+
inboxUrl: 'https://remote.example/users/bob/inbox',
|
|
139
|
+
followersUrl: FOLLOWERS,
|
|
140
|
+
followingUrl: FOLLOWING,
|
|
141
|
+
outboxUrl: OUTBOX,
|
|
142
|
+
featuredUrl: 'https://remote.example/users/bob/featured',
|
|
143
|
+
featuredTagsUrl: 'https://remote.example/users/bob/tags',
|
|
144
|
+
sharedInboxUrl: 'https://remote.example/inbox',
|
|
145
|
+
followersCount: 12,
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
121
150
|
describe('fetchRemoteActor — remote collection counts', () => {
|
|
122
151
|
it('stores reported totals, and a real 0 stays 0', async () => {
|
|
123
152
|
const { upserts } = await resolveWith({
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createSignedFetch, type SingleHopFetchInit } from '../node/signedFetch';
|
|
2
|
+
|
|
3
|
+
const INSTANCE_KEY = 'https://app.example/actor#main-key';
|
|
4
|
+
const ALICE_KEY = 'https://app.example/ap/users/alice#main-key';
|
|
5
|
+
|
|
6
|
+
/** A signed fetch over a scripted transport, recording each hop's URL and signing keyId. */
|
|
7
|
+
function rig(responses: Response[]) {
|
|
8
|
+
const hops: Array<{ url: string; keyId: string | null }> = [];
|
|
9
|
+
const signedFetch = createSignedFetch({
|
|
10
|
+
sign: async () => 'sig',
|
|
11
|
+
getInstanceKeyId: async () => INSTANCE_KEY,
|
|
12
|
+
fetchSingleHop: async (url: string, init: SingleHopFetchInit) => {
|
|
13
|
+
hops.push({ url, keyId: /keyId="([^"]+)"/.exec(init.headers.Signature ?? '')?.[1] ?? null });
|
|
14
|
+
const next = responses.shift();
|
|
15
|
+
if (!next) throw new Error(`unexpected hop to ${url}`);
|
|
16
|
+
return next;
|
|
17
|
+
},
|
|
18
|
+
userAgent: 'test',
|
|
19
|
+
});
|
|
20
|
+
return { signedFetch, hops };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
it('signs as the instance and retries a 5xx unsigned', async () => {
|
|
24
|
+
const { signedFetch, hops } = rig([new Response(null, { status: 502 }), new Response('{}')]);
|
|
25
|
+
|
|
26
|
+
const res = await signedFetch('https://remote.example/notes/1', 'application/activity+json');
|
|
27
|
+
|
|
28
|
+
expect(res.status).toBe(200);
|
|
29
|
+
expect(hops).toEqual([
|
|
30
|
+
{ url: 'https://remote.example/notes/1', keyId: INSTANCE_KEY },
|
|
31
|
+
{ url: 'https://remote.example/notes/1', keyId: null },
|
|
32
|
+
]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('signs every hop as the given actor and never retries an actor-signed request unsigned', async () => {
|
|
36
|
+
const redirected = rig([
|
|
37
|
+
new Response(null, { status: 301, headers: { location: '/statuses/1' } }),
|
|
38
|
+
new Response(null, { status: 401 }),
|
|
39
|
+
]);
|
|
40
|
+
const refused = await redirected.signedFetch(
|
|
41
|
+
'https://remote.example/notes/1',
|
|
42
|
+
'application/activity+json',
|
|
43
|
+
{ keyId: ALICE_KEY },
|
|
44
|
+
);
|
|
45
|
+
expect(refused.status).toBe(401);
|
|
46
|
+
expect(redirected.hops).toEqual([
|
|
47
|
+
{ url: 'https://remote.example/notes/1', keyId: ALICE_KEY },
|
|
48
|
+
{ url: 'https://remote.example/statuses/1', keyId: ALICE_KEY },
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const failing = rig([new Response(null, { status: 503 })]);
|
|
52
|
+
const res = await failing.signedFetch(
|
|
53
|
+
'https://remote.example/notes/1',
|
|
54
|
+
'application/activity+json',
|
|
55
|
+
{ keyId: ALICE_KEY },
|
|
56
|
+
);
|
|
57
|
+
expect(res.status).toBe(503);
|
|
58
|
+
expect(failing.hops).toHaveLength(1);
|
|
59
|
+
});
|
package/src/apUri.ts
CHANGED
|
@@ -72,6 +72,21 @@ export function isSameFederationHost(a: string, b: string): boolean {
|
|
|
72
72
|
return canonicalA === canonicalFederationHost(b);
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* The IRI a JSON-LD node reference names: the string itself, or the `id` of an
|
|
77
|
+
* embedded object. ActivityStreams lets a server embed a referenced node (an
|
|
78
|
+
* actor's `followers` collection with its `totalItems`, an `endpoints` value)
|
|
79
|
+
* instead of linking it, and both mean the same node. Anything else names none.
|
|
80
|
+
*/
|
|
81
|
+
export function apIri(value: unknown): string | undefined {
|
|
82
|
+
if (typeof value === 'string') return value || undefined;
|
|
83
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
84
|
+
const id = (value as { id?: unknown }).id;
|
|
85
|
+
return typeof id === 'string' && id ? id : undefined;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
75
90
|
/**
|
|
76
91
|
* Given an ActivityPub activity/object ID (URL), extract the actor URI by
|
|
77
92
|
* trimming everything from the first recognised post-path segment onward.
|
package/src/index.ts
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* document.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import { canonicalFederationHost, isSameFederationHost } from '../apUri';
|
|
22
|
+
import { apIri, canonicalFederationHost, isSameFederationHost } from '../apUri';
|
|
23
23
|
import {
|
|
24
24
|
readProxyDeclarations,
|
|
25
25
|
type DeriveNetworkIdentity,
|
|
@@ -507,7 +507,7 @@ export class ActorResolver<TActor extends FederatedActorRecordBase> {
|
|
|
507
507
|
|
|
508
508
|
const actor = await readBoundedJson(res, ACTOR_BODY_MAX_BYTES);
|
|
509
509
|
const actorId = asString(actor.id);
|
|
510
|
-
const actorInbox =
|
|
510
|
+
const actorInbox = apIri(actor.inbox);
|
|
511
511
|
if (!actorId || !actorInbox) {
|
|
512
512
|
this.config.logger.info(
|
|
513
513
|
`[FedSync] fetchRemoteActor missing fields for ${currentUri}: id=${!!actor.id} inbox=${!!actor.inbox} type=${String(actor.type)} keys=${Object.keys(actor).join(',')}`,
|
|
@@ -564,9 +564,9 @@ export class ActorResolver<TActor extends FederatedActorRecordBase> {
|
|
|
564
564
|
|
|
565
565
|
// Fetch collection counts (followers, following, posts) in parallel
|
|
566
566
|
const [followersCount, followingCount, postsCount] = await Promise.all([
|
|
567
|
-
this.fetchCollectionCount(
|
|
568
|
-
this.fetchCollectionCount(
|
|
569
|
-
this.fetchCollectionCount(
|
|
567
|
+
this.fetchCollectionCount(apIri(actor.followers)),
|
|
568
|
+
this.fetchCollectionCount(apIri(actor.following)),
|
|
569
|
+
this.fetchCollectionCount(apIri(actor.outbox)),
|
|
570
570
|
]);
|
|
571
571
|
|
|
572
572
|
// Extract profile fields (PropertyValue attachments). Sanitize BEFORE
|
|
@@ -606,7 +606,7 @@ export class ActorResolver<TActor extends FederatedActorRecordBase> {
|
|
|
606
606
|
? actor.alsoKnownAs.filter((v): v is string => typeof v === 'string')
|
|
607
607
|
: undefined;
|
|
608
608
|
// A move target is an actor URL; anything else is not one.
|
|
609
|
-
const movedToValue =
|
|
609
|
+
const movedToValue = apIri(actor.movedTo);
|
|
610
610
|
const movedTo = movedToValue && /^https?:\/\//.test(movedToValue) ? movedToValue : undefined;
|
|
611
611
|
|
|
612
612
|
// The IDENTITY the actor is stored under in Oxy, which is not necessarily the
|
|
@@ -648,10 +648,10 @@ export class ActorResolver<TActor extends FederatedActorRecordBase> {
|
|
|
648
648
|
avatarUrl,
|
|
649
649
|
headerUrl,
|
|
650
650
|
inboxUrl: actorInbox,
|
|
651
|
-
outboxUrl:
|
|
652
|
-
sharedInboxUrl:
|
|
653
|
-
followersUrl:
|
|
654
|
-
followingUrl:
|
|
651
|
+
outboxUrl: apIri(actor.outbox),
|
|
652
|
+
sharedInboxUrl: apIri(actorEndpoints?.sharedInbox),
|
|
653
|
+
followersUrl: apIri(actor.followers),
|
|
654
|
+
followingUrl: apIri(actor.following),
|
|
655
655
|
publicKeyPem: asString(actorPublicKey?.publicKeyPem) || undefined,
|
|
656
656
|
publicKeyId: asString(actorPublicKey?.id) || undefined,
|
|
657
657
|
type: asString(actor.type) || 'Person',
|
|
@@ -661,8 +661,8 @@ export class ActorResolver<TActor extends FederatedActorRecordBase> {
|
|
|
661
661
|
memorial: actor.memorial === true,
|
|
662
662
|
suspended: actor.suspended === true,
|
|
663
663
|
fields,
|
|
664
|
-
featuredUrl:
|
|
665
|
-
featuredTagsUrl:
|
|
664
|
+
featuredUrl: apIri(actor.featured),
|
|
665
|
+
featuredTagsUrl: apIri(actor.featuredTags),
|
|
666
666
|
alsoKnownAs,
|
|
667
667
|
...(movedTo && { movedTo }),
|
|
668
668
|
networkAcct: networkIdentity?.federatedUsername,
|
package/src/node/index.ts
CHANGED
package/src/node/signedFetch.ts
CHANGED
|
@@ -64,8 +64,26 @@ export interface CreateSignedFetchConfig {
|
|
|
64
64
|
logger?: SignedFetchLogger;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/** Request options for one {@link SignedFetch} call. */
|
|
68
|
+
export interface SignedFetchInit extends RequestInit {
|
|
69
|
+
/**
|
|
70
|
+
* Sign this request (and every re-signed redirect hop) with this actor key
|
|
71
|
+
* instead of the instance actor's — e.g. a local user's `…#main-key`, so a
|
|
72
|
+
* server that gates a followers-only or direct object on the requesting actor
|
|
73
|
+
* serves it to a follower or recipient. The signer must hold that key.
|
|
74
|
+
*
|
|
75
|
+
* An actor-signed request is never retried unsigned: its answer is the remote's
|
|
76
|
+
* verdict for that actor, so the response is returned as-is on any status.
|
|
77
|
+
*/
|
|
78
|
+
keyId?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
67
81
|
/** A signed ActivityPub GET, returning the standard WHATWG {@link Response}. */
|
|
68
|
-
export type SignedFetch = (
|
|
82
|
+
export type SignedFetch = (
|
|
83
|
+
url: string,
|
|
84
|
+
accept: string,
|
|
85
|
+
init?: SignedFetchInit,
|
|
86
|
+
) => Promise<Response>;
|
|
69
87
|
|
|
70
88
|
function requestInitHeaders(init: RequestInit): Record<string, string> {
|
|
71
89
|
if (!init.headers) return {};
|
|
@@ -97,18 +115,20 @@ function requestInitHeaders(init: RequestInit): Record<string, string> {
|
|
|
97
115
|
* `init.redirect === 'manual'`, the redirect `Response` is returned directly so
|
|
98
116
|
* the caller can apply its own stricter redirect policy.
|
|
99
117
|
*
|
|
100
|
-
* Signed for servers that enforce authorized fetch (e.g. Threads).
|
|
101
|
-
* request is retried unsigned (same SSRF-safe
|
|
102
|
-
* resources.
|
|
118
|
+
* Signed for servers that enforce authorized fetch (e.g. Threads). An
|
|
119
|
+
* instance-signed request that gets a 5xx is retried unsigned (same SSRF-safe
|
|
120
|
+
* path) as a fallback for public resources. `init.keyId` signs as another actor
|
|
121
|
+
* (see {@link SignedFetchInit.keyId}); such a request is never retried unsigned.
|
|
103
122
|
*/
|
|
104
123
|
export function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch {
|
|
105
124
|
return async function signedFetch(
|
|
106
125
|
url: string,
|
|
107
126
|
accept: string,
|
|
108
|
-
init:
|
|
127
|
+
init: SignedFetchInit = {},
|
|
109
128
|
): Promise<Response> {
|
|
110
129
|
const acceptHeader = `${accept}, application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
|
|
111
|
-
const
|
|
130
|
+
const actorSigned = init.keyId !== undefined;
|
|
131
|
+
const keyId = init.keyId ?? (await config.getInstanceKeyId());
|
|
112
132
|
const extraHeaders = requestInitHeaders(init);
|
|
113
133
|
const manualRedirect = init.redirect === 'manual';
|
|
114
134
|
|
|
@@ -151,6 +171,10 @@ export function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch
|
|
|
151
171
|
|
|
152
172
|
const { res, finalUrl } = await fetchFollowingRedirects(url, true);
|
|
153
173
|
|
|
174
|
+
// An actor-signed answer is the remote's verdict for THAT actor (a follower or
|
|
175
|
+
// recipient): an unsigned retry asks a different question, so it never runs.
|
|
176
|
+
if (actorSigned) return res;
|
|
177
|
+
|
|
154
178
|
// If the remote server returns a 5xx (e.g. it can't resolve our keyId to
|
|
155
179
|
// verify the signature), retry without the signature as a fallback for public
|
|
156
180
|
// resources. Retry from the post-redirect URL so we don't restart a chain that
|