Apache Lucene open-source search software
SOLR-3467: ExtendedDismax escaping is missing several reserved characters
Jan Høydahl01dd823f23b8
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex abfb96d209e..b955844fee6 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -517,6 +517,9 @@ Bug Fixes* SOLR-3522: fixed parsing of the 'literal()' function (hossman)+* SOLR-3467: ExtendedDismax escaping is missing several reserved characters+ (Michael Dodsworth via janhoy)+* SOLR-3548: Fixed a bug in the cachability of queries using the {!join}parser or the strdist() function, as well as some minor improvements tothe hashCode implementation of {!bbox} and {!geofilt} queries.diff --git a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.javaindex 5b28def029b..4a1eef34d32 100755--- a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java+++ b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java@@ -541,81 +541,81 @@ class ExtendedDismaxQParser extends QParser {}-- public static CharSequence partialEscape(CharSequence s) {- StringBuilder sb = new StringBuilder();-- int len = s.length();- for (int i = 0; i < len; i++) {- char c = s.charAt(i);- if (c == ':') {- // look forward to make sure it's something that won't- // cause a parse exception (something that won't be escaped... like- // +,-,:, whitespace- if (i+1<len && i>0) {- char ch = s.charAt(i+1);- if (!(Character.isWhitespace(ch) || ch=='+' || ch=='-' || ch==':')) {- // OK, at this point the chars after the ':' will be fine.- // now look back and try to determine if this is a fieldname- // [+,-]? [letter,_] [letter digit,_,-,.]*- // This won't cover *all* possible lucene fieldnames, but we should- // only pick nice names to begin with- int start, pos;- for (start=i-1; start>=0; start--) {- ch = s.charAt(start);- if (Character.isWhitespace(ch)) break;- }-- // skip whitespace- pos = start+1;-- // skip leading + or -- ch = s.charAt(pos);- if (ch=='+' || ch=='-') {- pos++;- }-- // we don't need to explicitly check for end of string- // since ':' will act as our sentinal-- // first char can't be '-' or '.'- ch = s.charAt(pos++);- if (Character.isJavaIdentifierPart(ch)) {-- for(;;) {- ch = s.charAt(pos++);- if (!(Character.isJavaIdentifierPart(ch) || ch=='-' || ch=='.')) {- break;- }- }-- if (pos<=i) {- // OK, we got to the ':' and everything looked like a valid fieldname, so- // don't escape the ':'- sb.append(':');- continue; // jump back to start of outer-most loop- }-- }--- }- }-- // we fell through to here, so we should escape this like other reserved chars.- sb.append('\\');- }- else if (c == '\\' || c == '!' || c == '(' || c == ')' ||- c == '^' || c == '[' || c == ']' ||- c == '{' || c == '}' || c == '~' || c == '*' || c == '?'- )- {- sb.append('\\');- }- sb.append(c);- }- return sb;- }+// FIXME: Not in use+// public static CharSequence partialEscape(CharSequence s) {+// StringBuilder sb = new StringBuilder();+//+// int len = s.length();+// for (int i = 0; i < len; i++) {+// char c = s.charAt(i);+// if (c == ':') {+// // look forward to make sure it's something that won't+// // cause a parse exception (something that won't be escaped... like+// // +,-,:, whitespace+// if (i+1<len && i>0) {+// char ch = s.charAt(i+1);+// if (!(Character.isWhitespace(ch) || ch=='+' || ch=='-' || ch==':')) {+// // OK, at this point the chars after the ':' will be fine.+// // now look back and try to determine if this is a fieldname+// // [+,-]? [letter,_] [letter digit,_,-,.]*+// // This won't cover *all* possible lucene fieldnames, but we should+// // only pick nice names to begin with+// int start, pos;+// for (start=i-1; start>=0; start--) {+// ch = s.charAt(start);+// if (Character.isWhitespace(ch)) break;+// }+//+// // skip whitespace+// pos = start+1;+//+// // skip leading + or -+// ch = s.charAt(pos);+// if (ch=='+' || ch=='-') {+// pos++;+// }+//+// // we don't need to explicitly check for end of string+// // since ':' will act as our sentinal+//+// // first char can't be '-' or '.'+// ch = s.charAt(pos++);+// if (Character.isJavaIdentifierPart(ch)) {+//+// for(;;) {+// ch = s.charAt(pos++);+// if (!(Character.isJavaIdentifierPart(ch) || ch=='-' || ch=='.')) {+// break;+// }+// }+//+// if (pos<=i) {+// // OK, we got to the ':' and everything looked like a valid fieldname, so+// // don't escape the ':'+// sb.append(':');+// continue; // jump back to start of outer-most loop+// }+//+// }+//+//+// }+// }+//+// // we fell through to here, so we should escape this like other reserved chars.+// sb.append('\\');+// }+// else if (c == '\\' || c == '!' || c == '(' || c == ')' ||+// c == '^' || c == '[' || c == ']' ||+// c == '{' || c == '}' || c == '~' || c == '*' || c == '?'+// )+// {+// sb.append('\\');+// }+// sb.append(c);+// }+// return sb;+// }static class Clause {@@ -726,6 +726,10 @@ class ExtendedDismaxQParser extends QParser {case '"':case '+':case '-':+ case '\\':+ case '|':+ case '&':+ case '/':clause.hasSpecialSyntax = true;sb.append('\\');}diff --git a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.javaindex bea40f44e70..2dd1fbb82d5 100755--- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java+++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java@@ -21,6 +21,7 @@ import java.io.IOException;import org.apache.solr.common.SolrException;import org.apache.solr.util.AbstractSolrTestCase;+import org.junit.Test;public class TestExtendedDismaxParser extends AbstractSolrTestCase {@Override@@ -701,4 +702,43 @@ public class TestExtendedDismaxParser extends AbstractSolrTestCase {);}++ /**+ * verify that all reserved characters are properly escaped when being set in+ * {@link org.apache.solr.search.ExtendedDismaxQParser.Clause#val}.+ *+ * @see ExtendedDismaxQParser#splitIntoClauses(String, boolean)+ */+ @Test+ public void testEscapingOfReservedCharacters() throws Exception {+ // create a document that contains all reserved characters+ String allReservedCharacters = "!():^[]{}~*?\"+-\\|&/";++ assertU(adoc("id", "reservedChars",+ "name", allReservedCharacters,+ "cat_s", "foo/"));+ assertU(commit());++ // the backslash needs to be manually escaped (the query parser sees the raw backslash as an escape the subsequent+ // character)+ String query = allReservedCharacters.replace("\\", "\\\\");++ // query for all those reserved characters. This will fail to parse in the initial parse, meaning that the escaped+ // query will then be used+ assertQ("Escaping reserved characters",+ req("q", query,+ "qf", "name",+ "mm", "100%",+ "defType", "edismax")+ , "*[count(//doc)=1]");++ // Query string field 'cat_s' for special char / - causes ParseException without patch SOLR-3467+ assertQ("Escaping string with reserved / character",+ req("q", "foo/",+ "qf", "cat_s",+ "mm", "100%",+ "defType", "edismax")+ , "*[count(//doc)=1]");++ }}