You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CsrfTokenMissingTestServer.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.server;
  17. import java.util.UUID;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.servlet.http.HttpServletRequest;
  21. import org.easymock.EasyMock;
  22. import org.junit.Assert;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import com.vaadin.server.ServiceException;
  26. import com.vaadin.server.VaadinService;
  27. import com.vaadin.server.VaadinServlet;
  28. import com.vaadin.server.VaadinServletRequest;
  29. import com.vaadin.server.VaadinServletService;
  30. import com.vaadin.server.VaadinSession;
  31. import com.vaadin.server.communication.ServerRpcHandler.RpcRequest;
  32. import com.vaadin.shared.ApplicationConstants;
  33. import com.vaadin.tests.util.AlwaysLockedVaadinSession;
  34. import com.vaadin.tests.util.MockDeploymentConfiguration;
  35. import elemental.json.JsonException;
  36. /**
  37. * Test the actual csrf token validation by the server.
  38. *
  39. * @since
  40. * @author Vaadin Ltd
  41. */
  42. public class CsrfTokenMissingTestServer {
  43. // Dummy fields just to run the test.
  44. private VaadinServlet mockServlet;
  45. // The mock deployment configuration.
  46. private MockDeploymentConfiguration mockDeploymentConfiguration;
  47. private VaadinServletService mockService;
  48. // The mock UI session.
  49. private VaadinSession mockSession;
  50. // The mock vaadin request.
  51. private VaadinServletRequest vaadinRequest;
  52. /**
  53. * Initialize the mock servlet and other stuff for our tests.
  54. */
  55. @Before
  56. public void initMockStuff() throws ServiceException {
  57. mockServlet = new VaadinServlet();
  58. mockDeploymentConfiguration = new MockDeploymentConfiguration();
  59. mockService = new VaadinServletService(mockServlet,
  60. mockDeploymentConfiguration);
  61. mockSession = new AlwaysLockedVaadinSession(mockService);
  62. vaadinRequest = new VaadinServletRequest(
  63. EasyMock.createMock(HttpServletRequest.class), mockService);
  64. }
  65. private enum TokenType {
  66. MISSING, INVALID, VALID
  67. }
  68. private TokenType tokenType;
  69. private String invalidToken;
  70. public String getInvalidToken() {
  71. if (invalidToken == null) {
  72. // Just making sure this will never be in the same format as a valid
  73. // token.
  74. invalidToken = UUID.randomUUID().toString().substring(1);
  75. }
  76. return invalidToken;
  77. }
  78. private String getValidToken() {
  79. return mockSession.getCsrfToken();
  80. }
  81. /*
  82. * Gets the payload with the default token.
  83. */
  84. private String getPayload() {
  85. switch (tokenType) {
  86. case MISSING:
  87. return getPayload(null);
  88. case INVALID:
  89. return getPayload(getInvalidToken());
  90. case VALID:
  91. return getPayload(getValidToken());
  92. }
  93. return null;
  94. }
  95. /*
  96. * Gets the payload with the specified token.
  97. */
  98. private String getPayload(String token) {
  99. return "{"
  100. + (token != null ? "\"csrfToken\":" + "\"" + token + "\", "
  101. : "")
  102. + "\"rpc\":[[\"0\",\"com.vaadin.shared.ui.ui.UIServerRpc\",\"resize\",[\"449\",\"1155\",\"1155\",\"449\"]],[\"4\",\"com.vaadin.shared.ui.button.ButtonServerRpc\",\"click\",[{\"clientY\":\"53\", \"clientX\":\"79\", \"shiftKey\":false, \"button\":\"LEFT\", \"ctrlKey\":false, \"type\":\"1\", \"metaKey\":false, \"altKey\":false, \"relativeY\":\"17\", \"relativeX\":\"61\"}]]], \"syncId\":1}";
  103. }
  104. /*
  105. * Init the test parameters.
  106. */
  107. private void initTest(boolean enableSecurity, TokenType tokenType) {
  108. mockDeploymentConfiguration.setXsrfProtectionEnabled(enableSecurity);
  109. this.tokenType = tokenType;
  110. }
  111. /*
  112. * Create the requets.
  113. */
  114. private RpcRequest createRequest() {
  115. try {
  116. return new RpcRequest(getPayload(), vaadinRequest);
  117. } catch (JsonException e) {
  118. LOGGER.log(Level.SEVERE, "", e);
  119. Assert.assertTrue(false);
  120. return null;
  121. }
  122. }
  123. /*
  124. * Gets whether the token from the request is the default one.
  125. */
  126. private boolean isDefaultToken(RpcRequest rpcRequest) {
  127. return ApplicationConstants.CSRF_TOKEN_DEFAULT_VALUE.equals(rpcRequest
  128. .getCsrfToken());
  129. }
  130. /*
  131. * Gets whether the token from the request is the invalid one.
  132. */
  133. private boolean isInvalidToken(RpcRequest rpcRequest) {
  134. return getInvalidToken().equals(rpcRequest.getCsrfToken());
  135. }
  136. /*
  137. * Gets whether the token from the request is the valid one.
  138. */
  139. private boolean isValidToken(RpcRequest rpcRequest) {
  140. return getValidToken().equals(rpcRequest.getCsrfToken());
  141. }
  142. /*
  143. * Gets whether the token from the request is valid.
  144. */
  145. private boolean isRequestValid(RpcRequest rpcRequest) {
  146. return VaadinService.isCsrfTokenValid(mockSession,
  147. rpcRequest.getCsrfToken());
  148. }
  149. private static Logger LOGGER = Logger
  150. .getLogger(CsrfTokenMissingTestServer.class.getName());
  151. static {
  152. LOGGER.setLevel(Level.ALL);
  153. }
  154. @Test
  155. public void securityOnAndNoToken() {
  156. initTest(true, TokenType.MISSING);
  157. RpcRequest rpcRequest = createRequest();
  158. Assert.assertTrue(isDefaultToken(rpcRequest));
  159. Assert.assertFalse(isRequestValid(rpcRequest));
  160. }
  161. @Test
  162. public void securityOffAndNoToken() {
  163. initTest(false, TokenType.MISSING);
  164. RpcRequest rpcRequest = createRequest();
  165. Assert.assertTrue(isDefaultToken(rpcRequest));
  166. Assert.assertTrue(isRequestValid(rpcRequest));
  167. }
  168. @Test
  169. public void securityOnAndInvalidToken() {
  170. initTest(true, TokenType.INVALID);
  171. RpcRequest rpcRequest = createRequest();
  172. Assert.assertTrue(isInvalidToken(rpcRequest));
  173. Assert.assertFalse(isRequestValid(rpcRequest));
  174. }
  175. @Test
  176. public void securityOffAndInvalidToken() {
  177. initTest(false, TokenType.INVALID);
  178. RpcRequest rpcRequest = createRequest();
  179. Assert.assertTrue(isInvalidToken(rpcRequest));
  180. Assert.assertTrue(isRequestValid(rpcRequest));
  181. }
  182. @Test
  183. public void securityOnAndValidToken() {
  184. initTest(true, TokenType.VALID);
  185. RpcRequest rpcRequest = createRequest();
  186. Assert.assertTrue(isValidToken(rpcRequest));
  187. Assert.assertTrue(isRequestValid(rpcRequest));
  188. }
  189. @Test
  190. public void securityOffAndValidToken() {
  191. initTest(false, TokenType.VALID);
  192. RpcRequest rpcRequest = createRequest();
  193. Assert.assertTrue(isValidToken(rpcRequest));
  194. Assert.assertTrue(isRequestValid(rpcRequest));
  195. }
  196. }