Skip to content

Fix Invalid UTF-8 character error for potential malformed user input.#7613

Closed
bytes-max wants to merge 2 commits into
EasyCorp:5.xfrom
bytes-max:bug/ensure-utf8-characters-before-joining
Closed

Fix Invalid UTF-8 character error for potential malformed user input.#7613
bytes-max wants to merge 2 commits into
EasyCorp:5.xfrom
bytes-max:bug/ensure-utf8-characters-before-joining

Conversation

@bytes-max
Copy link
Copy Markdown
Contributor

@bytes-max bytes-max commented May 16, 2026

We have observed. that user input some times can be malformed. Whilst its not a big deal for most of the times, some internal methods in easyAdmin cause the whole application to return a 500 page.

For example, a count of comments in a ticket CRUD Index will try to run the affected method, which itself carry potential to be optimized, however it will fail when the input is not correctly transformed to UTF-8.

This is the causing controller code:

yield CollectionField::new('comments', false)
            ->useEntryCrudForm(CommentCrudController::class)
            ->setPermission(RoleTypesEnum::ROLE_ADMIN->name)
            ->setColumns('col-sm-12')
            ->setHelp(t('Comments for this ticket'))
            ->formatValue(function (?Collection $comments): int {
                return $comments?->count() ?? 0;
            });

The fix prevents the error from happening.

@Seb33300
Copy link
Copy Markdown
Contributor

I dont understand how $comments?->count() can fail because of non UTF-8 text?

And why is your data not retrieved in UTF-8 encoding? Is your DB connection properly configured?

@bytes-max
Copy link
Copy Markdown
Contributor Author

@Seb33300 I am not sure if and how Symfony internally transforms input variables, but the .env is configured to use the proper &charset=utf8mb4" declaration, so I'd assume that the connection is correctly built.

Further, I could share the Stack Trace if you're interested, but I don't have it at hand; besides that FWIW I can say that the fix solves the problem for us.

If you have helpful other information, please let me know. For now we have the fix in our faulty application where it works now perfectly fine.

@javiereguiluz
Copy link
Copy Markdown
Collaborator

The reason why this code triggers the error is that we format the value even if you use a custom formatting. This was a bug on our side and we're fixing it in #7618.

About the underlying issue, it might be a data formatting issue. Maybe you can review the data inputs in your app and double check that you are no letting malformed data in? If the database size allows it, you could also create a quick command script with Symfony to find malformed data and fixed it. I did that recently in some Symfony app that was suffering from "mojibake" (double-encoded UTF-8) caused by some UTF-8 → Windows-1252 → UTF-8 data migration.

Meanwhile, let's close this PR in favor of the new one. Thanks!

javiereguiluz added a commit that referenced this pull request May 24, 2026
…is used (javiereguiluz)

This PR was merged into the 4.x branch.

Discussion
----------

Avoid formatting collection items when a custom formatting is used

An alternative solution for the issue reported in #7613.

Commits
-------

9b01b13 Avoid formatting collection items when a custom formatting is used
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.

3 participants