]> source.dussan.org Git - vaadin-framework.git/commitdiff
Store security key in VaadinSession (#11717)
authorLeif Åstrand <leif@vaadin.com>
Thu, 25 Apr 2013 13:12:35 +0000 (16:12 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 26 Apr 2013 13:06:23 +0000 (13:06 +0000)
* Also removes the WRITE_SECURITY_TOKEN_FLAG flag that was defined twice
but never set

Change-Id: I02d172b7ccd230df7c59b3b17227235bea9d2e7d

server/src/com/vaadin/server/LegacyCommunicationManager.java
server/src/com/vaadin/server/VaadinService.java
server/src/com/vaadin/server/VaadinSession.java
server/src/com/vaadin/server/communication/ServerRpcHandler.java
server/src/com/vaadin/server/communication/UIInitHandler.java
server/src/com/vaadin/server/communication/UidlRequestHandler.java

index 7dea5890e9d210cf09eeac2cb538b0d2c85dd2c2..c0194db2436f2bca2af63287f6cd104a300dd446 100644 (file)
@@ -30,7 +30,6 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
-import java.util.UUID;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -62,9 +61,6 @@ import com.vaadin.ui.UI;
 @SuppressWarnings("serial")
 public class LegacyCommunicationManager implements Serializable {
 
-    // TODO PUSH move
-    public static final String WRITE_SECURITY_TOKEN_FLAG = "writeSecurityToken";
-
     // TODO Refactor (#11410)
     private final HashMap<Integer, ClientCache> uiToClientCache = new HashMap<Integer, ClientCache>();
 
@@ -99,42 +95,6 @@ public class LegacyCommunicationManager implements Serializable {
         return session;
     }
 
-    /**
-     * Gets the security key (and generates one if needed) as UIDL.
-     * 
-     * @param request
-     * @return the security key UIDL or "" if the feature is turned off
-     */
-    public String getSecurityKeyUIDL(VaadinRequest request) {
-        final String seckey = getSecurityKey(request);
-        if (seckey != null) {
-            return "\"" + ApplicationConstants.UIDL_SECURITY_TOKEN_ID + "\":\""
-                    + seckey + "\",";
-        } else {
-            return "";
-        }
-    }
-
-    /**
-     * Gets the security key (and generates one if needed).
-     * 
-     * @param request
-     * @return the security key
-     */
-    protected String getSecurityKey(VaadinRequest request) {
-        String seckey = null;
-        WrappedSession session = request.getWrappedSession();
-        seckey = (String) session
-                .getAttribute(ApplicationConstants.UIDL_SECURITY_TOKEN_ID);
-        if (seckey == null) {
-            seckey = UUID.randomUUID().toString();
-            session.setAttribute(ApplicationConstants.UIDL_SECURITY_TOKEN_ID,
-                    seckey);
-        }
-
-        return seckey;
-    }
-
     /**
      * @deprecated As of 7.1. See #11411.
      */
index 6c540aec42806921155304292352ab29fa990919..c6fdca35f01e47cb9bba84c27a591989e4655793 100644 (file)
@@ -52,7 +52,6 @@ import com.vaadin.server.communication.HeartbeatHandler;
 import com.vaadin.server.communication.PublishedFileHandler;
 import com.vaadin.server.communication.SessionRequestHandler;
 import com.vaadin.server.communication.UidlRequestHandler;
-import com.vaadin.shared.ApplicationConstants;
 import com.vaadin.shared.JsonConstants;
 import com.vaadin.shared.ui.ui.UIConstants;
 import com.vaadin.ui.UI;
@@ -1569,10 +1568,9 @@ public abstract class VaadinService implements Serializable {
 
         if (session.getService().getDeploymentConfiguration()
                 .isXsrfProtectionEnabled()) {
-            String keyInSession = (String) session.getSession().getAttribute(
-                    ApplicationConstants.UIDL_SECURITY_TOKEN_ID);
+            String sessionToken = session.getCsrfToken();
 
-            if (keyInSession == null || !keyInSession.equals(requestToken)) {
+            if (sessionToken == null || !sessionToken.equals(requestToken)) {
                 return false;
             }
         }
index 9c803924e0e23ad28d2883a3a619d90d5fc5043f..57e9076342e11b72366314fee929b034ee0e1e04 100644 (file)
@@ -25,6 +25,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.UUID;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 import java.util.logging.Logger;
@@ -590,6 +591,8 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {
 
     private int connectorIdSequence = 0;
 
+    private final String csrfToken = UUID.randomUUID().toString();
+
     /**
      * Generate an id for the given Connector. Connectors must not call this
      * method more than once, the first time they need an id.
@@ -1092,4 +1095,16 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {
 
     }
 
+    /**
+     * Gets the CSRF token (aka double submit cookie) that is used to protect
+     * against Cross Site Request Forgery attacks.
+     * 
+     * @since 7.1
+     * @return the csrf token string
+     */
+    public String getCsrfToken() {
+        assert hasLock();
+        return csrfToken;
+    }
+
 }
index 62949615fb11eed0578448c64a15db350fbd0d5f..e0e7494ca8e54c27a78c3bea0e8d9d3894d00081 100644 (file)
@@ -69,10 +69,6 @@ public class ServerRpcHandler implements Serializable {
 
     private static final int MAX_BUFFER_SIZE = 64 * 1024;
 
-    // flag used in the request to indicate that the security token should be
-    // written to the response
-    private static final String WRITE_SECURITY_TOKEN_FLAG = "writeSecurityToken";
-
     /**
      * Reads JSON containing zero or more serialized RPC calls (including legacy
      * variable changes) and executes the calls.
index 7c8fc3a0d896a02f340c52b73a25e50a08b2c69d..97aaa6bd74486df4d6188d23e929e37025f0a73f 100644 (file)
@@ -37,6 +37,7 @@ import com.vaadin.server.VaadinRequest;
 import com.vaadin.server.VaadinResponse;
 import com.vaadin.server.VaadinService;
 import com.vaadin.server.VaadinSession;
+import com.vaadin.shared.ApplicationConstants;
 import com.vaadin.shared.communication.PushMode;
 import com.vaadin.shared.ui.ui.UIConstants;
 import com.vaadin.ui.UI;
@@ -267,9 +268,10 @@ public abstract class UIInitHandler extends SynchronizedRequestHandler {
         StringWriter writer = new StringWriter();
         try {
             writer.write("{");
-            if (uI.getSession().getConfiguration().isXsrfProtectionEnabled()) {
-                writer.write(uI.getSession().getCommunicationManager()
-                        .getSecurityKeyUIDL(request));
+
+            VaadinSession session = uI.getSession();
+            if (session.getConfiguration().isXsrfProtectionEnabled()) {
+                writer.write(getSecurityKeyUIDL(session));
             }
             new UidlWriter().write(uI, writer, true, false, false);
             writer.write("}");
@@ -282,6 +284,20 @@ public abstract class UIInitHandler extends SynchronizedRequestHandler {
         }
     }
 
+    /**
+     * Gets the security key (and generates one if needed) as UIDL.
+     * 
+     * @param session
+     *            the vaadin session to which the security key belongs
+     * @return the security key UIDL or "" if the feature is turned off
+     */
+    private static String getSecurityKeyUIDL(VaadinSession session) {
+        String seckey = session.getCsrfToken();
+
+        return "\"" + ApplicationConstants.UIDL_SECURITY_TOKEN_ID + "\":\""
+                + seckey + "\",";
+    }
+
     private static final Logger getLogger() {
         return Logger.getLogger(UIInitHandler.class.getName());
     }
index 04ff5f9e87c7fdf6e31a5ca0db1352dbcb12b9aa..73ff92f8bde0572565f31753b232bb0f10a9a180 100644 (file)
@@ -27,7 +27,6 @@ import org.json.JSONException;
 
 import com.vaadin.server.ClientConnector;
 import com.vaadin.server.Constants;
-import com.vaadin.server.LegacyCommunicationManager;
 import com.vaadin.server.LegacyCommunicationManager.InvalidUIDLSecurityKeyException;
 import com.vaadin.server.ServletPortletHelper;
 import com.vaadin.server.SessionExpiredHandler;
@@ -169,15 +168,6 @@ public class UidlRequestHandler extends SynchronizedRequestHandler implements
             throws IOException, JSONException {
         openJsonMessage(writer, response);
 
-        // security key
-        Object writeSecurityTokenFlag = request
-                .getAttribute(LegacyCommunicationManager.WRITE_SECURITY_TOKEN_FLAG);
-
-        if (writeSecurityTokenFlag != null) {
-            writer.write(ui.getSession().getCommunicationManager()
-                    .getSecurityKeyUIDL(request));
-        }
-
         new UidlWriter().write(ui, writer, repaintAll, analyzeLayouts, false);
 
         closeJsonMessage(writer);