Not allowing index names in request body for multi-get/search/bulk when indices are already given in url

closes #3636
This commit is contained in:
Shay Banon 2013-09-06 01:46:24 +02:00
parent 95b894e6f6
commit 3e92b1d6b8
7 changed files with 55 additions and 12 deletions

View File

@ -245,7 +245,7 @@ public class BulkProcessor {
}
public synchronized BulkProcessor add(BytesReference data, boolean contentUnsafe, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable Object payload) throws Exception {
bulkRequest.add(data, contentUnsafe, defaultIndex, defaultType, payload);
bulkRequest.add(data, contentUnsafe, defaultIndex, defaultType, payload, true);
executeIfNeeded();
return this;
}

View File

@ -235,10 +235,17 @@ public class BulkRequest extends ActionRequest<BulkRequest> {
* Adds a framed data in binary format
*/
public BulkRequest add(BytesReference data, boolean contentUnsafe, @Nullable String defaultIndex, @Nullable String defaultType) throws Exception {
return add(data, contentUnsafe, defaultIndex, defaultType, null);
return add(data, contentUnsafe, defaultIndex, defaultType, null, true);
}
public BulkRequest add(BytesReference data, boolean contentUnsafe, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable Object payload) throws Exception {
/**
* Adds a framed data in binary format
*/
public BulkRequest add(BytesReference data, boolean contentUnsafe, @Nullable String defaultIndex, @Nullable String defaultType, boolean allowExplicitIndex) throws Exception {
return add(data, contentUnsafe, defaultIndex, defaultType, null, allowExplicitIndex);
}
public BulkRequest add(BytesReference data, boolean contentUnsafe, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable Object payload, boolean allowExplicitIndex) throws Exception {
XContent xContent = XContentFactory.xContent(data);
int from = 0;
int length = data.length();
@ -287,6 +294,9 @@ public class BulkRequest extends ActionRequest<BulkRequest> {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("_index".equals(currentFieldName)) {
if (!allowExplicitIndex) {
throw new ElasticSearchIllegalArgumentException("explicit index in bulk is not allowed");
}
index = parser.text();
} else if ("_type".equals(currentFieldName)) {
type = parser.text();
@ -327,6 +337,9 @@ public class BulkRequest extends ActionRequest<BulkRequest> {
// we use internalAdd so we don't fork here, this allows us not to copy over the big byte array to small chunks
// of index request. All index requests are still unsafe if applicable.
if ("index".equals(action)) {
if (!allowExplicitIndex) {
throw new ElasticSearchIllegalArgumentException("explicit index in bulk is not allowed");
}
if (opType == null) {
internalAdd(new IndexRequest(index, type, id).routing(routing).parent(parent).timestamp(timestamp).ttl(ttl).version(version).versionType(versionType)
.source(data.slice(from, nextMarker - from), contentUnsafe), payload);

View File

@ -264,11 +264,15 @@ public class MultiGetRequest extends ActionRequest<MultiGetRequest> {
return this;
}
public void add(@Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, byte[] data, int from, int length) throws Exception {
add(defaultIndex, defaultType, defaultFields, defaultFetchSource, new BytesArray(data, from, length));
public MultiGetRequest add(@Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, byte[] data, int from, int length) throws Exception {
return add(defaultIndex, defaultType, defaultFields, defaultFetchSource, new BytesArray(data, from, length), true);
}
public void add(@Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, BytesReference data) throws Exception {
public MultiGetRequest add(@Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, BytesReference data) throws Exception {
return add(defaultIndex, defaultType, defaultFields, defaultFetchSource, data, true);
}
public MultiGetRequest add(@Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, BytesReference data, boolean allowExplicitIndex) throws Exception {
XContentParser parser = XContentFactory.xContent(data).createParser(data);
try {
XContentParser.Token token;
@ -298,6 +302,9 @@ public class MultiGetRequest extends ActionRequest<MultiGetRequest> {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("_index".equals(currentFieldName)) {
if (!allowExplicitIndex) {
throw new ElasticSearchIllegalArgumentException("explicit index in multi get is not allowed");
}
index = parser.text();
} else if ("_type".equals(currentFieldName)) {
type = parser.text();
@ -388,6 +395,7 @@ public class MultiGetRequest extends ActionRequest<MultiGetRequest> {
} finally {
parser.close();
}
return this;
}
@Override

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.search;
import com.google.common.collect.Lists;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
@ -69,11 +70,14 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> {
public MultiSearchRequest add(byte[] data, int from, int length, boolean contentUnsafe,
@Nullable String[] indices, @Nullable String[] types, @Nullable String searchType) throws Exception {
return add(new BytesArray(data, from, length), contentUnsafe, indices, types, searchType, IgnoreIndices.NONE);
return add(new BytesArray(data, from, length), contentUnsafe, indices, types, searchType, IgnoreIndices.NONE, true);
}
public MultiSearchRequest add(BytesReference data, boolean contentUnsafe,
@Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, IgnoreIndices ignoreIndices) throws Exception {
public MultiSearchRequest add(BytesReference data, boolean contentUnsafe, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, IgnoreIndices ignoreIndices) throws Exception {
return add(data, contentUnsafe, indices, types, searchType, ignoreIndices, true);
}
public MultiSearchRequest add(BytesReference data, boolean contentUnsafe, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, IgnoreIndices ignoreIndices, boolean allowExplicitIndex) throws Exception {
XContent xContent = XContentFactory.xContent(data);
int from = 0;
int length = data.length();
@ -115,6 +119,9 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("index".equals(currentFieldName) || "indices".equals(currentFieldName)) {
if (!allowExplicitIndex) {
throw new ElasticSearchIllegalArgumentException("explicit index in multi search is not allowed");
}
searchRequest.indices(Strings.splitStringByCommaToArray(parser.text()));
} else if ("type".equals(currentFieldName) || "types".equals(currentFieldName)) {
searchRequest.types(Strings.splitStringByCommaToArray(parser.text()));
@ -129,6 +136,9 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> {
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("index".equals(currentFieldName) || "indices".equals(currentFieldName)) {
if (!allowExplicitIndex) {
throw new ElasticSearchIllegalArgumentException("explicit index in multi search is not allowed");
}
searchRequest.indices(parseArray(parser));
} else if ("type".equals(currentFieldName) || "types".equals(currentFieldName)) {
searchRequest.types(parseArray(parser));

View File

@ -53,6 +53,8 @@ import static org.elasticsearch.rest.action.support.RestXContentBuilder.restCont
*/
public class RestBulkAction extends BaseRestHandler {
private final boolean allowExplicitIndex;
@Inject
public RestBulkAction(Settings settings, Client client, RestController controller) {
super(settings, client);
@ -63,6 +65,8 @@ public class RestBulkAction extends BaseRestHandler {
controller.registerHandler(PUT, "/{index}/_bulk", this);
controller.registerHandler(POST, "/{index}/{type}/_bulk", this);
controller.registerHandler(PUT, "/{index}/{type}/_bulk", this);
this.allowExplicitIndex = settings.getAsBoolean("rest.action.multi.allow_explicit_index", true);
}
@Override
@ -82,7 +86,7 @@ public class RestBulkAction extends BaseRestHandler {
}
bulkRequest.refresh(request.paramAsBoolean("refresh", bulkRequest.refresh()));
try {
bulkRequest.add(request.content(), request.contentUnsafe(), defaultIndex, defaultType);
bulkRequest.add(request.content(), request.contentUnsafe(), defaultIndex, defaultType, allowExplicitIndex);
} catch (Exception e) {
try {
XContentBuilder builder = restContentBuilder(request);

View File

@ -40,6 +40,8 @@ import static org.elasticsearch.rest.action.support.RestXContentBuilder.restCont
public class RestMultiGetAction extends BaseRestHandler {
private final boolean allowExplicitIndex;
@Inject
public RestMultiGetAction(Settings settings, Client client, RestController controller) {
super(settings, client);
@ -49,6 +51,8 @@ public class RestMultiGetAction extends BaseRestHandler {
controller.registerHandler(POST, "/{index}/_mget", this);
controller.registerHandler(GET, "/{index}/{type}/_mget", this);
controller.registerHandler(POST, "/{index}/{type}/_mget", this);
this.allowExplicitIndex = settings.getAsBoolean("rest.action.multi.allow_explicit_index", true);
}
@Override
@ -68,7 +72,7 @@ public class RestMultiGetAction extends BaseRestHandler {
FetchSourceContext defaultFetchSource = FetchSourceContext.parseFromRestRequest(request);
try {
multiGetRequest.add(request.param("index"), request.param("type"), sFields, defaultFetchSource, request.content());
multiGetRequest.add(request.param("index"), request.param("type"), sFields, defaultFetchSource, request.content(), allowExplicitIndex);
} catch (Exception e) {
try {
XContentBuilder builder = restContentBuilder(request);

View File

@ -42,6 +42,8 @@ import static org.elasticsearch.rest.action.support.RestXContentBuilder.restCont
*/
public class RestMultiSearchAction extends BaseRestHandler {
private final boolean allowExplicitIndex;
@Inject
public RestMultiSearchAction(Settings settings, Client client, RestController controller) {
super(settings, client);
@ -52,6 +54,8 @@ public class RestMultiSearchAction extends BaseRestHandler {
controller.registerHandler(POST, "/{index}/_msearch", this);
controller.registerHandler(GET, "/{index}/{type}/_msearch", this);
controller.registerHandler(POST, "/{index}/{type}/_msearch", this);
this.allowExplicitIndex = settings.getAsBoolean("rest.action.multi.allow_explicit_index", true);
}
@Override
@ -67,7 +71,7 @@ public class RestMultiSearchAction extends BaseRestHandler {
}
try {
multiSearchRequest.add(request.content(), request.contentUnsafe(), indices, types, request.param("search_type"), ignoreIndices);
multiSearchRequest.add(request.content(), request.contentUnsafe(), indices, types, request.param("search_type"), ignoreIndices, allowExplicitIndex);
} catch (Exception e) {
try {
XContentBuilder builder = restContentBuilder(request);