From f1a4feda3ece5eb61d06f229b26bb867dc9428fe Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Thu, 24 Jan 2013 17:57:28 +0100 Subject: [PATCH] security fix for suggest (don't let users ask for too much) --- htroot/suggest.java | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/htroot/suggest.java b/htroot/suggest.java index 27114fc99..a4c03ad4b 100644 --- a/htroot/suggest.java +++ b/htroot/suggest.java @@ -23,7 +23,7 @@ import java.util.ConcurrentModificationException; -import java.util.Iterator; +import java.util.SortedSet; import net.yacy.cora.protocol.HeaderFramework; import net.yacy.cora.protocol.RequestHeader; @@ -64,27 +64,29 @@ public class suggest { final String originalquerystring = (post == null) ? "" : post.get("query", post.get("q", "")).trim(); final String querystring = originalquerystring.replace('+', ' '); final int timeout = (post == null) ? 300 : post.getInt("timeout", 300); - final int count = (post == null) ? 20 : post.getInt("count", 20); + final int count = (post == null) ? 10 : Math.min(20, post.getInt("count", 10)); int c = 0; if (more || (sb.index.getQueryCount(querystring) == 0)) { final DidYouMean didYouMean = new DidYouMean(sb.index, new StringBuilder(querystring)); - final Iterator meanIt = didYouMean.getSuggestions(timeout, count).iterator(); - String suggestion; + final SortedSet suggestions = didYouMean.getSuggestions(timeout, count); //[#[query]#,[#{suggestions}##[text]##(eol)#,::#(/eol)##{/suggestions}#]] - while (c < meanMax && meanIt.hasNext()) { - try { - suggestion = meanIt.next().toString(); - if (json) { - prop.putJSON("suggestions_" + c + "_text", suggestion); - } else if (xml) { - prop.putXML("suggestions_" + c + "_text", suggestion); - } else { - prop.putHTML("suggestions_" + c + "_text", suggestion); - } - prop.put("suggestions_" + c + "_eol", 0); - c++; - } catch (ConcurrentModificationException e) {} + synchronized (suggestions) { + for (StringBuilder suggestion: suggestions) { + if (c >= meanMax) break; + try { + String s = suggestion.toString(); + if (json) { + prop.putJSON("suggestions_" + c + "_text", s); + } else if (xml) { + prop.putXML("suggestions_" + c + "_text", s); + } else { + prop.putHTML("suggestions_" + c + "_text", s); + } + prop.put("suggestions_" + c + "_eol", 0); + c++; + } catch (ConcurrentModificationException e) {} + } } }