-
Notifications
You must be signed in to change notification settings - Fork 377
Fix: Harden FreeMarker against SSTI #705
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
Merged
+82
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...r/opensagres/xdocreport/template/freemarker/FreemarkerTemplateEngineSecurityTestCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package fr.opensagres.xdocreport.template.freemarker; | ||
|
|
||
| import java.io.Reader; | ||
| import java.io.StringReader; | ||
| import java.io.StringWriter; | ||
| import java.io.Writer; | ||
|
|
||
| import junit.framework.TestCase; | ||
| import fr.opensagres.xdocreport.core.XDocReportException; | ||
| import fr.opensagres.xdocreport.template.IContext; | ||
| import freemarker.template.Configuration; | ||
| import freemarker.template.TemplateException; | ||
|
|
||
| /** | ||
| * Test case to verify security fixes for Server-Side Template Injection (SSTI) vulnerabilities | ||
| * in the FreeMarker template engine. | ||
| */ | ||
| public class FreemarkerTemplateEngineSecurityTestCase | ||
| extends TestCase | ||
| { | ||
|
|
||
| /** | ||
| * Test that legitimate template operations still work after security fix. | ||
| */ | ||
| public void testLegitimateTemplateOperationsStillWork() | ||
| throws Exception | ||
| { | ||
| FreemarkerTemplateEngine templateEngine = new FreemarkerTemplateEngine(); | ||
|
|
||
| // Test basic variable substitution | ||
| String legitimateTemplate = "Hello ${name}!"; | ||
| Reader reader = new StringReader( legitimateTemplate ); | ||
| Writer writer = new StringWriter(); | ||
| IContext context = templateEngine.createContext(); | ||
| context.put( "name", "World" ); | ||
|
|
||
| templateEngine.process( "", context, reader, writer ); | ||
| assertEquals( "Hello World!", writer.toString() ); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Test protection against SSTI payload with freemarker.template.utility.Execute. | ||
| */ | ||
| public void testSSTIProtectionAgainstExecutePayload() | ||
| throws Exception | ||
| { | ||
| FreemarkerTemplateEngine templateEngine = new FreemarkerTemplateEngine(); | ||
|
|
||
| String maliciousTemplate ="${\"freemarker.template.utility.Execute\"?new()(\"whoami\")}"; | ||
| Reader reader = new StringReader( maliciousTemplate ); | ||
| Writer writer = new StringWriter(); | ||
| IContext context = templateEngine.createContext(); | ||
|
|
||
| try | ||
| { | ||
| templateEngine.process( "", context, reader, writer ); | ||
| fail( "Security fix failed: Execute payload should be blocked" ); | ||
| } | ||
| catch ( XDocReportException e ) | ||
| { | ||
| assertTrue( "Expected security-related exception", | ||
| e.getCause() instanceof TemplateException ); | ||
| } | ||
| } | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this constant exists in any freemarker version? I - think you should catch Exception instead of catching TemplateException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@angelozerr Thanks for the question!
Configuration.NEW_BUILTIN_CLASS_RESOLVER_KEYwas introduced in FreeMarker 2.3.30, so it is supported in the FreeMarker version currently used in XDocReport (2.3.32).