@ai-sdk/provider-utils 5.0.79 → 5.0.81
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/CHANGELOG.md +21 -0
- package/dist/experimental-extraction/index.d.ts +38 -0
- package/dist/experimental-extraction/index.d.ts.map +1 -0
- package/dist/experimental-extraction/index.js +1400 -0
- package/dist/experimental-extraction/index.js.map +1 -0
- package/dist/index.d.ts +14 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -150
- package/dist/index.js.map +1 -1
- package/package.json +8 -4
- package/src/download-blob.ts +8 -1
- package/src/experimental-extraction/index.ts +1 -0
- package/src/extraction-language-model.ts +359 -0
- package/src/fetch-untrusted-url.ts +2 -2
- package/src/fetch-with-validated-redirects.ts +10 -34
- package/src/validate-download-url.ts +2 -30
- package/src/safe-node-fetch.ts +0 -201
package/dist/index.js
CHANGED
|
@@ -625,7 +625,7 @@ function handleFetchError({ error, url, requestBodyValues }) {
|
|
|
625
625
|
}
|
|
626
626
|
//#endregion
|
|
627
627
|
//#region src/version.ts
|
|
628
|
-
const VERSION = "5.0.
|
|
628
|
+
const VERSION = "5.0.81";
|
|
629
629
|
//#endregion
|
|
630
630
|
//#region src/normalize-headers.ts
|
|
631
631
|
/**
|
|
@@ -1177,13 +1177,58 @@ function isSameOrigin(url, baseUrl) {
|
|
|
1177
1177
|
}
|
|
1178
1178
|
}
|
|
1179
1179
|
//#endregion
|
|
1180
|
+
//#region src/sanitize-request-headers.ts
|
|
1181
|
+
/**
|
|
1182
|
+
* Request headers stripped before fetching an untrusted URL: host/virtual-host
|
|
1183
|
+
* routing, proxy/origin spoofing, cloud-metadata, cookies, and hop-by-hop
|
|
1184
|
+
* transport headers (RFC 7230 §6.1).
|
|
1185
|
+
*
|
|
1186
|
+
* `Authorization` and other credential-bearing caller headers (e.g. `x-key`)
|
|
1187
|
+
* are intentionally not listed because trusted provider requests may need
|
|
1188
|
+
* them. `fetchUntrustedUrl` separately restricts an untrusted first hop to an
|
|
1189
|
+
* explicit allowlist of non-credential request metadata. Both it and
|
|
1190
|
+
* `fetchWithValidatedRedirects` drop all caller headers except the user-agent
|
|
1191
|
+
* on a cross-origin redirect.
|
|
1192
|
+
*/
|
|
1193
|
+
const BLOCKED_REQUEST_HEADERS = [
|
|
1194
|
+
"connection",
|
|
1195
|
+
"keep-alive",
|
|
1196
|
+
"te",
|
|
1197
|
+
"trailer",
|
|
1198
|
+
"transfer-encoding",
|
|
1199
|
+
"upgrade",
|
|
1200
|
+
"host",
|
|
1201
|
+
"forwarded",
|
|
1202
|
+
"proxy-authorization",
|
|
1203
|
+
"via",
|
|
1204
|
+
"x-forwarded-for",
|
|
1205
|
+
"x-forwarded-host",
|
|
1206
|
+
"x-forwarded-proto",
|
|
1207
|
+
"x-real-ip",
|
|
1208
|
+
"metadata",
|
|
1209
|
+
"metadata-flavor",
|
|
1210
|
+
"x-aws-ec2-metadata-token",
|
|
1211
|
+
"x-metadata-token",
|
|
1212
|
+
"cookie",
|
|
1213
|
+
"set-cookie"
|
|
1214
|
+
];
|
|
1215
|
+
/**
|
|
1216
|
+
* Returns a fresh `Headers` built from `input` with {@link BLOCKED_REQUEST_HEADERS}
|
|
1217
|
+
* removed. The input is never mutated.
|
|
1218
|
+
*/
|
|
1219
|
+
function sanitizeRequestHeaders(input) {
|
|
1220
|
+
const headers = new Headers(input);
|
|
1221
|
+
for (const name of BLOCKED_REQUEST_HEADERS) headers.delete(name);
|
|
1222
|
+
return headers;
|
|
1223
|
+
}
|
|
1224
|
+
//#endregion
|
|
1180
1225
|
//#region src/validate-download-url.ts
|
|
1181
1226
|
/**
|
|
1182
1227
|
* Validates that a URL is safe to download from, blocking private/internal addresses
|
|
1183
1228
|
* to prevent SSRF attacks.
|
|
1184
1229
|
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
1230
|
+
* This function performs string/literal-IP checks only. DNS validation must
|
|
1231
|
+
* be provided by the fetch transport or network egress controls.
|
|
1187
1232
|
*
|
|
1188
1233
|
* @param url - The URL string to validate.
|
|
1189
1234
|
* @throws DownloadError if the URL is unsafe.
|
|
@@ -1226,16 +1271,6 @@ function validateDownloadUrl(url) {
|
|
|
1226
1271
|
});
|
|
1227
1272
|
}
|
|
1228
1273
|
}
|
|
1229
|
-
/**
|
|
1230
|
-
* Validates an address returned by DNS before it is used to open a socket.
|
|
1231
|
-
* This is intentionally not exported from the package entry point.
|
|
1232
|
-
*/
|
|
1233
|
-
function validateDownloadAddress({ address, family, hostname }) {
|
|
1234
|
-
if (family === 4 ? !isIPv4(address) || isPrivateIPv4(address) : family === 6 ? isPrivateIPv6(address) : true) throw new DownloadError({
|
|
1235
|
-
url: hostname,
|
|
1236
|
-
message: `Hostname ${hostname} resolved to disallowed IP address ${address}`
|
|
1237
|
-
});
|
|
1238
|
-
}
|
|
1239
1274
|
function isIPv4(hostname) {
|
|
1240
1275
|
const parts = hostname.split(".");
|
|
1241
1276
|
if (parts.length !== 4) return false;
|
|
@@ -1320,123 +1355,6 @@ function isPrivateIPv6(ip) {
|
|
|
1320
1355
|
return false;
|
|
1321
1356
|
}
|
|
1322
1357
|
//#endregion
|
|
1323
|
-
//#region src/safe-node-fetch.ts
|
|
1324
|
-
/**
|
|
1325
|
-
* Creates a DNS lookup hook that validates every returned address before
|
|
1326
|
-
* returning the callback shape requested by the HTTP connector. Because
|
|
1327
|
-
* resolution and validation happen inside the connector, the socket is pinned
|
|
1328
|
-
* to the validated result and DNS rebinding cannot introduce a second lookup.
|
|
1329
|
-
*/
|
|
1330
|
-
function createSafeLookup(lookup) {
|
|
1331
|
-
return ((hostname, options, callback) => {
|
|
1332
|
-
lookup(hostname, {
|
|
1333
|
-
...options,
|
|
1334
|
-
all: true
|
|
1335
|
-
}, (error, addresses) => {
|
|
1336
|
-
if (error) {
|
|
1337
|
-
callback(error);
|
|
1338
|
-
return;
|
|
1339
|
-
}
|
|
1340
|
-
try {
|
|
1341
|
-
const [firstAddress] = addresses;
|
|
1342
|
-
if (firstAddress == null) throw new Error(`Hostname ${hostname} did not resolve to an address`);
|
|
1343
|
-
for (const { address, family } of addresses) validateDownloadAddress({
|
|
1344
|
-
address,
|
|
1345
|
-
family,
|
|
1346
|
-
hostname
|
|
1347
|
-
});
|
|
1348
|
-
if (options.all === true) callback(null, addresses);
|
|
1349
|
-
else callback(null, firstAddress.address, firstAddress.family);
|
|
1350
|
-
} catch (error) {
|
|
1351
|
-
callback(error instanceof Error ? error : new Error(String(error)));
|
|
1352
|
-
}
|
|
1353
|
-
});
|
|
1354
|
-
});
|
|
1355
|
-
}
|
|
1356
|
-
let safeNodeFetchPromise;
|
|
1357
|
-
function isNodeRuntime() {
|
|
1358
|
-
const runtimeProcess = globalThis.process;
|
|
1359
|
-
return runtimeProcess?.release?.name === "node" && runtimeProcess.versions?.bun == null && runtimeProcess.versions?.deno == null && runtimeProcess.title !== "workerd" && globalThis.EdgeRuntime == null;
|
|
1360
|
-
}
|
|
1361
|
-
async function getDefaultDownloadFetch() {
|
|
1362
|
-
if (!isNodeRuntime()) return globalThis.fetch;
|
|
1363
|
-
return safeNodeFetchPromise ??= Promise.resolve().then(createSafeNodeFetch);
|
|
1364
|
-
}
|
|
1365
|
-
function createSafeNodeFetch() {
|
|
1366
|
-
const module = loadBuiltinModule("node:module");
|
|
1367
|
-
const { lookup } = loadBuiltinModule("node:dns");
|
|
1368
|
-
const { Agent, fetch } = module.createRequire(getCurrentModulePath())("undici");
|
|
1369
|
-
const dispatcher = new Agent({ connect: { lookup: createSafeLookup(lookup) } });
|
|
1370
|
-
return ((input, init) => fetch(input, {
|
|
1371
|
-
...init,
|
|
1372
|
-
dispatcher
|
|
1373
|
-
}));
|
|
1374
|
-
}
|
|
1375
|
-
function loadBuiltinModule(id) {
|
|
1376
|
-
const builtinModule = globalThis.process?.getBuiltinModule?.(id);
|
|
1377
|
-
if (builtinModule == null) throw new Error(`Node.js built-in module ${id} is unavailable`);
|
|
1378
|
-
return builtinModule;
|
|
1379
|
-
}
|
|
1380
|
-
function getCurrentModulePath() {
|
|
1381
|
-
const originalPrepareStackTrace = Error.prepareStackTrace;
|
|
1382
|
-
try {
|
|
1383
|
-
Error.prepareStackTrace = (_error, callSites) => callSites;
|
|
1384
|
-
const error = /* @__PURE__ */ new Error("Capture current module path");
|
|
1385
|
-
Error.captureStackTrace(error, getCurrentModulePath);
|
|
1386
|
-
const [caller] = error.stack;
|
|
1387
|
-
const fileName = caller?.getFileName();
|
|
1388
|
-
if (fileName == null) throw new Error("Unable to determine the current module path");
|
|
1389
|
-
return fileName;
|
|
1390
|
-
} finally {
|
|
1391
|
-
Error.prepareStackTrace = originalPrepareStackTrace;
|
|
1392
|
-
}
|
|
1393
|
-
}
|
|
1394
|
-
//#endregion
|
|
1395
|
-
//#region src/sanitize-request-headers.ts
|
|
1396
|
-
/**
|
|
1397
|
-
* Request headers stripped before fetching an untrusted URL: host/virtual-host
|
|
1398
|
-
* routing, proxy/origin spoofing, cloud-metadata, cookies, and hop-by-hop
|
|
1399
|
-
* transport headers (RFC 7230 §6.1).
|
|
1400
|
-
*
|
|
1401
|
-
* `Authorization` and other credential-bearing caller headers (e.g. `x-key`)
|
|
1402
|
-
* are intentionally not listed because trusted provider requests may need
|
|
1403
|
-
* them. `fetchUntrustedUrl` separately restricts an untrusted first hop to an
|
|
1404
|
-
* explicit allowlist of non-credential request metadata. Both it and
|
|
1405
|
-
* `fetchWithValidatedRedirects` drop all caller headers except the user-agent
|
|
1406
|
-
* on a cross-origin redirect.
|
|
1407
|
-
*/
|
|
1408
|
-
const BLOCKED_REQUEST_HEADERS = [
|
|
1409
|
-
"connection",
|
|
1410
|
-
"keep-alive",
|
|
1411
|
-
"te",
|
|
1412
|
-
"trailer",
|
|
1413
|
-
"transfer-encoding",
|
|
1414
|
-
"upgrade",
|
|
1415
|
-
"host",
|
|
1416
|
-
"forwarded",
|
|
1417
|
-
"proxy-authorization",
|
|
1418
|
-
"via",
|
|
1419
|
-
"x-forwarded-for",
|
|
1420
|
-
"x-forwarded-host",
|
|
1421
|
-
"x-forwarded-proto",
|
|
1422
|
-
"x-real-ip",
|
|
1423
|
-
"metadata",
|
|
1424
|
-
"metadata-flavor",
|
|
1425
|
-
"x-aws-ec2-metadata-token",
|
|
1426
|
-
"x-metadata-token",
|
|
1427
|
-
"cookie",
|
|
1428
|
-
"set-cookie"
|
|
1429
|
-
];
|
|
1430
|
-
/**
|
|
1431
|
-
* Returns a fresh `Headers` built from `input` with {@link BLOCKED_REQUEST_HEADERS}
|
|
1432
|
-
* removed. The input is never mutated.
|
|
1433
|
-
*/
|
|
1434
|
-
function sanitizeRequestHeaders(input) {
|
|
1435
|
-
const headers = new Headers(input);
|
|
1436
|
-
for (const name of BLOCKED_REQUEST_HEADERS) headers.delete(name);
|
|
1437
|
-
return headers;
|
|
1438
|
-
}
|
|
1439
|
-
//#endregion
|
|
1440
1358
|
//#region src/fetch-with-validated-redirects.ts
|
|
1441
1359
|
const MAX_DOWNLOAD_REDIRECTS = 10;
|
|
1442
1360
|
const REDIRECT_STATUS_CODES = /* @__PURE__ */ new Set([
|
|
@@ -1446,28 +1364,25 @@ const REDIRECT_STATUS_CODES = /* @__PURE__ */ new Set([
|
|
|
1446
1364
|
307,
|
|
1447
1365
|
308
|
|
1448
1366
|
]);
|
|
1449
|
-
async function getValidatedFetch(customFetch) {
|
|
1450
|
-
return customFetch == null || customFetch === globalThis.fetch ? await getDefaultDownloadFetch() : customFetch;
|
|
1451
|
-
}
|
|
1452
1367
|
/**
|
|
1453
1368
|
* Fetches one validated URL without following redirects.
|
|
1454
1369
|
*
|
|
1455
|
-
*
|
|
1456
|
-
*
|
|
1370
|
+
* Uses the supplied fetch or the runtime's global fetch. URL validation does
|
|
1371
|
+
* not resolve DNS; inject a transport such as guarded-fetch on Node.js
|
|
1372
|
+
* when connection-time DNS validation is needed.
|
|
1457
1373
|
* Redirects are rejected by default. Callers using `redirect: 'manual'` must
|
|
1458
1374
|
* validate the Location target before issuing another request.
|
|
1459
1375
|
*/
|
|
1460
1376
|
async function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, trustedOrigin, redirect = "error", requireDnsValidation = false }) {
|
|
1461
1377
|
const urlText = url.toString();
|
|
1462
|
-
|
|
1463
|
-
if (!isTrusted) {
|
|
1378
|
+
if (!(trustedOrigin !== void 0 && isSameOrigin(urlText, trustedOrigin))) {
|
|
1464
1379
|
validateDownloadUrl(urlText);
|
|
1465
|
-
if (requireDnsValidation && (customFetch == null || customFetch === globalThis.fetch) && !
|
|
1380
|
+
if (requireDnsValidation && (customFetch == null || customFetch === globalThis.fetch) && !(isBrowserRuntime() && typeof document !== "undefined")) throw new DownloadError({
|
|
1466
1381
|
url: urlText,
|
|
1467
1382
|
message: "This server runtime requires a custom fetch with connect-time DNS validation for untrusted URLs"
|
|
1468
1383
|
});
|
|
1469
1384
|
}
|
|
1470
|
-
return await (
|
|
1385
|
+
return await (customFetch ?? globalThis.fetch)(url, {
|
|
1471
1386
|
...init,
|
|
1472
1387
|
redirect
|
|
1473
1388
|
});
|
|
@@ -1513,11 +1428,8 @@ async function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, trust
|
|
|
1513
1428
|
* The returned response is the final (non-redirect) response. The caller is
|
|
1514
1429
|
* responsible for checking `response.ok` and reading the body.
|
|
1515
1430
|
*
|
|
1516
|
-
*
|
|
1517
|
-
*
|
|
1518
|
-
* hostname-to-private-IP and DNS-rebinding bypasses. An injected fetch is
|
|
1519
|
-
* responsible for equivalent connect-time validation. Other runtimes should
|
|
1520
|
-
* constrain egress at the network layer when handling untrusted URLs.
|
|
1431
|
+
* URL validation does not resolve DNS. Use guarded-fetch on Node.js
|
|
1432
|
+
* for connection-time DNS validation, or enforce network egress restrictions.
|
|
1521
1433
|
*
|
|
1522
1434
|
* @throws DownloadError if a hop is unsafe, the redirect limit is exceeded, or
|
|
1523
1435
|
* a redirect cannot be validated on a non-browser runtime.
|
|
@@ -1534,9 +1446,8 @@ async function fetchWithValidatedRedirects({ url, headers, abortSignal, maxRedir
|
|
|
1534
1446
|
};
|
|
1535
1447
|
let currentUrl = url;
|
|
1536
1448
|
for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
const fetch = isTrustedHop && customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getValidatedFetch(customFetch);
|
|
1449
|
+
if (!(trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin))) validateDownloadUrl(currentUrl);
|
|
1450
|
+
const fetch = customFetch ?? globalThis.fetch;
|
|
1540
1451
|
const response = await fetch(currentUrl, perHopInit("manual"));
|
|
1541
1452
|
if (response.type === "opaqueredirect") {
|
|
1542
1453
|
if (!isBrowserRuntime()) throw new DownloadError({
|
|
@@ -1590,9 +1501,9 @@ const SAFE_UNTRUSTED_FIRST_HOP_HEADERS = /* @__PURE__ */ new Set([
|
|
|
1590
1501
|
]);
|
|
1591
1502
|
/**
|
|
1592
1503
|
* Fetches an untrusted URL with first-hop credential isolation and validated
|
|
1593
|
-
* redirects. Uses the URL validation,
|
|
1504
|
+
* redirects. Uses the URL validation, runtime fetch transport, redirect
|
|
1594
1505
|
* limits, and cross-origin header stripping of {@link fetchWithValidatedRedirects}.
|
|
1595
|
-
*
|
|
1506
|
+
* Inject guarded-fetch on Node.js for connect-time DNS validation.
|
|
1596
1507
|
*
|
|
1597
1508
|
* Without a matching `credentialedOrigin` (or `trustedOrigin` when it is
|
|
1598
1509
|
* omitted), only allowlisted request metadata is sent on the first hop.
|
|
@@ -1708,6 +1619,7 @@ async function readResponseWithSizeLimit({ response, url, maxBytes = DEFAULT_MAX
|
|
|
1708
1619
|
* @param options - Optional settings for the download.
|
|
1709
1620
|
* @param options.maxBytes - Maximum allowed download size in bytes. Defaults to 100 MiB.
|
|
1710
1621
|
* @param options.abortSignal - An optional abort signal to cancel the download.
|
|
1622
|
+
* @param options.fetch - Fetch transport, e.g. guarded-fetch for DNS validation on Node.js.
|
|
1711
1623
|
* @returns A Promise that resolves to the downloaded Blob.
|
|
1712
1624
|
*
|
|
1713
1625
|
* @throws DownloadError if the download fails or exceeds maxBytes.
|
|
@@ -1716,7 +1628,8 @@ async function downloadBlob(url, options) {
|
|
|
1716
1628
|
try {
|
|
1717
1629
|
const response = await fetchUntrustedUrl({
|
|
1718
1630
|
url,
|
|
1719
|
-
abortSignal: options?.abortSignal
|
|
1631
|
+
abortSignal: options?.abortSignal,
|
|
1632
|
+
fetch: options?.fetch
|
|
1720
1633
|
});
|
|
1721
1634
|
if (!response.ok) {
|
|
1722
1635
|
await cancelResponseBody(response);
|