Skip to content

Commit bb5fe08

Browse files
SRE Fix: html injection issue resolved
1 parent 5c817f9 commit bb5fe08

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<validation-version>2.0.1.Final</validation-version>
3232
<json-version>20230227</json-version>
3333
<spring-web-version>6.0.7</spring-web-version>
34+
<org.apache.commons-text>1.10.0</org.apache.commons-text>
3435
</properties>
3536

3637
<developers>
@@ -78,6 +79,13 @@
7879
</organization>
7980

8081
<dependencies>
82+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
83+
<dependency>
84+
<groupId>org.apache.commons</groupId>
85+
<artifactId>commons-text</artifactId>
86+
<version>${org.apache.commons-text}</version>
87+
</dependency>
88+
8189
<dependency>
8290
<groupId>junit</groupId>
8391
<artifactId>junit</artifactId>

src/main/java/com/contentstack/utils/AutomateCommon.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
public class AutomateCommon {
2323

24-
private static final String ASSET = "asset";
24+
private static final String ASSET = AutomateCommon.class.getSimpleName();
2525

2626
private AutomateCommon() {
2727
throw new IllegalStateException("Not allowed to create instance of AutomateCommon");
@@ -176,7 +176,7 @@ private static String extractKeys(@NotNull JSONObject jsonNode, Option renderObj
176176
JSONObject contentToPass = filteredContent.get();
177177
return getStringOption(renderObject, metadata, contentToPass);
178178
} else {
179-
if (attrType.equalsIgnoreCase(ASSET)) {
179+
if (attrType.equalsIgnoreCase("asset")) {
180180
return renderObject.renderNode("img", jsonNode, nodeJsonArray -> doRawProcessing(nodeJsonArray, renderObject, embedItem));
181181
}
182182
}

src/main/java/com/contentstack/utils/render/DefaultOption.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.contentstack.utils.interfaces.NodeCallback;
55
import com.contentstack.utils.interfaces.Option;
66
import com.contentstack.utils.node.MarkType;
7+
import org.apache.commons.text.StringEscapeUtils;
78
import org.json.JSONObject;
89

910

@@ -58,22 +59,27 @@ public String renderMark(MarkType markType, String text) {
5859
}
5960
}
6061

62+
private String escapeInjectHtml(JSONObject nodeObj, String nodeType) {
63+
String injectedHtml = getNodeStr(nodeObj, nodeType);
64+
return StringEscapeUtils.escapeHtml4(injectedHtml);
65+
}
66+
6167
@Override
6268
public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback callback) {
6369
String children = callback.renderChildren(nodeObject.optJSONArray("children"));
6470
switch (nodeType) {
6571
case "p":
6672
return "<p>" + children + "</p>";
6773
case "a":
68-
return "<a href=\"" + getNodeStr(nodeObject, "href") + "\">" + children + "</a>";
74+
return "<a href=\"" + escapeInjectHtml(nodeObject, "href") + "\">" + children + "</a>";
6975
case "img":
7076
String assetLink = getNodeStr(nodeObject, "asset-link");
7177
if (!assetLink.isEmpty()) {
72-
return "<img src=\"" + assetLink + "\" />" + children;
78+
return "<img src=\"" + escapeInjectHtml(nodeObject, "asset-link") + "\" />" + children;
7379
}
74-
return "<img src=\"" + getNodeStr(nodeObject, "src") + "\" />" + children;
80+
return "<img src=\"" + escapeInjectHtml(nodeObject, "src") + "\" />" + children;
7581
case "embed":
76-
return "<iframe src=\"" + getNodeStr(nodeObject, "src") + "\"" + children + "</iframe>";
82+
return "<iframe src=\"" + escapeInjectHtml(nodeObject, "src") + "\"" + children + "</iframe>";
7783
case "h1":
7884
return "<h1>" + children + "</h1>";
7985
case "h2":

0 commit comments

Comments
 (0)