Unverified Commit 99a947ce authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Fix error propagation and teardown in Server Action request decoding (#87193)

This change switches to promise-based pipelines and uses
`AbortController` to ensure multipart pipelines are properly canceled
when decoding fails. Request bodies are fully consumed or aborted so
decoding cannot hang or silently drop errors.
parent aba09f4a
...@@ -858,8 +858,10 @@ export async function handleAction({ ...@@ -858,8 +858,10 @@ export async function handleAction({
temporaryReferences = createTemporaryReferenceSet() temporaryReferences = createTemporaryReferenceSet()
const { Transform, pipeline } = const { PassThrough, Readable, Transform } =
require('node:stream') as typeof import('node:stream') require('node:stream') as typeof import('node:stream')
const { pipeline } =
require('node:stream/promises') as typeof import('node:stream/promises')
const defaultBodySizeLimit = '1 MB' const defaultBodySizeLimit = '1 MB'
const bodySizeLimit = const bodySizeLimit =
...@@ -893,14 +895,6 @@ export async function handleAction({ ...@@ -893,14 +895,6 @@ export async function handleAction({
}, },
}) })
const sizeLimitedBody = pipeline(
req.body,
sizeLimitTransform,
// Avoid unhandled errors from `pipeline()` by passing an empty completion callback.
// We'll propagate the errors properly when consuming the stream.
() => {}
)
if (isMultipartAction) { if (isMultipartAction) {
if (isFetchAction) { if (isFetchAction) {
// A fetch action with a multipart body. // A fetch action with a multipart body.
...@@ -919,47 +913,51 @@ export async function handleAction({ ...@@ -919,47 +913,51 @@ export async function handleAction({
limits: { fieldSize: bodySizeLimitBytes }, limits: { fieldSize: bodySizeLimitBytes },
}) })
// We need to use `pipeline(one, two)` instead of `one.pipe(two)` to propagate size limit errors correctly. const abortController = new AbortController()
pipeline( try {
sizeLimitedBody, ;[, boundActionArguments] = await Promise.all([
busboy, pipeline(req.body, sizeLimitTransform, busboy, {
// Avoid unhandled errors from `pipeline()` by passing an empty completion callback. signal: abortController.signal,
// We'll propagate the errors properly when consuming the stream. }),
() => {} decodeReplyFromBusboy(busboy, serverModuleMap, {
) temporaryReferences,
}),
boundActionArguments = await decodeReplyFromBusboy( ])
busboy, } catch (err) {
serverModuleMap, abortController.abort()
{ temporaryReferences } throw err
) }
} else { } else {
// Multipart POST, but not a fetch action. // Multipart POST, but not a fetch action.
// Potentially an MPA action, we have to try decoding it to check. // Potentially an MPA action, we have to try decoding it to check.
const sizeLimitedBody = new PassThrough()
// React doesn't yet publish a busboy version of decodeAction // React doesn't yet publish a busboy version of decodeAction
// so we polyfill the parsing of FormData. // so we polyfill the parsing of FormData.
const fakeRequest = new Request('http://localhost', { const fakeRequest = new Request('http://localhost', {
method: 'POST', method: 'POST',
// @ts-expect-error // @ts-expect-error
headers: { 'Content-Type': contentType }, headers: { 'Content-Type': contentType },
body: new ReadableStream({ body: Readable.toWeb(
start: (controller) => { sizeLimitedBody
sizeLimitedBody.on('data', (chunk) => { ) as ReadableStream<Uint8Array>,
controller.enqueue(new Uint8Array(chunk))
})
sizeLimitedBody.on('end', () => {
controller.close()
})
sizeLimitedBody.on('error', (err) => {
controller.error(err)
})
},
}),
duplex: 'half', duplex: 'half',
}) })
const formData = await fakeRequest.formData() let formData: FormData
const abortController = new AbortController()
try {
;[, formData] = await Promise.all([
pipeline(req.body, sizeLimitTransform, sizeLimitedBody, {
signal: abortController.signal,
}),
fakeRequest.formData(),
])
} catch (err) {
abortController.abort()
throw err
}
if (areAllActionIdsValid(formData, serverModuleMap) === false) { if (areAllActionIdsValid(formData, serverModuleMap) === false) {
// TODO: This can be from skew or manipulated input. We should handle this case // TODO: This can be from skew or manipulated input. We should handle this case
...@@ -1023,10 +1021,17 @@ export async function handleAction({ ...@@ -1023,10 +1021,17 @@ export async function handleAction({
// In practice, this happens if `encodeReply` returned a string instead of FormData, // In practice, this happens if `encodeReply` returned a string instead of FormData,
// which can happen for very simple JSON-like values that don't need multiple flight rows. // which can happen for very simple JSON-like values that don't need multiple flight rows.
const sizeLimitedBody = new PassThrough()
const chunks: Buffer[] = [] const chunks: Buffer[] = []
for await (const chunk of sizeLimitedBody) { await Promise.all([
chunks.push(Buffer.from(chunk)) pipeline(req.body, sizeLimitTransform, sizeLimitedBody),
} (async () => {
for await (const chunk of sizeLimitedBody) {
chunks.push(Buffer.from(chunk))
}
})(),
])
const actionData = Buffer.concat(chunks).toString('utf-8') const actionData = Buffer.concat(chunks).toString('utf-8')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment