-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is addressing a critical error, see GHSA-8cp3-66vr-3r4c That was not trivial as I bumped into this bug node-formidable/formidable#959 which led me to move form parsing to the middleware stage
- Loading branch information
Showing
8 changed files
with
110 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
import { tmpdir } from 'node:os' | ||
import formidable from 'formidable' | ||
import formidable, { type Fields, type Files } from 'formidable' | ||
import { mkdirp } from '#lib/fs' | ||
|
||
const { IncomingForm } = formidable | ||
const uploadDir = `${tmpdir()}/formidable` | ||
await mkdirp(uploadDir) | ||
|
||
export default req => new Promise((resolve, reject) => { | ||
const form = new IncomingForm({ uploadDir }) | ||
return form.parse(req, (err, fields, files) => { | ||
if (err) reject(err) | ||
else resolve({ fields, files }) | ||
}) | ||
}) | ||
export interface ParsedForm { | ||
fields: Fields | ||
files: Files | ||
} | ||
|
||
// Parse forms in an early middleware to not let the time to any other middleware | ||
// to start consuming the form request stream, to avoid getting hanging requests | ||
// See https://github.com/node-formidable/formidable/issues/959 | ||
export async function parseFormMiddleware (req, res, next) { | ||
if (!req.headers['content-type'].startsWith('multipart/form-data')) return next() | ||
|
||
try { | ||
const form = formidable({ uploadDir }) | ||
const [ fields, files ] = await form.parse(req) | ||
const reqForm: ParsedForm = { fields, files } | ||
req.form = reqForm | ||
next() | ||
} catch (err) { | ||
next(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters