Skip to content

Validate uploaded files by content and size before parsing - #1210

Open
holystrings wants to merge 1 commit into
Stellopay:mainfrom
holystrings:fix/1172-validate-uploaded-files
Open

Validate uploaded files by content and size before parsing#1210
holystrings wants to merge 1 commit into
Stellopay:mainfrom
holystrings:fix/1172-validate-uploaded-files

Conversation

@holystrings

Copy link
Copy Markdown

Problem

File extensions and browser-provided MIME types can be spoofed or simply
wrong, which let unsupported or oversized files reach the import parser
undetected.

Fix

Adds utils/file-validation.ts, run before any file reaches the parser:

  • Size — rejects empty files and files over the configured limit
    before any content is read.
  • Content type — sniffs magic bytes instead of trusting file.name
    or file.type, so a renamed file is caught.
  • Encoding — decodes with TextDecoder(..., { fatal: true }) to
    reject invalid UTF-8 / binary content.
  • Structure — parses a lightweight structural check (JSON parse /
    CSV column-consistency) before the real parser runs.

Each check returns a specific error code (FILE_TOO_LARGE,
FILE_EMPTY, UNSUPPORTED_TYPE, INVALID_ENCODING,
STRUCTURAL_MISMATCH) so the UI can tell the user exactly what failed
instead of a generic "invalid file" message.

The upload handler now clears preview state in the same branch that
sets the validation error, so a rejected file can no longer leave a
stale preview from a previous successful upload on screen.

Testing

  • utils/file-validation.test.ts — renamed files, empty files, invalid
    encoding, and boundary sizes, plus malformed JSON/CSV structure.
  • Component test covering: upload valid file → preview renders → upload
    invalid file → preview is cleared and error is shown.
  • npm run lint, npm run type-check, npm test, npm run build all
    pass locally.

Acceptance criteria

  • Oversized and unsupported content is rejected early
  • Validation errors identify the failed check
  • A rejected file cannot leave a stale preview visible

Closes #1172

Adds byte-size, encoding, and structural checks that run before a file
is handed to the import parser. Content type is verified from magic
bytes rather than file extension or browser-reported MIME type, so a
renamed file can't slip through. Validation failures report which
specific check failed, and a rejected file no longer leaves a stale
preview visible.

Closes Stellopay#1172
@vercel

vercel Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@holystrings is attempting to deploy a commit to the Jagadeesh B's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate uploaded files by content and size before parsing

1 participant