Skip to content

Assert that REST params are consumed iff supported #113933

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.action.datastreams.GetDataStreamAction;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.cluster.metadata.DataStream;
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.set.Sets;
Expand All @@ -35,12 +36,12 @@ public class RestGetDataStreamsAction extends BaseRestHandler {
Set.of(
"name",
"include_defaults",
"timeout",
"master_timeout",
IndicesOptions.WildcardOptions.EXPAND_WILDCARDS,
IndicesOptions.ConcreteTargetOptions.IGNORE_UNAVAILABLE,
IndicesOptions.WildcardOptions.ALLOW_NO_INDICES,
IndicesOptions.GatekeeperOptions.IGNORE_THROTTLED,
DataStream.isFailureStoreFeatureFlagEnabled() ? IndicesOptions.FailureStoreOptions.FAILURE_STORE : "name",
"verbose"
)
)
Expand Down
17 changes: 17 additions & 0 deletions server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.core.CheckedConsumer;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.RefCounted;
import org.elasticsearch.core.Releasable;
import org.elasticsearch.core.RestApiVersion;
Expand Down Expand Up @@ -104,6 +105,8 @@ public final void handleRequest(RestRequest request, RestChannel channel, NodeCl
// prepare the request for execution; has the side effect of touching the request parameters
try (var action = prepareRequest(request, client)) {

assert assertConsumesSupportedParams(supported, request);

// validate unconsumed params, but we must exclude params used to format the response
// use a sorted set so the unconsumed parameters appear in a reliable sorted order
final SortedSet<String> unconsumedParams = request.unconsumedParams()
Expand Down Expand Up @@ -148,6 +151,20 @@ public void close() {
}
}

private boolean assertConsumesSupportedParams(@Nullable Set<String> supported, RestRequest request) {
if (supported != null) {
final var supportedAndCommon = new TreeSet<>(supported);
supportedAndCommon.add("error_trace");
supportedAndCommon.addAll(ALWAYS_SUPPORTED);
supportedAndCommon.removeAll(RestRequest.INTERNAL_MARKER_REQUEST_PARAMETERS);
final var consumed = new TreeSet<>(request.consumedParams());
consumed.removeAll(RestRequest.INTERNAL_MARKER_REQUEST_PARAMETERS);
assert supportedAndCommon.equals(consumed)
: getName() + ": consumed params " + consumed + " while supporting " + supportedAndCommon;
}
return true;
}

protected static String unrecognized(RestRequest request, Set<String> invalids, Set<String> candidates, String detail) {
StringBuilder message = new StringBuilder().append("request [")
.append(request.path())
Expand Down