From fec7b2682739a6ba099fb9475654ec81734682a6 Mon Sep 17 00:00:00 2001 From: Matt Pavlovich Date: Mon, 16 Nov 2026 13:17:46 -0600 Subject: [PATCH] Update blob transfer URL handling and add client settings - Add BlobTransferPolicy.setDownloadStrategy - Delete uses the URL on the message - Allowed protocols are file and https - Add org.apache.activemq.blob.AllowedProtocols to replace that list - A received URL must be under the upload URL path - The file system strategy stays inside the upload folder - Add org.apache.activemq.blob.HttpFollowRedirects, default true - Default upload URL is https://localhost:8080/uploads/ --- SECURITY.md | 2 +- .../java/org/apache/activemq/BlobMessage.java | 2 +- .../activemq/blob/BlobTransferPolicy.java | 61 ++- .../blob/DefaultBlobDownloadStrategy.java | 72 +++- .../blob/DefaultBlobUploadStrategy.java | 4 +- .../apache/activemq/blob/DefaultStrategy.java | 30 +- .../activemq/blob/FileSystemBlobStrategy.java | 15 +- .../activemq/command/ActiveMQBlobMessage.java | 3 + .../activemq/blob/BlobHttpRedirectTest.java | 153 ++++++++ .../activemq/blob/DownloadStrategyTest.java | 362 +++++++++++++++++- .../activemq/blob/HttpBlobTransferTest.java | 151 ++++++++ .../activemq/blob/LocalFileBlobServer.java | 104 +++++ 12 files changed, 927 insertions(+), 32 deletions(-) create mode 100644 activemq-client/src/test/java/org/apache/activemq/blob/BlobHttpRedirectTest.java create mode 100644 activemq-client/src/test/java/org/apache/activemq/blob/HttpBlobTransferTest.java create mode 100644 activemq-client/src/test/java/org/apache/activemq/blob/LocalFileBlobServer.java diff --git a/SECURITY.md b/SECURITY.md index 080b93d8e06..66d995e8842 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -109,7 +109,7 @@ Some of the most common reported examples: 3. Durable Subscriptions - The JMS spec allows authorized connections to connect to any existing durable subscription (combination of client id and subscription name) as long as it is offline. Authorized clients are allowed to delete the durable subscriptions as well even if they didn't create it. -4. BlobMessages - Blob message support is a side-channel for moving large messages with the JMS API by routing the large message through a different endpoint such as http, sftp or scp. Clients using BlobMessages are responsible for validating the authenticity and validity of the uri provided by the received message before taking any action such as downloading or deleting the file. ActiveMQ recommends using SSL secured transports, with two-way SSL as the most preferred. +4. BlobMessages - Blob message support is a side-channel for moving large messages with the JMS API by routing the large message through a different endpoint: an http or https server, or a shared file system. Clients using BlobMessages are responsible for validating the authenticity and validity of the uri provided by the received message before taking any action such as downloading or deleting the file. ActiveMQ recommends using SSL secured transports, with two-way SSL as the most preferred. ## Non-Security issues diff --git a/activemq-client/src/main/java/org/apache/activemq/BlobMessage.java b/activemq-client/src/main/java/org/apache/activemq/BlobMessage.java index 7ad16ae5c3a..56888c38aed 100644 --- a/activemq-client/src/main/java/org/apache/activemq/BlobMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/BlobMessage.java @@ -37,7 +37,7 @@ public interface BlobMessage extends Message { InputStream getInputStream() throws IOException, JMSException; /** - * Returns the URL for the blob if its available as an external URL (such as file, http, ftp etc) + * Returns the URL for the blob if its available as an external URL (file, http or https) * or null if there is no URL available */ URL getURL() throws MalformedURLException, JMSException; diff --git a/activemq-client/src/main/java/org/apache/activemq/blob/BlobTransferPolicy.java b/activemq-client/src/main/java/org/apache/activemq/blob/BlobTransferPolicy.java index 0e6cad69b2a..7b846d9f134 100644 --- a/activemq-client/src/main/java/org/apache/activemq/blob/BlobTransferPolicy.java +++ b/activemq-client/src/main/java/org/apache/activemq/blob/BlobTransferPolicy.java @@ -16,9 +16,14 @@ */ package org.apache.activemq.blob; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.util.Arrays; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; /** * The policy for configuring how BLOBs (Binary Large OBjects) are transferred @@ -27,7 +32,20 @@ * */ public class BlobTransferPolicy { - private String defaultUploadUrl = "http://localhost:8080/uploads/"; + + public static final Set DEFAULT_ALLOWED_PROTOCOLS = Set.of("file", "https"); + + /** + * Comma separated list of protocols that replaces the defaults. + */ + public static final String ALLOWED_PROTOCOLS_PROPERTY = "org.apache.activemq.blob.AllowedProtocols"; + + /** + * Set to false to stop following HTTP redirects. + */ + public static final String HTTP_FOLLOW_REDIRECTS_PROPERTY = "org.apache.activemq.blob.HttpFollowRedirects"; + + private String defaultUploadUrl = "https://localhost:8080/uploads/"; private String brokerUploadUrl; private String uploadUrl; private int bufferSize = 128 * 1024; @@ -111,6 +129,13 @@ public void setUploadStrategy(BlobUploadStrategy uploadStrategy) { this.uploadStrategy = uploadStrategy; } + /** + * Sets the download strategy to use for downloading BLOBs from some URL + */ + public void setDownloadStrategy(BlobDownloadStrategy downloadStrategy) { + this.downloadStrategy = downloadStrategy; + } + public int getBufferSize() { return bufferSize; } @@ -122,9 +147,34 @@ public void setBufferSize(int bufferSize) { this.bufferSize = bufferSize; } + public static boolean isHttpFollowRedirects() { + final String configured = System.getProperty(HTTP_FOLLOW_REDIRECTS_PROPERTY); + return configured == null || Boolean.parseBoolean(configured.trim()); + } + + public static Set getAllowedProtocols() { + final String configured = System.getProperty(ALLOWED_PROTOCOLS_PROPERTY); + if (configured == null) { + return DEFAULT_ALLOWED_PROTOCOLS; + } + return Arrays.stream(configured.split(",")) + .map(protocol -> protocol.trim().toLowerCase(Locale.ROOT)) + .filter(protocol -> !protocol.isEmpty()) + .collect(Collectors.toUnmodifiableSet()); + } + + public static void checkProtocolAllowed(String protocol) throws IOException { + final String name = protocol == null ? "" : protocol.toLowerCase(Locale.ROOT); + if (!getAllowedProtocols().contains(name)) { + throw new IOException("The URL protocol '" + protocol + "' is not allowed for blob transfer." + + " Allowed protocols are set with " + ALLOWED_PROTOCOLS_PROPERTY); + } + } + /** - * Returns the upload strategy depending on the information from the - * uploadURL. Currently supportet HTTP + * Returns the upload strategy for the upload URL: the file system + * strategy for a file URL, otherwise the default strategy, which + * supports http and https. * * @return */ @@ -147,8 +197,9 @@ protected BlobUploadStrategy createUploadStrategy() { } /** - * Returns the download strategy depending on the information from the - * uploadURL. Currently supportet HTTP + * Returns the download strategy for the upload URL: the file system + * strategy for a file URL, otherwise the default strategy, which + * supports http and https. * * @return */ diff --git a/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobDownloadStrategy.java b/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobDownloadStrategy.java index 51feec59d44..5315219e6b7 100644 --- a/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobDownloadStrategy.java +++ b/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobDownloadStrategy.java @@ -19,7 +19,10 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.URISyntaxException; import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; import jakarta.jms.JMSException; import org.apache.activemq.command.ActiveMQBlobMessage; @@ -38,31 +41,30 @@ public InputStream getInputStream(ActiveMQBlobMessage message) throws IOExceptio if (value == null) { return null; } + validateMessageURL(value); - // Do some checks on the received URL against the transfer policy - URL uploadURL = new URL(super.transferPolicy.getUploadUrl()); - String protocol = message.getURL().getProtocol(); - if (!protocol.equals(uploadURL.getProtocol())) { - throw new IOException("The message URL protocol is incorrect"); + final URLConnection connection = value.openConnection(); + if (connection instanceof HttpURLConnection && !BlobTransferPolicy.isHttpFollowRedirects()) { + final HttpURLConnection http = withRedirectSetting((HttpURLConnection) connection); + final int responseCode = http.getResponseCode(); + if (isRedirectCode(responseCode)) { + http.disconnect(); + throw new IOException("GET of " + value + " was redirected (" + responseCode + + ") and redirects are turned off by " + BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY); + } } - - String host = message.getURL().getHost(); - if (!host.equals(uploadURL.getHost())) { - throw new IOException("The message URL host is incorrect"); - } - - int port = message.getURL().getPort(); - if (uploadURL.getPort() != 0 && port != uploadURL.getPort()) { - throw new IOException("The message URL port is incorrect"); - } - - return value.openStream(); + return connection.getInputStream(); } public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSException { - URL url = createMessageURL(message); + // the stored URL can differ from the upload URL plus message id + URL url = message.getURL(); + if (url == null) { + return; + } + validateMessageURL(url); - HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + HttpURLConnection connection = openHttpConnection(url); connection.setRequestMethod("DELETE"); try { connection.connect(); @@ -77,4 +79,36 @@ public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSExcep } } + /** + * Checks a received URL against the upload URL and the allowed protocols. + */ + protected void validateMessageURL(URL messageURL) throws IOException { + URL uploadURL = new URL(super.transferPolicy.getUploadUrl()); + + if (!messageURL.getProtocol().equals(uploadURL.getProtocol())) { + throw new IOException("The message URL protocol is incorrect"); + } + if (!messageURL.getHost().equals(uploadURL.getHost())) { + throw new IOException("The message URL host is incorrect"); + } + if (uploadURL.getPort() != 0 && messageURL.getPort() != uploadURL.getPort()) { + throw new IOException("The message URL port is incorrect"); + } + // ".." is refused, servers resolve it differently + final String messagePath = decodedPath(messageURL); + if (!messagePath.startsWith(decodedPath(uploadURL)) || Arrays.asList(messagePath.split("/")).contains("..")) { + throw new IOException("The message URL path is incorrect"); + } + checkProtocolAllowed(messageURL); + } + + private static String decodedPath(URL url) throws IOException { + try { + final String path = url.toURI().getPath(); + return path == null ? "" : path; + } catch (URISyntaxException e) { + throw new IOException("The message URL path is incorrect", e); + } + } + } diff --git a/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobUploadStrategy.java b/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobUploadStrategy.java index abb90239718..c879920cb30 100644 --- a/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobUploadStrategy.java +++ b/activemq-client/src/main/java/org/apache/activemq/blob/DefaultBlobUploadStrategy.java @@ -30,7 +30,7 @@ /** * A default implementation of {@link BlobUploadStrategy} which uses the URL - * class to upload files or streams to a remote URL + * class to upload files or streams to a remote http or https URL */ public class DefaultBlobUploadStrategy extends DefaultStrategy implements BlobUploadStrategy { @@ -47,7 +47,7 @@ public URL uploadFile(ActiveMQBlobMessage message, File file) throws JMSExceptio public URL uploadStream(ActiveMQBlobMessage message, InputStream fis) throws JMSException, IOException { URL url = createMessageURL(message); - HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + HttpURLConnection connection = openHttpConnection(url); connection.setRequestMethod("PUT"); connection.setDoOutput(true); diff --git a/activemq-client/src/main/java/org/apache/activemq/blob/DefaultStrategy.java b/activemq-client/src/main/java/org/apache/activemq/blob/DefaultStrategy.java index 7cb67b77751..83c264307ec 100644 --- a/activemq-client/src/main/java/org/apache/activemq/blob/DefaultStrategy.java +++ b/activemq-client/src/main/java/org/apache/activemq/blob/DefaultStrategy.java @@ -16,21 +16,49 @@ */ package org.apache.activemq.blob; +import java.io.IOException; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLConnection; import jakarta.jms.JMSException; import org.apache.activemq.command.ActiveMQBlobMessage; public class DefaultStrategy { - + protected BlobTransferPolicy transferPolicy; public DefaultStrategy(BlobTransferPolicy transferPolicy) { this.transferPolicy = transferPolicy; } + protected void checkProtocolAllowed(URL url) throws IOException { + BlobTransferPolicy.checkProtocolAllowed(url.getProtocol()); + } + + protected HttpURLConnection openHttpConnection(URL url) throws IOException { + checkProtocolAllowed(url); + final URLConnection connection = url.openConnection(); + if (!(connection instanceof HttpURLConnection)) { + throw new IOException("The default blob strategy needs an http or https URL to upload or delete" + + " but the protocol is '" + url.getProtocol() + "'"); + } + return withRedirectSetting((HttpURLConnection) connection); + } + + protected HttpURLConnection withRedirectSetting(HttpURLConnection connection) { + if (!BlobTransferPolicy.isHttpFollowRedirects()) { + connection.setInstanceFollowRedirects(false); + } + return connection; + } + + protected boolean isRedirectCode(int responseCode) { + return responseCode >= 300 && responseCode < 400; + } + protected boolean isSuccessfulCode(int responseCode) { return responseCode >= 200 && responseCode < 300; // 2xx => successful } diff --git a/activemq-client/src/main/java/org/apache/activemq/blob/FileSystemBlobStrategy.java b/activemq-client/src/main/java/org/apache/activemq/blob/FileSystemBlobStrategy.java index 9964188896d..bdab87941ff 100644 --- a/activemq-client/src/main/java/org/apache/activemq/blob/FileSystemBlobStrategy.java +++ b/activemq-client/src/main/java/org/apache/activemq/blob/FileSystemBlobStrategy.java @@ -24,6 +24,7 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Path; import jakarta.jms.JMSException; @@ -115,6 +116,7 @@ public InputStream getInputStream(ActiveMQBlobMessage message) throws IOExceptio * @throws IOException */ protected File getFile(ActiveMQBlobMessage message) throws JMSException, IOException { + BlobTransferPolicy.checkProtocolAllowed("file"); if (message.getURL() != null) { // Do some checks on the received URL protocol String protocol = message.getURL().getProtocol(); @@ -123,7 +125,7 @@ protected File getFile(ActiveMQBlobMessage message) throws JMSException, IOExcep } try { - return new File(message.getURL().toURI()); + return checkUnderRootFolder(new File(message.getURL().toURI())); } catch (URISyntaxException e) { IOException ioe = new IOException("Unable to open file for message " + message); ioe.initCause(e); @@ -132,7 +134,16 @@ protected File getFile(ActiveMQBlobMessage message) throws JMSException, IOExcep } //replace all : with _ to make windows more happy String fileName = message.getJMSMessageID().replaceAll(":", "_"); - return new File(rootFile, fileName); + return checkUnderRootFolder(new File(rootFile, fileName)); } + + private File checkUnderRootFolder(File file) throws IOException { + // compared as written, so a symbolic link in the folder is followed + final Path root = rootFile.toPath().toAbsolutePath().normalize(); + if (!file.toPath().toAbsolutePath().normalize().startsWith(root)) { + throw new IOException("The message URL path is outside the upload URL path"); + } + return file; + } } diff --git a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQBlobMessage.java b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQBlobMessage.java index 3beaa376b22..32bf1f48bad 100644 --- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQBlobMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQBlobMessage.java @@ -180,6 +180,9 @@ public void onSend() throws JMSException { } public void deleteFile() throws IOException, JMSException { + if (blobDownloader == null) { + return; + } blobDownloader.deleteFile(this); } } diff --git a/activemq-client/src/test/java/org/apache/activemq/blob/BlobHttpRedirectTest.java b/activemq-client/src/test/java/org/apache/activemq/blob/BlobHttpRedirectTest.java new file mode 100644 index 00000000000..31cf4c18eaf --- /dev/null +++ b/activemq-client/src/test/java/org/apache/activemq/blob/BlobHttpRedirectTest.java @@ -0,0 +1,153 @@ +/** + * 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.activemq.blob; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.URL; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.apache.activemq.command.ActiveMQBlobMessage; +import org.apache.activemq.command.MessageId; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.sun.net.httpserver.HttpServer; + +public class BlobHttpRedirectTest { + + private static final String TARGET = "/elsewhere/target"; + + private final List requests = new CopyOnWriteArrayList<>(); + private HttpServer server; + private BlobTransferPolicy transferPolicy; + + @Before + public void setUp() throws Exception { + server = HttpServer.create(new InetSocketAddress("localhost", 0), 0); + server.createContext("/uploads/", exchange -> { + requests.add(exchange.getRequestMethod() + " " + exchange.getRequestURI().getPath()); + exchange.getRequestBody().readAllBytes(); + exchange.getResponseHeaders().add("Location", TARGET); + exchange.sendResponseHeaders(302, -1); + exchange.close(); + }); + server.createContext(TARGET, exchange -> { + requests.add(exchange.getRequestMethod() + " " + exchange.getRequestURI().getPath()); + exchange.getRequestBody().readAllBytes(); + final byte[] body = "moved".getBytes(); + exchange.sendResponseHeaders(200, body.length); + exchange.getResponseBody().write(body); + exchange.close(); + }); + server.start(); + + transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl("http://localhost:" + server.getAddress().getPort() + "/uploads/"); + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, "http"); + } + + @After + public void tearDown() { + System.clearProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY); + System.clearProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY); + if (server != null) { + server.stop(0); + } + } + + private ActiveMQBlobMessage message() throws Exception { + final ActiveMQBlobMessage message = new ActiveMQBlobMessage(); + message.setMessageId(new MessageId("ID:test-1:1:1:1")); + message.setURL(new URL(transferPolicy.getUploadUrl() + "blob")); + return message; + } + + private boolean targetReached() { + return requests.stream().anyMatch(request -> request.endsWith(" " + TARGET)); + } + + @Test + public void testDownloadFollowsRedirectByDefault() throws Exception { + assertTrue(BlobTransferPolicy.isHttpFollowRedirects()); + + try (var in = transferPolicy.getDownloadStrategy().getInputStream(message())) { + assertEquals("moved", new String(in.readAllBytes())); + } + assertTrue(requests.toString(), targetReached()); + } + + @Test + public void testDownloadRefusesRedirectWhenNotFollowing() throws Exception { + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, "false"); + final ActiveMQBlobMessage message = message(); + + IOException ex = assertThrows(IOException.class, + () -> transferPolicy.getDownloadStrategy().getInputStream(message)); + assertTrue(ex.getMessage(), ex.getMessage().contains("was redirected (302)")); + assertTrue(ex.getMessage(), ex.getMessage().contains(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY)); + assertEquals(List.of("GET /uploads/blob"), requests); + } + + @Test + public void testDeleteRefusesRedirectWhenNotFollowing() throws Exception { + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, "false"); + final ActiveMQBlobMessage message = message(); + + IOException ex = assertThrows(IOException.class, + () -> transferPolicy.getDownloadStrategy().deleteFile(message)); + assertTrue(ex.getMessage(), ex.getMessage().contains("302")); + assertEquals(List.of("DELETE /uploads/blob"), requests); + } + + @Test + public void testUploadRefusesRedirectWhenNotFollowing() throws Exception { + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, "false"); + final ActiveMQBlobMessage message = message(); + + IOException ex = assertThrows(IOException.class, + () -> transferPolicy.getUploadStrategy().uploadStream(message, new ByteArrayInputStream(new byte[] {1}))); + assertTrue(ex.getMessage(), ex.getMessage().contains("302")); + assertFalse(requests.toString(), targetReached()); + } + + @Test + public void testFollowRedirectsPropertyValues() throws Exception { + assertTrue("followed when not set", BlobTransferPolicy.isHttpFollowRedirects()); + + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, "true"); + assertTrue(BlobTransferPolicy.isHttpFollowRedirects()); + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, " TRUE "); + assertTrue(BlobTransferPolicy.isHttpFollowRedirects()); + + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, "false"); + assertFalse(BlobTransferPolicy.isHttpFollowRedirects()); + // anything that is not true turns redirects off + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, ""); + assertFalse(BlobTransferPolicy.isHttpFollowRedirects()); + System.setProperty(BlobTransferPolicy.HTTP_FOLLOW_REDIRECTS_PROPERTY, "no"); + assertFalse(BlobTransferPolicy.isHttpFollowRedirects()); + } +} diff --git a/activemq-client/src/test/java/org/apache/activemq/blob/DownloadStrategyTest.java b/activemq-client/src/test/java/org/apache/activemq/blob/DownloadStrategyTest.java index c70122e35c2..4e08381cfd7 100644 --- a/activemq-client/src/test/java/org/apache/activemq/blob/DownloadStrategyTest.java +++ b/activemq-client/src/test/java/org/apache/activemq/blob/DownloadStrategyTest.java @@ -18,18 +18,378 @@ package org.apache.activemq.blob; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.file.Files; +import java.util.Set; import jakarta.jms.JMSException; import org.apache.activemq.command.ActiveMQBlobMessage; +import org.apache.activemq.command.MessageId; +import org.junit.After; +import org.junit.Assume; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; public class DownloadStrategyTest { - + + private static final String NOT_ALLOWED = "' is not allowed for blob transfer." + + " Allowed protocols are set with " + BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY; + private static final String FTP_NOT_ALLOWED = "The URL protocol 'ftp" + NOT_ALLOWED; + private static final String FTP_NEEDS_HTTP = + "The default blob strategy needs an http or https URL to upload or delete but the protocol is 'ftp'"; + + @Rule + public TemporaryFolder blobFolder = new TemporaryFolder(); + + @After + public void clearAllowedProtocols() { + System.clearProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY); + } + + @Test + public void testDefaultUploadUrl() throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + assertEquals("https://localhost:8080/uploads/", transferPolicy.getDefaultUploadUrl()); + assertEquals("https://localhost:8080/uploads/", transferPolicy.getUploadUrl()); + } + + @Test + public void testDefaultAllowedProtocols() throws Exception { + assertEquals(Set.of("file", "https"), BlobTransferPolicy.DEFAULT_ALLOWED_PROTOCOLS); + assertEquals(BlobTransferPolicy.DEFAULT_ALLOWED_PROTOCOLS, BlobTransferPolicy.getAllowedProtocols()); + + assertNotAllowed("http", () -> upload("http://localhost/blobs/")); + assertNotAllowed("http", () -> download("http://localhost/blobs/")); + assertNotAllowed("http", () -> delete("http://localhost/blobs/")); + } + + @Test + public void testAllowedProtocolsProperty() throws Exception { + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, "https"); + assertEquals(Set.of("https"), BlobTransferPolicy.getAllowedProtocols()); + + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, " HTTPS , http "); + assertEquals(Set.of("http", "https"), BlobTransferPolicy.getAllowedProtocols()); + + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, ""); + assertEquals(Set.of(), BlobTransferPolicy.getAllowedProtocols()); + } + + @Test + public void testAllowedProtocolsPropertyCanAddFtp() throws Exception { + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, "ftp,https"); + assertEquals(Set.of("ftp", "https"), BlobTransferPolicy.getAllowedProtocols()); + BlobTransferPolicy.checkProtocolAllowed("ftp"); + + // nothing listens on port 1 + IOException downloadFailure = assertThrows(IOException.class, () -> download("ftp://localhost:1/blobs/")); + assertFalse(downloadFailure.toString(), downloadFailure.toString().contains("not allowed")); + + IOException uploadFailure = assertThrows(IOException.class, () -> upload("ftp://localhost/blobs/")); + assertEquals(FTP_NEEDS_HTTP, uploadFailure.getMessage()); + IOException deleteFailure = assertThrows(IOException.class, () -> delete("ftp://localhost/blobs/")); + assertEquals(FTP_NEEDS_HTTP, deleteFailure.getMessage()); + } + + @Test + public void testOnlyHttpsAllowed() throws Exception { + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, "https"); + + BlobTransferPolicy.checkProtocolAllowed("https"); + assertNotAllowed("http", () -> upload("http://localhost/blobs/")); + assertNotAllowed("http", () -> download("http://localhost/blobs/")); + assertNotAllowed("http", () -> delete("http://localhost/blobs/")); + assertNotAllowed("file", () -> upload(blobFolder.getRoot().toURI().toString())); + } + + @Test + public void testNoProtocolAllowed() throws Exception { + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, ""); + + assertNotAllowed("https", () -> upload("https://localhost/blobs/")); + assertNotAllowed("https", () -> download("https://localhost/blobs/")); + assertNotAllowed("http", () -> upload("http://localhost/blobs/")); + assertNotAllowed("file", () -> upload(blobFolder.getRoot().toURI().toString())); + assertNotAllowed("file", () -> download(blobFolder.getRoot().toURI().toString())); + assertNotAllowed("file", () -> delete(blobFolder.getRoot().toURI().toString())); + } + + @Test + public void testFileAllowedWhenListed() throws Exception { + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, "file"); + + final String uploadUrl = blobFolder.getRoot().toURI().toString(); + upload(uploadUrl); + download(uploadUrl).close(); + delete(uploadUrl); + assertNotAllowed("https", () -> upload("https://localhost/blobs/")); + } + + private static final String PATH_INCORRECT = "The message URL path is incorrect"; + + private static DefaultBlobDownloadStrategy defaultStrategy(String uploadUrl) { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadUrl); + return new DefaultBlobDownloadStrategy(transferPolicy); + } + + private static void assertPathIncorrect(DefaultBlobDownloadStrategy strategy, String url) throws Exception { + final URL messageUrl = new URL(url); + IOException ex = assertThrows(url, IOException.class, () -> strategy.validateMessageURL(messageUrl)); + assertEquals(url, PATH_INCORRECT, ex.getMessage()); + } + + @Test + public void testMessageUrlMustBeUnderUploadUrlPath() throws Exception { + final DefaultBlobDownloadStrategy strategy = defaultStrategy("https://blob-server/uploads/"); + + strategy.validateMessageURL(new URL("https://blob-server/uploads/ID:producer:1:1:1")); + strategy.validateMessageURL(new URL("https://blob-server/uploads/server/chosen/location")); + strategy.validateMessageURL(new URL("https://blob-server/uploads/file?version=2")); + + assertPathIncorrect(strategy, "https://blob-server/other"); + assertPathIncorrect(strategy, "https://blob-server/"); + assertPathIncorrect(strategy, "https://blob-server/uploads-other/file"); + assertPathIncorrect(strategy, "https://blob-server/uploads/../other"); + assertPathIncorrect(strategy, "https://blob-server/uploads/%2e%2e/other"); + assertPathIncorrect(strategy, "https://blob-server/uploads/sub/..%2f..%2fother"); + assertPathIncorrect(strategy, "https://blob-server/uploads//../other"); + assertPathIncorrect(strategy, "https://blob-server/uploads/sub/../file"); + assertPathIncorrect(strategy, "https://blob-server/uploads/..\\other"); + } + + @Test + public void testMessageUrlPathWithoutTrailingSlashOnUploadUrl() throws Exception { + final DefaultBlobDownloadStrategy strategy = defaultStrategy("https://blob-server/uploads"); + + strategy.validateMessageURL(new URL("https://blob-server/uploads/file")); + // the upload appends the message id straight onto the upload URL + strategy.validateMessageURL(new URL("https://blob-server/uploadsID:producer:1:1:1")); + + assertPathIncorrect(strategy, "https://blob-server/other"); + assertPathIncorrect(strategy, "https://blob-server/uploads/../other"); + } + + @Test + public void testMessageUrlPathWithNoPathOnUploadUrl() throws Exception { + final DefaultBlobDownloadStrategy strategy = defaultStrategy("https://blob-server"); + + strategy.validateMessageURL(new URL("https://blob-server/anything/at/all")); + assertPathIncorrect(strategy, "https://blob-server/anything/../at/all"); + } + + @Test + public void testDownloadAndDeleteRefuseUrlOutsideUploadUrlPath() throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setBrokerUploadUrl("https://blob-server/uploads/"); + final BlobDownloadStrategy strategy = transferPolicy.getDownloadStrategy(); + + final ActiveMQBlobMessage message = blobMessage(); + message.setURL(new URL("https://blob-server/other")); + + IOException read = assertThrows(IOException.class, () -> strategy.getInputStream(message)); + assertEquals(PATH_INCORRECT, read.getMessage()); + IOException delete = assertThrows(IOException.class, () -> strategy.deleteFile(message)); + assertEquals(PATH_INCORRECT, delete.getMessage()); + } + + private static final String OUTSIDE_UPLOAD_PATH = "The message URL path is outside the upload URL path"; + + private static ActiveMQBlobMessage fileMessage(String url) throws Exception { + ActiveMQBlobMessage message = blobMessage(); + message.setURL(new URL(url)); + return message; + } + + private void assertOutsideUploadPath(BlobDownloadStrategy strategy, String url) throws Exception { + final ActiveMQBlobMessage message = fileMessage(url); + IOException read = assertThrows(url, IOException.class, () -> strategy.getInputStream(message)); + assertEquals(OUTSIDE_UPLOAD_PATH, read.getMessage()); + IOException delete = assertThrows(url, IOException.class, () -> strategy.deleteFile(message)); + assertEquals(OUTSIDE_UPLOAD_PATH, delete.getMessage()); + } + + @Test + public void testFileStrategyRefusesFileOutsideUploadFolder() throws Exception { + final File uploadFolder = blobFolder.newFolder("blobs"); + final File outside = blobFolder.newFile("outside.txt"); + Files.writeString(outside.toPath(), "secret"); + blobFolder.newFolder("blobs-other"); + final File sibling = new File(blobFolder.getRoot(), "blobs-other/file.txt"); + Files.writeString(sibling.toPath(), "secret"); + + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadFolder.toURI().toString()); + BlobDownloadStrategy strategy = transferPolicy.getDownloadStrategy(); + + final String uploadUrl = uploadFolder.toURI().toString(); + assertOutsideUploadPath(strategy, outside.toURI().toString()); + assertOutsideUploadPath(strategy, uploadUrl + "../outside.txt"); + assertOutsideUploadPath(strategy, uploadUrl + "%2e%2e/outside.txt"); + assertOutsideUploadPath(strategy, uploadUrl + "sub/../../outside.txt"); + assertOutsideUploadPath(strategy, sibling.toURI().toString()); + + assertTrue("nothing outside the upload folder was deleted", outside.exists() && sibling.exists()); + } + + @Test + public void testFileStrategyRefusesFileOutsideBrokerUploadFolder() throws Exception { + final File uploadFolder = blobFolder.newFolder("blobs"); + final File outside = blobFolder.newFile("outside.txt"); + + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setBrokerUploadUrl(uploadFolder.toURI().toString()); + + assertOutsideUploadPath(transferPolicy.getDownloadStrategy(), outside.toURI().toString()); + assertTrue(outside.exists()); + } + + @Test + public void testFileStrategyRefusesMessageIdThatLeavesUploadFolder() throws Exception { + final File uploadFolder = blobFolder.newFolder("blobs"); + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadFolder.toURI().toString()); + BlobDownloadStrategy strategy = transferPolicy.getDownloadStrategy(); + + // no URL on the message, so the file name comes from the message id + ActiveMQBlobMessage message = new ActiveMQBlobMessage(); + message.setMessageId(new MessageId("ID:x/../../outside:1:1:1")); + + IOException ex = assertThrows(IOException.class, () -> strategy.deleteFile(message)); + assertEquals(OUTSIDE_UPLOAD_PATH, ex.getMessage()); + } + + @Test + public void testFileStrategyReadsAndDeletesUnderUploadFolder() throws Exception { + final File uploadFolder = blobFolder.newFolder("blobs"); + final File nested = new File(uploadFolder, "sub/inside.txt"); + new File(uploadFolder, "sub/other").mkdirs(); + Files.writeString(nested.toPath(), "blob"); + + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadFolder.toURI().toString()); + BlobDownloadStrategy strategy = transferPolicy.getDownloadStrategy(); + + final ActiveMQBlobMessage message = fileMessage(uploadFolder.toURI() + "sub/other/../inside.txt"); + try (var in = strategy.getInputStream(message)) { + assertEquals("blob", new String(in.readAllBytes())); + } + strategy.deleteFile(message); + assertFalse(nested.exists()); + } + + @Test + public void testFileStrategyFollowsSymbolicLinkInUploadFolder() throws Exception { + final File uploadFolder = blobFolder.newFolder("blobs"); + final File outside = blobFolder.newFile("outside.txt"); + Files.writeString(outside.toPath(), "linked"); + final File link = new File(uploadFolder, "link.txt"); + try { + Files.createSymbolicLink(link.toPath(), outside.toPath()); + } catch (IOException | UnsupportedOperationException e) { + Assume.assumeNoException("symbolic links are not available here", e); + } + + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadFolder.toURI().toString()); + + try (var in = transferPolicy.getDownloadStrategy().getInputStream(fileMessage(link.toURI().toString()))) { + assertEquals("linked", new String(in.readAllBytes())); + } + } + + private interface Transfer { + void run() throws Exception; + } + + private static void assertNotAllowed(String protocol, Transfer transfer) { + IOException ex = assertThrows(IOException.class, transfer::run); + assertEquals("The URL protocol '" + protocol + NOT_ALLOWED, ex.getMessage()); + } + + private static ActiveMQBlobMessage blobMessage() { + ActiveMQBlobMessage message = new ActiveMQBlobMessage(); + message.setMessageId(new MessageId("ID:test-1:1:1:1")); + return message; + } + + private static void upload(String uploadUrl) throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadUrl); + transferPolicy.getUploadStrategy().uploadStream(blobMessage(), new ByteArrayInputStream(new byte[] {1})); + } + + private static java.io.InputStream download(String uploadUrl) throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadUrl); + ActiveMQBlobMessage message = blobMessage(); + if (!uploadUrl.startsWith("file:")) { + message.setURL(new URL(uploadUrl + "file")); + } + return transferPolicy.getDownloadStrategy().getInputStream(message); + } + + private static void delete(String uploadUrl) throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl(uploadUrl); + ActiveMQBlobMessage message = blobMessage(); + if (!uploadUrl.startsWith("file:")) { + message.setURL(new URL(uploadUrl + "file")); + } + transferPolicy.getDownloadStrategy().deleteFile(message); + } + + @Test + public void testDefaultBlobDownloadStrategyRejectsFtp() throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl("ftp://localhost/blobs/"); + BlobDownloadStrategy downloadStrategy = transferPolicy.getDownloadStrategy(); + + ActiveMQBlobMessage message = new ActiveMQBlobMessage(); + message.setURL(new URL("ftp://localhost/blobs/file")); + + IOException ex = assertThrows(IOException.class, () -> downloadStrategy.getInputStream(message)); + assertEquals(FTP_NOT_ALLOWED, ex.getMessage()); + } + + @Test + public void testDefaultBlobDownloadStrategyRejectsFtpDelete() throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl("ftp://localhost/blobs/"); + BlobDownloadStrategy downloadStrategy = transferPolicy.getDownloadStrategy(); + + ActiveMQBlobMessage message = new ActiveMQBlobMessage(); + message.setMessageId(new MessageId("ID:test-1:1:1:1")); + message.setURL(new URL("ftp://localhost/blobs/file")); + + IOException ex = assertThrows(IOException.class, () -> downloadStrategy.deleteFile(message)); + assertEquals(FTP_NOT_ALLOWED, ex.getMessage()); + } + + @Test + public void testDefaultBlobUploadStrategyRejectsFtp() throws Exception { + BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); + transferPolicy.setUploadUrl("ftp://localhost/blobs/"); + BlobUploadStrategy uploadStrategy = transferPolicy.getUploadStrategy(); + + ActiveMQBlobMessage message = new ActiveMQBlobMessage(); + message.setMessageId(new MessageId("ID:test-1:1:1:1")); + + IOException ex = assertThrows(IOException.class, + () -> uploadStrategy.uploadStream(message, new ByteArrayInputStream(new byte[] {1}))); + assertEquals(FTP_NOT_ALLOWED, ex.getMessage()); + } + @Test public void testDefaultBlobDownloadStrategy() throws Exception { BlobTransferPolicy transferPolicy = new BlobTransferPolicy(); diff --git a/activemq-client/src/test/java/org/apache/activemq/blob/HttpBlobTransferTest.java b/activemq-client/src/test/java/org/apache/activemq/blob/HttpBlobTransferTest.java new file mode 100644 index 00000000000..a1739df3974 --- /dev/null +++ b/activemq-client/src/test/java/org/apache/activemq/blob/HttpBlobTransferTest.java @@ -0,0 +1,151 @@ +/** + * 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.activemq.blob; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +import jakarta.jms.JMSException; + +import org.apache.activemq.command.ActiveMQBlobMessage; +import org.apache.activemq.command.MessageId; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class HttpBlobTransferTest { + + private Path root; + private LocalFileBlobServer blobServer; + private BlobTransferPolicy policy; + + @Before + public void setUp() throws Exception { + root = Files.createTempDirectory("http-blob-test"); + blobServer = new LocalFileBlobServer(root); + blobServer.start(); + policy = new BlobTransferPolicy(); + policy.setUploadUrl(blobServer.getUploadUrl()); + System.setProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY, "http"); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(BlobTransferPolicy.ALLOWED_PROTOCOLS_PROPERTY); + if (blobServer != null) { + blobServer.stop(); + } + if (root != null) { + try (var paths = Files.walk(root)) { + paths.sorted(java.util.Comparator.reverseOrder()).forEach(p -> p.toFile().delete()); + } + } + } + + private ActiveMQBlobMessage blobMessage(String messageId) { + var message = new ActiveMQBlobMessage(); + message.setMessageId(new MessageId(messageId)); + return message; + } + + @Test + public void testUploadDownloadDeleteRoundTrip() throws Exception { + var message = blobMessage("ID:producer:1:1:1"); + var content = "large out-of-band payload".getBytes(StandardCharsets.UTF_8); + + var url = new DefaultBlobUploadStrategy(policy) + .uploadStream(message, new ByteArrayInputStream(content)); + message.setURL(url); + assertTrue("blob should be stored on the server", blobServer.exists("ID:producer:1:1:1")); + + var download = new DefaultBlobDownloadStrategy(policy); + try (var in = download.getInputStream(message)) { + assertEquals(new String(content, StandardCharsets.UTF_8), + new String(in.readAllBytes(), StandardCharsets.UTF_8)); + } + + download.deleteFile(message); + assertFalse("blob should be deleted from the server", blobServer.exists("ID:producer:1:1:1")); + } + + @Test + public void testDeleteFollowsStoredUrlNotReconstructed() throws Exception { + blobServer.put("server/chosen/location", "payload".getBytes(StandardCharsets.UTF_8)); + assertTrue(blobServer.exists("server/chosen/location")); + + var message = blobMessage("ID:producer:9:9:9"); + message.setURL(new URL(blobServer.getUploadUrl() + "server/chosen/location")); + + new DefaultBlobDownloadStrategy(policy).deleteFile(message); + + assertFalse("delete must follow the stored URL", blobServer.exists("server/chosen/location")); + } + + @Test + public void testDeleteFileWithoutDownloaderIsNoOp() throws Exception { + var message = blobMessage("ID:producer:2:2:2"); + message.deleteFile(); + } + + @Test + public void testDeleteFileViaMessageWithDownloader() throws Exception { + var message = blobMessage("ID:producer:3:3:3"); + var url = new DefaultBlobUploadStrategy(policy) + .uploadStream(message, new ByteArrayInputStream("payload".getBytes(StandardCharsets.UTF_8))); + message.setURL(url); + message.setBlobDownloader(new BlobDownloader(policy)); + assertTrue(blobServer.exists("ID:producer:3:3:3")); + + message.deleteFile(); + + assertFalse(blobServer.exists("ID:producer:3:3:3")); + } + + @Test + public void testSetDownloadStrategy() { + var custom = new DefaultBlobDownloadStrategy(policy); + policy.setDownloadStrategy(custom); + assertSame("explicitly set download strategy should be returned", + custom, policy.getDownloadStrategy()); + } + + @Test + public void testDeleteRejectsForeignHost() throws Exception { + var message = blobMessage("ID:producer:4:4:4"); + message.setURL(new URL("http://evil.example.com:" + hostPort() + "/uploads/ID:producer:4:4:4")); + try { + new DefaultBlobDownloadStrategy(policy).deleteFile(message); + fail("expected the foreign host to be rejected"); + } catch (IOException expected) { + assertEquals("The message URL host is incorrect", expected.getMessage()); + } + } + + private int hostPort() throws Exception { + return new URL(blobServer.getUploadUrl()).getPort(); + } +} diff --git a/activemq-client/src/test/java/org/apache/activemq/blob/LocalFileBlobServer.java b/activemq-client/src/test/java/org/apache/activemq/blob/LocalFileBlobServer.java new file mode 100644 index 00000000000..b2930bcceaa --- /dev/null +++ b/activemq-client/src/test/java/org/apache/activemq/blob/LocalFileBlobServer.java @@ -0,0 +1,104 @@ +/** + * 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.activemq.blob; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.file.Files; +import java.nio.file.Path; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; + +/** + * A blob server over a local folder for tests. + */ +public class LocalFileBlobServer { + + private static final String CONTEXT = "/uploads/"; + + private final Path root; + private HttpServer server; + + public LocalFileBlobServer(Path root) throws IOException { + this.root = root; + Files.createDirectories(root); + } + + public void start() throws IOException { + server = HttpServer.create(new InetSocketAddress("localhost", 0), 0); + server.createContext(CONTEXT, this::handle); + server.start(); + } + + public void stop() { + if (server != null) { + server.stop(0); + server = null; + } + } + + public String getUploadUrl() { + return "http://localhost:" + server.getAddress().getPort() + CONTEXT; + } + + public void put(String key, byte[] data) throws IOException { + Files.write(fileFor(key), data); + } + + public boolean exists(String key) { + return Files.isRegularFile(fileFor(key)); + } + + private Path fileFor(String key) { + return root.resolve(key.replaceAll("[^A-Za-z0-9._-]", "_")); + } + + private void handle(HttpExchange exchange) throws IOException { + try { + var key = exchange.getRequestURI().getPath().substring(CONTEXT.length()); + switch (exchange.getRequestMethod()) { + case "PUT": + try (var in = exchange.getRequestBody()) { + Files.write(fileFor(key), in.readAllBytes()); + } + exchange.sendResponseHeaders(204, -1); + break; + case "GET": + var file = fileFor(key); + if (!Files.isRegularFile(file)) { + exchange.sendResponseHeaders(404, -1); + break; + } + var body = Files.readAllBytes(file); + exchange.sendResponseHeaders(200, body.length); + try (var out = exchange.getResponseBody()) { + out.write(body); + } + break; + case "DELETE": + Files.deleteIfExists(fileFor(key)); + exchange.sendResponseHeaders(204, -1); + break; + default: + exchange.sendResponseHeaders(405, -1); + } + } finally { + exchange.close(); + } + } +}