Skip to content

feat: detect template injection in FreeMarker, Thymeleaf, Velocity, Pebble and Jinjava - #1076

Open
Kwstubbs wants to merge 6 commits into
CodeIntelligenceTesting:mainfrom
Kwstubbs:feature/template-models
Open

Kwstubbs wants to merge 6 commits into
CodeIntelligenceTesting:mainfrom
Kwstubbs:feature/template-models

Conversation

@Kwstubbs

@Kwstubbs Kwstubbs commented Dec 2, 2026 •

Copy link
Copy Markdown

Right now TemplateInjection only has the FreeMarker hook from #1007. It nudges templates towards ${"freemarker.template.utility.Execute"?new()("jazze")} and leaves the reporting to the OS command injection detector. That only works if ?new is allowed and the input lands somewhere FreeMarker evaluates ${...}. With TemplateClassResolver.SAFER_RESOLVER the payload can't run at all, inside a comment it does nothing, and strings passed to ?eval never go through the Template constructor, so they aren't guided.

This PR switches to a canary and uses it for FreeMarker, Thymeleaf, Velocity, Pebble and Jinjava. Each engine gets a guidance hook on the method all template source goes through. It adds hints that reference a variable called jazzerSsti in that engine's syntax, and some of them close a comment, string literal or raw block first, since user input ends up there a lot. A second hook on the engine's variable lookup reports a FuzzerSecurityIssueHigh when anything asks for jazzerSsti.

The canary only gets looked up if fuzzer data was parsed as template code, and regardless of whether the engine's sandbox would have stopped an RCE payload.

Engine Guidance Detection
FreeMarker FMParser.<init> (all templates, ?interpret, ?eval) Environment.getVariable
Thymeleaf ITemplateEngine.process, StandardExpressionPreprocessor.preprocess (__${...}__, e.g. Spring view names) IStandardVariableExpressionEvaluator.evaluate
Velocity RuntimeServices.parse (loaders, Velocity.evaluate, #evaluate) Context.get
Pebble Lexer.tokenize ScopeChain.get
Jinjava TreeParser.<init> JinjavaInterpreter.retraceVariable

Note: Jinjava renders printed values a second time if they contain template syntax ("nested interpretation", on by default). So {{ name }} with a user-controlled name is injectable, and the sanitizer reports it. That's correct, but expect new findings in projects that use Jinjava's defaults. The app-side fix is withNestedInterpretationEnabled(false).

Kwstubbs and others added 6 commits December 2, 2026 06:58
The FreeMarker guidance hook peeks at every Reader passed to a Template
constructor, but peekMarkableReader fails for readers that don't support
mark(). FreeMarker's TemplateCache reads template files through such
readers, so Configuration.getTemplate threw an IllegalStateException
from the hook.

Only peek at markable readers. libFuzzer only uses the needle, so other
templates are still guided.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Guide all source that FreeMarker parses (templates, ?interpret and
?eval) towards a reference to a canary variable and report a finding
when FreeMarker looks it up while rendering. This replaces guiding
templates towards an OS command payload.

Unlike the OS command payload, this also detects injections in
configurations that block ?new (e.g. TemplateClassResolver.SAFER_RESOLVER),
into comments and noparse blocks, into strings evaluated with ?eval and
with the square bracket tag and interpolation syntax. Findings are
reported as FuzzerSecurityIssueHigh.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Guide template strings passed to ITemplateEngine.process and expressions
that Thymeleaf preprocesses, such as Spring view names, towards a canary
variable expression and report a finding when Thymeleaf evaluates it.

Updates the OGNL test dependency to 3.4.17, the version Thymeleaf 3.1.7
depends on.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Guide all template source that Velocity parses (templates from resource
loaders, Velocity.evaluate and the #evaluate directive) towards a canary
variable reference and report a finding when Velocity looks up the
canary in its context while rendering.

The Velocity test dependency updates commons-lang3 from 3.11 to 3.17.0.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Guide all template source that Pebble tokenizes (getTemplate with any
loader, getLiteralTemplate, include and extends) towards a canary
variable reference and report a finding when Pebble looks up the canary
while rendering. This works regardless of the configured method access
validator.

Covers both the io.pebbletemplates.pebble package and the
com.mitchellbosecke.pebble package used before Pebble 3.2. As both share
Maven coordinates, the tests fetch Pebble 3.1.6 as a plain jar.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Guide all template text that Jinjava parses towards a canary variable
reference and report a finding when Jinjava resolves the canary while
rendering. With Jinjava's default configuration, this also reports
printed values that contain template code, as nested interpretation
renders them again.

Jinjava 2.8 and later require Java 17, so the tests use 2.7.7 from a
list that Renovate doesn't update. Jinjava's Jackson modules are pinned
to the version of the intentionally vulnerable jackson-databind, as the
current YAML module isn't compatible with it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant