@fedify/webfinger 2.2.18 → 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/deno.json +1 -1
- package/dist/lookup.test.cjs +440 -109
- package/dist/lookup.test.js +441 -109
- package/dist/mod.cjs +143 -84
- package/dist/mod.d.ts +1 -1
- package/dist/mod.js +144 -85
- package/package.json +3 -3
- package/src/lookup-impl.ts +234 -0
- package/src/lookup.test.ts +469 -36
- package/src/lookup.ts +2 -145
package/src/lookup.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getUserAgent,
|
|
3
|
-
type GetUserAgentOptions,
|
|
4
|
-
UrlError,
|
|
5
|
-
validatePublicUrl,
|
|
6
|
-
} from "@fedify/vocab-runtime";
|
|
7
|
-
import { getLogger } from "@logtape/logtape";
|
|
1
|
+
import type { GetUserAgentOptions } from "@fedify/vocab-runtime";
|
|
8
2
|
import {
|
|
9
3
|
SpanKind,
|
|
10
4
|
SpanStatusCode,
|
|
@@ -12,12 +6,8 @@ import {
|
|
|
12
6
|
type TracerProvider,
|
|
13
7
|
} from "@opentelemetry/api";
|
|
14
8
|
import metadata from "../deno.json" with { type: "json" };
|
|
15
|
-
import { BodyTooLargeError, MAX_BODY_SIZE, readBoundedText } from "./body.ts";
|
|
16
9
|
import type { ResourceDescriptor } from "./jrd.ts";
|
|
17
|
-
|
|
18
|
-
const logger = getLogger(["fedify", "webfinger", "lookup"]);
|
|
19
|
-
|
|
20
|
-
const DEFAULT_MAX_REDIRECTION = 5;
|
|
10
|
+
import { lookupWebFingerInternal } from "./lookup-impl.ts";
|
|
21
11
|
|
|
22
12
|
/**
|
|
23
13
|
* Options for {@link lookupWebFinger}.
|
|
@@ -108,136 +98,3 @@ export async function lookupWebFinger(
|
|
|
108
98
|
},
|
|
109
99
|
);
|
|
110
100
|
}
|
|
111
|
-
|
|
112
|
-
async function lookupWebFingerInternal(
|
|
113
|
-
resource: URL | string,
|
|
114
|
-
options: LookupWebFingerOptions = {},
|
|
115
|
-
): Promise<ResourceDescriptor | null> {
|
|
116
|
-
if (typeof resource === "string") resource = new URL(resource);
|
|
117
|
-
let protocol = "https:";
|
|
118
|
-
let server: string;
|
|
119
|
-
if (resource.protocol === "acct:") {
|
|
120
|
-
const atPos = resource.pathname.lastIndexOf("@");
|
|
121
|
-
if (atPos < 0) return null;
|
|
122
|
-
server = resource.pathname.substring(atPos + 1);
|
|
123
|
-
if (server === "") return null;
|
|
124
|
-
} else {
|
|
125
|
-
protocol = resource.protocol;
|
|
126
|
-
server = resource.host;
|
|
127
|
-
}
|
|
128
|
-
let url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
129
|
-
url.searchParams.set("resource", resource.href);
|
|
130
|
-
let redirected = 0;
|
|
131
|
-
while (true) {
|
|
132
|
-
logger.debug(
|
|
133
|
-
"Fetching WebFinger resource descriptor from {url}...",
|
|
134
|
-
{ url: url.href },
|
|
135
|
-
);
|
|
136
|
-
let response: Response;
|
|
137
|
-
if (options.allowPrivateAddress !== true) {
|
|
138
|
-
try {
|
|
139
|
-
await validatePublicUrl(url.href);
|
|
140
|
-
} catch (e) {
|
|
141
|
-
if (e instanceof UrlError) {
|
|
142
|
-
if (e.reason === "dns") {
|
|
143
|
-
logger.debug("DNS lookup failed for {url}", {
|
|
144
|
-
url: url.href,
|
|
145
|
-
error: e,
|
|
146
|
-
});
|
|
147
|
-
} else {
|
|
148
|
-
logger.error(
|
|
149
|
-
"Invalid URL for WebFinger resource descriptor: {error}",
|
|
150
|
-
{ error: e },
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
// TODO: When merging forward into 2.3-maintenance and later, where
|
|
154
|
-
// this returns { resource: null, result: "network_error", remoteHost }
|
|
155
|
-
// instead of null, keep that return value for both reasons.
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
throw e;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
try {
|
|
162
|
-
response = await fetch(url, {
|
|
163
|
-
headers: {
|
|
164
|
-
Accept: "application/jrd+json",
|
|
165
|
-
"User-Agent": typeof options.userAgent === "string"
|
|
166
|
-
? options.userAgent
|
|
167
|
-
: getUserAgent(options.userAgent),
|
|
168
|
-
},
|
|
169
|
-
redirect: "manual",
|
|
170
|
-
signal: options.signal,
|
|
171
|
-
});
|
|
172
|
-
} catch (error) {
|
|
173
|
-
logger.debug(
|
|
174
|
-
"Failed to fetch WebFinger resource descriptor: {error}",
|
|
175
|
-
{ url: url.href, error },
|
|
176
|
-
);
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
if (
|
|
180
|
-
response.status >= 300 && response.status < 400 &&
|
|
181
|
-
response.headers.has("Location")
|
|
182
|
-
) {
|
|
183
|
-
redirected++;
|
|
184
|
-
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
185
|
-
if (redirected >= maxRedirection) {
|
|
186
|
-
logger.error(
|
|
187
|
-
"Too many redirections ({redirections}) while fetching WebFinger " +
|
|
188
|
-
"resource descriptor.",
|
|
189
|
-
{ redirections: redirected },
|
|
190
|
-
);
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
const redirectedUrl = new URL(
|
|
194
|
-
response.headers.get("Location")!,
|
|
195
|
-
response.url == null || response.url === "" ? url : response.url,
|
|
196
|
-
);
|
|
197
|
-
if (redirectedUrl.protocol !== url.protocol) {
|
|
198
|
-
logger.error(
|
|
199
|
-
"Redirected to a different protocol ({protocol} to " +
|
|
200
|
-
"{redirectedProtocol}) while fetching WebFinger resource " +
|
|
201
|
-
"descriptor.",
|
|
202
|
-
{
|
|
203
|
-
protocol: url.protocol,
|
|
204
|
-
redirectedProtocol: redirectedUrl.protocol,
|
|
205
|
-
},
|
|
206
|
-
);
|
|
207
|
-
return null;
|
|
208
|
-
}
|
|
209
|
-
url = redirectedUrl;
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
if (!response.ok) {
|
|
213
|
-
logger.debug(
|
|
214
|
-
"Failed to fetch WebFinger resource descriptor: {status} {statusText}.",
|
|
215
|
-
{
|
|
216
|
-
url: url.href,
|
|
217
|
-
status: response.status,
|
|
218
|
-
statusText: response.statusText,
|
|
219
|
-
},
|
|
220
|
-
);
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
try {
|
|
224
|
-
return JSON.parse(
|
|
225
|
-
await readBoundedText(
|
|
226
|
-
response,
|
|
227
|
-
MAX_BODY_SIZE,
|
|
228
|
-
url,
|
|
229
|
-
),
|
|
230
|
-
) as ResourceDescriptor;
|
|
231
|
-
} catch (e) {
|
|
232
|
-
if (e instanceof BodyTooLargeError) return null;
|
|
233
|
-
if (e instanceof SyntaxError) {
|
|
234
|
-
logger.debug(
|
|
235
|
-
"Failed to parse WebFinger resource descriptor as JSON: {error}",
|
|
236
|
-
{ error: e },
|
|
237
|
-
);
|
|
238
|
-
return null;
|
|
239
|
-
}
|
|
240
|
-
throw e;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|