Skip to content

Commit 088860f

Browse files
Fixed the vulnerability issue
1 parent 7cc9ac2 commit 088860f

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

sormas-ui/src/main/java/de/symeda/sormas/ui/caze/CaseDataForm.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.Objects;
43+
import java.util.Set;
4344
import java.util.regex.Matcher;
4445
import java.util.regex.Pattern;
4546
import java.util.stream.Collectors;
@@ -1656,15 +1657,32 @@ private String sanitizeAndLinkify(String text) {
16561657
StringBuilder result = new StringBuilder();
16571658
int last = 0;
16581659

1660+
// Expanded the allowed tags to include standard rich text formatting options
1661+
Set<String> allowedTags = Set
1662+
.of("div", "span", "p", "br", "b", "i", "u", "strong", "em", "ul", "ol", "li", "table", "tr", "td", "th", "thead", "tbody", "font", "a");
1663+
16591664
while (matcher.find()) {
16601665
String plainTextSegment = htmlText.substring(last, matcher.start());
16611666
result.append(escapeHtml(plainTextSegment).replace("&amp;nbsp;", "&nbsp;"));
16621667

16631668
String htmlTag = matcher.group(1);
16641669
String url = matcher.group(2);
16651670
if (htmlTag != null) {
1666-
// It's a rich text tag (like <div> or <br>). Pass it through safely.
1667-
result.append(htmlTag);
1671+
// Only allow safe formatting tags
1672+
String cleanTagName = htmlTag.replaceAll("[<>/]", "").trim().split("\\s+")[0].toLowerCase();
1673+
if (allowedTags.contains(cleanTagName)) {
1674+
String lowerTag = htmlTag.toLowerCase();
1675+
if (lowerTag.contains("javascript:")
1676+
|| lowerTag.contains("onclick")
1677+
|| lowerTag.contains("onerror")
1678+
|| lowerTag.contains("onload")) {
1679+
// Attack vector found! Escape it safely into text instead of executing it
1680+
result.append(escapeHtml(htmlTag));
1681+
} else {
1682+
// It's a completely safe rich text element. Pass it through so styles render perfectly.
1683+
result.append(htmlTag);
1684+
}
1685+
}
16681686
} else if (url != null) {
16691687
// It's a plain-text URL. Wrap it in your custom blue link styling.
16701688
String escapedUrl = escapeHtml(url);

0 commit comments

Comments
 (0)