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.

OAuth2AuthenticationParametersImplTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.authentication;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Optional;
  25. import javax.annotation.Nullable;
  26. import javax.servlet.http.Cookie;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.mockito.ArgumentCaptor;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.mockito.ArgumentMatchers.any;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.never;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.when;
  39. @RunWith(DataProviderRunner.class)
  40. public class OAuth2AuthenticationParametersImplTest {
  41. private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
  42. private final ArgumentCaptor<Cookie> cookieArgumentCaptor = ArgumentCaptor.forClass(Cookie.class);
  43. private final HttpServletResponse response = mock(HttpServletResponse.class);
  44. private final HttpServletRequest request = mock(HttpServletRequest.class);
  45. private final OAuth2AuthenticationParameters underTest = new OAuth2AuthenticationParametersImpl();
  46. @Before
  47. public void setUp() {
  48. when(request.getContextPath()).thenReturn("");
  49. }
  50. @Test
  51. public void init_create_cookie() {
  52. when(request.getParameter("return_to")).thenReturn("/admin/settings");
  53. underTest.init(request, response);
  54. verify(response).addCookie(cookieArgumentCaptor.capture());
  55. Cookie cookie = cookieArgumentCaptor.getValue();
  56. assertThat(cookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
  57. assertThat(cookie.getValue()).isNotEmpty();
  58. assertThat(cookie.getPath()).isEqualTo("/");
  59. assertThat(cookie.isHttpOnly()).isTrue();
  60. assertThat(cookie.getMaxAge()).isEqualTo(300);
  61. assertThat(cookie.getSecure()).isFalse();
  62. }
  63. @Test
  64. public void init_does_not_create_cookie_when_no_parameter() {
  65. underTest.init(request, response);
  66. verify(response, never()).addCookie(any(Cookie.class));
  67. }
  68. @Test
  69. public void init_does_not_create_cookie_when_parameters_are_empty() {
  70. when(request.getParameter("return_to")).thenReturn("");
  71. when(request.getParameter("allowEmailShift")).thenReturn("");
  72. underTest.init(request, response);
  73. verify(response, never()).addCookie(any(Cookie.class));
  74. }
  75. @Test
  76. public void init_does_not_create_cookie_when_parameters_are_null() {
  77. when(request.getParameter("return_to")).thenReturn(null);
  78. when(request.getParameter("allowEmailShift")).thenReturn(null);
  79. underTest.init(request, response);
  80. verify(response, never()).addCookie(any(Cookie.class));
  81. }
  82. @Test
  83. @DataProvider({"http://example.com", "/\t/example.com", "//local_file", "/\\local_file", "something_else"})
  84. public void get_return_to_is_not_set_when_not_local(String url) {
  85. when(request.getParameter("return_to")).thenReturn(url);
  86. assertThat(underTest.getReturnTo(request)).isEmpty();
  87. }
  88. @Test
  89. public void get_return_to_parameter() {
  90. when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/settings\"}")});
  91. Optional<String> redirection = underTest.getReturnTo(request);
  92. assertThat(redirection).contains("/settings");
  93. }
  94. @Test
  95. public void get_return_to_is_empty_when_no_cookie() {
  96. when(request.getCookies()).thenReturn(new Cookie[]{});
  97. Optional<String> redirection = underTest.getReturnTo(request);
  98. assertThat(redirection).isEmpty();
  99. }
  100. @Test
  101. public void get_return_to_is_empty_when_no_value() {
  102. when(request.getCookies()).thenReturn(new Cookie[]{new Cookie(AUTHENTICATION_COOKIE_NAME, "{}")});
  103. Optional<String> redirection = underTest.getReturnTo(request);
  104. assertThat(redirection).isEmpty();
  105. }
  106. @Test
  107. public void delete() {
  108. when(request.getCookies()).thenReturn(new Cookie[]{new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/settings\"}")});
  109. underTest.delete(request, response);
  110. verify(response).addCookie(cookieArgumentCaptor.capture());
  111. Cookie updatedCookie = cookieArgumentCaptor.getValue();
  112. assertThat(updatedCookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
  113. assertThat(updatedCookie.getValue()).isNull();
  114. assertThat(updatedCookie.getPath()).isEqualTo("/");
  115. assertThat(updatedCookie.getMaxAge()).isZero();
  116. }
  117. @DataProvider
  118. public static Object[][] payloadToSanitizeAndExpectedOutcome() {
  119. return new Object[][]{
  120. {generatePath("/admin/settings"), "/admin/settings"},
  121. {generatePath("/admin/../../settings"), "/settings"},
  122. {generatePath("/admin/../settings"), "/settings"},
  123. {generatePath("/admin/settings/.."), "/admin"},
  124. {generatePath("/admin/..%2fsettings/"), "/settings"},
  125. {generatePath("/admin/%2e%2e%2fsettings/"), "/settings"},
  126. {generatePath("../admin/settings"), null},
  127. };
  128. }
  129. private static String generatePath(String returnTo) {
  130. return "{\"return_to\":\"" + returnTo + "\"}";
  131. }
  132. @Test
  133. @UseDataProvider("payloadToSanitizeAndExpectedOutcome")
  134. public void getReturnTo_whenContainingPathTraversalCharacters_sanitizeThem(String payload, @Nullable String expectedSanitizedUrl) {
  135. when(request.getCookies()).thenReturn(new Cookie[]{wrapCookie(AUTHENTICATION_COOKIE_NAME, payload)});
  136. Optional<String> redirection = underTest.getReturnTo(request);
  137. assertThat(redirection).isEqualTo(Optional.ofNullable(expectedSanitizedUrl));
  138. }
  139. private Cookie wrapCookie(String name, String value) {
  140. return new javax.servlet.http.Cookie(name, value);
  141. }
  142. }