Spring View Manipulation
Last updated
Was this helpful?
Last updated
Was this helpful?
Spring application that uses Thymeleaf as its templating engine, if template name or fragment is concatenated with untrusted data, it can lead to expression language injection and hence RCE.
If Thymeleaf view engine is used (the most popular for Spring), the template might look like this one:
Thymeleaf engine supports , that allows you to specify a fragment in the template by using <div th:fragment="main">
and then request only this fragment from the view:
Thymeleaf is intelligent enough to return only the main div
from the welcome
view, but not the whole document.
Before loading the template from the filesystem, class parses the template name as an expression:
As a result, if template name or fragment is concatenated with untrusted data, it can lead to expression language injection and hence RCE.
This means that at first glance such a controller may seem completely innocent, it does almost nothing, but since Spring does not know which view name to use, it takes it from the request URI.
Specifically, DefaultRequestToViewNameTranslator
does the following:
So, it becomes vulnerable because the user controlled data (URI) comes in directly to view name and is resolved as an expression:
This exploit uses . In order for the expression to be executed by the Thymeleaf, no matter what prefixes or suffixes are, it is necessary to surround it with __${
and }__::.x
.
Controllers do not always return strings that explicitly tell Spring what view name to use. As described in the , for some return types such as void
, java.util.Map
or org.springframework.ui.Model
, the view name is implicitly determined through a RequestToViewNameTranslator
.