Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit c1b09f9

Browse files
committed
restrict velocity engine features (SORMAS-Foundation#4231)
1 parent 0dcb04e commit c1b09f9

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

sormas-backend/src/main/java/de/symeda/sormas/backend/docgeneration/TemplateEngine.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@
3535
import org.apache.velocity.Template;
3636
import org.apache.velocity.VelocityContext;
3737
import org.apache.velocity.app.VelocityEngine;
38+
import org.apache.velocity.exception.VelocityException;
3839
import org.apache.velocity.runtime.RuntimeConstants;
3940
import org.apache.velocity.runtime.RuntimeSingleton;
4041
import org.apache.velocity.runtime.parser.ParseException;
4142
import org.apache.velocity.runtime.parser.node.SimpleNode;
4243
import org.apache.velocity.util.introspection.SecureUberspector;
4344
import org.docx4j.openpackaging.exceptions.Docx4JException;
4445
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
46+
import org.slf4j.Logger;
47+
import org.slf4j.LoggerFactory;
4548

4649
import de.symeda.sormas.api.docgeneneration.DocumentTemplateException;
4750
import de.symeda.sormas.api.docgeneneration.DocumentVariables;
@@ -53,12 +56,29 @@
5356
import fr.opensagres.xdocreport.template.FieldExtractor;
5457
import fr.opensagres.xdocreport.template.FieldsExtractor;
5558
import fr.opensagres.xdocreport.template.IContext;
56-
import fr.opensagres.xdocreport.template.TemplateEngineKind;
59+
import fr.opensagres.xdocreport.template.ITemplateEngine;
5760
import fr.opensagres.xdocreport.template.velocity.internal.ExtractVariablesVelocityVisitor;
61+
import fr.opensagres.xdocreport.template.velocity.internal.VelocityTemplateEngine;
5862

5963
public class TemplateEngine {
6064

6165
private static final Pattern VARIABLE_PATTERN = Pattern.compile("([{] *(!)? *([A-Za-z0-9._]+) *[}]| *(!)? *([A-Za-z0-9._]+) *)");
66+
private static final Logger logger = LoggerFactory.getLogger(TemplateEngine.class);
67+
68+
private Properties xdocVelocityProperties;
69+
70+
public TemplateEngine() {
71+
xdocVelocityProperties = new Properties();
72+
try {
73+
xdocVelocityProperties.load(VelocityTemplateEngine.class.getClassLoader().getResourceAsStream("xdocreport-velocity.properties"));
74+
} catch (IOException e) {
75+
logger.error("Could not read velocity properties.", e);
76+
}
77+
// Disable Reflection and Classloader related methods
78+
xdocVelocityProperties.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, SecureUberspector.class.getCanonicalName());
79+
// Disable Includes
80+
xdocVelocityProperties.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, NoIncludesEventHandler.class.getCanonicalName());
81+
}
6282

6383
public DocumentVariables extractTemplateVariablesDocx(File templateFile) throws DocumentTemplateException {
6484
try {
@@ -105,7 +125,7 @@ public byte[] generateDocumentDocx(Properties properties, File templateFile) thr
105125
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
106126
report.process(context, outputStream);
107127
return outputStream.toByteArray();
108-
} catch (IOException | XDocReportException e) {
128+
} catch (IOException | XDocReportException | VelocityException e) {
109129
throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.errorDocumentGeneration), templateFile.getName()));
110130
}
111131
}
@@ -161,7 +181,8 @@ protected IXDocReport readXDocReport(InputStream templateInputStream) throws Doc
161181

162182
try {
163183
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
164-
return XDocReportRegistry.getRegistry().loadReport(inStream, TemplateEngineKind.Velocity);
184+
ITemplateEngine templateEngine = new XDocTemplateEngine(xdocVelocityProperties);
185+
return XDocReportRegistry.getRegistry().loadReport(inStream, templateEngine);
165186
} catch (IOException | XDocReportException | NullPointerException e) {
166187
throw new DocumentTemplateException(I18nProperties.getString(Strings.errorProcessingTemplate));
167188
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
3+
* Copyright © 2016-2021 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
14+
*/
15+
16+
package de.symeda.sormas.backend.docgeneration;
17+
18+
import java.util.Properties;
19+
20+
import fr.opensagres.xdocreport.core.XDocReportException;
21+
import fr.opensagres.xdocreport.template.velocity.internal.VelocityTemplateEngine;
22+
23+
public class XDocTemplateEngine extends VelocityTemplateEngine {
24+
25+
public XDocTemplateEngine(Properties velocityEngineProperties) {
26+
super(velocityEngineProperties);
27+
}
28+
29+
@Override
30+
public void initializeVelocityEngine(Properties velocityEngineProperties) throws XDocReportException {
31+
32+
try {
33+
getVelocityEngine().setProperty("velocityTemplateEngine", this);
34+
getVelocityEngine().init(velocityEngineProperties);
35+
} catch (Exception var4) {
36+
throw new XDocReportException(var4);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)