Fix server-side template (SpEL) injection in Java and Spring Boot
=================================================================

Evaluate user templates with a restricted context:
SimpleEvaluationContext.forReadOnlyDataBinding() allows only property access
on your data, so T(...) type references, constructors and reflection fail.
Never parse input into a StandardEvaluationContext. For plain values, bind
them to a constant template. Do not denylist T( or Runtime;
''.class.forName(...) and string concatenation get past it.

Page: https://softwaresecurity.com/fix/template-injection/java-spring/

What is in this archive
-----------------------
template-injection/java-spring/   the project: three implementations of the same code and one test suite
  src/main/java/com/example/templating/impl/VulnerableTemplateRenderer.java   (vulnerable)
  src/main/java/com/example/templating/impl/FixedTemplateRenderer.java   (fixed)
  src/main/java/com/example/templating/impl/WrongTemplateRenderer.java   (a plausible fix that still fails)
run-all.mjs   runs the tests in all three modes and checks the results (Node.js only)
LICENSE-CODE.txt

Run it
------
  Tested with OpenJDK 21.0.12.1 (Homebrew), Maven 3.9.16. run-all.mjs needs
  Node.js 22 or later. Supported versions:
  https://softwaresecurity.com/fix/versions/

  From the template-injection/java-spring folder, install the dependencies once
  (this needs network access):

    mvn -B -q test

  Then run the tests. IMPL chooses the implementation the tests load:

    # every test passes
    SSTI_SECRET=TMPL-INJECTION-PROOF-4d9f2a7c IMPL=fixed mvn -B -o -q test
    # an exploit test fails
    SSTI_SECRET=TMPL-INJECTION-PROOF-4d9f2a7c IMPL=vulnerable mvn -B -o -q test
    # a test fails
    SSTI_SECRET=TMPL-INJECTION-PROOF-4d9f2a7c IMPL=wrong mvn -B -o -q test

  The JUnit report is written to target/surefire-reports/TEST-*.xml. To run all
  three modes and check the results in one step, run the harness from this top
  folder (it needs only Node.js; --install runs the install step first):

    node run-all.mjs --install template-injection/java-spring
    node run-all.mjs template-injection/java-spring

Verified
--------
  11 October 2026, with java OpenJDK 21.0.12.1, maven 3.9.16, springBoot 4.1.1,
  springFramework 7.0.9, junitJupiter 6.0.3, jackson 3.1.7. Versions and results
  hold for that date: run the tests with your own versions before relying on the
  fix.

Licence
-------
  The code is MIT licensed (LICENSE-CODE.txt). The page text is CC BY 4.0.
