CVE-2020-36634 (published 2022-12-27T13:15Z)

A vulnerability classified as problematic has been found in Indeed Engineering util up to 1.0.33. Affected is the function visit/appendTo of the file varexport/src/main/java/com/indeed/util/varexport/servlet/ViewExportedVariablesServlet.java. The manipulation leads to cross site scripting. It is possible to launch the attack remotely. Upgrading to version 1.0.34 is able to address this issue. The name of the patch is c0952a9db51a880e9544d9fac2a2218a6bfc9c63. It is recommended to upgrade the affected component. VDB-216882 is the identifier assigned to this vulnerability.

CVSS v3 base score: 5.4, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/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/indeedeng/util
commit_details: repo_url: https://github.com/indeedeng/util
author: Sergey Savenko
author_date: 2020-07-20 14:49:15+09:00
msg: COMMON-3969: Explicit alphanumeric n-gram indexes

Explicitly build alphanumeric n-gram indexes to avoid XSS due
to unescaped characters in the rendered n-gram indexes JSON
repository: repo_url: https://github.com/indeedeng/util
repo_name: indeedeng/util
description: Useful Java utilities
repo_language: Java
owner: indeedeng
file_changes: filename: Variable.java
change_type: ModificationType.MODIFY
diff: @@ -51,6 +51,11 @@ public abstract class Variable<T> {
         return name;
     }
 
+    /**
+     * @deprecated do not use this.
+     * Please implement your own tokenization of variable names, if needed.
+     */
+    @Deprecated
     public String[] getIndexableNames() {
         return indexableNames;
     }

programming_language: Java
filename: ViewExportedVariablesServlet.java
change_type: ModificationType.MODIFY
diff: @@ -4,7 +4,11 @@ package com.indeed.util.varexport.servlet;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Joiner;
 import com.google.common.base.Strings;
-import com.google.common.collect.*;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.SetMultimap;
+import com.google.common.collect.TreeMultimap;
 import com.indeed.util.varexport.VarExporter;
 import com.indeed.util.varexport.Variable;
 import com.indeed.util.varexport.VariableHost;
@@ -14,6 +18,7 @@ import freemarker.template.DefaultObjectWrapper;
 import freemarker.template.Template;
 import org.apache.log4j.Logger;
 
