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

Change default Freemarker template file extension to .ftlh to enable HTML escaping #15131

Closed
jamesbarnett91 opened this issue Nov 8, 2018 · 11 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@jamesbarnett91
Copy link

Hi

When using the Spring Boot Freemarker Starter, content within the standard Freemarker interpolation blocks ${...} is not escaped by default.

The Freemarker docs say:

FreeMarker automatically escapes all values printed with ${...} if it's properly configured (that's the responsibility of the programmers). The recommended practice is using ftlh file extension to activate HTML auto-escaping, and ftlx file extension to activate XML auto-escaping.

I think most users would expect the Spring Boot Starter to do this 'proper configuration', since it does a lot of other sensible auto configuration, and I can't see why you would ever want to globally disable HTML escaping except in very rare circumstances.

Additionally, the recommended practice of using the .ftlh file extension to enable HTML auto-escaping does not work out-of-the-box with the Spring Boot starter as it seems to only be configured to scan for .ftl files.

The safest way to enable escaping seems to be to set spring.freemarker.settings.output_format=HTMLOutputFormat within the application.properties to force Freemarker to treat all templates as HTML regardless of their extension (so escaping will be done even if the h is forgotten on the file extension).

Or alternatively the spring.freemarker.suffix can also be set to .ftlh along with the enabling the spring.freemarker.settings.recognize_standard_file_extension

I would like to suggest that the default behaviour be changed to enable escaping by setting the Freemarker output format to HTML. However I appreciate the potential regression impact of making this change, so understand if this is not possible.
At the least, I would suggest that some mention of the need to do this configuration should be included in the documentation. Perhaps a list of recommended configuration options for each Templating engine or something?

Currently everything else about the default configuration is sensible and works well, so users are unlikely to examine any other configuration options, making it very easy to miss the need to configure the escaping and likely leading to XSS vulnerabilities.

For comparison, both the Thymeleaf and Mustache templating starters for Spring Boot escape html content by out-of-the-box when using the standard interpolation syntax.

Tested with spring-boot-2.0.4.RELEASE and spring-boot-starter-freemarker:2.0.4.RELEASE

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 8, 2018
@bclozel bclozel added the for: team-attention An issue we'd like other members of the team to review label Nov 9, 2018
@bclozel bclozel added this to the 2.2.x milestone Nov 9, 2018
@bclozel
Copy link
Member

bclozel commented Nov 12, 2018

Thanks for the report @jamesbarnett91 !

FreeMarker users should indeed apply those defaults for better security. Spring Boot auto-configures FreeMarker for Web and non-Web use cases (rendering email templates or text files, etc).

As you've described here, changing the default for spring.freemarker.suffix will break existing applications, since people will have to rename their template files. On the other hand, setting spring.freemarker.settings.output_format=HTMLOutputFormat by default will change the format even if you're not writing HTML templates; this is also a breaking change.

The team discussed this and we've decided to first tackle #15156 and then decide what we should do here.

@bclozel bclozel added type: enhancement A general enhancement and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Nov 12, 2018
@jamesbarnett91
Copy link
Author

Thanks for the update @bclozel , I understand the need to maintain backwards compatibility.

Perhaps the Spring Initializr could be updated to apply the Freemarker output_format=HTMLOutputFormat property automatically? Then at least new projects will be safe by default.

This could only be applied if the Web dependency is selected to avoid setting the default format for non HTML templating applications.

If this sounds sensible I can raise an issue on the Spring Initializr repo?
Thanks

@snicoll
Copy link
Member

snicoll commented Nov 19, 2018

@jamesbarnett91 this issue is not closed and we're going to address this, we just don't know how yet. I wouldn't update Spring Intializr as it must remain a convenience.

@bclozel bclozel self-assigned this Feb 22, 2019
@bclozel
Copy link
Member

bclozel commented Jun 12, 2019

Given the conclusion reached in #15156, we should make sure that Freemarker provides a safe configuration for web templates.

In the case of Freemarker, the prefix/suffix data is only used in the view resolver, so changing
those won't impact the non-web use case. We should then change the default suffix from .ftl
to .ftlh and configure Freemarker to associate the proper output format by setting the
recognize_standard_file_extensions Freemarker option to true by default. See Freemarker reference docs.

This is a breaking change, but we've made similar changes in the past, see #8997.

@bclozel bclozel modified the milestones: 2.2.x, 2.2.0.M4 Jun 12, 2019
@bclozel bclozel changed the title Spring Boot Freemarker Starter uses unsafe HTML escaping policy by default Change default Freemarker template file extension to .ftlh to enable HTML escaping Jun 12, 2019
@jamesbarnett91
Copy link
Author

Thanks for the fix @bclozel 👍

@janbodnar
Copy link

janbodnar commented Jan 15, 2020

This is a breaking change which leads to the infamous 'whitelabel error' and is quite confusing. There is not a single line about this in the docs: https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines
Only after a couple of hours I spotted the ftlh extension there which made me curious and finally brought me here.

In my opinion, breaking changes should be only reserved for major updates and should have red notes in the docs.

@wilkinsona
Copy link
Member

wilkinsona commented Jan 15, 2020

Thanks for the feedback and sorry for the inconvenience. We occasionally make breaking changes in minor releases when we believe that it will not make upgrading too difficult. We communicate those changes in the release notes rather than in the reference documentation. The latter is intended to describe how things are for a particular release whereas the former are intended to describe how things have changed since the previous release.

@duttonw
Copy link

duttonw commented Jan 21, 2020

Please add it to the spring boot 2.2 release notes as it also caught my group offguard i.e. why is nothing showing up.. https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.2-Release-Notes
or set back to old way by adding spring.freemarker.suffix: ".ftl" but best to be on ".ftlh" for automatic html escaping etc.

@philwebb
Copy link
Member

@duttonw It's covered in the release notes under the Freemarker templates configuration section.

@duttonw
Copy link

duttonw commented Jan 22, 2020

to make my templates work again i found https://freemarker.apache.org/docs/dgui_misc_autoescaping.html

I removed strings <#escape x as x?xhtml> ... </#escape>
and swapped <#noescape> ... </#noescape> to <#noautoesc> ... </#noautoesc>
and stripped ?xhtml or ?html off variables

there might be other things needing to be done, but this should be the gist of it if they are global wrapping their templates instead of piece meal escaping.

commonquail added a commit to commonquail/prstatbucket that referenced this issue Feb 29, 2020
This is primarily motivated by an upcoming dependency on
HeaderResultMatchers::dateValue, which has a hard dependency on JUnit 4,
fixed in v5.1.8 and v5.2.4 [1]. It was either reintroduce JUnit 4 until
this inevitable upgrade, or upgrade now. We'll be in a better position
for Spring Boot 2.3, at least.

Other exciting changes include changing Freemarker view extensions from
.ftl to .ftlh [2], trivial API churn, new Flyway, and new PostgreSQL
JDBC driver.

[1] spring-projects/spring-framework@233b225
[2] spring-projects/spring-boot#15131
@IsraelAfangideh

This comment was marked as resolved.

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

No branches or pull requests

9 participants