CVE-2020-26282: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection') (CWE-74)
value: BrowserUp Proxy allows you to manipulate HTTP requests and responses, capture HTTP content, and export performance data as a HAR file. BrowserUp Proxy works well as a standalone proxy server, but it is especially useful when embedded in Selenium tests. A Server-Side Template Injection was identified in BrowserUp Proxy enabling attackers to inject arbitrary Java EL expressions, leading to unauthenticated Remote Code Execution (RCE) vulnerability. This has been patched in version 2.1.2.
CWE description: The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.
CVSS v3 base score: 10
Published: 2020-12-24T21:15Z
Repository: https://github.com/browserup/browserup-proxy
Language: Java
Files changed: browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/LongPositiveConstraint.java, browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/NotBlankConstraint.java, browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PatternConstraint.java, browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PortWithExistingProxyConstraint.java, browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/util/MessageSanitizer.java
Commit date: 2020-12-14 12:02:17 +0300
Commit message: Fix Critical Java EL Injection RCE vulnerability from GHSL-2020-213

Diff: diff --git a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/LongPositiveConstraint.java b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/LongPositiveConstraint.java
index 1ff0ff3..2750db1 100644
--- a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/LongPositiveConstraint.java
+++ b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/LongPositiveConstraint.java
@@ -6,10 +6,11 @@ import java.lang.annotation.RetentionPolicy;
 import javax.validation.Constraint;
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;
 import javax.validation.Payload;
 
+import com.browserup.bup.rest.validation.util.MessageSanitizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Retention(RetentionPolicy.RUNTIME)
 @Constraint(validatedBy = { LongPositiveConstraint.LongPositiveValidator.class })
@@ -39,16 +40,18 @@ public @interface LongPositiveConstraint {
       String errorMessage = "";
       try {
         longValue = Long.parseLong(value);
       } catch (NumberFormatException ex) {
         failed = true;
-        errorMessage = String.format("Invalid integer value: '%s'", value);
+        String escapedValue = MessageSanitizer.escape(value);
+        errorMessage = String.format("Invalid integer value: '%s'", escapedValue);
       }
 
       if (!failed && longValue < 0) {
         failed = true;
-        errorMessage = String.format("Expected positive integer value, got: '%s'", value);
+        String escapedValue = MessageSanitizer.escape(value);
+        errorMessage = String.format("Expected positive integer value, got: '%s'", escapedValue);
       }
 
       if (!failed) {
         return true;
       }
@@ -57,6 +60,6 @@ public @interface LongPositiveConstraint {
       context.buildConstraintViolationWithTemplate(errorMessage).addConstraintViolation();
 
       return false;
     }
   }
-}
\ No newline at end of file
+}
diff --git a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/NotBlankConstraint.java b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/NotBlankConstraint.java
index 6505d86..3b0cc40 100644
--- a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/NotBlankConstraint.java
+++ b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/NotBlankConstraint.java
@@ -6,10 +6,11 @@ import java.lang.annotation.RetentionPolicy;
 import javax.validation.Constraint;
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;
 import javax.validation.Payload;
 
+import com.browserup.bup.rest.validation.util.MessageSanitizer;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Retention(RetentionPolicy.RUNTIME)
@@ -34,13 +35,15 @@ public @interface NotBlankConstraint {
     @Override
     public boolean isValid(Object value, ConstraintValidatorContext context) {
       if (value != null && StringUtils.isNotEmpty(String.valueOf(value))) {
         return true;
       }
-      String errorMessage = String.format("Expected not empty value, got '%s'", value);
+
+      String escapedValue = MessageSanitizer.escape(value == null ? null : value.toString());
+      String errorMessage = String.format("Expected not empty value, got '%s'", escapedValue);
       LOG.warn(errorMessage);
 
       context.buildConstraintViolationWithTemplate(errorMessage).addConstraintViolation();
       return false;
     }
   }
-}
\ No newline at end of file
+}
diff --git a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PatternConstraint.java b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PatternConstraint.java
index cf68ee3..dc4c5dc 100644
--- a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PatternConstraint.java
+++ b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PatternConstraint.java
@@ -7,10 +7,11 @@ import java.util.regex.Pattern;
 import javax.validation.Constraint;
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;
 import javax.validation.Payload;
 
+import com.browserup.bup.rest.validation.util.MessageSanitizer;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Retention(RetentionPolicy.RUNTIME)
@@ -40,14 +41,15 @@ public @interface PatternConstraint {
 
       try {
         Pattern.compile(value);
         return true;
       } catch (Exception ex) {
-        String errorMessage = String.format("URL parameter '%s' is not a valid regexp", value);
+        String escapedValue = MessageSanitizer.escape(value);
+        String errorMessage = String.format("URL parameter '%s' is not a valid regexp", escapedValue);
         LOG.warn(errorMessage);
 
         context.buildConstraintViolationWithTemplate(errorMessage).addConstraintViolation();
       }
       return false;
     }
   }
