Skip to content
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

Mustache templates return with ISO-8859-1 charset rather than UTF-8 in Content-Type response header #44053

Closed
mmorrisontx opened this issue Feb 4, 2025 · 4 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@mmorrisontx
Copy link

mmorrisontx commented Feb 4, 2025

The spring boot mustache view resolver accepts a charset parameter (via MustacheProperties), but that parameter is only used by the template loader, it is not used as part of the response content type. The default returned content type is text/html (with no charset), since that's the content type set on the ViewResolver in MustacheServletWebConfiguration.

In spring boot 2, this fell back on jetty's default implied charset list, via jetty's MimeTypes.getCharsetInferredFromContentType. By default, jetty returns utf-8 for text/html, which effectively made the response headers contain Content-Type: text/html;charset=utf-8. All was happy.

However, in spring boot 3, this added line totally wipes out jetty's inferred charset list. This causes jetty to fall back to it's hard-coded last resort charset, ISO-8859-1 (see jetty's ServletContextResponse ).

This means that in spring boot 3, all mustache templates are rendered using utf8, but return an iso-8859-1 content type header, which is incorrect and breaks all non-latin characters.

@mmorrisontx mmorrisontx changed the title Mustache templates return with ISO-8859-1 charset rather than UTF-8 Mustache templates return with ISO-8859-1 charset rather than UTF-8 in Content-Type response header Feb 4, 2025
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 4, 2025
@mmorrisontx
Copy link
Author

This bug can be temporary fixed by setting spring.mustache.servlet.content-type: text/html; charset=utf-8

@mmorrisontx
Copy link
Author

mmorrisontx commented Feb 4, 2025

It seems that this issue doesn't exist in other template systems like freemarker because FreeMarkerProperties extends from AbstractTemplateViewResolverProperties / AbstractViewResolverProperties, which handle this case properly by including their charset in the response from getContentType(). The 'proper' fix might be to refactor MustacheProperties to extend from AbstractTemplateViewResolverProperties as well.

@wilkinsona
Copy link
Member

Thanks for the report, @mmorrisontx.

The 'proper' fix might be to refactor MustacheProperties to extend from AbstractTemplateViewResolverProperties as well.

Unfortunately, we can't do that because Mustache is supported with both the reactive and servlet web stacks and the structure of AbstractTemplateViewResolverProperties is servlet-only.

@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 10, 2025
@wilkinsona wilkinsona added this to the 3.3.x milestone Feb 10, 2025
@wilkinsona wilkinsona self-assigned this Feb 10, 2025
@mmorrisontx
Copy link
Author

Gotchya. We've worked around this temporarily by setting the above property in a property source wired up by a common starter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants