CVE-2023-33962 (published 2023-05-30T22:15Z)

JStachio is a  type-safe Java Mustache templating engine. Prior to version 1.0.1, JStachio fails to escape single quotes `\'` in HTML, allowing an attacker to inject malicious code. This vulnerability can be exploited by an attacker to execute arbitrary JavaScript code in the context of other users visiting pages that use this template engine. This can lead to various consequences, including session hijacking, defacement of web pages, theft of sensitive information, or even the propagation of malware.\n\nVersion 1.0.1 contains a patch for this issue. To mitigate this vulnerability, the template engine should properly escape special characters, including single quotes. Common practice is to escape `\'` as `&#39`. As a workaround, users can avoid this issue by using only double quotes `"` for HTML attributes.

CVSS v3 base score: 6.1, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N)

Weakness information:
cwe_id: CWE-79
cwe_name: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
description: The product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users.
extended_description: 
            Cross-site scripting (XSS) vulnerabilities occur when:
            
               Untrusted data enters a web application, typically from a web request.
               The web application dynamically generates a web page that contains this untrusted data.
               During page generation, the application does not prevent the data from containing content that is executable by a web browser, such as JavaScript, HTML tags, HTML attributes, mouse events, Flash, ActiveX, etc.
               A victim visits the generated web page through a web browser, which contains malicious script that was injected using the untrusted data.
               Since the script comes from a web page that was sent by the web server, the victim's web browser executes the malicious script in the context of the web server's domain.
               This effectively violates the intention of the web browser's same-origin policy, which states that scripts in one domain should not be able to access resources or run code in a different domain.
            
            There are three main kinds of XSS:
            
               
                  Type 1: Reflected XSS (or Non-Persistent) - 
         			The server reads data directly from the HTTP request and reflects it back in the HTTP response. Reflected XSS exploits occur when an attacker causes a victim to supply dangerous content to a vulnerable web application, which is then reflected back to the victim and executed by the web browser. The most common mechanism for delivering malicious content is to include it as a parameter in a URL that is posted publicly or e-mailed directly to the victim. URLs constructed in this manner constitute the core of many phishing schemes, whereby an attacker convinces a victim to visit a URL that refers to a vulnerable site. After the site reflects the attacker's content back to the victim, the content is executed by the victim's browser.
               
                  Type 2: Stored XSS (or Persistent) - 
               The application stores dangerous data in a database, message forum, visitor log, or other trusted data store. At a later time, the dangerous data is subsequently read back into the application and included in dynamic content. From an attacker's perspective, the optimal place to inject malicious content is in an area that is displayed to either many users or particularly interesting users. Interesting users typically have elevated privileges in the application or interact with sensitive data that is valuable to the attacker. If one of these users executes malicious content, the attacker may be able to perform privileged operations on behalf of the user or gain access to sensitive data belonging to the user. For example, the attacker might inject XSS into a log message, which might not be handled properly when an administrator views the logs.
            
               
                  Type 0: DOM-Based XSS - 
               In DOM-based XSS, the client performs the injection of XSS into the page; in the other types, the server performs the injection. DOM-based XSS generally involves server-controlled, trusted script that is sent to the client, such as Javascript that performs sanity checks on a form before the user submits it. If the server-supplied script processes user-supplied data and then injects it back into the web page (such as with dynamic HTML), then DOM-based XSS is possible.
            
            
            Once the malicious script is injected, the attacker can perform a variety of malicious activities. The attacker could transfer private information, such as cookies that may include session information, from the victim's machine to the attacker. The attacker could send malicious requests to a web site on behalf of the victim, which could be especially dangerous to the site if the victim has administrator privileges to manage that site. Phishing attacks could be used to emulate trusted web sites and trick the victim into entering a password, allowing the attacker to compromise the victim's account on that web site. Finally, the script could exploit a vulnerability in the web browser itself possibly taking over the victim's machine, sometimes referred to as "drive-by hacking."
            In many cases, the attack can be launched without the victim even being aware of it. Even with careful users, attackers frequently use a variety of methods to encode the malicious portion of the attack, such as URL encoding or Unicode, so the request looks less suspicious.
         
         

Fix information:
repo_url: https://github.com/jstachio/jstachio
commit_details: repo_url: https://github.com/jstachio/jstachio
author: Adam Gent
author_date: 2023-05-28 15:58:19-04:00
msg: Fix #157 add more aggressive html5 escaping
repository: repo_url: https://github.com/jstachio/jstachio
repo_name: jstachio/jstachio
description: Java type safe statically compiled mustache
repo_language: Java
owner: jstachio
file_changes: filename: Html.java
change_type: ModificationType.MODIFY
diff: @@ -10,13 +10,42 @@ import io.jstach.jstachio.Escaper;
  * Provides a mustache spec based HTML escaper which is the default in normal mustache.
  * <p>
  * The escaper simply escapes:
