🧯 fix: Add Pre-Parse File Size Guard to Document Parser#12275
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a pre-parse file size guard (15 MB) to the document parser to prevent memory exhaustion DoS attacks. It also refactors the mime-type-to-parser dispatch into a dedicated helper function for improved readability.
Changes:
- Adds a 15 MB file size check before reading documents into memory, throwing a descriptive error for oversized files.
- Extracts the mime-type → parser-function mapping into
getParserForMimeType()for clarity and maintainability. - Adds a test verifying that files exceeding the 15 MB limit are rejected.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
packages/api/src/files/documents/crud.ts |
Adds DOCUMENT_PARSER_MAX_FILE_SIZE constant, file size guard, and refactors parser selection into getParserForMimeType(). |
packages/api/src/files/documents/crud.spec.ts |
Adds a test case for the 15 MB size limit rejection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Prevent memory exhaustion DoS by rejecting documents exceeding 15MB before reading them into memory, closing the gap between the 512MB upload limit and unbounded in-memory parsing.
c249659 to
d16bd6b
Compare
jcbartle
pushed a commit
to jcbartle/LibreChat
that referenced
this pull request
May 11, 2026
…12275) Prevent memory exhaustion DoS by rejecting documents exceeding 15MB before reading them into memory, closing the gap between the 512MB upload limit and unbounded in-memory parsing.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Added a pre-parse file size guard to the document parser to close a memory exhaustion DoS vector between the 512 MB upload limit and the unbounded in-memory reads performed by the PDF, Word, and Excel parsers. Also refactored the MIME-type-to-parser dispatch into a dedicated helper for clarity.
DOCUMENT_PARSER_MAX_FILE_SIZE(15 MB) derived from the sharedmegabyteconstant imported fromlibrechat-data-provider, replacing a locally duplicated definition.parseDocument, short-circuiting thenew Uint8Array(readFile(...))and equivalent allocations for oversized files.DOCUMENT_PARSER_MAX_FILE_SIZEso the message can never drift from the enforced constant.Math.ceilfor the reported file size in the error message so files just over the limit never produce a misleading "exceeds 15MB limit (15MB)" string.fs.promises.stat()fallback behind afile.path != nullcheck to prevent a silentTypeErrorwhenfile.pathis undefined.getParserForMimeType()with a JSDoc comment consistent with the surrounding helpers.Change Type
Testing
Added unit tests to
packages/api/src/files/documents/crud.spec.ts:size: 16 * 1024 * 1024is rejected with an error matching/exceeds the 15MB document parser limit \(16MB\)/.size: 15 * 1024 * 1024(exactly at the limit) resolves successfully, pinning the strictly-greater-than semantics of the guard.size) exercise thefs.promises.stat()fallback path on real fixture files, validating that branch in the success direction.Test Configuration:
Run from the
packages/apiworkspace:Checklist