Skip to content

SOLR-111 : Validate JSONP callback function name to avoid XSS #4

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
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/java/fr/paris/lutece/plugins/search/solr/util/SolrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public final class SolrUtil
private static final String PROPERTY_ENCODE_URI_ENCODING = "search.encode.uri.encoding";
private static final String DEFAULT_URI_ENCODING = "ISO-8859-1";

private static final String PROPERTY_CALLBACK_FUNCTION_NAME_PATTERN = "search.callbackFunctionName.pattern" ;
private static final String CONSTANT_DEFAULT_FUNCTION_NAME_PATTERN = "[_$A-Za-z0-9]+";
public static final String PROPERTY_CALLBACK_FUNCTION_NAME_ERROR_MESSAGE = "search.callbackFunctionName.error.message" ;

/**
* Empty private constructor
*/
Expand Down Expand Up @@ -201,4 +205,17 @@ public static String getEncoding( )

return strURIEncoding;
}

/**
* Test if the name is a valid javascript function name
*
* @param strName
* @return true if valid
*/
public static boolean isValidJavascriptFunctionName( String strName )
{
String strFunctionNamePattern = AppPropertiesService.getProperty( PROPERTY_CALLBACK_FUNCTION_NAME_PATTERN, CONSTANT_DEFAULT_FUNCTION_NAME_PATTERN );

return ( strName != null && strName.matches( strFunctionNamePattern ) ) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
package fr.paris.lutece.plugins.search.solr.web;

import fr.paris.lutece.plugins.search.solr.business.SolrSearchEngine;
import fr.paris.lutece.plugins.search.solr.util.SolrUtil;
import fr.paris.lutece.portal.service.util.AppPropertiesService;

import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
Expand All @@ -59,6 +61,7 @@ public class SolrSuggestServlet extends HttpServlet
{
private static final long serialVersionUID = -3273825949482572338L;


public void init( )
{
}
Expand All @@ -76,6 +79,13 @@ public String getSuggest( HttpServletRequest request )

SolrSearchEngine engine = SolrSearchEngine.getInstance( );
StringBuffer result = new StringBuffer( );

// XSS control
if ( !SolrUtil.isValidJavascriptFunctionName( callback ) )
{
return AppPropertiesService.getProperty( SolrUtil.PROPERTY_CALLBACK_FUNCTION_NAME_ERROR_MESSAGE, "Invalid function name" ) ;
}

result.append( callback );

result.append( "({\"response\":{\"docs\":[" );
Expand Down
6 changes: 5 additions & 1 deletion webapp/WEB-INF/conf/plugins/search-solr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ solr.field.or=OR

solr.field.switch=SWITCH

solr.field.and=AND
solr.field.and=AND

#Callback jsonp function control
search.callbackFunctionName.pattern=[_$A-Za-z0-9]+
search.callbackFunctionName.error.message=Invalid Function Name