@ai-sdk/provider-utils 4.0.64 → 4.0.66

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "4.0.64",
3
+ "version": "4.0.66",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -3,7 +3,7 @@ import { DownloadError } from './download-error';
3
3
  import type { FetchFunction } from './fetch-function';
4
4
  import { isBrowserRuntime } from './is-browser-runtime';
5
5
  import { isSameOrigin } from './is-same-origin';
6
- import { getDefaultDownloadFetch } from './safe-node-fetch';
6
+ import { getDefaultDownloadFetch, isNodeRuntime } from './safe-node-fetch';
7
7
  import { sanitizeRequestHeaders } from './sanitize-request-headers';
8
8
  import { validateDownloadUrl } from './validate-download-url';
9
9
 
@@ -39,6 +39,7 @@ export async function fetchWithValidatedEndpoint({
39
39
  fetch: customFetch,
40
40
  trustedOrigin,
41
41
  redirect = 'error',
42
+ requireDnsValidation = false,
42
43
  }: {
43
44
  url: string | URL;
44
45
  init?: RequestInit;
@@ -49,6 +50,11 @@ export async function fetchWithValidatedEndpoint({
49
50
  */
50
51
  trustedOrigin?: string;
51
52
  redirect?: 'error' | 'manual';
53
+ /**
54
+ * Reject untrusted URLs on server runtimes without a DNS-pinned default
55
+ * transport unless an explicit custom fetch provides equivalent validation.
56
+ */
57
+ requireDnsValidation?: boolean;
52
58
  }): Promise<Response> {
53
59
  const urlText = url.toString();
54
60
  const isTrusted =
@@ -56,6 +62,20 @@ export async function fetchWithValidatedEndpoint({
56
62
 
57
63
  if (!isTrusted) {
58
64
  validateDownloadUrl(urlText);
65
+
66
+ if (
67
+ requireDnsValidation &&
68
+ (customFetch == null || customFetch === globalThis.fetch) &&
69
+ !isNodeRuntime() &&
70
+ // A window alias alone does not distinguish older Deno from a browser.
71
+ !(isBrowserRuntime() && typeof document !== 'undefined')
72
+ ) {
73
+ throw new DownloadError({
74
+ url: urlText,
75
+ message:
76
+ 'This server runtime requires a custom fetch with connect-time DNS validation for untrusted URLs',
77
+ });
78
+ }
59
79
  }
60
80
 
61
81
  const fetch =
@@ -80,6 +100,9 @@ export async function fetchWithValidatedEndpoint({
80
100
  * target (e.g. an internal address) before we ever see its URL, defeating the
81
101
  * guard.
82
102
  *
103
+ * Manually followed redirects from HTTPS to HTTP are rejected before requesting
104
+ * the target.
105
+ *
83
106
  * Request headers are also protected: {@link sanitizeRequestHeaders} strips
84
107
  * proxy/metadata/cookie/hop-by-hop headers before the first request, and all
85
108
  * caller headers except `User-Agent` are dropped on a cross-origin redirect.
@@ -192,6 +215,16 @@ export async function fetchWithValidatedRedirects({
192
215
  void cancelResponseBody(response);
193
216
  const nextUrl = new URL(location, currentUrl).toString();
194
217
 
218
+ if (
219
+ new URL(currentUrl).protocol === 'https:' &&
220
+ new URL(nextUrl).protocol === 'http:'
221
+ ) {
222
+ throw new DownloadError({
223
+ url,
224
+ message: 'Redirect from HTTPS to HTTP was blocked',
225
+ });
226
+ }
227
+
195
228
  // Drop all caller headers except the user-agent before following a
196
229
  // redirect that crosses origin. Only stripping Authorization (as the
197
230
  // fetch spec does) is not enough on the server: providers authenticate