|
| 1 | +package fr.opensagres.xdocreport.template.freemarker; |
| 2 | + |
| 3 | +import java.io.Reader; |
| 4 | +import java.io.StringReader; |
| 5 | +import java.io.StringWriter; |
| 6 | +import java.io.Writer; |
| 7 | + |
| 8 | +import junit.framework.TestCase; |
| 9 | +import fr.opensagres.xdocreport.core.XDocReportException; |
| 10 | +import fr.opensagres.xdocreport.template.IContext; |
| 11 | +import freemarker.template.Configuration; |
| 12 | +import freemarker.template.TemplateException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test case to verify security fixes for Server-Side Template Injection (SSTI) vulnerabilities |
| 16 | + * in the FreeMarker template engine. |
| 17 | + */ |
| 18 | +public class FreemarkerTemplateEngineSecurityTestCase |
| 19 | + extends TestCase |
| 20 | +{ |
| 21 | + |
| 22 | + /** |
| 23 | + * Test that legitimate template operations still work after security fix. |
| 24 | + */ |
| 25 | + public void testLegitimateTemplateOperationsStillWork() |
| 26 | + throws Exception |
| 27 | + { |
| 28 | + FreemarkerTemplateEngine templateEngine = new FreemarkerTemplateEngine(); |
| 29 | + |
| 30 | + // Test basic variable substitution |
| 31 | + String legitimateTemplate = "Hello ${name}!"; |
| 32 | + Reader reader = new StringReader( legitimateTemplate ); |
| 33 | + Writer writer = new StringWriter(); |
| 34 | + IContext context = templateEngine.createContext(); |
| 35 | + context.put( "name", "World" ); |
| 36 | + |
| 37 | + templateEngine.process( "", context, reader, writer ); |
| 38 | + assertEquals( "Hello World!", writer.toString() ); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Test protection against SSTI payload with freemarker.template.utility.Execute. |
| 44 | + */ |
| 45 | + public void testSSTIProtectionAgainstExecutePayload() |
| 46 | + throws Exception |
| 47 | + { |
| 48 | + FreemarkerTemplateEngine templateEngine = new FreemarkerTemplateEngine(); |
| 49 | + |
| 50 | + String maliciousTemplate ="${\"freemarker.template.utility.Execute\"?new()(\"whoami\")}"; |
| 51 | + Reader reader = new StringReader( maliciousTemplate ); |
| 52 | + Writer writer = new StringWriter(); |
| 53 | + IContext context = templateEngine.createContext(); |
| 54 | + |
| 55 | + try |
| 56 | + { |
| 57 | + templateEngine.process( "", context, reader, writer ); |
| 58 | + fail( "Security fix failed: Execute payload should be blocked" ); |
| 59 | + } |
| 60 | + catch ( XDocReportException e ) |
| 61 | + { |
| 62 | + assertTrue( "Expected security-related exception", |
| 63 | + e.getCause() instanceof TemplateException ); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
0 commit comments