Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Add filenameCharset and filenameCountUnit options to File constraint#58485

Merged
fabpot merged 1 commit into
symfony:7.3from
IssamRaouf:fix_58482
Mar 1, 2025
Merged

[Validator] Add filenameCharset and filenameCountUnit options to File constraint#58485
fabpot merged 1 commit into
symfony:7.3from
IssamRaouf:fix_58482

Conversation

@IssamRaouf
Copy link
Copy Markdown
Contributor

@IssamRaouf IssamRaouf commented Oct 7, 2024

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #58482
License MIT

@carsonbot carsonbot added this to the 6.4 milestone Oct 7, 2024
@carsonbot carsonbot changed the title [Validator][FileValidator] filenameLength bug non-Latin characters ,replace strlen with mb_strlen to count characters instead of bytes [Validator] [FileValidator] filenameLength bug non-Latin characters ,replace strlen with mb_strlen to count characters instead of bytes Oct 7, 2024
Copy link
Copy Markdown
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a testcase to avoid further regression

@nicolas-grekas
Copy link
Copy Markdown
Member

nicolas-grekas commented Oct 8, 2024

I'm 👎 here: the length in bytes is what matters. Eg a storage can have a 255-max string field counted in bytes, and this validator guards against longer filenames. Validating in code points makes it really hard to validate any predictable maxlength.

Also, nothing tells this accepts only UTF-8 (it doesn't, bytes are bytes, no encoding expected here).

@IssamRaouf
Copy link
Copy Markdown
Contributor Author

IssamRaouf commented Oct 8, 2024

Hello @nicolas-grekas

For the context of the filenameMaxLength constraint option, from its name alone, it clearly suggests a validation on the string's length.

let's take this example :

file-to-upload.pdf (Latin characters)

  • character count = 14
  • mb_strlen($basename) = 14
  • strlen($basename) = 14

файл-завантаження.pdf (non-Latin characters) each character can require 1 to 4 bytes

  • character count = 17
  • mb_strlen($basename) = 17
  • strlen($basename) = 33;

#[Assert\File(
maxSize: '10M',
mimeTypes: ['application/pdf'],
filenameMaxLength: 20,
)]

Result:
Latin characters: passes
Non-Latin characters: fails validation

I find this contradictory 😅.

@IssamRaouf
Copy link
Copy Markdown
Contributor Author

Hello @OskarStark

ok, I'm waiting for the issue to be validated

@nicolas-grekas
Copy link
Copy Markdown
Member

We could make this opt-in, eg with a countUnit and a charset option (names taken from the Length constraint)

@IssamRaouf IssamRaouf changed the title [Validator] [FileValidator] filenameLength bug non-Latin characters ,replace strlen with mb_strlen to count characters instead of bytes [Validator] [FileValidator] filenameLength bug non-Latin characters ,add options Charset and Count unit Oct 14, 2024
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.2 Nov 2, 2024
@nicolas-grekas nicolas-grekas added Feature and removed Bug labels Nov 2, 2024
@IssamRaouf IssamRaouf changed the title [Validator] [FileValidator] filenameLength bug non-Latin characters ,add options Charset and Count unit [Validator] Add filenameCharset and filenameCountUnit options to File constraint Nov 2, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
Copy link
Copy Markdown
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, here are some minor last changes.
Please also rebase to target 7.3 since this is a new feature.
And move the changelog lines in the 7.3 section.

Comment thread src/Symfony/Component/Validator/CHANGELOG.md Outdated
Comment thread src/Symfony/Component/Validator/CHANGELOG.md Outdated
Comment thread src/Symfony/Component/Validator/CHANGELOG.md Outdated
Comment thread src/Symfony/Component/Validator/Constraints/File.php Outdated
Comment thread src/Symfony/Component/Validator/Constraints/File.php Outdated
@OskarStark OskarStark changed the title [Validator] Add filenameCharset and filenameCountUnit options to File constraint [Validator] Add filenameCharset and filenameCountUnit options to File constraint Feb 10, 2025
Comment thread src/Symfony/Component/Validator/Constraints/Image.php Outdated
Comment thread src/Symfony/Component/Validator/Constraints/FileValidator.php
@IssamRaouf IssamRaouf changed the base branch from 6.4 to 7.3 February 16, 2025 12:42
Comment thread src/Symfony/Component/Validator/Constraints/FileValidator.php Outdated
$payload,
$filenameCharset,
$filenameCountUnit,
$filenameCharsetMessage
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$filenameCharsetMessage
$filenameCharsetMessage,

@fabpot
Copy link
Copy Markdown
Member

fabpot commented Mar 1, 2025

Thank you @IssamRaouf.

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.

FileValidator filenameMaxLength constraint

6 participants