@oxy.so/federation 2.14.0 → 2.15.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.
@@ -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). On a 5xx the
61
- * request is retried unsigned (same SSRF-safe path) as a fallback for public
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 keyId = await config.getInstanceKeyId();
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
@@ -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). On a 5xx the
58
- * request is retried unsigned (same SSRF-safe path) as a fallback for public
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 keyId = await config.getInstanceKeyId();
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
@@ -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?: RequestInit) => Promise<Response>;
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). On a 5xx the
71
- * request is retried unsigned (same SSRF-safe path) as a fallback for public
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.14.0",
3
+ "version": "2.15.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",
@@ -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/node/index.ts CHANGED
@@ -20,6 +20,7 @@
20
20
  export {
21
21
  createSignedFetch,
22
22
  type SignedFetch,
23
+ type SignedFetchInit,
23
24
  type CreateSignedFetchConfig,
24
25
  type SingleHopFetch,
25
26
  type SingleHopFetchInit,
@@ -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 = (url: string, accept: string, init?: RequestInit) => Promise<Response>;
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). On a 5xx the
101
- * request is retried unsigned (same SSRF-safe path) as a fallback for public
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: RequestInit = {},
127
+ init: SignedFetchInit = {},
109
128
  ): Promise<Response> {
110
129
  const acceptHeader = `${accept}, application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
111
- const keyId = await config.getInstanceKeyId();
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