@ai-sdk/provider-utils 3.0.45 → 3.0.47

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 CHANGED
@@ -1,5 +1,21 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 3.0.47
4
+
5
+ ### Patch Changes
6
+
7
+ - d4deb3b: fix(provider-utils): limit the size of a single server-sent event
8
+
9
+ `parseJsonEventStream` and `createEventSourceResponseHandler` buffered an event until it was complete, without a limit. A streaming response that never completed an event could exhaust the memory and crash the process. A single event is now limited to 67,108,864 characters (64 MiB of ASCII text). A stream that exceeds the limit fails with an error and the response body is cancelled.
10
+
11
+ The limit also applies to the streams that `useChat` and `useCompletion` read. `parseJsonEventStream` has a new `maxEventSize` option to change it.
12
+
13
+ ## 3.0.46
14
+
15
+ ### Patch Changes
16
+
17
+ - c31e0a2: Require a DNS-pinned fetch for untrusted MCP OAuth discovery and metadata-derived token/registration URLs on server runtimes without a protected default transport. Preserve developer-configured endpoints and allow explicitly supplied fetch implementations that provide equivalent connect-time validation. Browser workers without a DOM follow the same explicit custom-fetch requirement.
18
+
3
19
  ## 3.0.45
4
20
 
5
21
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -93,7 +93,7 @@ type FetchFunction = typeof globalThis.fetch;
93
93
  * Redirects are rejected by default. Callers using `redirect: 'manual'` must
94
94
  * validate the Location target before issuing another request.
95
95
  */
96
- declare function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, trustedOrigin, redirect, }: {
96
+ declare function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, trustedOrigin, redirect, requireDnsValidation, }: {
97
97
  url: string | URL;
98
98
  init?: RequestInit;
99
99
  fetch?: FetchFunction;
@@ -103,6 +103,11 @@ declare function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, tru
103
103
  */
104
104
  trustedOrigin?: string;
105
105
  redirect?: 'error' | 'manual';
106
+ /**
107
+ * Reject untrusted URLs on server runtimes without a DNS-pinned default
108
+ * transport unless an explicit custom fetch provides equivalent validation.
109
+ */
110
+ requireDnsValidation?: boolean;
106
111
  }): Promise<Response>;
107
112
  /**
108
113
  * Fetches a URL while enforcing the SSRF download guard on every hop.
@@ -497,9 +502,15 @@ declare function normalizeHeaders(headers: HeadersInit | Record<string, string |
497
502
  /**
498
503
  * Parses a JSON event stream into a stream of parsed JSON objects.
499
504
  */
500
- declare function parseJsonEventStream<T>({ stream, schema, }: {
505
+ declare function parseJsonEventStream<T>({ stream, schema, maxEventSize, }: {
501
506
  stream: ReadableStream<Uint8Array>;
502
507
  schema: FlexibleValidator<T>;
508
+ /**
509
+ * Maximum size of a single event in characters. The returned stream fails
510
+ * with an error when an event is larger. Defaults to 64 * 1024 * 1024
511
+ * characters.
512
+ */
513
+ maxEventSize?: number;
503
514
  }): ReadableStream<ParseResult<T>>;
504
515
 
505
516
  declare function parseProviderOptions<OPTIONS>({ provider, providerOptions, schema, }: {
package/dist/index.d.ts CHANGED
@@ -93,7 +93,7 @@ type FetchFunction = typeof globalThis.fetch;
93
93
  * Redirects are rejected by default. Callers using `redirect: 'manual'` must
94
94
  * validate the Location target before issuing another request.
95
95
  */
96
- declare function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, trustedOrigin, redirect, }: {
96
+ declare function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, trustedOrigin, redirect, requireDnsValidation, }: {
97
97
  url: string | URL;
98
98
  init?: RequestInit;
99
99
  fetch?: FetchFunction;
@@ -103,6 +103,11 @@ declare function fetchWithValidatedEndpoint({ url, init, fetch: customFetch, tru
103
103
  */
104
104
  trustedOrigin?: string;
105
105
  redirect?: 'error' | 'manual';
106
+ /**
107
+ * Reject untrusted URLs on server runtimes without a DNS-pinned default
108
+ * transport unless an explicit custom fetch provides equivalent validation.
109
+ */
110
+ requireDnsValidation?: boolean;
106
111
  }): Promise<Response>;
107
112
  /**
108
113
  * Fetches a URL while enforcing the SSRF download guard on every hop.
@@ -497,9 +502,15 @@ declare function normalizeHeaders(headers: HeadersInit | Record<string, string |
497
502
  /**
498
503
  * Parses a JSON event stream into a stream of parsed JSON objects.
499
504
  */
500
- declare function parseJsonEventStream<T>({ stream, schema, }: {
505
+ declare function parseJsonEventStream<T>({ stream, schema, maxEventSize, }: {
501
506
  stream: ReadableStream<Uint8Array>;
502
507
  schema: FlexibleValidator<T>;
508
+ /**
509
+ * Maximum size of a single event in characters. The returned stream fails
510
+ * with an error when an event is larger. Defaults to 64 * 1024 * 1024
511
+ * characters.
512
+ */
513
+ maxEventSize?: number;
503
514
  }): ReadableStream<ParseResult<T>>;
504
515
 
505
516
  declare function parseProviderOptions<OPTIONS>({ provider, providerOptions, schema, }: {
package/dist/index.js CHANGED
@@ -543,12 +543,20 @@ async function fetchWithValidatedEndpoint({
543
543
  init,
544
544
  fetch: customFetch,
545
545
  trustedOrigin,
546
- redirect = "error"
546
+ redirect = "error",
547
+ requireDnsValidation = false
547
548
  }) {
548
549
  const urlText = url.toString();
549
550
  const isTrusted = trustedOrigin !== void 0 && isSameOrigin(urlText, trustedOrigin);
550
551
  if (!isTrusted) {
551
552
  validateDownloadUrl(urlText);
553
+ if (requireDnsValidation && (customFetch == null || customFetch === globalThis.fetch) && !isNodeRuntime() && // A window alias alone does not distinguish older Deno from a browser.
554
+ !(isBrowserRuntime() && typeof document !== "undefined")) {
555
+ throw new DownloadError({
556
+ url: urlText,
557
+ message: "This server runtime requires a custom fetch with connect-time DNS validation for untrusted URLs"
558
+ });
559
+ }
552
560
  }
553
561
  const fetch = isTrusted && customFetch != null ? customFetch : isTrusted ? globalThis.fetch : await getValidatedFetch(customFetch);
554
562
  return await fetch(url, {
@@ -861,7 +869,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
861
869
  }
862
870
 
863
871
  // src/version.ts
864
- var VERSION = true ? "3.0.45" : "0.0.0-test";
872
+ var VERSION = true ? "3.0.47" : "0.0.0-test";
865
873
 
866
874
  // src/get-from-api.ts
867
875
  var getOriginalFetch = () => globalThis.fetch;
@@ -1273,11 +1281,13 @@ function isParsableJson(input) {
1273
1281
 
1274
1282
  // src/parse-json-event-stream.ts
1275
1283
  var import_stream = require("eventsource-parser/stream");
1284
+ var DEFAULT_MAX_EVENT_SIZE = 64 * 1024 * 1024;
1276
1285
  function parseJsonEventStream({
1277
1286
  stream,
1278
- schema
1287
+ schema,
1288
+ maxEventSize = DEFAULT_MAX_EVENT_SIZE
1279
1289
  }) {
1280
- return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough(
1290
+ return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream({ maxBufferSize: maxEventSize })).pipeThrough(
1281
1291
  new TransformStream({
1282
1292
  async transform({ data }, controller) {
1283
1293
  if (data === "[DONE]") {