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

Commit 8b686c9

Browse files
committed
clean html in data and templates (SORMAS-Foundation#2906)
1 parent 0c4fd85 commit 8b686c9

12 files changed

Lines changed: 71 additions & 14 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 org.apache.velocity.app.event.ReferenceInsertionEventHandler;
19+
20+
import de.symeda.sormas.api.utils.HtmlHelper;
21+
22+
public class CleanHtmlReference implements ReferenceInsertionEventHandler {
23+
24+
@Override
25+
public Object referenceInsert(String s, Object o) {
26+
return o == null ? null : HtmlHelper.cleanHtml(o.toString(), HtmlHelper.EVENTACTION_WHITELIST);
27+
}
28+
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@
4343
import org.apache.velocity.util.introspection.SecureUberspector;
4444
import org.docx4j.openpackaging.exceptions.Docx4JException;
4545
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
46+
import org.jsoup.Jsoup;
47+
import org.jsoup.nodes.Document.OutputSettings;
48+
import org.jsoup.safety.Whitelist;
4649
import org.slf4j.Logger;
4750
import org.slf4j.LoggerFactory;
4851

4952
import de.symeda.sormas.api.docgeneneration.DocumentTemplateException;
5053
import de.symeda.sormas.api.docgeneneration.DocumentVariables;
5154
import de.symeda.sormas.api.i18n.I18nProperties;
5255
import de.symeda.sormas.api.i18n.Strings;
56+
import de.symeda.sormas.api.utils.HtmlHelper;
5357
import fr.opensagres.xdocreport.core.XDocReportException;
5458
import fr.opensagres.xdocreport.document.IXDocReport;
5559
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
@@ -63,9 +67,11 @@
6367
public class TemplateEngine {
6468

6569
private static final Pattern VARIABLE_PATTERN = Pattern.compile("([{] *(!)? *([A-Za-z0-9._]+) *[}]| *(!)? *([A-Za-z0-9._]+) *)");
70+
private static final Whitelist HTML_TEMPLATE_WHITELIST =
71+
HtmlHelper.EVENTACTION_WHITELIST.addAttributes("div", "class").addAttributes("span", "class").addAttributes("table", "class");
6672
private static final Logger logger = LoggerFactory.getLogger(TemplateEngine.class);
6773

68-
private Properties xdocVelocityProperties;
74+
private final Properties xdocVelocityProperties;
6975

7076
public TemplateEngine() {
7177
xdocVelocityProperties = new Properties();
@@ -136,6 +142,9 @@ public String generateDocumentTxt(Properties properties, File templateFile) {
136142
velocityEngine.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, SecureUberspector.class.getCanonicalName());
137143
// Disable Includes
138144
velocityEngine.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, NoIncludesEventHandler.class.getCanonicalName());
145+
// Clean Html
146+
velocityEngine.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, CleanHtmlReference.class.getCanonicalName());
147+
139148
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
140149
velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FilenameUtils.getFullPathNoEndSeparator(templateFile.getPath()));
141150
Template template = velocityEngine.getTemplate(templateFile.getName());
@@ -152,7 +161,9 @@ public String generateDocumentTxt(Properties properties, File templateFile) {
152161

153162
StringWriter stringWriter = new StringWriter();
154163
template.merge(velocityContext, stringWriter);
155-
return stringWriter.toString();
164+
OutputSettings outputSettings = new OutputSettings();
165+
outputSettings.prettyPrint(false);
166+
return Jsoup.clean(stringWriter.toString(), "", HTML_TEMPLATE_WHITELIST, outputSettings);
156167
}
157168

158169
public void validateTemplateDocx(InputStream templateInputStream) throws DocumentTemplateException {

sormas-backend/src/main/resources/docgeneration/sormasStyle.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
div.actions {
3636
margin-top: 35px;
3737
}
38+
.red {
39+
color: #f00;
40+
}
3841
</style>
3942
</header>
4043
<body>

sormas-backend/src/test/resources/docgeneration/eventHandout/EventHandout.cmp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
div.actions {
3636
margin-top: 35px;
3737
}
38+
.red {
39+
color: #f00;
40+
}
3841
</style>
3942
</header>
4043
<body>
@@ -44,18 +47,18 @@
4447
<p>...where people meet</p>
4548

4649
<table>
47-
<tr><th>Disease</th><th>Status</th><th>Event Date</th><th>Report Date</th></tr>
50+
<tbody><tr><th>Disease</th><th>Status</th><th>Event Date</th><th>Report Date</th></tr>
4851
<tr><td>COVID-19</td><td>Signal</td><td>11/12/2020</td><td>11/13/2020</td></tr>
49-
</table>
52+
</tbody></table>
5053

5154
<h2>Event Participants</h2>
5255

5356
<table>
54-
<tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Contacted</th></tr>
57+
<tbody><tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Contacted</th></tr>
5558
<tr><td>Georges</td><td>Bataille</td><td>+49 681 8901</td><td>[ ]</td></tr>
5659
<tr><td>Guy</td><td>Debord</td><td>+49 681 4567</td><td>[ ]</td></tr>
5760
<tr><td>Isidore</td><td>Isou</td><td>+49 681 1234</td><td>[ ]</td></tr>
58-
</table>
61+
</tbody></table>
5962

6063

6164
<div class="actions">
@@ -68,7 +71,7 @@
6871
<h2>Another action</h2>
6972
<p>11/15/2020</p>
7073
<div> This action hast no reply </div>
71-
<div> <span style="color:#f00">*</span> </div>
74+
<div> <span class="red">*</span> </div>
7275
</div>
7376

7477
</body>

sormas-backend/src/test/resources/docgeneration/eventHandout/EventHandout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ <h2>Event Participants</h2>
2323
<h2>$action.title</h2>
2424
<p>$F.format($action.getDate())</p>
2525
<div>#if($action.getDescription()) $action.getDescription() #else <span style="color:#f00">*</span> #end</div>
26-
<div>#if($action.reply) $action.reply #else <span style="color:#f00">*</span> #end</div>
26+
<div>#if($action.reply) $action.reply #else <span class="red">*</span> #end</div>
2727
</div>
2828
#end

sormas-backend/src/test/resources/docgeneration/eventHandout/EventHandoutError.cmp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
div.actions {
3636
margin-top: 35px;
3737
}
38+
.red {
39+
color: #f00;
40+
}
3841
</style>
3942
</header>
4043
<body>

sormas-backend/src/test/resources/docgeneration/eventHandout/EventHandoutNullableVariables.cmp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
div.actions {
3636
margin-top: 35px;
3737
}
38+
.red {
39+
color: #f00;
40+
}
3841
</style>
3942
</header>
4043
<body>

sormas-backend/src/test/resources/docgeneration/eventHandout/EventHandoutPreformatting.cmp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
div.actions {
3636
margin-top: 35px;
3737
}
38+
.red {
39+
color: #f00;
40+
}
3841
</style>
3942
</header>
4043
<body>
@@ -44,18 +47,18 @@
4447
<p>...where people meet</p>
4548

4649
<table>
47-
<tr><th>Disease</th><th>Status</th><th>Event Date</th><th>Report Date</th><th>User</th></tr>
50+
<tbody><tr><th>Disease</th><th>Status</th><th>Event Date</th><th>Report Date</th><th>User</th></tr>
4851
<tr><td>COVID-19</td><td>Signal</td><td>11/12/2020</td><td>11/13/2020</td><td>Surv Sup</td></tr>
49-
</table>
52+
</tbody></table>
5053

5154
<h2>Event Participants</h2>
5255

5356
<table>
54-
<tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Contacted</th></tr>
57+
<tbody><tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Contacted</th></tr>
5558
<tr><td>Georges</td><td>Bataille</td><td>+49 681 8901</td><td>[ ]</td></tr>
5659
<tr><td>Guy</td><td>Debord</td><td>+49 681 4567</td><td>[ ]</td></tr>
5760
<tr><td>Isidore</td><td>Isou</td><td>+49 681 1234</td><td>[ ]</td></tr>
58-
</table>
61+
</tbody></table>
5962

6063

6164
<div class="actions">
@@ -68,7 +71,7 @@
6871
<h2>Another action</h2>
6972
<p>11/15/2020</p>
7073
<div> This action hast no reply </div>
71-
<div> <span style="color:#f00">*</span> </div>
74+
<div> <span class="red">*</span> </div>
7275
</div>
7376

7477
</body>

sormas-backend/src/test/resources/docgeneration/eventHandout/EventHandoutPreformatting.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ <h2>Event Participants</h2>
2323
<h2>$action.title</h2>
2424
<p>$F.format($action.getDate())</p>
2525
<div>#if($action.getDescription()) $action.getDescription() #else <span style="color:#f00">*</span> #end</div>
26-
<div>#if($action.reply) $action.reply #else <span style="color:#f00">*</span> #end</div>
26+
<div>#if($action.reply) $action.reply #else <span class="red">*</span> #end</div>
2727
</div>
2828
#end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test:

0 commit comments

Comments
 (0)