]> source.dussan.org Git - vaadin-framework.git/commitdiff
When InputStream cannot be opened while writing static resource response, display...
authorFabian Lange <lange.fabian@gmail.com>
Thu, 13 Jun 2013 13:11:39 +0000 (15:11 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 10 Jul 2013 06:07:31 +0000 (06:07 +0000)
A security audit revealed that it is possible to trigger an error 500 with
stack trace by just trying a directory traversal. An example of this can be
found in the sampler: http://demo.vaadin.com/sampler/VAADIN/widgetsets/
While there are other scenarios that can produce exceptions, in this place
Vaadin can handle it more graceful by just catching the exception and
returning 404.

Change-Id: Iec68d81d3bca365ec133737a9cd3e3b825d192b2

server/src/com/vaadin/server/VaadinServlet.java

index 94601d6599bcc290c415f09cd4e1de2a6a4d7ff5..803a903341373e92408112dc3ad50a7e091fcd62 100644 (file)
@@ -16,6 +16,7 @@
 package com.vaadin.server;
 
 import java.io.BufferedWriter;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -645,19 +646,19 @@ public class VaadinServlet extends HttpServlet implements Constants {
                             "Failed to find out last modified timestamp. Continuing without it.",
                             e);
         } finally {
-            if (connection instanceof URLConnection) {
-                try {
-                    // Explicitly close the input stream to prevent it
-                    // from remaining hanging
-                    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4257700
-                    InputStream is = connection.getInputStream();
-                    if (is != null) {
-                        is.close();
-                    }
-                } catch (IOException e) {
-                    getLogger().log(Level.INFO,
-                            "Error closing URLConnection input stream", e);
+            try {
+                // Explicitly close the input stream to prevent it
+                // from remaining hanging
+                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4257700
+                InputStream is = connection.getInputStream();
+                if (is != null) {
+                    is.close();
                 }
+            } catch (FileNotFoundException e) {
+                // Not logging when the file does not exist.
+            } catch (IOException e) {
+                getLogger().log(Level.INFO,
+                        "Error closing URLConnection input stream", e);
             }
         }
 
@@ -720,14 +721,17 @@ public class VaadinServlet extends HttpServlet implements Constants {
             // prevent it from hanging, but that is done below.
         }
 
-        InputStream is = connection.getInputStream();
+        InputStream is = null;
         try {
+            is = connection.getInputStream();
             final OutputStream os = response.getOutputStream();
             final byte buffer[] = new byte[DEFAULT_BUFFER_SIZE];
             int bytes;
             while ((bytes = is.read(buffer)) >= 0) {
                 os.write(buffer, 0, bytes);
             }
+        } catch (FileNotFoundException e) {
+            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
         } finally {
             if (is != null) {
                 is.close();