fix(iot): authenticate unsigned MQTT over WebSocket with the default authorizer (#8653)

This commit is contained in:
rajp33 authored and GitHub committed 2026-11-18 21:04:28 -04:00
1 parent 6871348ea6
commit 813f89bd8e
15 files changed
+235 -67

No files matched your search

@@ -62,6 +62,11 @@ public class IotCustomAuthorizer {
*/
record WebSocketUpgrade(Map<String, String> headers, String queryString, String serverName, String sourceIp,
int sourcePort) {
boolean isSigV4Signed() {
String signature = params(null, Map.of(), queryString).get("X-Amz-Signature");
return signature != null && !signature.isEmpty();
}
}
@Inject
@@ -134,14 +139,19 @@ public class IotCustomAuthorizer {
}
/**
* The policies an MQTT CONNECT through the named authorizer is admitted with. None when the
* authorizer is not ACTIVE, which never invokes the function, or when the function denies or
* returns no policy; an {@link AwsException} when the authorizer does not exist, a signature
* does not verify (the function is not invoked) or the answer is one TestInvokeAuthorizer rejects.
* The policies an MQTT CONNECT through the named authorizer, or the default authorizer when
* {@code name} is null, is admitted with. None when the authorizer is not ACTIVE, which never
* invokes the function, or when the function denies or returns no policy; an
* {@link AwsException} when the authorizer does not exist or no default authorizer is set, a
* signature does not verify (the function is not invoked) or the answer is one
* TestInvokeAuthorizer rejects.
*/
List<String> connectPolicies(String name, String clientId, String username, String password, WebSocketUpgrade upgrade) {
// ponytail: the broker has no request context, so a connection finds authorizers in the default account and region.
IotAuthorizer authorizer = authorizers.describeAuthorizer(name, regionResolver.getDefaultRegion());
String region = regionResolver.getDefaultRegion();
IotAuthorizer authorizer = name == null
? authorizers.describeDefaultAuthorizer(region)
: authorizers.describeAuthorizer(name, region);
if (!"ACTIVE".equals(authorizer.getStatus())) {
return List.of();
}
@@ -446,8 +446,9 @@ public class IotMqttBrokerService implements Resettable {
* has registered and whose attached policies allow {@code iot:Connect} for the client id;
* anything else is answered with CONNACK not authorized (return code 5, reason code 0x87 on
* MQTT 5) and never becomes a session, so a client already holding that client id keeps its
* connection. The plaintext listener, which the WebSocket bridge also lands on, stays open to
* every client except one that names a custom authorizer, see {@link #authorizeCustom}.
* connection. The plaintext listener stays open to every client except one that names a custom
* authorizer, see {@link #authorizeCustom}. A WebSocket client that names none goes through the
* default authorizer unless its upgrade carries a SigV4 signature, which is not verified.
*/
void handleEndpoint(MqttEndpoint endpoint, boolean verifyDevice) {
long connectNanos = nanoTime.getAsLong();
@@ -472,7 +473,7 @@ public class IotMqttBrokerService implements Resettable {
}
String username = endpoint.auth() == null ? null : endpoint.auth().getUsername();
String authorizerName = IotCustomAuthorizer.authorizerName(username, upgrade);
if (authorizerName != null) {
if (authorizerName != null || (upgrade != null && !upgrade.isSigV4Signed())) {
authorizeCustom(endpoint, authorizerName, upgrade, sourceIp, sourcePort, connectNanos);
return;
}
@@ -481,10 +482,11 @@ public class IotMqttBrokerService implements Resettable {
}
/**
* A CONNECT naming a custom authorizer is admitted only when the authorizer's function allows
* it and the policies it returns allow {@code iot:Connect} for the client id. The function runs
* off the event loop. A refusal closes an MQTT 3.1.1 connection without a CONNACK and answers
* MQTT 5 with reason code 0x87, as AWS IoT does.
* A CONNECT through the named custom authorizer, or the default authorizer when
* {@code authorizerName} is null, is admitted only when the authorizer's function allows it and
* the policies it returns allow {@code iot:Connect} for the client id; with no default authorizer
* set it is refused. The function runs off the event loop. A refusal closes an MQTT 3.1.1
* connection without a CONNACK and answers MQTT 5 with reason code 0x87, as AWS IoT does.
*/
private void authorizeCustom(MqttEndpoint endpoint, String authorizerName, IotCustomAuthorizer.WebSocketUpgrade upgrade,
String sourceIp, int sourcePort, long connectNanos) {
@@ -505,7 +507,8 @@ public class IotMqttBrokerService implements Resettable {
admit(endpoint, null, sourceIp, sourcePort, connectNanos);
return;
}
LOG.debugv("IoT MQTT client {0} refused by custom authorizer {1}: {2}", clientId, authorizerName,
LOG.debugv("IoT MQTT client {0} refused by custom authorizer {1}: {2}", clientId,
authorizerName == null ? "(default)" : authorizerName,
decision.failed() ? decision.cause().getMessage() : "iot:Connect is not allowed");
if (endpoint.protocolVersion() != 5) {
endpoint.close();
@@ -24,9 +24,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
* are piped byte for byte to the broker's plaintext listener, so every broker feature and every
* authorization rule applies without a second code path. No client certificate is involved: on
* AWS this path authenticates by SigV4 or a custom authorizer. The broker enforces a custom
* authorizer named in the MQTT username, the URL query or an upgrade header, and stays permissive
* otherwise; the upgrade's headers, query, server name and client address reach it through
* {@link IotMqttBrokerService#webSocketUpgrades()}.
* authorizer named in the MQTT username, the URL query or an upgrade header, else the default
* authorizer unless the upgrade carries a SigV4 signature; the upgrade's headers, query, server
* name and client address reach it through {@link IotMqttBrokerService#webSocketUpgrades()}.
*
* <p>Everything for one session runs on the event loop of the WebSocket's connection, the
* broker socket included, so the handlers below never race each other.