Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Validator] Add theCharset constraint#53154

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@alexandre-daubois
Copy link
Member

QA
Branch?7.1
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

Our use case: we receive some file contents in our DTOs that we only want to process if their encoding matches UTF-8 and reject the whole thing at validation otherwise.

Copy link
Contributor

@94noni94noni left a comment
edited by OskarStark
Loading

Choose a reason for hiding this comment

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

I second this, exactly the day our production crashed on some file upload ^^
(excel file wrongly saved with bad encoding)

alexandre-daubois reacted with laugh emoji
@alexandre-dauboisalexandre-daubois changed the title[Validator] Add theEncoding constraint[Validator] Add theCharacterEncoding constraintDec 21, 2023
@nicolas-grekas
Copy link
Member

Just bikeshadding about the name: what about Charset?

zerkms reacted with thumbs down emoji

@alexandre-daubois
Copy link
MemberAuthor

alexandre-daubois commentedDec 21, 2023
edited
Loading

I like Charset very much! I find it clearer. I updated the PR. I kept theencodings constraint option name to match the name of themb_detect_encoding() function argument.

@nicolas-grekasnicolas-grekas changed the title[Validator] Add theCharacterEncoding constraint[Validator] Add theCharset constraintDec 26, 2023
@nicolas-grekas
Copy link
Member

My thoughts: On all my apps, I'd want to reject any non-UTF-8 content. It might already be the case when the DB refuses to persist invalid UTF-8. So this contraints looks both needed and not needed since I think we might want to add this check earlier when processing the input.
Does that make sense?

@alexandre-daubois can you share your use case more precisely? When can one submit non-UTF-8 in your case?

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

we receive some file contents in our DTOs

hum, actually you already answered that sorry :)

here are some minor comments, LGTM

alexandre-daubois reacted with laugh emoji
@alexandre-daubois
Copy link
MemberAuthor

Thank you for the review, comments are addressed 👍

Copy link
Member

@dunglasdunglas left a comment

Choose a reason for hiding this comment

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

Maybe could you make the new classesfinal?

@alexandre-daubois
Copy link
MemberAuthor

final suits me fine, added 👍

@nicolas-grekas
Copy link
Member

Thank you@alexandre-daubois.

@smnandre
Copy link
Member

Hi@alexandre-daubois

There is a small problem in the implementation, i poke you there but if you prefer i can create a full issue

As you use the "valid" encodings in your call to mb_detect_encodings, if the value isnot encoded in one of those values, the function will always return false.

Meaning you'll never be able to display anything in the {{detected}} value of the message.

if (!\in_array($detected = mb_detect_encoding($value, $constraint->encodings, true), (array) $constraint->encodings, true)) {    $this->context->buildViolation($constraint->message)        ->setParameter('{{ detected }}', $detected)        ->setParameter('{{ encodings }}', implode(', ', $constraint->encodings))        ->setCode(Charset::BAD_ENCODING_ERROR)        ->addViolation();  }

With '😊' and ['ASCII'], the generated message will be

The detected character encoding is invalid (). Allowed encodings are ASCII.

Oh and just another (minor) thing.. the mb_string extension seems to not be required by Validator, but the mb_string polyfill does only check ASCII, UTF-8 and latin-ish encodings. Maybe something to add in doc, or to check at runtime ?

@alexandre-daubois
Copy link
MemberAuthor

Hi! Thank you for letting me know Simon, I'll provide a fix for this 🙂

smnandre reacted with laugh emoji

xabbuh added a commit that referenced this pull requestJan 16, 2024
…dator` (alexandre-daubois)This PR was merged into the 7.1 branch.Discussion----------[Validator] Fix charset encoding detection in `CharsetValidator`| Q             | A| ------------- | ---| Branch?       | 7.1| Bug fix?      | yes| New feature?  | no| Deprecations? | no| Issues        |Fix#53154 (comment)| License       | MITAfter `@smnandre` suggestion, I updated the constraint to display a better message on the detected encoding. Indeed, if we fail to check the encoding is in the provided ones, then we fall back on any encoding detected by mbstring.Commits-------6393e29 [Validator] Fix charset encoding detection in `CharsetValidator`
@fabpotfabpot mentioned this pull requestMay 2, 2024
@zerkms
Copy link
Contributor

zerkms commentedSep 10, 2024
edited
Loading

Why the constraint is namedCharset when it validates the encoding? Why it's notEncoding?

It even uses themb_detect_encoding internally, which is named..._encoding not..._charset :-D

@OskarStark
Copy link
Contributor

Can you please open a new issue to discuss this before the 7.2 release? Thanks

@derrabus
Copy link
Member

@OskarStark This feature has been shipped with 7.1 already.

@OskarStark
Copy link
Contributor

Indeed, time flies 😄

alexandre-daubois reacted with laugh emoji

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

Reviewers

@xabbuhxabbuhxabbuh left review comments

@dunglasdunglasdunglas approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

+2 more reviewers

@ro0NLro0NLro0NL left review comments

@94noni94noni94noni approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

11 participants

@alexandre-daubois@nicolas-grekas@smnandre@zerkms@OskarStark@derrabus@dunglas@ro0NL@94noni@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp