Summary
In affected Fastify multipart configurations, saveRequestFiles can mistake a forged request-body object for an uploaded file and preserve its filename without reducing it to a basename. An unauthenticated client may use this behavior to write attacker-chosen bytes to a relative path if the application uses the returned filename as a destination.
Affected context
The issue concerns @fastify/multipart when it is registered with the attachFieldsToBody option and an application calls saveRequestFiles on parsed request data. A plain JSON request or a JSON-typed multipart field can provide an object with truthy file and buffer properties that the helper treats as an uploaded file. Risk is greater when the application uses the returned filename as a filesystem destination; the helper's handling can also bypass upload hooks and file count and size limits.
Exploit preconditions
- Attack surface: A Fastify endpoint processes request data with the affected multipart plugin configuration.
- Required access: The described request can be sent by an unauthenticated client, provided the endpoint is reachable.
- Trigger point: The application calls
saveRequestFileson parsed request data, including data originating from a plain JSON request or a JSON-typed multipart field. - Why exploitation is possible: Objects with truthy
fileandbufferproperties can be treated as files, and the returned filename is not reduced to its basename. A relative-path write requires the application to use that filename as a write destination.
Why this happens
The helper relies on attacker-controlled object properties to identify uploaded files rather than verifying that each entry is a genuine file produced through the expected multipart parsing path. This type confusion is associated with CWE-843. Preserving a filename containing parent-directory segments can then enable path traversal, mapping to CWE-22, when downstream code uses it to construct a filesystem destination.
Potential impact
- Attacker-controlled bytes may be written to the temporary directory while upload hooks and file count or size limits are bypassed.
- An application that uses the returned filename as a destination may write a file outside its intended directory.
- Sensitive files or application data could be affected, depending on filesystem permissions and the destination logic.
General mitigation guidance
Defensive patterns include:
- Call file-saving helpers only after confirming the request follows the expected multipart parsing path.
- Treat request-body objects and filenames as untrusted; do not infer that an object is an uploaded file from properties alone.
- Reduce filenames to their basename or validate them against a strict allowlist before using them in filesystem operations.
- Construct destinations within a fixed, controlled directory and verify that resolved paths remain inside it.
- Preserve upload hooks and enforce file count and size limits for every accepted upload path.
- Run the service with least-privilege filesystem permissions to limit the consequences of unintended writes.
How teams detect issues like this
- Review code paths that call
saveRequestFileswhenattachFieldsToBodyis enabled, especially if they accept JSON requests or JSON-typed multipart fields. - Search for downstream filesystem operations that use returned upload filenames without basename reduction, allowlist validation, or resolved-path containment checks.
- Use static analysis or code review rules to flag file classification based only on attacker-controlled properties and path construction from untrusted filenames.
- Enforce request-type policies and upload validation checks consistently across routes and parsing paths.

