From 9b7547d745d0620bd320aa97e3a2c91bd4ede8c0 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 1 Feb 2023 18:08:36 +0100 Subject: [PATCH] Add maxByteLength option to Body consume utilities This commit adds an option bag to `Body/arrayBuffer`, `Body/blob`, `Body/formData`, `Body/json`, and `Body/text` that allows configuration of a max byte length for the underlying consumed body. This allows a developer to limit how much data their application parses without having to resort to a costly user-land TransformStream that may break internal optimizations of the body consume helpers. --- fetch.bs | 75 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/fetch.bs b/fetch.bs index c4c2ff47c..a7419fc1b 100644 --- a/fetch.bs +++ b/fetch.bs @@ -1464,11 +1464,12 @@ must be an algorithm accepting an exception.
-

To fully read a body body, given an algorithm -processBody, an algorithm processBodyError, and an optional null, -parallel queue, or global object taskDestination (default -null), run these steps. processBody must be an algorithm accepting a -byte sequence. processBodyError must be an algorithm accepting no arguments. +

To fully read a body body, given an +optional number maxByteLength, an algorithm processBody, an +algorithm processBodyError, and an optional null, parallel queue, or +global object taskDestination (default null), run these steps. +processBody must be an algorithm accepting a byte sequence. +processBodyError must be an algorithm accepting no arguments.

  1. If taskDestination is null, then set taskDestination to the result of @@ -1486,7 +1487,7 @@ null), run these steps. processBody must be an algorithm accepting a errorSteps with that exception and return.

  2. Read all bytes from - reader, given successSteps and errorSteps. + reader, given maxByteLength, successSteps and errorSteps.

@@ -6902,11 +6903,15 @@ steps: interface mixin Body { readonly attribute ReadableStream? body; readonly attribute boolean bodyUsed; - [NewObject] Promise<ArrayBuffer> arrayBuffer(); - [NewObject] Promise<Blob> blob(); - [NewObject] Promise<FormData> formData(); - [NewObject] Promise<any> json(); - [NewObject] Promise<USVString> text(); + [NewObject] Promise<ArrayBuffer> arrayBuffer(optional BodyConsumeOptions options = {}); + [NewObject] Promise<Blob> blob(optional BodyConsumeOptions options = {}); + [NewObject] Promise<FormData> formData(optional BodyConsumeOptions options = {}); + [NewObject] Promise<any> json(optional BodyConsumeOptions options = {}); + [NewObject] Promise<USVString> text(optional BodyConsumeOptions options = {}); +}; + +dictionary BodyConsumeOptions { + [EnforceRange] unsigned long long maxByteLength; };

Formats you would not want a network layer to be dependent upon, such as @@ -6935,19 +6940,19 @@ returns failure or a MIME type.

requestOrResponse . bodyUsed

Returns whether requestOrResponse's body has been read from. -

requestOrResponse . arrayBuffer() +
requestOrResponse . arrayBuffer(options)

Returns a promise fulfilled with requestOrResponse's body as {{ArrayBuffer}}. -

requestOrResponse . blob() +
requestOrResponse . blob(options)

Returns a promise fulfilled with requestOrResponse's body as {{Blob}}. -

requestOrResponse . formData() +
requestOrResponse . formData(options)

Returns a promise fulfilled with requestOrResponse's body as {{FormData}}. -

requestOrResponse . json() +
requestOrResponse . json(options)

Returns a promise fulfilled with requestOrResponse's body parsed as JSON. -

requestOrResponse . text() +
requestOrResponse . text(options)

Returns a promise fulfilled with requestOrResponse's body as string. @@ -6967,9 +6972,10 @@ returns failure or a MIME type.

The consume body -algorithm, given an object that includes {{Body}} object and an algorithm that takes a -byte sequence and returns a JavaScript value or throws an exception -convertBytesToJSValue, runs these steps: +algorithm, given an object that includes {{Body}} object, a +{{BodyConsumeOptions}} options and an algorithm that takes a byte sequence and +returns a JavaScript value or throws an exception convertBytesToJSValue, runs these +steps:

  1. If object is unusable, then return a promise rejected with @@ -6989,32 +6995,32 @@ algorithm, given an object that includes {{Body}} object and an algor with an empty byte sequence.

  2. Otherwise, fully read object's body given - successSteps, errorSteps, and object's - relevant global object. + options's {{BodyConsumeOptions/maxByteLength}}, successSteps, errorSteps, + and object's relevant global object.

  3. Return promise.

-

The arrayBuffer() method steps are to return the result -of running consume body with this and the following step given a -byte sequence bytes: return a new {{ArrayBuffer}} whose contents are +

The arrayBuffer(options) method steps are to return the result +of running consume body with this, options and the following step +given a byte sequence bytes: return a new {{ArrayBuffer}} whose contents are bytes.

The above method can reject with a {{RangeError}}.

-

The blob() method steps are to return the result -of running consume body with this and the following step given a -byte sequence bytes: return a {{Blob}} whose contents are bytes -and whose {{Blob/type}} attribute is this's MIME type. +

The blob(options) method steps are to return the result +of running consume body with this, options and the following step +given a byte sequence bytes: return a {{Blob}} whose contents are +bytes and whose {{Blob/type}} attribute is this's MIME type.

-

The formData() method steps are to return the result of -running consume body with this and the following step given a +

The formData(options) method steps are to return the result of +running consume body with this, options and the following step given a byte sequence bytes: switch on this's MIME type's essence and run the corresponding steps: @@ -7075,15 +7081,16 @@ running consume body with this and the following step giv

-

The json() method steps are to return the result -of running consume body with this and parse JSON from bytes. +

The json(options) method steps are to return the result +of running consume body with this, options and +parse JSON from bytes.

The above method can reject with a {{SyntaxError}}.

-

The text() method steps are to return the result -of running consume body with this and UTF-8 decode. +

The text(options) method steps are to return the result +of running consume body with this, options and UTF-8 decode.