-
Notifications
You must be signed in to change notification settings - Fork 2.5k
perf(web): optimize document upload validation with static sets, MIME checks, and 50MB size guard #1560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sruhvx-jpg
wants to merge
1
commit into
supermemoryai:main
Choose a base branch
from
Sruhvx-jpg:perf/web-file-upload-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−30
Open
perf(web): optimize document upload validation with static sets, MIME checks, and 50MB size guard #1560
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { describe, expect, it } from "bun:test" | ||
| import { | ||
| isAcceptedFile, | ||
| isAcceptedFileType, | ||
| MAX_DOCUMENT_FILE_BYTES, | ||
| } from "./document-file-validation" | ||
|
|
||
| function createMockFile(name: string, size = 1024, type = ""): File { | ||
| return new File([new Uint8Array(size)], name, { type }) | ||
| } | ||
|
|
||
| describe("document file validation", () => { | ||
| it("accepts standard documents by extension", () => { | ||
| expect(isAcceptedFile(createMockFile("document.pdf"))).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("notes.md"))).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("data.json"))).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("sheet.xlsx"))).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("report.docx"))).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("data.csv"))).toBe(true) | ||
| }) | ||
|
|
||
| it("accepts files with uppercase extensions and multi-dot filenames", () => { | ||
| expect(isAcceptedFile(createMockFile("DOCUMENT.PDF"))).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("archive.v1.0.final.docx"))).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("report.2026.08.19.csv"))).toBe(true) | ||
| }) | ||
|
|
||
| it("accepts extensionless or generic files matching valid MIME types", () => { | ||
| expect( | ||
| isAcceptedFile(createMockFile("blob", 1024, "application/pdf")), | ||
| ).toBe(true) | ||
| expect( | ||
| isAcceptedFile(createMockFile("uploaded-file", 1024, "application/json")), | ||
| ).toBe(true) | ||
| expect( | ||
| isAcceptedFile(createMockFile("image-upload", 1024, "image/png")), | ||
| ).toBe(true) | ||
| expect(isAcceptedFile(createMockFile("photo", 1024, "image/jpeg"))).toBe( | ||
| true, | ||
| ) | ||
| }) | ||
|
|
||
| it("correctly evaluates isAcceptedFileType independent of file size", () => { | ||
| expect(isAcceptedFileType(createMockFile("empty.pdf", 0))).toBe(true) | ||
| expect( | ||
| isAcceptedFileType( | ||
| createMockFile("large.pdf", MAX_DOCUMENT_FILE_BYTES + 1), | ||
| ), | ||
| ).toBe(true) | ||
| expect(isAcceptedFileType(createMockFile("script.sh"))).toBe(false) | ||
| }) | ||
|
|
||
| it("rejects files exceeding the 50MB limit in isAcceptedFile", () => { | ||
| const oversized = MAX_DOCUMENT_FILE_BYTES + 1 | ||
| expect(isAcceptedFile(createMockFile("large.pdf", oversized))).toBe(false) | ||
| }) | ||
|
|
||
| it("rejects empty files with 0 bytes in isAcceptedFile", () => { | ||
| expect(isAcceptedFile(createMockFile("empty.pdf", 0))).toBe(false) | ||
| }) | ||
|
|
||
| it("rejects unsupported extensions and executables", () => { | ||
| expect(isAcceptedFile(createMockFile("malware.exe"))).toBe(false) | ||
| expect(isAcceptedFile(createMockFile("script.sh"))).toBe(false) | ||
| expect(isAcceptedFile(createMockFile("archive.zip"))).toBe(false) | ||
| expect(isAcceptedFile(createMockFile("binary.bin"))).toBe(false) | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| export const MAX_DOCUMENT_FILE_BYTES = 50 * 1024 * 1024 // 50MB | ||
|
|
||
| export const ALLOWED_EXTENSIONS = new Set([ | ||
| ".pdf", | ||
| ".doc", | ||
| ".docx", | ||
| ".xls", | ||
| ".xlsx", | ||
| ".csv", | ||
| ".txt", | ||
| ".md", | ||
| ".mdx", | ||
| ".json", | ||
| ".html", | ||
| ".htm", | ||
| ]) | ||
|
|
||
| export const ALLOWED_MIME_TYPES = new Set([ | ||
| "application/pdf", | ||
| "application/json", | ||
| "application/msword", | ||
| "application/vnd.openxmlformats-officedocument.wordprocessingml.document", | ||
| "application/vnd.ms-excel", | ||
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
| "text/markdown", | ||
| "text/html", | ||
| "text/plain", | ||
| "text/csv", | ||
| ]) | ||
|
|
||
| export function isAcceptedFileType(file: File): boolean { | ||
| if (file.type) { | ||
| const baseMime = file.type.split(";")[0]?.trim().toLowerCase() ?? "" | ||
| if (baseMime.startsWith("image/")) return true | ||
| if (ALLOWED_MIME_TYPES.has(baseMime)) return true | ||
| } | ||
|
|
||
| const extIndex = file.name.lastIndexOf(".") | ||
| if (extIndex === -1) return false | ||
|
|
||
| return ALLOWED_EXTENSIONS.has(file.name.slice(extIndex).toLowerCase()) | ||
| } | ||
|
|
||
| export function isAcceptedFile(file: File): boolean { | ||
| if (file.size <= 0 || file.size > MAX_DOCUMENT_FILE_BYTES) return false | ||
| return isAcceptedFileType(file) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty files (size = 0) are incorrectly categorized as "unsupported" rather than being handled separately. An empty PDF file will show "One file type is not supported" even though the file type is valid.
Impact: Misleading user feedback - users will think they uploaded the wrong file type when they actually uploaded an empty file.
Fix: Add a separate check for empty files:
Then add appropriate toast message for empty files.
Spotted by Graphite

Is this helpful? React 👍 or 👎 to let us know.