Skip to content

Commit f214503

Browse files
fix: sanitize error message, escape characters, add nonce, render as innerText [IDE-967] (#273)
* fix: sanitize error message, escape characters, add nonce, render errors as innerText * chore: fix PMD * fix: use custom escapeHTML function * fix: PMD --------- Co-authored-by: Bastian Doetsch <bastian.doetsch@snyk.io>
1 parent 930f0cb commit f214503

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

plugin/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bundle-ActivationPolicy: lazy
3131
Bundle-ClassPath: .,
3232
target/dependency/commons-codec-1.17.0.jar,
3333
target/dependency/commons-lang3-3.12.0.jar,
34+
target/dependency/commons-text-1.10.0.jar,
3435
target/dependency/commons-logging-1.3.4.jar,
3536
target/dependency/httpclient-4.5.14.jar,
3637
target/dependency/httpcore-4.4.16.jar,

plugin/build.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bin.includes = plugin.xml,\
1515
target/dependency/jackson-annotations-2.16.2.jar,\
1616
target/dependency/jackson-core-2.16.2.jar,\
1717
target/dependency/jackson-databind-2.16.2.jar,\
18-
target/dependency/javax.inject-1.jar
18+
target/dependency/javax.inject-1.jar,\
19+
target/dependency/commons-text-1.10.0.jar
1920
src.includes =src/,\
2021
icons/

plugin/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
<version>3.12.0</version>
3939
<type>jar</type>
4040
</dependency>
41+
<dependency>
42+
<groupId>org.apache.commons</groupId>
43+
<artifactId>commons-text</artifactId>
44+
<version>1.10.0</version>
45+
<type>jar</type>
46+
</dependency>
4147
<dependency>
4248
<groupId>org.apache.httpcomponents</groupId>
4349
<artifactId>httpcore</artifactId>

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55
import java.util.Random;
66

7+
import org.apache.commons.text.StringEscapeUtils;
78
import org.eclipse.core.runtime.Platform;
89
import org.eclipse.jface.resource.ColorRegistry;
910
import org.eclipse.jface.resource.JFaceResources;
@@ -22,7 +23,6 @@ public class BaseHtmlProvider {
2223
private final Random random = new Random();
2324
private final Map<String, String> colorCache = new HashMap<>();
2425
private String nonce = "";
25-
2626
public String getCss() {
2727
return "";
2828
}
@@ -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;
@@ -206,16 +206,19 @@ public ITheme getCurrentTheme() {
206206
currentTheme = themeManager.getCurrentTheme();
207207
return currentTheme;
208208
}
209-
210209
public String getErrorHtml(String errorMessage, String path) {
211-
var html = """
210+
String escapedErrorMessage = errorMessage == null ? "Unknown error" : StringEscapeUtils.escapeHtml3((errorMessage));
211+
String escapedPath = path == null ? "Unknown path" : StringEscapeUtils.escapeHtml3(path);
212+
var html = String.format("""
212213
<!DOCTYPE html>
213214
<html lang="en">
214215
<head>
216+
<meta http-equiv='Content-Type' content='text/html; charset=unicode' />
215217
<meta charset="UTF-8">
216218
<meta name="viewport" content="width=device-width, initial-scale=1.0">
219+
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'nonce-ideNonce'; style-src 'self' 'nonce-ideNonce';">
217220
<title>Snyk for Eclipse</title>
218-
<style>
221+
<style nonce=ideNonce>
219222
body {
220223
font-family: var(--default-font);
221224
background-color: var(--background-color);
@@ -236,16 +239,16 @@ public String getErrorHtml(String errorMessage, String path) {
236239
<p><strong>An error occurred:</strong></p>
237240
<p>
238241
<table>
239-
<tr><td width="150" >Error message:</td><td>%s</td></tr>
242+
<tr><td width="150" >Error message:</td><td id="errorContainer">%s</td></tr>
240243
<tr></tr>
241-
<tr><td>Path:</td><td>%s</td></tr>
244+
<tr><td width="150" >Path:</td><td id="pathContainer">%s</td></tr>
242245
</table>
243246
</p>
244247
</div>
245248
</div>
246249
</body>
247250
</html>
248-
""".formatted(errorMessage, path);
251+
""",escapedErrorMessage, escapedPath);
249252
return replaceCssVariables(html);
250253
}
251254
}

target-platform/target-platform.target

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<version>3.17.0</version>
5959
<type>jar</type>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.apache.commons</groupId>
63+
<artifactId>commons-text</artifactId>
64+
<version>1.10.0</version>
65+
<type>jar</type>
66+
</dependency>
6167
<dependency>
6268
<groupId>org.mockito</groupId>
6369
<artifactId>mockito-inline</artifactId>

0 commit comments

Comments
 (0)