Skip to content

fix(common): Support fallbackToMimetype without buffer#16013

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
chenjieya:alvis-refactor-filetypevalidator-pipe
Dec 5, 2025
Merged

fix(common): Support fallbackToMimetype without buffer#16013
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
chenjieya:alvis-refactor-filetypevalidator-pipe

Conversation

@chenjieya

@chenjieya chenjieya commented Dec 3, 2025

Copy link
Copy Markdown

Allow FileTypeValidator to validate files saved on disk where no buffer is available.
Use mimetype as a fallback when fallbackToMimetype=true and buffer is missing.
Preserves existing behavior when fallbackToMimetype is not enabled.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

When using Multer’s diskStorage, uploaded files do not include a buffer.
FileTypeValidator requires a buffer unless skipMagicNumbersValidation is enabled.
Even when fallbackToMimetype=true, validation fails because the buffer is missing.

This makes it impossible to validate disk-stored files using mimetype fallback.

Issue Number: N/A

What is the new behavior?

  • When buffer is missing:
    • If fallbackToMimetype=true, validation uses mimetype as a fallback.
    • If fallbackToMimetype is not enabled, behavior remains unchanged (validation fails).

This enables file validation for diskStorage without breaking existing logic.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This change is backward-compatible and only affects cases where
fallbackToMimetype is explicitly enabled. All existing behavior is preserved.

Allow FileTypeValidator to validate files saved on disk where no buffer is available.
Use mimetype as a fallback when fallbackToMimetype=true and buffer is missing.
Preserves existing behavior when fallbackToMimetype is not enabled.
@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 530d5789-20c6-4be9-8007-847427805075

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.005%) to 88.944%

Totals Coverage Status
Change from base Build 360082e1-1510-4dc0-83b3-4311204590dc: 0.005%
Covered Lines: 7329
Relevant Lines: 8240

💛 - Coveralls

@chenjieya

Copy link
Copy Markdown
Author

This CodeSandbox demonstrates that FileTypeValidator fails when using Multer's diskStorage, because uploaded files in this mode do not include a buffer.

Even with fallbackToMimetype=true, the validator still expects a buffer internally, which causes validation to fail incorrectly.

Reproduction: https://codesandbox.io/p/devbox/nest-filetypevalidator-pipe-lqt4wv

This PR fixes the behavior by:

  • Allowing MIME type fallback when buffer is missing

  • Preserving the current behavior when fallbackToMimetype is disabled

  • Ensuring compatibility with both memoryStorage and diskStorage

This resolves the inconsistency between the documented behavior and the actual validator behavior.

@kamilmysliwiec kamilmysliwiec merged commit 7769180 into nestjs:master Dec 5, 2025
2 checks passed
@kamilmysliwiec

Copy link
Copy Markdown
Member

lgtm

@chenjieya chenjieya deleted the alvis-refactor-filetypevalidator-pipe branch December 8, 2025 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants