From 4822b9e8af9b73c9a5da5f8359dc84a0763d7f05 Mon Sep 17 00:00:00 2001 From: mohitdeuex Date: Fri, 9 Oct 2026 20:12:18 +0530 Subject: [PATCH] Fixes #35037: reject negative search pagination before it reaches the engine /v1/search/query, /v1/search/nlq/query and /v1/search/fieldQuery handed from/size to Elasticsearch/OpenSearch unchecked, so a negative value was only caught by the engine: a 500 with a server-side stack trace on 2.0.x, and still a 500 on main for the NLQ and field-query endpoints. Bound both parameters with @Min(0) so Jersey answers 400 before the resource runs (no cache lookup, no engine call), and check the /v1/search/preview request body the same way. Negative values are rejected rather than clamped, so a caller's paging bug stays visible instead of quietly serving page 1. Co-Authored-By: Claude Opus 5.5 --- .../it/tests/SearchResourceIT.java | 66 +++++++++++++++++-- .../resources/search/SearchResource.java | 17 +++++ .../resources/search/SearchResourceTest.java | 45 +++++++++++++ 3 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 openmetadata-service/src/test/java/org/openmetadata/service/resources/search/SearchResourceTest.java diff --git a/openmetadata-integration-tests/src/test/java/org/openmetadata/it/tests/SearchResourceIT.java b/openmetadata-integration-tests/src/test/java/org/openmetadata/it/tests/SearchResourceIT.java index 8fdb7304fa44..99ebe21dba9a 100644 --- a/openmetadata-integration-tests/src/test/java/org/openmetadata/it/tests/SearchResourceIT.java +++ b/openmetadata-integration-tests/src/test/java/org/openmetadata/it/tests/SearchResourceIT.java @@ -1816,12 +1816,22 @@ void testSearchWithVeryLargeSize(TestNamespace ns) throws Exception { void testSearchWithNegativeOffset(TestNamespace ns) throws Exception { OpenMetadataClient client = SdkClients.adminClient(); - // Negative offset is invalid - the search engine rejects it. That rejection is the caller's - // error, so it now arrives as a 400 rather than the 500 the engine's status used to be - // flattened into (#27990). - assertThrows( - InvalidRequestException.class, - () -> client.search().query("*").index("table_search_index").from(-1).size(10).execute()); + // The engine would also answer 400 (#27990); the message shows the request never reached it. + InvalidRequestException thrown = + assertThrows( + InvalidRequestException.class, + () -> + client + .search() + .query("*") + .index("table_search_index") + .from(-25) + .size(25) + .execute()); + + assertTrue( + thrown.getMessage().contains("query param from must be greater than or equal to 0"), + thrown.getMessage()); } @Test @@ -2144,4 +2154,48 @@ void testValidQueryFilterStillApplies() throws Exception { assertEquals(200, response.statusCode(), response.body()); } + + @ParameterizedTest + @ValueSource( + strings = { + "/v1/search/query?q=*&index=table_search_index&size=-1", + "/v1/search/nlq/query?q=tables&index=table_search_index&from=-25", + "/v1/search/fieldQuery?fieldName=name&fieldValue=orders&index=table_search_index&from=-25", + "/v1/search/fieldQuery?fieldName=name&fieldValue=orders&index=table_search_index&size=-1" + }) + void testNegativePaginationIsRejectedAsBadRequest(String path) throws Exception { + HttpResponse response = httpGetSearch(path); + + assertEquals(400, response.statusCode(), path + " returned " + response.body()); + assertTrue( + response.body().contains("must be greater than or equal to 0"), + path + " returned " + response.body()); + } + + @Test + void testPreviewSearchWithNegativeOffsetIsRejectedAsBadRequest() throws Exception { + String previewRequest = + """ + {"query": "*", "index": "table", "from": -25, "size": 25, "searchSettings": {}} + """; + + HttpResponse response = httpPostJson("/v1/search/preview", previewRequest); + + assertEquals(400, response.statusCode(), response.body()); + assertTrue( + response.body().contains("from must be greater than or equal to 0"), response.body()); + } + + private HttpResponse httpPostJson(String path, String jsonBody) throws Exception { + HttpRequest request = + HttpRequest.newBuilder() + .uri(URI.create(SdkClients.getServerUrl() + path)) + .header("Authorization", "Bearer " + SdkClients.getAdminToken()) + .header("Accept", "application/json") + .header("Content-Type", "application/json") + .timeout(Duration.ofSeconds(30)) + .POST(HttpRequest.BodyPublishers.ofString(jsonBody)) + .build(); + return HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); + } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/search/SearchResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/search/SearchResource.java index 860d8c7d58f6..e6391f04fcc0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/search/SearchResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/search/SearchResource.java @@ -27,6 +27,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import jakarta.validation.constraints.Min; +import jakarta.ws.rs.BadRequestException; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.DELETE; import jakarta.ws.rs.DefaultValue; @@ -105,6 +107,7 @@ @Collection(name = "search") @LatencyPhase public class SearchResource { + private static final String MUST_BE_NON_NEGATIVE = "must be greater than or equal to 0"; private final SearchRepository searchRepository; private final Authorizer authorizer; @@ -164,10 +167,12 @@ public Response search( Boolean deleted, @Parameter(description = "From field to paginate the results, defaults to 0") @DefaultValue("0") + @Min(value = 0, message = MUST_BE_NON_NEGATIVE) @QueryParam("from") int from, @Parameter(description = "Size field to limit the no.of results returned, defaults to 10") @DefaultValue("10") + @Min(value = 0, message = MUST_BE_NON_NEGATIVE) @QueryParam("size") int size, @Parameter( @@ -546,6 +551,8 @@ public Response previewSearch( @RequestBody(description = "Preview request containing search settings", required = true) PreviewSearchRequest previewRequest) throws IOException { + requireNonNegative("from", previewRequest.getFrom()); + requireNonNegative("size", previewRequest.getSize()); SubjectContext subjectContext = getSubjectContext(securityContext); @@ -569,6 +576,12 @@ public Response previewSearch( searchRequest, subjectContext, previewRequest.getSearchSettings()); } + private static void requireNonNegative(String fieldName, Integer value) { + if (value != null && value < 0) { + throw new BadRequestException(String.format("%s %s", fieldName, MUST_BE_NON_NEGATIVE)); + } + } + @GET @Path("/nlq/query") @Operation( @@ -600,10 +613,12 @@ public Response searchWithNLQ( boolean deleted, @Parameter(description = "From field to paginate the results, defaults to 0") @DefaultValue("0") + @Min(value = 0, message = MUST_BE_NON_NEGATIVE) @QueryParam("from") int from, @Parameter(description = "Size field to limit the no.of results returned, defaults to 10") @DefaultValue("10") + @Min(value = 0, message = MUST_BE_NON_NEGATIVE) @QueryParam("size") int size, @Parameter( @@ -735,10 +750,12 @@ public Response searchByField( boolean deleted, @Parameter(description = "From field to paginate the results, defaults to 0") @DefaultValue("0") + @Min(value = 0, message = MUST_BE_NON_NEGATIVE) @QueryParam("from") int from, @Parameter(description = "Size field to limit the no.of results returned, defaults to 10") @DefaultValue("10") + @Min(value = 0, message = MUST_BE_NON_NEGATIVE) @QueryParam("size") int size) throws IOException { diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/resources/search/SearchResourceTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/resources/search/SearchResourceTest.java new file mode 100644 index 000000000000..413858f362a3 --- /dev/null +++ b/openmetadata-service/src/test/java/org/openmetadata/service/resources/search/SearchResourceTest.java @@ -0,0 +1,45 @@ +package org.openmetadata.service.resources.search; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.verifyNoInteractions; + +import jakarta.ws.rs.BadRequestException; +import jakarta.ws.rs.core.SecurityContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.mockito.MockedStatic; +import org.openmetadata.schema.search.PreviewSearchRequest; +import org.openmetadata.service.Entity; +import org.openmetadata.service.search.SearchRepository; +import org.openmetadata.service.security.Authorizer; + +class SearchResourceTest { + + @ParameterizedTest + @CsvSource({"-25, 25, from", "0, -1, size", ", -1, size"}) + void previewSearchRejectsNegativePaginationWithoutSearching( + Integer from, Integer size, String rejectedField) { + SearchRepository searchRepository = mock(SearchRepository.class); + try (MockedStatic entity = mockStatic(Entity.class)) { + entity.when(Entity::getSearchRepository).thenReturn(searchRepository); + SearchResource resource = new SearchResource(mock(Authorizer.class)); + PreviewSearchRequest request = + new PreviewSearchRequest() + .withQuery("*") + .withIndex("table") + .withFrom(from) + .withSize(size); + + BadRequestException error = + assertThrows( + BadRequestException.class, + () -> resource.previewSearch(mock(SecurityContext.class), request)); + + assertEquals(rejectedField + " must be greater than or equal to 0", error.getMessage()); + verifyNoInteractions(searchRepository); + } + } +}