Skip to content

Commit fd67dcd

Browse files
committed
fix: sanitize error message, escape characters, add nonce, render errors as innerText
1 parent a0d1f42 commit fd67dcd

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

plugin/src/main/java/io/snyk/eclipse/plugin/html/BaseHtmlProvider.java

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public String replaceCssVariables(String html) {
132132

133133
htmlStyled = htmlStyled.replace("${headerEnd}", "");
134134
htmlStyled = htmlStyled.replace("${nonce}", nonce);
135-
htmlStyled = htmlStyled.replace("ideNonce", nonce);
135+
htmlStyled = htmlStyled.replaceAll("ideNonce", nonce);
136136
htmlStyled = htmlStyled.replace("${ideScript}", "");
137137

138138
return htmlStyled;
@@ -207,15 +207,58 @@ public ITheme getCurrentTheme() {
207207
return currentTheme;
208208
}
209209

210+
private String escapeHtml(String text) {
211+
if (text == null || text.isEmpty()) {
212+
return "";
213+
}
214+
StringBuilder escaped = new StringBuilder();
215+
for (char c : text.toCharArray()) {
216+
switch (c) {
217+
case '&':
218+
escaped.append("&");
219+
break;
220+
case '<':
221+
escaped.append("&lt;");
222+
break;
223+
case '>':
224+
escaped.append("&gt;");
225+
break;
226+
case '"':
227+
escaped.append("&quot;");
228+
break;
229+
case '\'':
230+
escaped.append("&#039;");
231+
break;
232+
case '\n':
233+
escaped.append("&#10;");
234+
break;
235+
case '\r':
236+
escaped.append("&#13;");
237+
break;
238+
default:
239+
if (c > 0x7F) {
240+
escaped.append("&#").append((int) c).append(";");
241+
} else {
242+
escaped.append(c);
243+
}
244+
break;
245+
}
246+
}
247+
return escaped.toString();
248+
}
210249
public String getErrorHtml(String errorMessage, String path) {
250+
String escapedErrorMessage = escapeHtml(errorMessage);
251+
String escapedPath = escapeHtml(path);
211252
var html = """
212253
<!DOCTYPE html>
213254
<html lang="en">
214255
<head>
256+
<meta http-equiv='Content-Type' content='text/html; charset=unicode' />
215257
<meta charset="UTF-8">
216258
<meta name="viewport" content="width=device-width, initial-scale=1.0">
259+
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'nonce-ideNonce'; style-src 'self' 'nonce-ideNonce';">
217260
<title>Snyk for Eclipse</title>
218-
<style>
261+
<style nonce=ideNonce>
219262
body {
220263
font-family: var(--default-font);
221264
background-color: var(--background-color);
@@ -236,16 +279,23 @@ public String getErrorHtml(String errorMessage, String path) {
236279
<p><strong>An error occurred:</strong></p>
237280
<p>
238281
<table>
239-
<tr><td width="150" >Error message:</td><td>%s</td></tr>
282+
<tr><td width="150" >Error message:</td><td id="errorContainer"></td></tr>
240283
<tr></tr>
241-
<tr><td>Path:</td><td>%s</td></tr>
284+
<tr><td width="150" >Path:</td><td id="pathContainer"></td></tr>
242285
</table>
243286
</p>
244287
</div>
245288
</div>
246289
</body>
290+
<script nonce=ideNonce>
291+
const errMsgElement = document.getElementById("errorContainer");
292+
errMsgElement.innerText = "%s";
293+
const pathElem = document.getElementById("pathContainer");
294+
pathElem.innerText = "%s";
295+
</script>
247296
</html>
248-
""".formatted(errorMessage, path);
297+
""".formatted(escapedErrorMessage, escapedPath);
298+
System.out.print(html);
249299
return replaceCssVariables(html);
250300
}
251301
}

0 commit comments

Comments
 (0)