import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
+import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
private static final int FIVE_MINUTES_IN_SECONDS = 5 * 60;
+ private static final Pattern VALID_RETURN_TO = Pattern.compile("^/\\w.*");
/**
* The HTTP parameter that contains the path where the user should be redirect to.
if (Strings.isNullOrEmpty(url)) {
return empty();
}
- if (url.startsWith("//") || url.startsWith("/\\")) {
- return empty();
- }
- if (!url.startsWith("/")) {
+
+ String sanitizedUrl = url.trim();
+ boolean isValidUrl = VALID_RETURN_TO.matcher(sanitizedUrl).matches();
+ if (!isValidUrl) {
return empty();
}
- return Optional.of(url);
- }
+ return Optional.of(sanitizedUrl);
+ }
}
*/
package org.sonar.server.authentication;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import java.util.Optional;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+@RunWith(DataProviderRunner.class)
public class OAuth2AuthenticationParametersImplTest {
private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
}
@Test
- public void return_to_is_not_set_when_not_local() {
- when(request.getParameter("return_to")).thenReturn("http://external_url");
- underTest.init(request, response);
- verify(response, never()).addCookie(any());
+ @DataProvider({"http://example.com", "/\t/example.com", "//local_file", "/\\local_file", "something_else"})
+ public void return_to_is_not_set_when_not_local(String url) {
+ when(request.getParameter("return_to")).thenReturn(url);
- when(request.getParameter("return_to")).thenReturn("//local_file");
underTest.init(request, response);
- verify(response, never()).addCookie(any());
- when(request.getParameter("return_to")).thenReturn("/\\local_file");
- underTest.init(request, response);
- verify(response, never()).addCookie(any());
-
- when(request.getParameter("return_to")).thenReturn("something_else");
- underTest.init(request, response);
verify(response, never()).addCookie(any());
}