Skip to content

Commit 73b6ff5

Browse files
committed
Fix Server-side template injection
Note that this is only a demo app. https://github.com/fugerit-org/fj-doc/security/code-scanning/9
1 parent e327d32 commit 73b6ff5

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- favicon and logo for playground quarkus
1717

18+
### Fixed
19+
20+
- [Server-side template injection](https://github.com/fugerit-org/fj-doc/security/code-scanning/9)
21+
1822
## [3.1.5] - 2023-10-15
1923

2024
### Added

fj-doc-playground-quarkus/src/main/java/org/fugerit/java/doc/playground/doc/GenerateRest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import org.fugerit.java.doc.playground.facade.BasicInput;
3232
import org.fugerit.java.doc.playground.facade.InputFacade;
3333

34+
import com.fasterxml.jackson.databind.JsonNode;
35+
import com.fasterxml.jackson.databind.ObjectMapper;
36+
3437
import freemarker.cache.StringTemplateLoader;
3538
import freemarker.template.Configuration;
3639
import freemarker.template.Template;
@@ -57,21 +60,29 @@ private void doHandle( DocTypeHandler handler, String type, int sourceType, Read
5760
} );
5861
}
5962

63+
private void handleConfiguration( Configuration configuration, String freemarkerJsonData, String ftlData, String chainId ) {
64+
StringTemplateLoader loader = new StringTemplateLoader();
65+
String chainData = "<#assign ftlData = "+freemarkerJsonData+">"+ftlData;
66+
loader.putTemplate( chainId , chainData );
67+
configuration.setTemplateLoader( loader );
68+
}
69+
6070
private void handleFtlx( DocTypeHandler handler, String type, int sourceType, Reader reader, ByteArrayOutputStream baos, String freemarkerJsonData ) {
6171
SafeFunction.apply( () -> {
6272
// volatile FreeMarker Template configuration
63-
String templateName = "current"+System.currentTimeMillis();
73+
String chainId = "current_"+System.currentTimeMillis();
6474
Configuration configuration = new Configuration( new Version( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION_LATEST ) );
65-
StringTemplateLoader loader = new StringTemplateLoader();
66-
String templateData = "<#assign ftlData = "+freemarkerJsonData+">"+StreamIO.readString( reader );
67-
loader.putTemplate( templateName , templateData );
68-
configuration.setTemplateLoader( loader );
69-
Template template = configuration.getTemplate( templateName );
70-
Map<Object, Object> data = new HashMap<>();
71-
try ( StringWriter writer = new StringWriter() ) {
72-
template.process( data , writer );
73-
try ( StringReader ftlReader = new StringReader( writer.toString() ) ) {
74-
this.doHandle(handler, type, sourceType, ftlReader, baos);
75+
ObjectMapper mapper = new ObjectMapper();
76+
try ( StringReader jsonReader = new StringReader(freemarkerJsonData) ) {
77+
JsonNode node = mapper.readTree( jsonReader ); // parse json node to sanitize input
78+
this.handleConfiguration(configuration, mapper.writeValueAsString( node ), StreamIO.readString( reader ), chainId );
79+
Template currentChain = configuration.getTemplate( chainId );
80+
Map<Object, Object> data = new HashMap<>();
81+
try ( StringWriter writer = new StringWriter() ) {
82+
currentChain.process( data , writer );
83+
try ( StringReader ftlReader = new StringReader( writer.toString() ) ) {
84+
this.doHandle(handler, type, sourceType, ftlReader, baos);
85+
}
7586
}
7687
}
7788
configuration.clearTemplateCache();

0 commit comments

Comments
 (0)