-}
\ No newline at end of file
+}
diff --git a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PortWithExistingProxyConstraint.java b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PortWithExistingProxyConstraint.java
index 6468936..704f6ec 100644
--- a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PortWithExistingProxyConstraint.java
+++ b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/PortWithExistingProxyConstraint.java
@@ -9,10 +9,11 @@ import javax.validation.ConstraintValidatorContext;
 import javax.validation.Payload;
 import javax.ws.rs.core.Context;
 
 import com.browserup.bup.proxy.ProxyManager;
 
+import com.browserup.bup.rest.validation.util.MessageSanitizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Retention(RetentionPolicy.RUNTIME)
 @Constraint(validatedBy = { PortWithExistingProxyConstraint.PortWithExistingProxyConstraintValidator.class })
@@ -45,13 +46,14 @@ public @interface PortWithExistingProxyConstraint {
     public boolean isValid(Integer value, ConstraintValidatorContext context) {
       if (proxyManager.get(value) != null) {
         return true;
       }
 
-      String errorMessage = String.format("No proxy server found for specified port %d", value);
+      String escapedValue = MessageSanitizer.escape(value.toString());
+      String errorMessage = String.format("No proxy server found for specified port %s", escapedValue);
       LOG.warn(errorMessage);
 
       context.buildConstraintViolationWithTemplate(errorMessage).addPropertyNode(PARAM_NAME).addConstraintViolation();
       return false;
     }
   }
-}
\ No newline at end of file
+}
diff --git a/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/util/MessageSanitizer.java b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/util/MessageSanitizer.java
new file mode 100644
index 0000000..0459399
--- /dev/null
+++ b/browserup-proxy-rest/src/main/java/com/browserup/bup/rest/validation/util/MessageSanitizer.java
@@ -0,0 +1,33 @@
+package com.browserup.bup.rest.validation.util;
+/*
+ * Modifications Copyright (c) 2019 BrowserUp, Inc.
+ * Original from:
+ * https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/messageinterpolation/util/InterpolationHelper.java
+ */
+/*
+ * License: Apache License, Version 2.0
+ * See the license file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
+ */
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class MessageSanitizer {
+
+  public static final char BEGIN_CHAR = '{';
+  public static final char END_CHAR = '}';
+  public static final char EL_DESIGNATOR = '$';
+  public static final char ESCAPE_CHARACTER = '\\';
+
+  private static final Pattern ESCAPE_PATTERN = Pattern.compile( "([\\" + ESCAPE_CHARACTER + BEGIN_CHAR + END_CHAR + EL_DESIGNATOR + "])" );
+
+  private MessageSanitizer() {
+  }
+
+  public static String escape(String message) {
+    if ( message == null ) {
+      return null;
+    }
+    return ESCAPE_PATTERN.matcher( message ).replaceAll( Matcher.quoteReplacement( String.valueOf( ESCAPE_CHARACTER ) ) + "$1" );
+  }
+}

Vulnerable code:         errorMessage = String.format("Invalid integer value: '%s'", value);
        errorMessage = String.format("Expected positive integer value, got: '%s'", value);
}
      String errorMessage = String.format("Expected not empty value, got '%s'", value);
}
        String errorMessage = String.format("URL parameter '%s' is not a valid regexp", value);
}
      String errorMessage = String.format("No proxy server found for specified port %d", value);
}
Fixed code: import com.browserup.bup.rest.validation.util.MessageSanitizer;
        String escapedValue = MessageSanitizer.escape(value);
        errorMessage = String.format("Invalid integer value: '%s'", escapedValue);
        String escapedValue = MessageSanitizer.escape(value);
        errorMessage = String.format("Expected positive integer value, got: '%s'", escapedValue);
}
import com.browserup.bup.rest.validation.util.MessageSanitizer;

      String escapedValue = MessageSanitizer.escape(value == null ? null : value.toString());
      String errorMessage = String.format("Expected not empty value, got '%s'", escapedValue);
}
import com.browserup.bup.rest.validation.util.MessageSanitizer;
        String escapedValue = MessageSanitizer.escape(value);
        String errorMessage = String.format("URL parameter '%s' is not a valid regexp", escapedValue);
}
import com.browserup.bup.rest.validation.util.MessageSanitizer;
      String escapedValue = MessageSanitizer.escape(value.toString());
      String errorMessage = String.format("No proxy server found for specified port %s", escapedValue);
}
package com.browserup.bup.rest.validation.util;
/*
 * Modifications Copyright (c) 2019 BrowserUp, Inc.
 * Original from:
 * https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/messageinterpolation/util/InterpolationHelper.java
 */
/*
 * License: Apache License, Version 2.0
 * See the license file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageSanitizer {

  public static final char BEGIN_CHAR = '{';
  public static final char END_CHAR = '}';
  public static final char EL_DESIGNATOR = '$';
  public static final char ESCAPE_CHARACTER = '\\';

  private static final Pattern ESCAPE_PATTERN = Pattern.compile( "([\\" + ESCAPE_CHARACTER + BEGIN_CHAR + END_CHAR + EL_DESIGNATOR + "])" );

  private MessageSanitizer() {
  }

  public static String escape(String message) {
    if ( message == null ) {
      return null;
    }
    return ESCAPE_PATTERN.matcher( message ).replaceAll( Matcher.quoteReplacement( String.valueOf( ESCAPE_CHARACTER ) ) + "$1" );
  }
}