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 1 commit
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 Down Expand Up @@ -68,12 +69,12 @@ public Set<String> supportedQueryParameters() {
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
16 changes: 16 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 @@ -103,6 +104,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 @@ -147,6 +150,19 @@ public void close() {
}
}

private boolean assertConsumesSupportedParams(@Nullable Set<String> supported, RestRequest request) {
if (supported != null) {
final var supportedAndCommon = new TreeSet<>(supported);
supportedAndCommon.addAll(List.of("error_trace", "filter_path", "format", "pretty", "human"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have these common params defined anywhere so this isn't an arbitrary list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh not all of them. There's org.elasticsearch.rest.BaseRestHandler#ALWAYS_SUPPORTED but that doesn't include error_trace. I pushed a commit that uses ALWAYS_SUPPORTED. Not a huge deal here in any case, if we get this wrong then this assertion will trip.

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