This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-indexer.git
The following commit(s) were added to refs/heads/master by this push:
new 4948dae9 Limit the size of search.maven.org responses read
4948dae9 is described below
commit 4948dae9c676acd3a6b9799f5be69c8e39f4e56a
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Nov 16 09:49:22 2026 +0100
Limit the size of search.maven.org responses read
---
.../backend/smo/internal/SmoSearchBackendImpl.java | 11 ++-
.../smo/internal/SmoSearchBackendResponseTest.java | 98 ++++++++++++++++++++++
2 files changed, 108 insertions(+), 1 deletion(-)
diff --git
a/search-backend-smo/src/main/java/org/apache/maven/search/backend/smo/internal/SmoSearchBackendImpl.java
b/search-backend-smo/src/main/java/org/apache/maven/search/backend/smo/internal/SmoSearchBackendImpl.java
index 928aa784..158a50e7 100644
---
a/search-backend-smo/src/main/java/org/apache/maven/search/backend/smo/internal/SmoSearchBackendImpl.java
+++
b/search-backend-smo/src/main/java/org/apache/maven/search/backend/smo/internal/SmoSearchBackendImpl.java
@@ -73,6 +73,11 @@ public class SmoSearchBackendImpl extends
SearchBackendSupport implements SmoSea
"1");
}
+ /**
+ * The largest search response read, in bytes.
+ */
+ private static final int MAX_RESPONSE_SIZE = 64 * 1024 * 1024;
+
protected final String smoUri;
protected final Transport transport;
@@ -165,7 +170,11 @@ public class SmoSearchBackendImpl extends
SearchBackendSupport implements SmoSea
protected String fetch(String serviceUri, Map<String, String> headers)
throws IOException {
try (Transport.Response response = transport.get(serviceUri, headers))
{
if (response.getCode() == HttpURLConnection.HTTP_OK) {
- return new String(response.getBody().readAllBytes(),
StandardCharsets.UTF_8);
+ byte[] body = response.getBody().readNBytes(MAX_RESPONSE_SIZE
+ 1);
+ if (body.length > MAX_RESPONSE_SIZE) {
+ throw new IOException("Response larger than " +
MAX_RESPONSE_SIZE + " bytes: " + serviceUri);
+ }
+ return new String(body, StandardCharsets.UTF_8);
} else {
throw new IOException("Unexpected response: " + response);
}
diff --git
a/search-backend-smo/src/test/java/org/apache/maven/search/backend/smo/internal/SmoSearchBackendResponseTest.java
b/search-backend-smo/src/test/java/org/apache/maven/search/backend/smo/internal/SmoSearchBackendResponseTest.java
new file mode 100644
index 00000000..0456d2b2
--- /dev/null
+++
b/search-backend-smo/src/test/java/org/apache/maven/search/backend/smo/internal/SmoSearchBackendResponseTest.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.search.backend.smo.internal;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Map;
+
+import org.apache.maven.search.api.transport.Transport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Offline UT for reading SMO responses.
+ */
+class SmoSearchBackendResponseTest {
+
+ @Test
+ void responseIsRead() throws IOException {
+ byte[] json = "{\"response\":{}}".getBytes(StandardCharsets.UTF_8);
+
+ assertEquals(
+ "{\"response\":{}}",
+ backend(new
ByteArrayInputStream(json)).fetch("https://search.example/", Map.of()));
+ }
+
+ @Test
+ void endlessResponseIsRejected() {
+ InputStream endless = new InputStream() {
+ @Override
+ public int read() {
+ return ' ';
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) {
+ Arrays.fill(b, off, off + len, (byte) ' ');
+ return len;
+ }
+ };
+
+ assertThrows(IOException.class, () ->
backend(endless).fetch("https://search.example/", Map.of()));
+ }
+
+ private static SmoSearchBackendImpl backend(InputStream body) {
+ Transport transport = new Transport() {
+ @Override
+ public Response get(String serviceUri, Map<String, String>
headers) {
+ return new Response() {
+ @Override
+ public int getCode() {
+ return 200;
+ }
+
+ @Override
+ public Map<String, String> getHeaders() {
+ return Map.of();
+ }
+
+ @Override
+ public InputStream getBody() {
+ return body;
+ }
+
+ @Override
+ public void close() {}
+ };
+ }
+
+ @Override
+ public Response head(String serviceUri, Map<String, String>
headers) throws IOException {
+ throw new IOException("offline");
+ }
+ };
+ return new SmoSearchBackendImpl("smo", "central",
"https://search.example/solrsearch/select", transport);
+ }
+}