-
Notifications
You must be signed in to change notification settings - Fork 60
Lutece 2206 #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seboo
wants to merge
2
commits into
develop
Choose a base branch
from
LUTECE-2206
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lutece 2206 #144
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -33,6 +33,8 @@ | |
*/ | ||
package fr.paris.lutece.util.http; | ||
|
||
import fr.paris.lutece.portal.service.util.AppPathService; | ||
import fr.paris.lutece.portal.service.util.AppPropertiesService; | ||
import fr.paris.lutece.portal.web.LocalVariables; | ||
import fr.paris.lutece.util.string.StringUtil; | ||
|
||
|
@@ -43,6 +45,7 @@ | |
import java.util.Enumeration; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import org.springframework.util.AntPathMatcher; | ||
|
||
/** | ||
* Security utils | ||
|
@@ -61,6 +64,8 @@ public final class SecurityUtil | |
"..", "/", "\\" | ||
}; | ||
|
||
public static final String PROPERTY_REDIRECT_URL_SAFE_PATTERNS = "lutece.security.redirectUrlSafePatterns"; | ||
|
||
// private static final String PATTERN_CLEAN_PARAMETER = "^[\\w/]+$+"; | ||
|
||
/** | ||
|
@@ -266,6 +271,82 @@ public static String getRealIp( HttpServletRequest request ) | |
|
||
return strIPAddress; | ||
} | ||
|
||
/** | ||
* Validate a forward URL to avoid open redirect | ||
* with url safe patterns found in properties | ||
* | ||
* @see SecurityUtil#isInternalRedirectUrlSafe(java.lang.String, javax.servlet.http.HttpServletRequest, java.lang.String) | ||
* | ||
* @param strUrl | ||
* @param request | ||
* @return true if valid | ||
*/ | ||
public static boolean isInternalRedirectUrlSafe( String strUrl, HttpServletRequest request ) | ||
{ | ||
String strAntPathMatcherPatternsValues = AppPropertiesService.getProperty( SecurityUtil.PROPERTY_REDIRECT_URL_SAFE_PATTERNS ); | ||
|
||
return isInternalRedirectUrlSafe( strUrl, request, strAntPathMatcherPatternsValues ); | ||
} | ||
|
||
|
||
/** | ||
* Validate an internal redirect URL to avoid internal open redirect. | ||
* (Use this function only if the use of internal url redirect keys is not possible. | ||
* For external url redirection control, use the plugin plugin-verifybackurl) | ||
* | ||
* the url should : | ||
* - not be blank (null or empty string or spaces) | ||
* - not start with "http://" or "https://" or "//" OR match the base URL or any URL in the pattern list | ||
* | ||
* example with a base url "https://lutece.fr/ : | ||
* - valid : myapp/jsp/site/Portal.jsp , Another.jsp , https://lutece.fr/myapp/jsp/site/Portal.jsp | ||
* - invalid : http://anothersite.com , https://anothersite.com , //anothersite.com , file://my.txt , ... | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should be turned into unit tests |
||
* | ||
* @param strUrl the Url to validate | ||
* @param request the current request (containing the baseUrl) | ||
* @param strAntPathMatcherPatterns a comma separated list of AntPathMatcher patterns, as "http://**.lutece.com,https://**.lutece.com" | ||
* @return true if valid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When and why should I use that ? |
||
*/ | ||
public static boolean isInternalRedirectUrlSafe( String strUrl, HttpServletRequest request, String strAntPathMatcherPatterns ) | ||
{ | ||
|
||
if ( StringUtils.isBlank( strUrl ) ) return true ; // this is not a valid redirect Url, but it is not unsafe | ||
|
||
// filter schemes | ||
if ( !strUrl.startsWith( "//" ) | ||
&& !strUrl.startsWith("http:" ) | ||
&& !strUrl.startsWith("https:" ) | ||
&& !strUrl.contains( "://" ) | ||
&& !strUrl.startsWith("javascript:" ) ) | ||
return true ; // should be a relative path | ||
|
||
// compare with current baseUrl | ||
if ( strUrl.startsWith( AppPathService.getBaseUrl( request ) ) ) | ||
return true ; | ||
|
||
// compare with allowed url patterns | ||
if ( !StringUtils.isBlank( strAntPathMatcherPatterns ) ) | ||
{ | ||
AntPathMatcher pathMatcher = new AntPathMatcher(); | ||
|
||
String[] strAntPathMatcherPatternsTab = strAntPathMatcherPatterns.split( CONSTANT_COMMA ) ; | ||
for ( String pattern : strAntPathMatcherPatternsTab ) | ||
{ | ||
if ( pattern != null && pathMatcher.match( pattern , strUrl ) ) | ||
return true ; | ||
} | ||
} | ||
|
||
|
||
// the Url does not match the allowed patterns | ||
Logger logger = Logger.getLogger( LOGGER_NAME ); | ||
logger.warn( "SECURITY WARNING : OPEN_REDIRECT DETECTED : " + dumpRequest( request ) ); | ||
|
||
return false; | ||
|
||
} | ||
|
||
/** | ||
* Identify user data saved in log files to prevent Log Forging attacks | ||
|
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a commented out entry in the default propertied file, with an explanation as to what this property does, when to use it, etc.