- * <ul>
- * <li>'&quot;'</li>
- * <li>'&gt;'</li>
- * <li>'&lt;'</li>
- * <li>'&amp;'</li>
- * </ul>
- *
+ * <table border="1">
+ * <caption><strong>Escape table</strong></caption>
+ * <tr>
+ * <th>Character</th>
+ * <th>Escaped String</th>
+ * </tr>
+ * <tr>
+ * <td>'<code>&quot;</code>'</td>
+ * <td>{@value HtmlEscaper#QUOT}</td>
+ * </tr>
+ * <tr>
+ * <td>'<code>&amp;</code>'</td>
+ * <td>{@value HtmlEscaper#AMP}</td>
+ * </tr>
+ * <tr>
+ * <td>'<code>&#x27;</code>'</td>
+ * <td>{@value HtmlEscaper#APOS}</td>
+ * </tr>
+ * <tr>
+ * <td>'<code>&lt;</code>'</td>
+ * <td>{@value HtmlEscaper#LT}</td>
+ * </tr>
+ * <tr>
+ * <td>'<code>=</code>'</td>
+ * <td>{@value HtmlEscaper#EQUAL}</td>
+ * </tr>
+ * <tr>
+ * <td>'<code>&gt;</code>'</td>
+ * <td>{@value HtmlEscaper#GT}</td>
+ * </tr>
+ * <tr>
+ * <td>'<code>&#x60;</code>'</td>
+ * <td>{@value HtmlEscaper#BACK_TICK}</td>
+ * </tr>
+ * </table>
+ * <br />
  * <em>N.B. Unlike many XML escapers this escaper does not differentiate attribute and
  * element content. Furthermore Mustache unlike many other templating languages is content
  * agnostic. If more flexibile attribute escaping is needed a custom lambda could be used

programming_language: Java
filename: HtmlEscaper.java
change_type: ModificationType.MODIFY
diff: @@ -7,13 +7,19 @@ enum HtmlEscaper implements Escaper {
 
 	Html;
 
-	private static final String QUOT = "&quot;";
+	static final String QUOT = "&quot;";
 
-	private static final String GT = "&gt;";
+	static final String AMP = "&amp;";
 
-	private static final String LT = "&lt;";
+	static final String APOS = "&#x27;";
 
-	private static final String AMP = "&amp;";
+	static final String LT = "&lt;";
+
+	static final String EQUAL = "&#x3D;";
+
+	static final String GT = "&gt;";
+
+	static final String BACK_TICK = "&#x60;";
 
 	@Override
 	public <A extends Output<E>, E extends Exception> void append(A a, CharSequence s) throws E {
@@ -27,29 +33,42 @@ enum HtmlEscaper implements Escaper {
 		for (int i = start; i < end; i++) {
 			char c = csq.charAt(i);
 			switch (c) {
-				case '&' -> {
+				case '"' -> { // 34
+					a.append(csq, start, i);
+					start = i + 1;
+					a.append(QUOT);
+				}
+				case '&' -> { // 38
 					a.append(csq, start, i);
 					start = i + 1;
 					a.append(AMP);
 
 				}
-				case '<' -> {
+				case '\'' -> { // 39
+					a.append(csq, start, i);
+					start = i + 1;
+					a.append(APOS);
+				}
+				case '<' -> { // 60
 					a.append(csq, start, i);
 					start = i + 1;
 					a.append(LT);
-
 				}
-				case '>' -> {
+				case '=' -> { // 61
+					a.append(csq, start, i);
+					start = i + 1;
+					a.append(EQUAL);
+				}
+				case '>' -> { // 62
 					a.append(csq, start, i);
 					start = i + 1;
 					a.append(GT);
 				}
-				case '"' -> {
+				case '`' -> { // 96
 					a.append(csq, start, i);
 					start = i + 1;
-					a.append(QUOT);
+					a.append(BACK_TICK);
 				}
-
 			}
 		}
 		a.append(csq, start, end);
@@ -59,17 +78,26 @@ enum HtmlEscaper implements Escaper {
 	@Override
 	public <A extends Output<E>, E extends Exception> void append(A a, char c) throws E {
 		switch (c) {
+			case '"' -> {
+				a.append(QUOT);
+			}
 			case '&' -> {
 				a.append(AMP);
 			}
+			case '\'' -> {
+				a.append(APOS);
+			}
 			case '<' -> {
 				a.append(LT);
 			}
+			case '=' -> {
+				a.append(EQUAL);
+			}
 			case '>' -> {
 				a.append(GT);
 			}
-			case '"' -> {
-				a.append(QUOT);
+			case '`' -> {
+				a.append(BACK_TICK);
 			}
 			default -> {
 				a.append(c);

programming_language: Java
filename: HtmlEscapeTest.java
change_type: ModificationType.ADD
diff: @@ -0,0 +1,33 @@
+package io.jstach.examples.htmlescaper;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import io.jstach.jstache.JStache;
+
+public class HtmlEscapeTest {
+
+	@JStache(template = """
+			<div alt="{{attr}}">
+			{{body}}
+			</div>
+			""")
+	public record EscapeModel(String attr, String body) {
+
+	}
+
+	@Test
+	public void testName() throws Exception {
+		var m = new EscapeModel("\"&\'<=>`", "\"&\'<=>`");
+		String actual = EscapeModelRenderer.of().execute(m);
+		String expected = """
+				<div alt="&quot;&amp;&#x27;&lt;&#x3D;&gt;&#x60;">
+				&quot;&amp;&#x27;&lt;&#x3D;&gt;&#x60;
+				</div>
+				""";
+
+		assertEquals(expected, actual);
+	}
+
+}

programming_language: Java
filename: package-info.java
change_type: ModificationType.ADD
diff: @@ -0,0 +1,2 @@
+@org.eclipse.jdt.annotation.NonNullByDefault
+package io.jstach.examples.htmlescaper;
\ No newline at end of file

programming_language: Batchfile
filename: LambdaPartialTest.java
change_type: ModificationType.MODIFY
diff: @@ -26,7 +26,7 @@ public class LambdaPartialTest {
 		String actual = JStachio.render(m);
 		String expected = """
 				bingo
-				LambdaSectionParent[stuff=ignore]
+				LambdaSectionParent[stuff&#x3D;ignore]
 								""";
 		assertEquals(expected, actual);
 	}

programming_language: Java