Skip to content
Open
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
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,7 +32,20 @@
*
*/
public class BlobTransferPolicy {
private String defaultUploadUrl = "http://localhost:8080/uploads/";

public static final Set<String> 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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<String> 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
*/
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ public void onSend() throws JMSException {
}

public void deleteFile() throws IOException, JMSException {
if (blobDownloader == null) {
return;
}
blobDownloader.deleteFile(this);
}
}
Loading
Loading