Add an additional layer of protection in case app fails to protect against an XSS.
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 8 Jun 2008 11:30:44 +0000 (11:30 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 8 Jun 2008 11:30:44 +0000 (11:30 +0000)
Copied filter code to jasper module so no new dependency is created.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@664483 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/jasper/security/SecurityUtil.java
java/org/apache/jasper/servlet/JspServlet.java

index 657a872..22d0668 100644 (file)
@@ -40,5 +40,42 @@ public final class SecurityUtil{
         return false;
     }
     
-    
+
+    /**
+     * Filter the specified message string for characters that are sensitive
+     * in HTML.  This avoids potential attacks caused by including JavaScript
+     * codes in the request URL that is often reported in error messages.
+     *
+     * @param message The message string to be filtered
+     */
+    public static String filter(String message) {
+
+        if (message == null)
+            return (null);
+
+        char content[] = new char[message.length()];
+        message.getChars(0, message.length(), content, 0);
+        StringBuffer result = new StringBuffer(content.length + 50);
+        for (int i = 0; i < content.length; i++) {
+            switch (content[i]) {
+            case '<':
+                result.append("&lt;");
+                break;
+            case '>':
+                result.append("&gt;");
+                break;
+            case '&':
+                result.append("&amp;");
+                break;
+            case '"':
+                result.append("&quot;");
+                break;
+            default:
+                result.append(content[i]);
+            }
+        }
+        return (result.toString());
+
+    }
+
 }
index c345d8e..1c12003 100644 (file)
@@ -35,6 +35,7 @@ import org.apache.jasper.EmbeddedServletOptions;
 import org.apache.jasper.Options;
 import org.apache.jasper.compiler.JspRuntimeContext;
 import org.apache.jasper.compiler.Localizer;
+import org.apache.jasper.security.SecurityUtil;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 
@@ -311,8 +312,12 @@ public class JspServlet extends HttpServlet implements PeriodicEventListener {
                         if (includeRequestUri != null) {
                             // This file was included. Throw an exception as
                             // a response.sendError() will be ignored
-                            throw new ServletException(Localizer.getMessage(
-                                    "jsp.error.file.not.found",jspUri));
+                            String msg = Localizer.getMessage(
+                                    "jsp.error.file.not.found",jspUri);
+                            // Strictly, filtering this is an application
+                            // responsibility but just in case...
+                            throw new ServletException(
+                                    SecurityUtil.filter(msg));
                         } else {
                             try {
                                 response.sendError(