-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine): add support for url redirection param (#3013)
covers [BPM-85](https://bonitasoft.atlassian.net/browse/BPM-85)
- Loading branch information
Showing
5 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...-web-server/src/main/java/org/bonitasoft/console/common/server/filter/RedirectFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (C) 2024 Bonitasoft S.A. | ||
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble | ||
* This library is free software; you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation | ||
* version 2.1 of the License. | ||
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* You should have received a copy of the GNU Lesser General Public License along with this | ||
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth | ||
* Floor, Boston, MA 02110-1301, USA. | ||
**/ | ||
package org.bonitasoft.console.common.server.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.bonitasoft.console.common.server.auth.AuthenticationManager; | ||
|
||
/** | ||
* Filter that redirects to a URL specified as a parameter in the request. | ||
* The URL must be relative to the current domain. | ||
* | ||
* @author Haroun EL ALAMI | ||
*/ | ||
@Slf4j | ||
public class RedirectFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
|
||
final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; | ||
final HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; | ||
|
||
String redirectUrl = httpRequest.getParameter(AuthenticationManager.REDIRECT_URL); | ||
if (StringUtils.isNoneBlank(redirectUrl)) { | ||
// avoid redirecting to a different domain | ||
if (!redirectUrl.contains("//")) { | ||
httpResponse.sendRedirect(redirectUrl); | ||
return; | ||
} | ||
// If the redirectUrl is not valid | ||
log.warn("Invalid redirect URL: {}", redirectUrl); | ||
} | ||
|
||
filterChain.doFilter(servletRequest, servletResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...-server/src/test/java/org/bonitasoft/console/common/server/filter/RedirectFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (C) 2024 Bonitasoft S.A. | ||
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble | ||
* This library is free software; you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation | ||
* version 2.1 of the License. | ||
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* You should have received a copy of the GNU Lesser General Public License along with this | ||
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth | ||
* Floor, Boston, MA 02110-1301, USA. | ||
**/ | ||
package org.bonitasoft.console.common.server.filter; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class RedirectFilterTest { | ||
|
||
@Mock | ||
private HttpServletRequest httpRequest; | ||
|
||
@Mock | ||
private HttpServletResponse httpResponse; | ||
|
||
@Mock | ||
private FilterChain filterChain; | ||
|
||
private RedirectFilter redirectFilter; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
redirectFilter = new RedirectFilter(); | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithInternalRedirect() throws IOException, ServletException { | ||
when(httpRequest.getParameter("redirectUrl")).thenReturn("/redirectLink"); | ||
when(httpRequest.getRequestURL()).thenReturn(new StringBuffer("http://bonita:8080/currentPath")); | ||
when(httpRequest.getRequestURI()).thenReturn("/currentPath"); | ||
|
||
redirectFilter.doFilter(httpRequest, httpResponse, filterChain); | ||
|
||
verify(httpResponse).sendRedirect("/redirectLink"); | ||
verify(filterChain, never()).doFilter(httpRequest, httpResponse); | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithExternalRedirect() throws IOException, ServletException { | ||
// using http:// as a prefix to simulate an external redirect | ||
when(httpRequest.getParameter("redirectUrl")).thenReturn("http://external:9090/redirectLink"); | ||
when(httpRequest.getRequestURL()).thenReturn(new StringBuffer("http://bonita:8080/currentPath")); | ||
when(httpRequest.getRequestURI()).thenReturn("/currentPath"); | ||
|
||
redirectFilter.doFilter(httpRequest, httpResponse, filterChain); | ||
|
||
verify(httpResponse, never()).sendRedirect(anyString()); | ||
verify(filterChain).doFilter(httpRequest, httpResponse); | ||
|
||
// using // as a prefix to simulate an external redirect | ||
when(httpRequest.getParameter("redirectUrl")).thenReturn("//external:9090/redirectLink"); | ||
when(httpRequest.getRequestURL()).thenReturn(new StringBuffer("http://bonita:8080/currentPath")); | ||
when(httpRequest.getRequestURI()).thenReturn("/currentPath"); | ||
|
||
redirectFilter.doFilter(httpRequest, httpResponse, filterChain); | ||
|
||
verify(httpResponse, never()).sendRedirect(anyString()); | ||
verify(filterChain, times(2)).doFilter(httpRequest, httpResponse); | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithoutRedirect() throws IOException, ServletException { | ||
when(httpRequest.getParameter("redirectUrl")).thenReturn(null); | ||
|
||
redirectFilter.doFilter(httpRequest, httpResponse, filterChain); | ||
|
||
verify(httpResponse, never()).sendRedirect(anyString()); | ||
verify(filterChain).doFilter(httpRequest, httpResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters