@fedify/fedify 2.2.19 → 2.2.20
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/{builder-UmydCxtK.mjs → builder-BolQ56ab.mjs} +2 -2
- package/dist/{client-CAM_bQXx.d.cts → client-BIBzqEIp.d.cts} +0 -1
- package/dist/{client-9F_u23dc.mjs → client-DhxaVgOv.mjs} +113 -64
- package/dist/{client-CSddvgWN.d.ts → client-jE7oGvRh.d.ts} +0 -1
- package/dist/compat/mod.d.cts +1 -1
- package/dist/compat/mod.d.ts +1 -1
- package/dist/compat/transformers.test.mjs +1 -1
- package/dist/{context-tJbL_Hkw.d.cts → context-CFQGaq5x.d.cts} +28 -2
- package/dist/{context-OkwEpc3-.d.ts → context-DdbLVV_k.d.ts} +28 -2
- package/dist/{deno-CD-8UlUi.mjs → deno-BUj4roJc.mjs} +1 -1
- package/dist/docloader-CbtF_0cD.mjs +125 -0
- package/dist/federation/builder.test.mjs +1 -1
- package/dist/federation/handler.test.mjs +5 -5
- package/dist/federation/idempotency.test.mjs +2 -2
- package/dist/federation/inbox.test.mjs +1 -1
- package/dist/federation/middleware.test.mjs +389 -28
- package/dist/federation/mod.cjs +1 -1
- package/dist/federation/mod.d.cts +2 -2
- package/dist/federation/mod.d.ts +2 -2
- package/dist/federation/mod.js +1 -1
- package/dist/federation/send.test.mjs +445 -70
- package/dist/federation/temporal.test.mjs +1 -1
- package/dist/federation/webfinger.test.mjs +1 -1
- package/dist/{http-Dl00HXr4.d.ts → http-9IFUxdkp.d.cts} +1 -2
- package/dist/{http-DJK2udgX.js → http-C5g_VrA_.js} +172 -103
- package/dist/{http-BBBUbwKh.mjs → http-CQMVisM0.mjs} +195 -97
- package/dist/{http-DrUG4fPS.cjs → http-D8I7LBfx.cjs} +173 -104
- package/dist/{http-DqZhyEso.d.cts → http-SbfezERn.d.ts} +1 -2
- package/dist/{inbox-Dca_0fwc.mjs → inbox-CD8IDILN.mjs} +1 -1
- package/dist/{key-DolXUaLH.mjs → key-C3o3DGap.mjs} +1 -1
- package/dist/{kv-cache-DDm80t1K.js → kv-cache-CSKXXleR.js} +60 -18
- package/dist/{kv-cache-Bk3V1Tiu.cjs → kv-cache-DC9LcfoC.cjs} +59 -17
- package/dist/{ld-rTdIfZ63.mjs → ld-ijSr-0AB.mjs} +2 -2
- package/dist/{middleware-CpXoRHs3.mjs → middleware-CYAaUTi5.mjs} +78 -38
- package/dist/{middleware-BWOwEGpy.cjs → middleware-DNueR0GD.cjs} +184 -59
- package/dist/{middleware-B4-Rzdi3.cjs → middleware-DmcY2TWS.cjs} +1 -1
- package/dist/{middleware-BimXKBcP.js → middleware-sjhz30sm.js} +184 -59
- package/dist/{middleware-DYK_res8.mjs → middleware-yqqfI7oJ.mjs} +1 -1
- package/dist/{mod-B6LmugJ4.d.ts → mod-BRnwh2oy.d.ts} +2 -8
- package/dist/{mod-D2JyWtW3.d.cts → mod-BYWp77-z.d.cts} +1 -1
- package/dist/{mod-DQGjRJUG.d.ts → mod-BuRRdsfD.d.ts} +1 -1
- package/dist/{mod-ej_rLx8h.d.ts → mod-CM5_J_cx.d.ts} +1 -1
- package/dist/{mod-a6kmB8xD.d.cts → mod-CVSKw-5H.d.cts} +1 -1
- package/dist/{mod-V2vDnYNu.d.cts → mod-Co7LEK9X.d.cts} +2 -8
- package/dist/mod.cjs +5 -5
- package/dist/mod.d.cts +6 -6
- package/dist/mod.d.ts +6 -6
- package/dist/mod.js +5 -5
- package/dist/nodeinfo/client.test.mjs +337 -19
- package/dist/nodeinfo/handler.test.mjs +1 -1
- package/dist/nodeinfo/mod.cjs +1 -1
- package/dist/nodeinfo/mod.d.cts +1 -1
- package/dist/nodeinfo/mod.d.ts +1 -1
- package/dist/nodeinfo/mod.js +1 -1
- package/dist/otel/exporter.test.mjs +292 -30
- package/dist/otel/mod.cjs +36 -30
- package/dist/otel/mod.d.cts +13 -0
- package/dist/otel/mod.d.ts +13 -0
- package/dist/otel/mod.js +36 -30
- package/dist/{owner-CVfzyM5V.mjs → owner-B8kXzD0q.mjs} +2 -2
- package/dist/{proof-gOGM-sJS.mjs → proof-CBQOjOgA.mjs} +2 -2
- package/dist/{proof-CaQ4KG6y.js → proof-DKX6nASL.js} +1 -1
- package/dist/{proof-DhaTTn-P.cjs → proof-qiuGJCD7.cjs} +1 -1
- package/dist/{send-D35Va72B.mjs → send-DRpisX_f.mjs} +117 -32
- package/dist/sig/http.test.mjs +528 -2
- package/dist/sig/key.test.mjs +2 -2
- package/dist/sig/ld.test.mjs +2 -2
- package/dist/sig/mod.cjs +2 -2
- package/dist/sig/mod.d.cts +2 -2
- package/dist/sig/mod.d.ts +2 -2
- package/dist/sig/mod.js +2 -2
- package/dist/sig/owner.test.mjs +1 -1
- package/dist/sig/proof.test.mjs +1 -1
- package/dist/{temporal-CA4Y9hBJ.mjs → temporal-BQGEINad.mjs} +1 -1
- package/dist/testing/mod.d.mts +26 -1
- package/dist/{types-miAYfiGO.js → types-B1_pRFE_.js} +112 -63
- package/dist/{types-DRPjmHVV.cjs → types-DOvyymM4.cjs} +111 -62
- package/dist/utils/docloader.test.mjs +318 -34
- package/dist/utils/mod.cjs +1 -1
- package/dist/utils/mod.d.cts +1 -1
- package/dist/utils/mod.d.ts +1 -1
- package/dist/utils/mod.js +1 -1
- package/package.json +6 -6
- package/dist/docloader-D1ghurDJ.mjs +0 -83
package/dist/testing/mod.d.mts
CHANGED
|
@@ -5,7 +5,6 @@ import { Activity, Actor, Collection, CryptographicKey, Hashtag, Link, LookupObj
|
|
|
5
5
|
import { Link as Link$1, LookupWebFingerOptions, ResourceDescriptor } from "@fedify/webfinger";
|
|
6
6
|
import { TracerProvider } from "@opentelemetry/api";
|
|
7
7
|
import { DocumentLoader, GetUserAgentOptions } from "@fedify/vocab-runtime";
|
|
8
|
-
|
|
9
8
|
//#region src/nodeinfo/types.d.ts
|
|
10
9
|
/**
|
|
11
10
|
* The type of the result of parsing JSON.
|
|
@@ -1942,6 +1941,11 @@ interface RequestContext<TContextData> extends Context<TContextData> {
|
|
|
1942
1941
|
* Gets the public key of the sender, if any exists and it is verified.
|
|
1943
1942
|
* Otherwise, `null` is returned.
|
|
1944
1943
|
*
|
|
1944
|
+
* Successful results are reused for this request regardless of the options.
|
|
1945
|
+
* Pending checks and `null` results are reused for the same effective
|
|
1946
|
+
* document and context loaders, including failed lookups. Rejected checks
|
|
1947
|
+
* can be retried. Shared work uses the first caller's tracer provider.
|
|
1948
|
+
*
|
|
1945
1949
|
* This can be used for implementing [authorized fetch] (also known as
|
|
1946
1950
|
* secure mode) in ActivityPub.
|
|
1947
1951
|
*
|
|
@@ -1955,6 +1959,11 @@ interface RequestContext<TContextData> extends Context<TContextData> {
|
|
|
1955
1959
|
* Gets the public key of the sender, if any exists and it is verified.
|
|
1956
1960
|
* Otherwise, `null` is returned.
|
|
1957
1961
|
*
|
|
1962
|
+
* Successful results are reused for this request regardless of the options.
|
|
1963
|
+
* Pending checks and `null` results are reused for the same effective
|
|
1964
|
+
* document and context loaders, including failed lookups. Rejected checks
|
|
1965
|
+
* can be retried. Shared work uses the first caller's tracer provider.
|
|
1966
|
+
*
|
|
1958
1967
|
* This can be used for implementing [authorized fetch] (also known as
|
|
1959
1968
|
* secure mode) in ActivityPub.
|
|
1960
1969
|
*
|
|
@@ -1974,6 +1983,14 @@ interface RequestContext<TContextData> extends Context<TContextData> {
|
|
|
1974
1983
|
* Gets the owner of the signed key, if any exists and it is verified.
|
|
1975
1984
|
* Otherwise, `null` is returned.
|
|
1976
1985
|
*
|
|
1986
|
+
* Verified keys and owners are reused for this request regardless of the
|
|
1987
|
+
* options. Pending checks are shared for the same effective document and
|
|
1988
|
+
* context loaders. A `null` result from an owner lookup after key
|
|
1989
|
+
* verification is retained for those loaders. A `null` result caused by
|
|
1990
|
+
* an unverified key is not retained, allowing an owner lookup after another
|
|
1991
|
+
* loader verifies the key. Rejected checks can be retried. Shared work
|
|
1992
|
+
* uses the first caller's tracer provider.
|
|
1993
|
+
*
|
|
1977
1994
|
* This can be used for implementing [authorized fetch] (also known as
|
|
1978
1995
|
* secure mode) in ActivityPub.
|
|
1979
1996
|
*
|
|
@@ -1988,6 +2005,14 @@ interface RequestContext<TContextData> extends Context<TContextData> {
|
|
|
1988
2005
|
* Gets the owner of the signed key, if any exists and it is verified.
|
|
1989
2006
|
* Otherwise, `null` is returned.
|
|
1990
2007
|
*
|
|
2008
|
+
* Verified keys and owners are reused for this request regardless of the
|
|
2009
|
+
* options. Pending checks are shared for the same effective document and
|
|
2010
|
+
* context loaders. A `null` result from an owner lookup after key
|
|
2011
|
+
* verification is retained for those loaders. A `null` result caused by
|
|
2012
|
+
* an unverified key is not retained, allowing an owner lookup after another
|
|
2013
|
+
* loader verifies the key. Rejected checks can be retried. Shared work
|
|
2014
|
+
* uses the first caller's tracer provider.
|
|
2015
|
+
*
|
|
1991
2016
|
* This can be used for implementing [authorized fetch] (also known as
|
|
1992
2017
|
* secure mode) in ActivityPub.
|
|
1993
2018
|
*
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import { URLPattern } from "urlpattern-polyfill";
|
|
3
3
|
import { getLogger } from "@logtape/logtape";
|
|
4
|
-
import { FetchError, UrlError, getUserAgent
|
|
4
|
+
import { FetchError, UrlError, getUserAgent } from "@fedify/vocab-runtime";
|
|
5
|
+
import { openPublicFetch } from "@fedify/vocab-runtime/internal";
|
|
5
6
|
//#region src/utils/body.ts
|
|
6
7
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
7
8
|
const MAX_BODY_SIZE = 16 * 1024 * 1024;
|
|
@@ -84,23 +85,34 @@ const logger = getLogger([
|
|
|
84
85
|
]);
|
|
85
86
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
86
87
|
/**
|
|
87
|
-
* Fetches a URL while guarding against SSRF.
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* bounce the request to an internal
|
|
88
|
+
* Fetches a URL while guarding against SSRF. Every request—including each
|
|
89
|
+
* redirect hop—is made in its own session opened with `openPublicFetch()`,
|
|
90
|
+
* which rejects loopback, link-local, and private addresses, as well as
|
|
91
|
+
* non-HTTP(S) schemes such as `data:`, and connects only to the validated
|
|
92
|
+
* addresses where the runtime supports it. Redirects are followed manually
|
|
93
|
+
* with a cap, so that a public URL cannot bounce the request to an internal
|
|
94
|
+
* address.
|
|
95
|
+
*
|
|
96
|
+
* The final response is passed to `consume`, which must consume or cancel its
|
|
97
|
+
* body; the session is closed after `consume` settles. The cancellation of
|
|
98
|
+
* redirect response bodies is started before their sessions are closed.
|
|
93
99
|
* @param url The URL to fetch.
|
|
94
100
|
* @param headers The request headers.
|
|
95
|
-
* @
|
|
96
|
-
*
|
|
101
|
+
* @param consume The function to consume the final response with.
|
|
102
|
+
* @param deps The dependencies.
|
|
103
|
+
* @returns The value returned by `consume`, or `null` if the URL is
|
|
104
|
+
* disallowed, its hostname cannot be resolved, or there are too many
|
|
105
|
+
* redirects.
|
|
106
|
+
* @internal
|
|
97
107
|
*/
|
|
98
|
-
async function fetchPublicUrl(url, headers) {
|
|
108
|
+
async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
109
|
+
const openHop = deps.openHop ?? ((hopUrl) => openPublicFetch(hopUrl));
|
|
99
110
|
let current = url instanceof URL ? url : new URL(url);
|
|
100
111
|
let redirected = 0;
|
|
101
112
|
while (true) {
|
|
113
|
+
let session;
|
|
102
114
|
try {
|
|
103
|
-
await
|
|
115
|
+
session = await openHop(current.href);
|
|
104
116
|
} catch (e) {
|
|
105
117
|
if (e instanceof UrlError) {
|
|
106
118
|
if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
|
|
@@ -115,52 +127,89 @@ async function fetchPublicUrl(url, headers) {
|
|
|
115
127
|
}
|
|
116
128
|
throw e;
|
|
117
129
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
130
|
+
let location;
|
|
131
|
+
try {
|
|
132
|
+
const response = await session.fetch(current, {
|
|
133
|
+
headers,
|
|
134
|
+
redirect: "manual"
|
|
135
|
+
});
|
|
136
|
+
if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
|
|
137
|
+
location = response.headers.get("Location");
|
|
138
|
+
cancelBody(response, current);
|
|
139
|
+
} finally {
|
|
140
|
+
await session.close();
|
|
141
|
+
}
|
|
142
|
+
redirected++;
|
|
143
|
+
if (redirected >= DEFAULT_MAX_REDIRECTION) {
|
|
144
|
+
logger.error("Too many redirections ({redirections}) while fetching {url}.", {
|
|
145
|
+
redirections: redirected,
|
|
146
|
+
url: current.href
|
|
147
|
+
});
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const next = new URL(location, current);
|
|
151
|
+
if (next.protocol !== current.protocol) {
|
|
152
|
+
logger.error("Refused a redirect to a different protocol ({protocol} to {redirectedProtocol}) while fetching NodeInfo.", {
|
|
153
|
+
protocol: current.protocol,
|
|
154
|
+
redirectedProtocol: next.protocol
|
|
155
|
+
});
|
|
156
|
+
return null;
|
|
141
157
|
}
|
|
142
|
-
|
|
158
|
+
current = next;
|
|
143
159
|
}
|
|
144
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Starts cancelling a response body that is not read, without waiting for it
|
|
163
|
+
* to settle. The cancellation of a branch of a tee (e.g., a body cloned by
|
|
164
|
+
* a fetch mock) settles only after the other branch is cancelled too, per
|
|
165
|
+
* the Streams standard, so awaiting it could block forever. A failure is
|
|
166
|
+
* only logged.
|
|
167
|
+
*/
|
|
168
|
+
function cancelBody(response, url) {
|
|
169
|
+
response.body?.cancel().catch((error) => {
|
|
170
|
+
logger.debug("Failed to cancel the response body from {url}: {error}", {
|
|
171
|
+
url: url.toString(),
|
|
172
|
+
error
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
|
145
176
|
async function getNodeInfo(url, options = {}) {
|
|
177
|
+
return await getNodeInfoInternal(url, options);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The implementation of {@link getNodeInfo}, which is not exported from any
|
|
181
|
+
* entry point.
|
|
182
|
+
* @param url The base URL of the server, or the URL of the NodeInfo document
|
|
183
|
+
* if `options.direct` is turned on.
|
|
184
|
+
* @param options Options for fetching the NodeInfo document.
|
|
185
|
+
* @param deps The dependencies.
|
|
186
|
+
* @returns The NodeInfo document if it could be fetched successfully.
|
|
187
|
+
* Otherwise, `undefined` is returned.
|
|
188
|
+
* @internal
|
|
189
|
+
*/
|
|
190
|
+
async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
146
191
|
try {
|
|
192
|
+
const headers = {
|
|
193
|
+
Accept: "application/json",
|
|
194
|
+
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
|
|
195
|
+
};
|
|
147
196
|
let nodeInfoUrl = url;
|
|
148
197
|
if (!options.direct) {
|
|
149
198
|
const wellKnownUrl = new URL("/.well-known/nodeinfo", url);
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const wellKnownRd =
|
|
199
|
+
const wellKnown = await fetchPublicUrl(wellKnownUrl, headers, async (response) => {
|
|
200
|
+
if (!response.ok) {
|
|
201
|
+
logger.error("Failed to fetch {url}: {status} {statusText}", {
|
|
202
|
+
url: wellKnownUrl.href,
|
|
203
|
+
status: response.status,
|
|
204
|
+
statusText: response.statusText
|
|
205
|
+
});
|
|
206
|
+
cancelBody(response, wellKnownUrl);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
|
|
210
|
+
}, deps);
|
|
211
|
+
if (wellKnown == null) return void 0;
|
|
212
|
+
const wellKnownRd = wellKnown.rd;
|
|
164
213
|
const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
|
|
165
214
|
if (link == null || link.href == null) {
|
|
166
215
|
logger.error("Failed to find a NodeInfo document link from {url}: {resourceDescriptor}", {
|
|
@@ -171,20 +220,20 @@ async function getNodeInfo(url, options = {}) {
|
|
|
171
220
|
}
|
|
172
221
|
nodeInfoUrl = link.href;
|
|
173
222
|
}
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const data =
|
|
223
|
+
const document = await fetchPublicUrl(nodeInfoUrl, headers, async (response) => {
|
|
224
|
+
if (!response.ok) {
|
|
225
|
+
logger.error("Failed to fetch NodeInfo document from {url}: {status} {statusText}", {
|
|
226
|
+
url: nodeInfoUrl.toString(),
|
|
227
|
+
status: response.status,
|
|
228
|
+
statusText: response.statusText
|
|
229
|
+
});
|
|
230
|
+
cancelBody(response, nodeInfoUrl);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
|
|
234
|
+
}, deps);
|
|
235
|
+
if (document == null) return void 0;
|
|
236
|
+
const { data } = document;
|
|
188
237
|
if (options.parse === "none") return data;
|
|
189
238
|
return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
|
|
190
239
|
} catch (error) {
|
|
@@ -3,6 +3,7 @@ const { URLPattern } = require("urlpattern-polyfill");
|
|
|
3
3
|
require("./chunk-DDcVe30Y.cjs");
|
|
4
4
|
let _logtape_logtape = require("@logtape/logtape");
|
|
5
5
|
let _fedify_vocab_runtime = require("@fedify/vocab-runtime");
|
|
6
|
+
let _fedify_vocab_runtime_internal = require("@fedify/vocab-runtime/internal");
|
|
6
7
|
//#region src/utils/body.ts
|
|
7
8
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
8
9
|
const MAX_BODY_SIZE = 16 * 1024 * 1024;
|
|
@@ -85,23 +86,34 @@ const logger = (0, _logtape_logtape.getLogger)([
|
|
|
85
86
|
]);
|
|
86
87
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
87
88
|
/**
|
|
88
|
-
* Fetches a URL while guarding against SSRF.
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* bounce the request to an internal
|
|
89
|
+
* Fetches a URL while guarding against SSRF. Every request—including each
|
|
90
|
+
* redirect hop—is made in its own session opened with `openPublicFetch()`,
|
|
91
|
+
* which rejects loopback, link-local, and private addresses, as well as
|
|
92
|
+
* non-HTTP(S) schemes such as `data:`, and connects only to the validated
|
|
93
|
+
* addresses where the runtime supports it. Redirects are followed manually
|
|
94
|
+
* with a cap, so that a public URL cannot bounce the request to an internal
|
|
95
|
+
* address.
|
|
96
|
+
*
|
|
97
|
+
* The final response is passed to `consume`, which must consume or cancel its
|
|
98
|
+
* body; the session is closed after `consume` settles. The cancellation of
|
|
99
|
+
* redirect response bodies is started before their sessions are closed.
|
|
94
100
|
* @param url The URL to fetch.
|
|
95
101
|
* @param headers The request headers.
|
|
96
|
-
* @
|
|
97
|
-
*
|
|
102
|
+
* @param consume The function to consume the final response with.
|
|
103
|
+
* @param deps The dependencies.
|
|
104
|
+
* @returns The value returned by `consume`, or `null` if the URL is
|
|
105
|
+
* disallowed, its hostname cannot be resolved, or there are too many
|
|
106
|
+
* redirects.
|
|
107
|
+
* @internal
|
|
98
108
|
*/
|
|
99
|
-
async function fetchPublicUrl(url, headers) {
|
|
109
|
+
async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
110
|
+
const openHop = deps.openHop ?? ((hopUrl) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl));
|
|
100
111
|
let current = url instanceof URL ? url : new URL(url);
|
|
101
112
|
let redirected = 0;
|
|
102
113
|
while (true) {
|
|
114
|
+
let session;
|
|
103
115
|
try {
|
|
104
|
-
await (
|
|
116
|
+
session = await openHop(current.href);
|
|
105
117
|
} catch (e) {
|
|
106
118
|
if (e instanceof _fedify_vocab_runtime.UrlError) {
|
|
107
119
|
if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
|
|
@@ -116,52 +128,89 @@ async function fetchPublicUrl(url, headers) {
|
|
|
116
128
|
}
|
|
117
129
|
throw e;
|
|
118
130
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
131
|
+
let location;
|
|
132
|
+
try {
|
|
133
|
+
const response = await session.fetch(current, {
|
|
134
|
+
headers,
|
|
135
|
+
redirect: "manual"
|
|
136
|
+
});
|
|
137
|
+
if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
|
|
138
|
+
location = response.headers.get("Location");
|
|
139
|
+
cancelBody(response, current);
|
|
140
|
+
} finally {
|
|
141
|
+
await session.close();
|
|
142
|
+
}
|
|
143
|
+
redirected++;
|
|
144
|
+
if (redirected >= DEFAULT_MAX_REDIRECTION) {
|
|
145
|
+
logger.error("Too many redirections ({redirections}) while fetching {url}.", {
|
|
146
|
+
redirections: redirected,
|
|
147
|
+
url: current.href
|
|
148
|
+
});
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const next = new URL(location, current);
|
|
152
|
+
if (next.protocol !== current.protocol) {
|
|
153
|
+
logger.error("Refused a redirect to a different protocol ({protocol} to {redirectedProtocol}) while fetching NodeInfo.", {
|
|
154
|
+
protocol: current.protocol,
|
|
155
|
+
redirectedProtocol: next.protocol
|
|
156
|
+
});
|
|
157
|
+
return null;
|
|
142
158
|
}
|
|
143
|
-
|
|
159
|
+
current = next;
|
|
144
160
|
}
|
|
145
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Starts cancelling a response body that is not read, without waiting for it
|
|
164
|
+
* to settle. The cancellation of a branch of a tee (e.g., a body cloned by
|
|
165
|
+
* a fetch mock) settles only after the other branch is cancelled too, per
|
|
166
|
+
* the Streams standard, so awaiting it could block forever. A failure is
|
|
167
|
+
* only logged.
|
|
168
|
+
*/
|
|
169
|
+
function cancelBody(response, url) {
|
|
170
|
+
response.body?.cancel().catch((error) => {
|
|
171
|
+
logger.debug("Failed to cancel the response body from {url}: {error}", {
|
|
172
|
+
url: url.toString(),
|
|
173
|
+
error
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
146
177
|
async function getNodeInfo(url, options = {}) {
|
|
178
|
+
return await getNodeInfoInternal(url, options);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The implementation of {@link getNodeInfo}, which is not exported from any
|
|
182
|
+
* entry point.
|
|
183
|
+
* @param url The base URL of the server, or the URL of the NodeInfo document
|
|
184
|
+
* if `options.direct` is turned on.
|
|
185
|
+
* @param options Options for fetching the NodeInfo document.
|
|
186
|
+
* @param deps The dependencies.
|
|
187
|
+
* @returns The NodeInfo document if it could be fetched successfully.
|
|
188
|
+
* Otherwise, `undefined` is returned.
|
|
189
|
+
* @internal
|
|
190
|
+
*/
|
|
191
|
+
async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
147
192
|
try {
|
|
193
|
+
const headers = {
|
|
194
|
+
Accept: "application/json",
|
|
195
|
+
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
|
|
196
|
+
};
|
|
148
197
|
let nodeInfoUrl = url;
|
|
149
198
|
if (!options.direct) {
|
|
150
199
|
const wellKnownUrl = new URL("/.well-known/nodeinfo", url);
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const wellKnownRd =
|
|
200
|
+
const wellKnown = await fetchPublicUrl(wellKnownUrl, headers, async (response) => {
|
|
201
|
+
if (!response.ok) {
|
|
202
|
+
logger.error("Failed to fetch {url}: {status} {statusText}", {
|
|
203
|
+
url: wellKnownUrl.href,
|
|
204
|
+
status: response.status,
|
|
205
|
+
statusText: response.statusText
|
|
206
|
+
});
|
|
207
|
+
cancelBody(response, wellKnownUrl);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
|
|
211
|
+
}, deps);
|
|
212
|
+
if (wellKnown == null) return void 0;
|
|
213
|
+
const wellKnownRd = wellKnown.rd;
|
|
165
214
|
const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
|
|
166
215
|
if (link == null || link.href == null) {
|
|
167
216
|
logger.error("Failed to find a NodeInfo document link from {url}: {resourceDescriptor}", {
|
|
@@ -172,20 +221,20 @@ async function getNodeInfo(url, options = {}) {
|
|
|
172
221
|
}
|
|
173
222
|
nodeInfoUrl = link.href;
|
|
174
223
|
}
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const data =
|
|
224
|
+
const document = await fetchPublicUrl(nodeInfoUrl, headers, async (response) => {
|
|
225
|
+
if (!response.ok) {
|
|
226
|
+
logger.error("Failed to fetch NodeInfo document from {url}: {status} {statusText}", {
|
|
227
|
+
url: nodeInfoUrl.toString(),
|
|
228
|
+
status: response.status,
|
|
229
|
+
statusText: response.statusText
|
|
230
|
+
});
|
|
231
|
+
cancelBody(response, nodeInfoUrl);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
|
|
235
|
+
}, deps);
|
|
236
|
+
if (document == null) return void 0;
|
|
237
|
+
const { data } = document;
|
|
189
238
|
if (options.parse === "none") return data;
|
|
190
239
|
return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
|
|
191
240
|
} catch (error) {
|