@better-auth/core 1.7.0-beta.1 → 1.7.0-beta.2
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/api/index.mjs +29 -3
- package/dist/context/global.mjs +1 -1
- package/dist/db/adapter/factory.mjs +1 -1
- package/dist/db/adapter/get-id-field.mjs +1 -1
- package/dist/instrumentation/api.mjs +12 -0
- package/dist/instrumentation/noop.mjs +42 -0
- package/dist/instrumentation/pure.index.d.mts +7 -0
- package/dist/instrumentation/pure.index.mjs +7 -0
- package/dist/instrumentation/tracer.mjs +6 -3
- package/dist/oauth2/index.d.mts +2 -2
- package/dist/oauth2/index.mjs +2 -2
- package/dist/oauth2/utils.d.mts +10 -1
- package/dist/oauth2/utils.mjs +13 -1
- package/dist/social-providers/apple.d.mts +11 -2
- package/dist/social-providers/apple.mjs +7 -1
- package/dist/social-providers/atlassian.mjs +1 -1
- package/dist/social-providers/cognito.d.mts +1 -1
- package/dist/social-providers/cognito.mjs +3 -2
- package/dist/social-providers/facebook.d.mts +1 -1
- package/dist/social-providers/facebook.mjs +7 -0
- package/dist/social-providers/figma.mjs +1 -1
- package/dist/social-providers/google.d.mts +1 -1
- package/dist/social-providers/google.mjs +3 -2
- package/dist/social-providers/microsoft-entra-id.d.mts +2 -2
- package/dist/social-providers/microsoft-entra-id.mjs +6 -1
- package/dist/social-providers/paybin.mjs +1 -1
- package/dist/social-providers/paypal.mjs +1 -1
- package/dist/social-providers/salesforce.mjs +1 -1
- package/dist/utils/async.d.mts +22 -0
- package/dist/utils/async.mjs +32 -0
- package/dist/utils/host.d.mts +147 -0
- package/dist/utils/host.mjs +291 -0
- package/dist/utils/is-api-error.d.mts +6 -0
- package/dist/utils/is-api-error.mjs +8 -0
- package/package.json +10 -1
- package/src/api/index.ts +39 -5
- package/src/db/adapter/get-id-field.ts +2 -2
- package/src/instrumentation/api.ts +17 -0
- package/src/instrumentation/noop.ts +74 -0
- package/src/instrumentation/pure.index.ts +31 -0
- package/src/instrumentation/tracer.ts +8 -3
- package/src/oauth2/index.ts +5 -1
- package/src/oauth2/utils.ts +13 -0
- package/src/social-providers/apple.ts +10 -2
- package/src/social-providers/cognito.ts +3 -2
- package/src/social-providers/facebook.ts +10 -1
- package/src/social-providers/google.ts +3 -2
- package/src/social-providers/microsoft-entra-id.ts +13 -3
- package/src/utils/async.ts +53 -0
- package/src/utils/host.ts +401 -0
- package/src/utils/is-api-error.ts +10 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { isValidIP, normalizeIP } from "./ip";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Host classification per RFC 6890 (Special-Purpose IP Address Registries),
|
|
5
|
+
* RFC 6761 (Special-Use Domain Names), and RFC 8252 §7.3 (loopback redirect URIs).
|
|
6
|
+
*
|
|
7
|
+
* This module is the single source of truth for "is this host public? private?
|
|
8
|
+
* loopback? link-local?" in the codebase. Consumers MUST prefer these predicates
|
|
9
|
+
* over bespoke regexes or substring matches; divergent checks are how bypass
|
|
10
|
+
* vulnerabilities get introduced (e.g. Oligo's "0.0.0.0 Day" 2024).
|
|
11
|
+
*
|
|
12
|
+
* Four user-facing primitives:
|
|
13
|
+
*
|
|
14
|
+
* - `classifyHost(host)` — the workhorse. Returns a {@link HostClassification}
|
|
15
|
+
* with `kind`, `literal`, and `canonical` fields.
|
|
16
|
+
* - `isLoopbackIP(host)` — strict: IPv4 `127.0.0.0/8` or IPv6 `::1` only.
|
|
17
|
+
* Use this for RFC 8252 §7.3 loopback redirect URI matching where IP
|
|
18
|
+
* literals are REQUIRED.
|
|
19
|
+
* - `isLoopbackHost(host)` — permissive: also accepts `localhost` and RFC 6761
|
|
20
|
+
* `.localhost` subdomains. Use this for developer ergonomics (CORS, cookie
|
|
21
|
+
* secure bypass, dev-mode HTTP allow-list).
|
|
22
|
+
* - `isPublicRoutableHost(host)` — SSRF gate. Returns false for every
|
|
23
|
+
* non-`public` kind. Use this before server-side fetches to user-controlled
|
|
24
|
+
* URLs.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The semantic kind of a host, derived from RFC 6890 special-purpose registries
|
|
29
|
+
* plus a few domain-name categories (localhost, cloud metadata FQDNs).
|
|
30
|
+
*/
|
|
31
|
+
export type HostKind =
|
|
32
|
+
/** IPv4 `127.0.0.0/8` or IPv6 `::1`. */
|
|
33
|
+
| "loopback"
|
|
34
|
+
/** DNS name `localhost` or RFC 6761 `.localhost` TLD. */
|
|
35
|
+
| "localhost"
|
|
36
|
+
/** IPv4 `0.0.0.0` or IPv6 `::` — "this host on this network", not loopback. */
|
|
37
|
+
| "unspecified"
|
|
38
|
+
/** RFC 1918 `10/8`, `172.16/12`, `192.168/16`, or IPv6 ULA `fc00::/7`. */
|
|
39
|
+
| "private"
|
|
40
|
+
/** IPv4 `169.254/16` or IPv6 `fe80::/10`. Includes AWS IMDS `169.254.169.254`. */
|
|
41
|
+
| "linkLocal"
|
|
42
|
+
/** RFC 6598 carrier-grade NAT `100.64.0.0/10`. */
|
|
43
|
+
| "sharedAddressSpace"
|
|
44
|
+
/** RFC 5737 `192.0.2/24`, `198.51.100/24`, `203.0.113/24`, or RFC 3849 `2001:db8::/32`. */
|
|
45
|
+
| "documentation"
|
|
46
|
+
/** RFC 2544 `198.18.0.0/15`. */
|
|
47
|
+
| "benchmarking"
|
|
48
|
+
/** IPv4 `224.0.0.0/4` or IPv6 `ff00::/8`. */
|
|
49
|
+
| "multicast"
|
|
50
|
+
/** IPv4 limited broadcast `255.255.255.255`. */
|
|
51
|
+
| "broadcast"
|
|
52
|
+
/** Other RFC 6890 special-purpose ranges (0/8, 192.0.0/24, 240/4, 2001::/32, etc.). */
|
|
53
|
+
| "reserved"
|
|
54
|
+
/** Cloud metadata service FQDN (e.g. `metadata.google.internal`). */
|
|
55
|
+
| "cloudMetadata"
|
|
56
|
+
/** Any host not matching a special-purpose range above. */
|
|
57
|
+
| "public";
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The syntactic form of the input host: an IPv4 literal, an IPv6 literal, or
|
|
61
|
+
* a domain name. IPv4-mapped IPv6 (`::ffff:192.0.2.1`) is reported as `ipv4`
|
|
62
|
+
* because it's unmapped during canonicalization.
|
|
63
|
+
*/
|
|
64
|
+
export type HostLiteral = "ipv4" | "ipv6" | "fqdn";
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Result of {@link classifyHost}. All fields are readonly.
|
|
68
|
+
*
|
|
69
|
+
* @property kind - Semantic classification per RFC 6890 + RFC 6761.
|
|
70
|
+
* @property literal - Syntactic form of the input (IPv4, IPv6, or FQDN).
|
|
71
|
+
* @property canonical - Lowercase, port-stripped, bracket-stripped, zone-id-stripped
|
|
72
|
+
* form suitable for equality comparison. IPv6 is expanded to full form.
|
|
73
|
+
* IPv4-mapped IPv6 is collapsed to the underlying IPv4.
|
|
74
|
+
*/
|
|
75
|
+
export interface HostClassification {
|
|
76
|
+
readonly kind: HostKind;
|
|
77
|
+
readonly literal: HostLiteral;
|
|
78
|
+
readonly canonical: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Cloud provider instance metadata service FQDNs. These resolve to link-local
|
|
83
|
+
* IPs (usually `169.254.169.254`) inside their respective clouds and are
|
|
84
|
+
* prime SSRF targets.
|
|
85
|
+
*
|
|
86
|
+
* The IPs themselves are already caught by the `linkLocal` kind; this set
|
|
87
|
+
* only exists for the FQDN form that a naive server-side fetch might resolve
|
|
88
|
+
* via its own resolver.
|
|
89
|
+
*/
|
|
90
|
+
const CLOUD_METADATA_HOSTS: ReadonlySet<string> = new Set([
|
|
91
|
+
"metadata.google.internal",
|
|
92
|
+
"metadata.goog",
|
|
93
|
+
"metadata",
|
|
94
|
+
"instance-data",
|
|
95
|
+
"instance-data.ec2.internal",
|
|
96
|
+
]);
|
|
97
|
+
|
|
98
|
+
/** Strip `[...]` if the entire input is bracketed (IPv6 literal form). */
|
|
99
|
+
function stripBrackets(host: string): string {
|
|
100
|
+
if (host.length >= 2 && host.startsWith("[") && host.endsWith("]")) {
|
|
101
|
+
return host.slice(1, -1);
|
|
102
|
+
}
|
|
103
|
+
return host;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Strip trailing `:port` from host-with-port strings.
|
|
108
|
+
*
|
|
109
|
+
* - Bracketed IPv6 with port: `[::1]:8080` → `[::1]`
|
|
110
|
+
* - IPv4/FQDN with port: `127.0.0.1:3000` / `example.com:443` → base form
|
|
111
|
+
* - Bare IPv6: `::1` / `fe80::1` → unchanged (multiple colons means no port)
|
|
112
|
+
*/
|
|
113
|
+
function stripPort(host: string): string {
|
|
114
|
+
if (host.startsWith("[")) {
|
|
115
|
+
const end = host.indexOf("]");
|
|
116
|
+
if (end === -1) return host;
|
|
117
|
+
return host.slice(0, end + 1);
|
|
118
|
+
}
|
|
119
|
+
const firstColon = host.indexOf(":");
|
|
120
|
+
if (firstColon === -1) return host;
|
|
121
|
+
if (host.indexOf(":", firstColon + 1) !== -1) return host;
|
|
122
|
+
return host.slice(0, firstColon);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Strip IPv6 zone identifier: `fe80::1%eth0` → `fe80::1`. */
|
|
126
|
+
function stripZoneId(host: string): string {
|
|
127
|
+
const zone = host.indexOf("%");
|
|
128
|
+
if (zone === -1) return host;
|
|
129
|
+
return host.slice(0, zone);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Strip trailing dots (RFC 1034 absolute DNS form): `localhost.` → `localhost`.
|
|
134
|
+
* Without this, `metadata.google.internal.` would fall through to `public` and
|
|
135
|
+
* bypass the cloud-metadata / `.localhost` checks, since WHATWG URL parsing
|
|
136
|
+
* preserves the trailing dot in `url.hostname`.
|
|
137
|
+
*/
|
|
138
|
+
function stripTrailingDot(host: string): string {
|
|
139
|
+
return host.replace(/\.+$/, "");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Fast dotted-decimal shape check. Does NOT validate octet bounds. */
|
|
143
|
+
function looksLikeIPv4(host: string): boolean {
|
|
144
|
+
return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Pack a validated dotted-decimal IPv4 into a 32-bit unsigned integer. */
|
|
148
|
+
function ipv4ToUint32(ip: string): number {
|
|
149
|
+
const parts = ip.split(".");
|
|
150
|
+
return (
|
|
151
|
+
((Number(parts[0]) << 24) |
|
|
152
|
+
(Number(parts[1]) << 16) |
|
|
153
|
+
(Number(parts[2]) << 8) |
|
|
154
|
+
Number(parts[3])) >>>
|
|
155
|
+
0
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Check whether a 32-bit value matches `prefix/length` (both unsigned). */
|
|
160
|
+
function inIPv4Range(value: number, prefix: number, length: number): boolean {
|
|
161
|
+
if (length === 0) return true;
|
|
162
|
+
const mask = length === 32 ? 0xffffffff : (~0 << (32 - length)) >>> 0;
|
|
163
|
+
return (value & mask) === (prefix & mask);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function classifyIPv4(ip: string): HostKind {
|
|
167
|
+
if (ip === "0.0.0.0") return "unspecified";
|
|
168
|
+
if (ip === "255.255.255.255") return "broadcast";
|
|
169
|
+
|
|
170
|
+
const n = ipv4ToUint32(ip);
|
|
171
|
+
|
|
172
|
+
if (inIPv4Range(n, ipv4ToUint32("127.0.0.0"), 8)) return "loopback";
|
|
173
|
+
if (inIPv4Range(n, ipv4ToUint32("10.0.0.0"), 8)) return "private";
|
|
174
|
+
if (inIPv4Range(n, ipv4ToUint32("172.16.0.0"), 12)) return "private";
|
|
175
|
+
if (inIPv4Range(n, ipv4ToUint32("192.168.0.0"), 16)) return "private";
|
|
176
|
+
if (inIPv4Range(n, ipv4ToUint32("169.254.0.0"), 16)) return "linkLocal";
|
|
177
|
+
if (inIPv4Range(n, ipv4ToUint32("100.64.0.0"), 10))
|
|
178
|
+
return "sharedAddressSpace";
|
|
179
|
+
if (inIPv4Range(n, ipv4ToUint32("192.0.2.0"), 24)) return "documentation";
|
|
180
|
+
if (inIPv4Range(n, ipv4ToUint32("198.51.100.0"), 24)) return "documentation";
|
|
181
|
+
if (inIPv4Range(n, ipv4ToUint32("203.0.113.0"), 24)) return "documentation";
|
|
182
|
+
if (inIPv4Range(n, ipv4ToUint32("198.18.0.0"), 15)) return "benchmarking";
|
|
183
|
+
if (inIPv4Range(n, ipv4ToUint32("224.0.0.0"), 4)) return "multicast";
|
|
184
|
+
if (inIPv4Range(n, ipv4ToUint32("0.0.0.0"), 8)) return "reserved";
|
|
185
|
+
if (inIPv4Range(n, ipv4ToUint32("192.0.0.0"), 24)) return "reserved";
|
|
186
|
+
if (inIPv4Range(n, ipv4ToUint32("240.0.0.0"), 4)) return "reserved";
|
|
187
|
+
|
|
188
|
+
return "public";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Extract an IPv4 address embedded in an expanded IPv6 literal.
|
|
193
|
+
*
|
|
194
|
+
* Used to recurse into tunnel/translation forms (6to4, NAT64, Teredo) so a
|
|
195
|
+
* private destination cannot be smuggled behind a syntactically-public IPv6
|
|
196
|
+
* literal. `startGroup` is the index of the first of two 16-bit groups in the
|
|
197
|
+
* expanded form (`0000:0000:...`). With `xor: true`, the 32-bit value is XORed
|
|
198
|
+
* with `0xffffffff` before decoding (Teredo obfuscates the client IPv4 this
|
|
199
|
+
* way).
|
|
200
|
+
*/
|
|
201
|
+
function extractEmbeddedIPv4(
|
|
202
|
+
expanded: string,
|
|
203
|
+
startGroup: number,
|
|
204
|
+
options: { xor?: boolean } = {},
|
|
205
|
+
): string | null {
|
|
206
|
+
const offset = startGroup * 5;
|
|
207
|
+
const g1 = Number.parseInt(expanded.slice(offset, offset + 4), 16);
|
|
208
|
+
const g2 = Number.parseInt(expanded.slice(offset + 5, offset + 9), 16);
|
|
209
|
+
if (!Number.isFinite(g1) || !Number.isFinite(g2)) return null;
|
|
210
|
+
let combined = ((g1 << 16) | g2) >>> 0;
|
|
211
|
+
if (options.xor) combined = (combined ^ 0xffffffff) >>> 0;
|
|
212
|
+
return `${(combined >>> 24) & 0xff}.${(combined >>> 16) & 0xff}.${(combined >>> 8) & 0xff}.${combined & 0xff}`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Classify an expanded, full-form, lowercase IPv6 address (no IPv4-mapped
|
|
217
|
+
* input — those are unmapped to IPv4 before reaching here).
|
|
218
|
+
*
|
|
219
|
+
* 6to4 (`2002::/16`), NAT64 (`64:ff9b::/96`) and Teredo (`2001:0000::/32`)
|
|
220
|
+
* embed an IPv4 that can route to private/loopback space. If the embedded
|
|
221
|
+
* IPv4 classifies as non-`public`, return `reserved` — blocks SSRF without
|
|
222
|
+
* advertising the address as a loopback literal for RFC 8252 §7.3 matching.
|
|
223
|
+
*/
|
|
224
|
+
function classifyIPv6(expanded: string): HostKind {
|
|
225
|
+
if (expanded === "0000:0000:0000:0000:0000:0000:0000:0000")
|
|
226
|
+
return "unspecified";
|
|
227
|
+
if (expanded === "0000:0000:0000:0000:0000:0000:0000:0001") return "loopback";
|
|
228
|
+
|
|
229
|
+
const firstByte = Number.parseInt(expanded.slice(0, 2), 16);
|
|
230
|
+
const secondByte = Number.parseInt(expanded.slice(2, 4), 16);
|
|
231
|
+
|
|
232
|
+
if (firstByte === 0xff) return "multicast";
|
|
233
|
+
if (firstByte === 0xfe && (secondByte & 0xc0) === 0x80) return "linkLocal";
|
|
234
|
+
if ((firstByte & 0xfe) === 0xfc) return "private";
|
|
235
|
+
|
|
236
|
+
if (expanded.startsWith("2001:0db8:")) return "documentation";
|
|
237
|
+
|
|
238
|
+
if (expanded.startsWith("2002:")) {
|
|
239
|
+
const embedded = extractEmbeddedIPv4(expanded, 1);
|
|
240
|
+
if (embedded && classifyIPv4(embedded) !== "public") return "reserved";
|
|
241
|
+
return "public";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (expanded.startsWith("0064:ff9b:0000:0000:0000:0000:")) {
|
|
245
|
+
const embedded = extractEmbeddedIPv4(expanded, 6);
|
|
246
|
+
if (embedded && classifyIPv4(embedded) !== "public") return "reserved";
|
|
247
|
+
return "reserved";
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (expanded.startsWith("2001:0000:")) {
|
|
251
|
+
const embedded = extractEmbeddedIPv4(expanded, 6, { xor: true });
|
|
252
|
+
if (embedded && classifyIPv4(embedded) !== "public") return "reserved";
|
|
253
|
+
return "reserved";
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (expanded.startsWith("0100:0000:0000:0000:")) return "reserved";
|
|
257
|
+
|
|
258
|
+
return "public";
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Classify a host string according to RFC 6890 / RFC 6761.
|
|
263
|
+
*
|
|
264
|
+
* Accepts inputs in any of these shapes and normalizes before classifying:
|
|
265
|
+
*
|
|
266
|
+
* - Bare IPv4: `127.0.0.1`
|
|
267
|
+
* - Bare IPv6: `::1`, `fe80::1%eth0`
|
|
268
|
+
* - Bracketed IPv6: `[::1]`
|
|
269
|
+
* - Host with port: `localhost:3000`, `127.0.0.1:443`, `[::1]:8080`
|
|
270
|
+
* - FQDN: `example.com`, `tenant.localhost`
|
|
271
|
+
* - IPv4-mapped IPv6: `::ffff:192.0.2.1` (reported as `literal: "ipv4"`)
|
|
272
|
+
*
|
|
273
|
+
* Invalid or non-resolvable FQDNs are returned as `{ kind: "public", literal: "fqdn" }`
|
|
274
|
+
* — this function never throws. Callers that need structural validation must
|
|
275
|
+
* combine this with a URL/hostname validator upstream.
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* classifyHost("127.0.0.1")
|
|
279
|
+
* // { kind: "loopback", literal: "ipv4", canonical: "127.0.0.1" }
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* classifyHost("[::1]:8080")
|
|
283
|
+
* // { kind: "loopback", literal: "ipv6", canonical: "0000:0000:...:0001" }
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* classifyHost("::ffff:192.0.2.1")
|
|
287
|
+
* // { kind: "documentation", literal: "ipv4", canonical: "192.0.2.1" }
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* classifyHost("tenant-a.localhost")
|
|
291
|
+
* // { kind: "localhost", literal: "fqdn", canonical: "tenant-a.localhost" }
|
|
292
|
+
*/
|
|
293
|
+
export function classifyHost(host: string): HostClassification {
|
|
294
|
+
const stripped = stripTrailingDot(
|
|
295
|
+
stripZoneId(stripBrackets(stripPort(host.trim()))),
|
|
296
|
+
);
|
|
297
|
+
const lowered = stripped.toLowerCase();
|
|
298
|
+
|
|
299
|
+
if (lowered === "") {
|
|
300
|
+
return { kind: "reserved", literal: "fqdn", canonical: "" };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (!isValidIP(lowered)) {
|
|
304
|
+
if (lowered === "localhost" || lowered.endsWith(".localhost")) {
|
|
305
|
+
return { kind: "localhost", literal: "fqdn", canonical: lowered };
|
|
306
|
+
}
|
|
307
|
+
if (CLOUD_METADATA_HOSTS.has(lowered)) {
|
|
308
|
+
return { kind: "cloudMetadata", literal: "fqdn", canonical: lowered };
|
|
309
|
+
}
|
|
310
|
+
return { kind: "public", literal: "fqdn", canonical: lowered };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (looksLikeIPv4(lowered)) {
|
|
314
|
+
return { kind: classifyIPv4(lowered), literal: "ipv4", canonical: lowered };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const canonical = normalizeIP(lowered, { ipv6Subnet: 128 });
|
|
318
|
+
|
|
319
|
+
if (looksLikeIPv4(canonical)) {
|
|
320
|
+
return {
|
|
321
|
+
kind: classifyIPv4(canonical),
|
|
322
|
+
literal: "ipv4",
|
|
323
|
+
canonical,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return { kind: classifyIPv6(canonical), literal: "ipv6", canonical };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Strict loopback-IP-literal check per RFC 8252 §7.3.
|
|
332
|
+
*
|
|
333
|
+
* Returns true ONLY for IPv4 `127.0.0.0/8` or IPv6 `::1`. The DNS name
|
|
334
|
+
* `localhost` returns false — RFC 8252 §8.3 explicitly recommends against
|
|
335
|
+
* relying on name resolution for loopback redirect URIs.
|
|
336
|
+
*
|
|
337
|
+
* Use this for OAuth redirect URI matching.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* isLoopbackIP("127.0.0.1") // true
|
|
341
|
+
* isLoopbackIP("::1") // true
|
|
342
|
+
* isLoopbackIP("[::1]:8080") // true
|
|
343
|
+
* isLoopbackIP("localhost") // false (use isLoopbackHost for DNS names)
|
|
344
|
+
* isLoopbackIP("0.0.0.0") // false (unspecified, not loopback)
|
|
345
|
+
*/
|
|
346
|
+
export function isLoopbackIP(host: string): boolean {
|
|
347
|
+
return classifyHost(host).kind === "loopback";
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Permissive loopback check for developer-ergonomics code paths.
|
|
352
|
+
*
|
|
353
|
+
* Returns true for IPv4 `127.0.0.0/8`, IPv6 `::1`, the literal name `localhost`,
|
|
354
|
+
* and any RFC 6761 `.localhost` subdomain (`tenant.localhost`, `app.localhost`).
|
|
355
|
+
*
|
|
356
|
+
* Use this for things like: allowing HTTP for dev servers, skipping Secure
|
|
357
|
+
* cookie requirements, browser-trust heuristics. Do NOT use this for OAuth
|
|
358
|
+
* redirect URI matching — use {@link isLoopbackIP} there.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* isLoopbackHost("localhost") // true
|
|
362
|
+
* isLoopbackHost("tenant.localhost") // true (RFC 6761)
|
|
363
|
+
* isLoopbackHost("127.0.0.1") // true
|
|
364
|
+
* isLoopbackHost("0.0.0.0") // false (unspecified, NOT loopback)
|
|
365
|
+
*/
|
|
366
|
+
export function isLoopbackHost(host: string): boolean {
|
|
367
|
+
const kind = classifyHost(host).kind;
|
|
368
|
+
return kind === "loopback" || kind === "localhost";
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* First-line SSRF gate: returns true ONLY for hosts that classify as `public`.
|
|
373
|
+
*
|
|
374
|
+
* Every RFC 6890 special-purpose range (loopback, private, link-local,
|
|
375
|
+
* unspecified, documentation, multicast, broadcast, reserved, shared address
|
|
376
|
+
* space, benchmarking) and cloud-metadata FQDN returns false.
|
|
377
|
+
*
|
|
378
|
+
* Use this BEFORE issuing a server-side fetch to a user-supplied URL, e.g.
|
|
379
|
+
* OAuth introspection endpoints, webhook targets, or metadata-document
|
|
380
|
+
* fetches (CIMD).
|
|
381
|
+
*
|
|
382
|
+
* Limitations (this is a syntactic check, not a complete SSRF mitigation):
|
|
383
|
+
* - No DNS resolution: a public-looking FQDN that resolves to a private IP
|
|
384
|
+
* passes this check. Re-verify the resolved address before connecting, or
|
|
385
|
+
* pin the socket to the resolved IP.
|
|
386
|
+
* - No DNS-rebinding defense: attackers can return a public IP on the first
|
|
387
|
+
* lookup and a private IP on the second. Resolve once and reuse the IP.
|
|
388
|
+
* - No redirect following: HTTP 3xx responses can redirect to private hosts.
|
|
389
|
+
* Re-run this check on every redirect target, or disable auto-follow.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* isPublicRoutableHost("example.com") // true
|
|
393
|
+
* isPublicRoutableHost("127.0.0.1") // false (loopback)
|
|
394
|
+
* isPublicRoutableHost("169.254.169.254") // false (linkLocal / AWS IMDS)
|
|
395
|
+
* isPublicRoutableHost("metadata.google.internal") // false (cloudMetadata)
|
|
396
|
+
* isPublicRoutableHost("10.0.0.1") // false (private)
|
|
397
|
+
* isPublicRoutableHost("::ffff:127.0.0.1") // false (mapped loopback)
|
|
398
|
+
*/
|
|
399
|
+
export function isPublicRoutableHost(host: string): boolean {
|
|
400
|
+
return classifyHost(host).kind === "public";
|
|
401
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { APIError as BaseAPIError } from "better-call";
|
|
2
|
+
import { APIError } from "../error";
|
|
3
|
+
|
|
4
|
+
export function isAPIError(error: unknown): error is APIError {
|
|
5
|
+
return (
|
|
6
|
+
error instanceof BaseAPIError ||
|
|
7
|
+
error instanceof APIError ||
|
|
8
|
+
(error as { name?: string })?.name === "APIError"
|
|
9
|
+
);
|
|
10
|
+
}
|