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

Skip to content

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

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

Merged
merged 1 commit into from
Mar 1, 2025

Conversation

IssamRaouf
Copy link
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
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
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
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
Contributor Author

Hello @OskarStark

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

@nicolas-grekas
Copy link
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
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.

@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
@IssamRaouf IssamRaouf changed the base branch from 6.4 to 7.3 February 16, 2025 12:42
$payload,
$filenameCharset,
$filenameCountUnit,
$filenameCharsetMessage
Copy link
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
Member

fabpot commented Mar 1, 2025

Thank you @IssamRaouf.

@fabpot fabpot merged commit 46e00d5 into symfony:7.3 Mar 1, 2025
7 of 9 checks passed
@fabpot fabpot mentioned this pull request May 2, 2025
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