You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/10/01 19:50:24 UTC
(pulsar) 25/32: [fix][ws] Check dead letter topic permission for WebSocket consumers (#26772)
This is an automated email from the ASF dual-hosted git repository.
lhotari pushed a commit to branch lh-branch-4.0-backports-2026-10-01
in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit 97938e1db57e6b7774bca133254a34accf8631af
Author: Lari Hotari <lh...@users.noreply.github.com>
AuthorDate: Wed Sep 30 17:57:03 2026 +0300
[fix][ws] Check dead letter topic permission for WebSocket consumers (#26772)
(cherry picked from commit 2516cf3abdd7316750b5991390b952ebbd687357)
---
.../apache/pulsar/websocket/ConsumerHandler.java | 28 ++
.../websocket/MultiTopicConsumerHandler.java | 8 +-
.../pulsar/websocket/ConsumerHandlerTest.java | 283 +++++++++++++++++++++
3 files changed, 317 insertions(+), 2 deletions(-)
diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ConsumerHandler.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ConsumerHandler.java
index 8566ab5b5117..ba82b36efab1 100644
--- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ConsumerHandler.java
+++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ConsumerHandler.java
@@ -20,6 +20,8 @@ package org.apache.pulsar.websocket;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+import static org.apache.commons.lang3.StringUtils.trim;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Enums;
import com.google.common.base.Splitter;
@@ -28,6 +30,7 @@ import com.google.common.cache.CacheBuilder;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
+import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
@@ -48,6 +51,7 @@ import org.apache.pulsar.client.api.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.SubscriptionMode;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.impl.ConsumerBuilderImpl;
+import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.policies.data.TopicOperation;
import org.apache.pulsar.common.util.Codec;
import org.apache.pulsar.common.util.DateFormatter;
@@ -77,6 +81,7 @@ public class ConsumerHandler extends AbstractWebSocketHandler {
private SubscriptionType subscriptionType;
private SubscriptionMode subscriptionMode;
private Consumer<byte[]> consumer;
+ private TopicName deadLetterTopic;
private int maxPendingMessages = 0;
private final AtomicInteger pendingMessages = new AtomicInteger();
@@ -120,6 +125,15 @@ public class ConsumerHandler extends AbstractWebSocketHandler {
}
this.subscriptionType = builder.getConf().getSubscriptionType();
this.subscriptionMode = builder.getConf().getSubscriptionMode();
+ if (service.isAuthorizationEnabled() && builder.getConf().getDeadLetterPolicy() != null) {
+ DeadLetterPolicy deadLetterPolicy = builder.getConf().getDeadLetterPolicy();
+ // Trim like ProducerBuilder.topic() so the checked topic is the one the DLQ producer uses.
+ String destination = trim(deadLetterPolicy.getDeadLetterTopic());
+ // A blank topic would make the client derive a separate DLQ topic for each partition.
+ checkArgument(isNotBlank(destination), "Dead letter topic must not be blank");
+ this.deadLetterTopic = TopicName.get(destination);
+ deadLetterPolicy.setDeadLetterTopic(destination);
+ }
if (!checkAuth(response)) {
return;
@@ -495,6 +509,12 @@ public class ConsumerHandler extends AbstractWebSocketHandler {
this.subscription);
return service.getAuthorizationService()
.allowTopicOperationAsync(topic, TopicOperation.CONSUME, authRole, subscription)
+ .thenCompose(allowed -> {
+ if (!allowed) {
+ return CompletableFuture.completedFuture(allowed);
+ }
+ return authorizeDeadLetterTopic(authRole, authenticationData);
+ })
.get(service.getConfig().getMetadataStoreOperationTimeoutSeconds(), SECONDS);
} catch (TimeoutException e) {
log.warn("Time-out {} sec while checking authorization on {} ",
@@ -507,6 +527,14 @@ public class ConsumerHandler extends AbstractWebSocketHandler {
}
}
+ CompletableFuture<Boolean> authorizeDeadLetterTopic(String authRole, AuthenticationDataSource authenticationData) {
+ if (deadLetterTopic == null) {
+ return CompletableFuture.completedFuture(true);
+ }
+ return service.getAuthorizationService().allowTopicOperationAsync(
+ deadLetterTopic, TopicOperation.PRODUCE, authRole, authenticationData);
+ }
+
public String extractSubscription(HttpServletRequest request) {
String uri = request.getRequestURI();
List<String> parts = Splitter.on("/").splitToList(uri);
diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/MultiTopicConsumerHandler.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/MultiTopicConsumerHandler.java
index f70ab554aed1..82421067dc33 100644
--- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/MultiTopicConsumerHandler.java
+++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/MultiTopicConsumerHandler.java
@@ -59,12 +59,16 @@ public class MultiTopicConsumerHandler extends ConsumerHandler {
.allowTopicOperationAsync(TopicName.get(topicName),
TopicOperation.CONSUME, authRole, subscription));
}
- FutureUtil.waitForAll(futures)
+ return FutureUtil.waitForAll(futures)
+ .thenCompose(__ -> futures.stream().allMatch(f -> f.join())
+ ? authorizeDeadLetterTopic(authRole, authenticationData)
+ : CompletableFuture.completedFuture(false))
.get(service.getConfig().getMetadataStoreOperationTimeoutSeconds(), SECONDS);
- return futures.stream().allMatch(f -> f.join());
} else {
return service.getAuthorizationService()
.allowTopicOperationAsync(topic, TopicOperation.CONSUME, authRole, subscription)
+ .thenCompose(allowed -> allowed ? authorizeDeadLetterTopic(authRole, authenticationData)
+ : CompletableFuture.completedFuture(false))
.get(service.getConfig().getMetadataStoreOperationTimeoutSeconds(), SECONDS);
}
} catch (TimeoutException e) {
diff --git a/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/ConsumerHandlerTest.java b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/ConsumerHandlerTest.java
new file mode 100644
index 000000000000..1ea2980dd22d
--- /dev/null
+++ b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/ConsumerHandlerTest.java
@@ -0,0 +1,283 @@
+/*
+ * 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.pulsar.websocket;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
+import org.apache.pulsar.broker.authentication.AuthenticationService;
+import org.apache.pulsar.broker.authorization.AuthorizationService;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.impl.ConsumerBuilderImpl;
+import org.apache.pulsar.client.impl.ProducerBuilderImpl;
+import org.apache.pulsar.common.naming.TopicName;
+import org.apache.pulsar.common.policies.data.TopicOperation;
+import org.eclipse.jetty.ee8.websocket.server.JettyServerUpgradeResponse;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+public class ConsumerHandlerTest {
+ private static final String SOURCE = "persistent://tenant/ns/source";
+ private static final String DESTINATION = "persistent://other/ns/destination";
+ private static final String SUBSCRIPTION = "subscription";
+ private static final String ROLE = "client-role";
+
+ @DataProvider
+ public Object[][] destinations() {
+ return new Object[][] {
+ {false, false, true}, {false, false, false},
+ {false, true, true}, {false, true, false},
+ {true, false, true}, {true, false, false},
+ {true, true, true}, {true, true, false}
+ };
+ }
+
+ @Test(dataProvider = "destinations")
+ public void testDestinationAuthorization(boolean multiTopic, boolean useDefault, boolean allowed) throws Exception {
+ Fixture fixture = new Fixture(true);
+ Map<String, String[]> params = dlqParams(useDefault ? null : DESTINATION);
+ String destination = useDefault ? SOURCE + "-" + SUBSCRIPTION + "-DLQ" : DESTINATION;
+ fixture.allowConsume(true);
+ fixture.allowProduce(destination, CompletableFuture.completedFuture(allowed));
+
+ ConsumerHandler handler = fixture.create(params, multiTopic);
+
+ assertThat(handler.isAllowConnect()).isEqualTo(allowed);
+ verify(fixture.authorization).allowTopicOperationAsync(eq(TopicName.get(destination)),
+ eq(TopicOperation.PRODUCE), eq(ROLE), any(AuthenticationDataSource.class));
+ if (allowed) {
+ verify(fixture.builder).subscribe();
+ verifyNoInteractions(fixture.response);
+ assertThat(fixture.builder.getConf().getDeadLetterPolicy().getDeadLetterTopic()).isEqualTo(destination);
+ } else {
+ verify(fixture.builder, never()).subscribe();
+ verify(fixture.response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
+ }
+ }
+
+ @DataProvider
+ public Object[][] paddedDestinations() {
+ return new Object[][] {
+ {" destination", false}, {"destination ", false}, {" destination ", false},
+ {" destination", true}, {"destination ", true}, {" destination ", true}
+ };
+ }
+
+ @Test(dataProvider = "paddedDestinations")
+ public void testDestinationMatchesProducerNormalization(String destination, boolean allowed) throws Exception {
+ Fixture fixture = new Fixture(true);
+ fixture.allowConsume(true);
+ ProducerBuilderImpl<byte[]> producer = new ProducerBuilderImpl<>(null, Schema.BYTES);
+ producer.topic(destination);
+ String actualDestination = producer.getConf().getTopicName();
+ assertThat(TopicName.get(destination)).isNotEqualTo(TopicName.get(actualDestination));
+ // Only the trimmed topic that the producer uses should be checked.
+ fixture.allowProduce(destination, CompletableFuture.completedFuture(true));
+ fixture.allowProduce(actualDestination, CompletableFuture.completedFuture(allowed));
+
+ ConsumerHandler handler = fixture.create(dlqParams(destination), false);
+
+ assertThat(handler.isAllowConnect()).isEqualTo(allowed);
+ verify(fixture.authorization).allowTopicOperationAsync(eq(TopicName.get(actualDestination)),
+ eq(TopicOperation.PRODUCE), eq(ROLE), any(AuthenticationDataSource.class));
+ verify(fixture.authorization, never()).allowTopicOperationAsync(eq(TopicName.get(destination)),
+ eq(TopicOperation.PRODUCE), eq(ROLE), any(AuthenticationDataSource.class));
+ assertThat(fixture.builder.getConf().getDeadLetterPolicy().getDeadLetterTopic()).isEqualTo(actualDestination);
+ if (allowed) {
+ verify(fixture.builder).subscribe();
+ } else {
+ verify(fixture.builder, never()).subscribe();
+ verify(fixture.response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
+ }
+ }
+
+ @Test(dataProvider = "failures")
+ public void testPatternDestinationAuthorization(boolean allowed) throws Exception {
+ Fixture fixture = new Fixture(true);
+ fixture.allowConsume(true);
+ fixture.allowProduce(DESTINATION, CompletableFuture.completedFuture(allowed));
+ Map<String, String[]> params = dlqParams(DESTINATION);
+ params.put("topicsPattern", new String[] {"persistent://tenant/ns/source.*"});
+ ConsumerHandler handler = fixture.create(params, true);
+ assertThat(handler.isAllowConnect()).isEqualTo(allowed);
+ verify(fixture.authorization).allowTopicOperationAsync(eq(TopicName.get(DESTINATION)),
+ eq(TopicOperation.PRODUCE), eq(ROLE), any(AuthenticationDataSource.class));
+ if (allowed) {
+ verify(fixture.builder).subscribe();
+ } else {
+ verify(fixture.builder, never()).subscribe();
+ }
+ }
+
+ @Test
+ public void testPartitionDefaultDestination() throws Exception {
+ Fixture fixture = new Fixture(true);
+ fixture.source = SOURCE + "-partition-0";
+ String destination = fixture.source + "-" + SUBSCRIPTION + "-DLQ";
+ fixture.allowConsume(true);
+ fixture.allowProduce(destination, CompletableFuture.completedFuture(true));
+ ConsumerHandler handler = fixture.create(dlqParams(null), false);
+ assertThat(handler.isAllowConnect()).isTrue();
+ assertThat(fixture.builder.getConf().getDeadLetterPolicy().getDeadLetterTopic()).isEqualTo(destination);
+ verify(fixture.authorization).allowTopicOperationAsync(eq(TopicName.get(destination)),
+ eq(TopicOperation.PRODUCE), eq(ROLE), any(AuthenticationDataSource.class));
+ }
+
+ @Test
+ public void testConsumeDeniedBeforeDestinationCheck() throws Exception {
+ Fixture fixture = new Fixture(true);
+ fixture.allowConsume(false);
+ ConsumerHandler handler = fixture.create(dlqParams(DESTINATION), false);
+ assertThat(handler.isAllowConnect()).isFalse();
+ verify(fixture.authorization, never()).allowTopicOperationAsync(any(), eq(TopicOperation.PRODUCE),
+ anyString(), any(AuthenticationDataSource.class));
+ verify(fixture.builder, never()).subscribe();
+ verify(fixture.response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
+ }
+
+ @Test
+ public void testNoDeadLetterPolicy() throws Exception {
+ Fixture fixture = new Fixture(true);
+ fixture.allowConsume(true);
+ ConsumerHandler handler = fixture.create(new HashMap<>(), false);
+ assertThat(handler.isAllowConnect()).isTrue();
+ verify(fixture.authorization, never()).allowTopicOperationAsync(any(), eq(TopicOperation.PRODUCE),
+ anyString(), any(AuthenticationDataSource.class));
+ verify(fixture.builder).subscribe();
+ }
+
+ @DataProvider
+ public Object[][] failures() {
+ return new Object[][] {{false}, {true}};
+ }
+
+ @Test(dataProvider = "failures")
+ public void testDestinationAuthorizationFailure(boolean timeout) throws Exception {
+ Fixture fixture = new Fixture(true);
+ fixture.allowConsume(true);
+ fixture.config.setMetadataStoreOperationTimeoutSeconds(timeout ? 0 : 30);
+ fixture.allowProduce(DESTINATION, timeout ? new CompletableFuture<>()
+ : CompletableFuture.failedFuture(new IllegalStateException("authorization unavailable")));
+ ConsumerHandler handler = fixture.create(dlqParams(DESTINATION), false);
+ assertThat(handler.isAllowConnect()).isFalse();
+ verify(fixture.builder, never()).subscribe();
+ verify(fixture.response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), anyString());
+ }
+
+ @DataProvider
+ public Object[][] invalidDestinations() {
+ return new Object[][] {{""}, {" "}, {"invalid://tenant/ns/topic"}};
+ }
+
+ @Test(dataProvider = "invalidDestinations")
+ public void testInvalidDestination(String destination) throws Exception {
+ Fixture fixture = new Fixture(true);
+ ConsumerHandler handler = fixture.create(dlqParams(destination), false);
+ assertThat(handler.isAllowConnect()).isFalse();
+ verify(fixture.builder, never()).subscribe();
+ verify(fixture.response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
+ }
+
+ @Test
+ public void testAuthorizationDisabledPreservesBlankDestination() throws Exception {
+ Fixture fixture = new Fixture(false);
+ ConsumerHandler handler = fixture.create(dlqParams(""), false);
+ assertThat(handler.isAllowConnect()).isTrue();
+ verifyNoInteractions(fixture.authorization);
+ verify(fixture.builder).subscribe();
+ assertThat(fixture.builder.getConf().getDeadLetterPolicy().getDeadLetterTopic()).isEmpty();
+ }
+
+ private static Map<String, String[]> dlqParams(String destination) {
+ Map<String, String[]> params = new HashMap<>();
+ params.put("maxRedeliverCount", new String[] {"1"});
+ if (destination != null) {
+ params.put("deadLetterTopic", new String[] {destination});
+ }
+ return params;
+ }
+
+ private static class Fixture {
+ private String source = SOURCE;
+ private final WebSocketService service = mock(WebSocketService.class);
+ private final AuthorizationService authorization = mock(AuthorizationService.class);
+ private final ServiceConfiguration config = new ServiceConfiguration();
+ private final ConsumerBuilderImpl<byte[]> builder = spy(new ConsumerBuilderImpl<>(null, Schema.BYTES));
+ private final JettyServerUpgradeResponse response = mock(JettyServerUpgradeResponse.class);
+
+ @SuppressWarnings({"unchecked", "deprecation"})
+ Fixture(boolean authorizationEnabled) throws Exception {
+ PulsarClient client = mock(PulsarClient.class);
+ AuthenticationService authentication = mock(AuthenticationService.class);
+ when(service.getPulsarClient()).thenReturn(client);
+ when(client.newConsumer()).thenReturn(builder);
+ doReturn(mock(Consumer.class)).when(builder).subscribe();
+ when(service.getCryptoKeyReader()).thenReturn(Optional.empty());
+ when(service.addConsumer(any())).thenReturn(true);
+ when(service.isAuthenticationEnabled()).thenReturn(true);
+ when(service.isAuthorizationEnabled()).thenReturn(authorizationEnabled);
+ when(service.getAuthenticationService()).thenReturn(authentication);
+ when(authentication.authenticateHttpRequest(any(HttpServletRequest.class))).thenReturn(ROLE);
+ when(service.getAuthorizationService()).thenReturn(authorization);
+ when(service.getConfig()).thenReturn(config);
+ }
+
+ void allowConsume(boolean allowed) {
+ when(authorization.allowTopicOperationAsync(any(), eq(TopicOperation.CONSUME), eq(ROLE),
+ any(AuthenticationDataSource.class))).thenReturn(CompletableFuture.completedFuture(allowed));
+ }
+
+ void allowProduce(String destination, CompletableFuture<Boolean> result) {
+ when(authorization.allowTopicOperationAsync(eq(TopicName.get(destination)), eq(TopicOperation.PRODUCE),
+ eq(ROLE), any(AuthenticationDataSource.class))).thenReturn(result);
+ }
+
+ ConsumerHandler create(Map<String, String[]> params, boolean multiTopic) {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ if (multiTopic && !params.containsKey("topicsPattern")) {
+ params.put("topics", new String[] {source + ",persistent://tenant/ns/second"});
+ }
+ when(request.getParameterMap()).thenReturn(params);
+ when(request.getRemoteAddr()).thenReturn("127.0.0.1");
+ when(request.getRequestURI()).thenReturn(multiTopic ? "/ws/v3/consumer/" + SUBSCRIPTION
+ : "/ws/v2/consumer/persistent/tenant/ns/" + TopicName.get(source).getLocalName() + "/"
+ + SUBSCRIPTION);
+ return multiTopic ? new MultiTopicConsumerHandler(service, request, response)
+ : new ConsumerHandler(service, request, response);
+ }
+ }
+}