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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 org.junit.Before;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.mockito.ArgumentCaptor;
  30. import org.sonar.api.server.http.Cookie;
  31. import org.sonar.api.server.http.HttpRequest;
  32. import org.sonar.api.server.http.HttpResponse;
  33. import org.sonar.server.http.JavaxHttpRequest;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.ArgumentMatchers.any;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.never;
  38. import static org.mockito.Mockito.verify;
  39. import static org.mockito.Mockito.when;
  40. @RunWith(DataProviderRunner.class)
  41. public class OAuth2AuthenticationParametersImplTest {
  42. private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
  43. private final ArgumentCaptor<Cookie> cookieArgumentCaptor = ArgumentCaptor.forClass(Cookie.class);
  44. private final HttpResponse response = mock(HttpResponse.class);
  45. private final HttpRequest request = mock(HttpRequest.class);
  46. private final OAuth2AuthenticationParameters underTest = new OAuth2AuthenticationParametersImpl();
  47. @Before
  48. public void setUp() {
  49. when(request.getContextPath()).thenReturn("");
  50. }
  51. @Test
  52. public void init_create_cookie() {
  53. when(request.getParameter("return_to")).thenReturn("/admin/settings");
  54. underTest.init(request, response);
  55. verify(response).addCookie(cookieArgumentCaptor.capture());
  56. Cookie cookie = cookieArgumentCaptor.getValue();
  57. assertThat(cookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
  58. assertThat(cookie.getValue()).isNotEmpty();
  59. assertThat(cookie.getPath()).isEqualTo("/");
  60. assertThat(cookie.isHttpOnly()).isTrue();
  61. assertThat(cookie.getMaxAge()).isEqualTo(300);
  62. assertThat(cookie.isSecure()).isFalse();
  63. }
  64. @Test
  65. public void init_does_not_create_cookie_when_no_parameter() {
  66. underTest.init(request, response);
  67. verify(response, never()).addCookie(any(Cookie.class));
  68. }
  69. @Test
  70. public void init_does_not_create_cookie_when_parameters_are_empty() {
  71. when(request.getParameter("return_to")).thenReturn("");
  72. when(request.getParameter("allowEmailShift")).thenReturn("");
  73. underTest.init(request, response);
  74. verify(response, never()).addCookie(any(Cookie.class));
  75. }
  76. @Test
  77. public void init_does_not_create_cookie_when_parameters_are_null() {
  78. when(request.getParameter("return_to")).thenReturn(null);
  79. when(request.getParameter("allowEmailShift")).thenReturn(null);
  80. underTest.init(request, response);
  81. verify(response, never()).addCookie(any(Cookie.class));
  82. }
  83. @Test
  84. @DataProvider({"http://example.com", "/\t/example.com", "//local_file", "/\\local_file", "something_else"})
  85. public void get_return_to_is_not_set_when_not_local(String url) {
  86. when(request.getParameter("return_to")).thenReturn(url);
  87. assertThat(underTest.getReturnTo(request)).isEmpty();
  88. }
  89. @Test
  90. public void get_return_to_parameter() {
  91. when(request.getCookies()).thenReturn(new Cookie[]{wrapCookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/admin/settings\"}")});
  92. Optional<String> redirection = underTest.getReturnTo(request);
  93. assertThat(redirection).contains("/admin/settings");
  94. }
  95. @Test
  96. public void get_return_to_is_empty_when_no_cookie() {
  97. when(request.getCookies()).thenReturn(new Cookie[]{});
  98. Optional<String> redirection = underTest.getReturnTo(request);
  99. assertThat(redirection).isEmpty();
  100. }
  101. @Test
  102. public void get_return_to_is_empty_when_no_value() {
  103. when(request.getCookies()).thenReturn(new Cookie[]{wrapCookie(AUTHENTICATION_COOKIE_NAME, "{}")});
  104. Optional<String> redirection = underTest.getReturnTo(request);
  105. assertThat(redirection).isEmpty();
  106. }
  107. @Test
  108. public void delete() {
  109. when(request.getCookies()).thenReturn(new Cookie[]{wrapCookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/admin/settings\"}")});
  110. underTest.delete(request, response);
  111. verify(response).addCookie(cookieArgumentCaptor.capture());
  112. Cookie updatedCookie = cookieArgumentCaptor.getValue();
  113. assertThat(updatedCookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
  114. assertThat(updatedCookie.getValue()).isNull();
  115. assertThat(updatedCookie.getPath()).isEqualTo("/");
  116. assertThat(updatedCookie.getMaxAge()).isZero();
  117. }
  118. @DataProvider
  119. public static Object[][] payloadToSanitizeAndExpectedOutcome() {
  120. return new Object[][]{
  121. {generatePath("/admin/settings"), "/admin/settings"},
  122. {generatePath("/admin/../../settings"), "/settings"},
  123. {generatePath("/admin/../settings"), "/settings"},
  124. {generatePath("/admin/settings/.."), "/admin"},
  125. {generatePath("/admin/..%2fsettings/"), "/settings"},
  126. {generatePath("/admin/%2e%2e%2fsettings/"), "/settings"},
  127. {generatePath("../admin/settings"), null},
  128. };
  129. }
  130. private static String generatePath(String returnTo) {
  131. return "{\"return_to\":\"" + returnTo + "\"}";
  132. }
  133. @Test
  134. @UseDataProvider("payloadToSanitizeAndExpectedOutcome")
  135. public void getReturnTo_whenContainingPathTraversalCharacters_sanitizeThem(String payload, @Nullable String expectedSanitizedUrl) {
  136. when(request.getCookies()).thenReturn(new Cookie[]{wrapCookie(AUTHENTICATION_COOKIE_NAME, payload)});
  137. Optional<String> redirection = underTest.getReturnTo(request);
  138. assertThat(redirection).isEqualTo(Optional.ofNullable(expectedSanitizedUrl));
  139. }
  140. private JavaxHttpRequest.JavaxCookie wrapCookie(String name, String value) {
  141. return new JavaxHttpRequest.JavaxCookie(new javax.servlet.http.Cookie(name, value));
  142. }
  143. }