+import javax.annotation.Nullable;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -25,7 +30,18 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
-import java.util.*;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
 
 /**
  * Servlet for displaying variables exported by {@link VarExporter}.
@@ -287,8 +303,7 @@ public class ViewExportedVariablesServlet extends HttpServlet {
         }
         root.put("vars", varList);
         if (withIndex) {
-            final String varsIndex = buildIndex(varList);
-            root.put("varsIndex", varsIndex);
+            root.put("varsIndex", alphanumericNGramIndexesJSON(varList));
         }
 
         try {
@@ -298,19 +313,24 @@ public class ViewExportedVariablesServlet extends HttpServlet {
         }
     }
 
-    private String buildIndex(final List<Variable> varList) {
-        final SetMultimap<String, Integer> uniGram = buildNGramIndex(varList, 1);
-        final SetMultimap<String, Integer> biGram = buildNGramIndex(varList, 2);
-        final SetMultimap<String, Integer> triGram = buildNGramIndex(varList, 3);
+    private String alphanumericNGramIndexesJSON(final List<Variable> varList) {
+        // The JSON we build here is valid as we only use alphanumeric n-grams
+
         final StringBuilder json = new StringBuilder();
+
         json.append('{').append('\n');
-        json.append("\"uniGram\":").append('\n');
-        appendTo(json, uniGram).append(',').append('\n');
+
+        json.append("\n\"uniGram\":").append('\n');
+        appendTo(json, buildAlphanumericNGramIndex(varList, 1)).append(',').append('\n');
+
         json.append("\"biGram\":").append('\n');
-        appendTo(json, biGram).append(',').append('\n');;
+        appendTo(json, buildAlphanumericNGramIndex(varList, 2)).append(',').append('\n');;
+
         json.append("\"triGram\":").append('\n');
-        appendTo(json, triGram);
+        appendTo(json, buildAlphanumericNGramIndex(varList, 3));
+
         json.append('}');
+
         return json.toString();
     }
 
@@ -333,19 +353,32 @@ public class ViewExportedVariablesServlet extends HttpServlet {
         return json;
     }
 
-    private SetMultimap<String, Integer> buildNGramIndex(final List<Variable> varList, final int n) {
-        final TreeMultimap<String, Integer> uniGram = TreeMultimap.create();
-        for (int index = 0; index < varList.size(); index++) {
-            final Variable var = varList.get(index);
-            final String[] indexableNames = var.getIndexableNames();
-            for (final String indexableName : indexableNames) {
-                for (int i = 0; i < indexableName.length() - n + 1; i++) {
-                    final String key = indexableName.substring(i, i + n);
-                    uniGram.put(key, index);
-                }
-            }
+    @VisibleForTesting
+    static SetMultimap<String, Integer> buildAlphanumericNGramIndex(final List<Variable> varList, final int n) {
+        final TreeMultimap<String, Integer> nGramIndex = TreeMultimap.create();
+        IntStream
+                .range(0, varList.size())
+                .forEach(i ->
+                        alphanumericNGrams(varList.get(i).getName(), n)
+                                .forEach(ngram -> nGramIndex.put(ngram, i))
+                );
+        return nGramIndex;
+    }
+
+    private static final Pattern NON_ALPHANUMERIC = Pattern.compile("[^a-z0-9]+");
+
+    @VisibleForTesting
+    static Stream<String> alphanumericNGrams(@Nullable final String in, final int n) {
+        if (in == null) {
+            return Stream.empty();
         }
-        return uniGram;
+        return NON_ALPHANUMERIC
+                .splitAsStream(in.toLowerCase(Locale.US))
+                .flatMap(alnumSubString ->
+                        IntStream
+                                .rangeClosed(0, alnumSubString.length() - n)
+                                .mapToObj(i -> alnumSubString.substring(i, i + n))
+                );
     }
 
     private void addVariable(final Variable v, final List<Variable> out) {

programming_language: Java
filename: ViewExportedVariablesServletTest.java
change_type: ModificationType.MODIFY
diff: @@ -2,6 +2,8 @@ package com.indeed.util.varexport.servlet;
 
 import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
+import com.google.common.collect.SetMultimap;
+import com.google.common.collect.TreeMultimap;
 import com.indeed.util.varexport.Export;
 import com.indeed.util.varexport.ManagedVariable;
 import com.indeed.util.varexport.VarExporter;
@@ -19,13 +21,16 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.expectLastCall;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 
@@ -164,6 +169,63 @@ public class ViewExportedVariablesServletTest {
         assertFalse(bodyWriter.toString().contains(unescaped));
     }
 
+    @Test
+    public void buildAlphaNumericNGramIndex() {
+        final TreeMultimap<String, Integer> expectedTriGramIndex = TreeMultimap.create();
+        expectedTriGramIndex.put("abc", 0);
+        expectedTriGramIndex.put("bcd", 0);
+        expectedTriGramIndex.put("bcd", 1);
+        expectedTriGramIndex.put("zzz", 2);
+
+        final SetMultimap<String, Integer> actualTriGramIndex = ViewExportedVariablesServlet.buildAlphanumericNGramIndex(
+                Arrays.asList(
+                        ManagedVariable.builder().setName("abcd").build(),
+                        ManagedVariable.builder().setName("bcd").build(),
+                        ManagedVariable.builder().setName("zzz").build()
+                ),
+                3
+        );
+
+        assertEquals(expectedTriGramIndex, actualTriGramIndex);
+    }
+
+    @Test
+    public void alphanumericNGrams() {
+        assertEquals(0, ViewExportedVariablesServlet.alphanumericNGrams(null, 1).count());
+        assertEquals(0, ViewExportedVariablesServlet.alphanumericNGrams("zzz", 42).count());
+
+        assertEquals(
+                Arrays.asList("m", "1", "3", "b", "i", "t", "r", "i"),
+                ViewExportedVariablesServlet
+                        .alphanumericNGrams("m1-3-/bi<</tri", 1)
+                        .collect(Collectors.toList())
+        );
+
+        assertEquals(
+                Arrays.asList("m1", "bi", "tr", "ri"),
+                ViewExportedVariablesServlet
+                        .alphanumericNGrams("m1-3-/bi<</tri", 2)
+                        .collect(Collectors.toList())
+        );
+
+        assertEquals(
+                Arrays.asList(
+                        "m1x",
+                        "1x3",
+                        "x3d",
+                        "3dc",
+                        "dca",
+                        "cas",
+                        "ase",
+                        "tri",
+                        "tri"
+                ),
+                ViewExportedVariablesServlet
+                        .alphanumericNGrams("m1x3dCaSe-1-/bi<</tri?tri", 3)
+                        .collect(Collectors.toList())
+        );
+    }
+
     @Before
     public void setUp() throws Exception {
         VarExporter.resetGlobal();

programming_language: Java