[fix][broker] Reject topic names with leading or trailing whitespace on creation - #26281
Open
crossoverJie wants to merge 7 commits into
Open
[fix][broker] Reject topic names with leading or trailing whitespace on creation#26281crossoverJie wants to merge 7 commits into
crossoverJie wants to merge 7 commits into
Conversation
…espace Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
Signed-off-by: crossoverJie <crossoverJie@gmail.com>
…alableTopicsListByPropertyTest Signed-off-by: crossoverJie <crossoverJie@gmail.com>
…Test Signed-off-by: crossoverJie <crossoverJie@gmail.com>
dao-jun
approved these changes
Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Creating a topic whose local name has leading or trailing whitespace
(e.g.
persistent://public/default/test-topic-x) succeeds via the adminAPI, but producing to / consuming from it fails with
TopicDoesNotExistExceptionwhenallowAutoTopicCreation=false.Root cause: Pulsar clients (Java
ConsumerBuilder/ProducerBuilder/ReaderBuilder) trim topic names, so the client resolves to the trimmedname while the created znode keeps the whitespace (
Codecencodes the spaceas
+). The orphaned topic can never be reached — and can't even be deleted,because the delete path also trims the name and returns 404.
Rejecting on creation is preferred over server-side trimming: trimming would
silently reroute non-Java clients (Go/C++/Python, which do not trim) to a
different name after an upgrade, and would make already-broken whitespace
topics un-deletable.
Modifications
TopicName: addedisValidForCreation(TopicName)andvalidateTopicNameForCreation(TopicName)— reject leading/trailingwhitespace in the local name; internal whitespace (e.g.
my topic) staysallowed.
Broker admin creation paths now reject with HTTP 412
(
PreconditionFailedException) instead of persisting an unreachable topic:AdminResource.validateCreateTopic(TopicName)(moved fromPersistentTopicsBase) so persistent, non-persistent and scalabletopics share one source of truth.
BrokerService.isAllowAutoTopicCreationAsyncreturnsfalsefor suchnames, so auto-creation is also refused (covers clients that do not trim).
Client admin fail-fast:
TopicsImpl(partitioned + non-partitioned),NonPersistentTopicsImpl, andScalableTopicsImplnow reject whitespacenames locally and fail the future with
PreconditionFailedException(412),consistent with the server response — so callers catching
PreconditionFailedExceptionwork for both the client fast-fail and theserver path.
Verifying this change
This change added tests and can be verified as follows:
pulsar-common:TopicNameTest.testValidateTopicNameForCreationpulsar-broker(broker-api):ConsumerCreationTest.testCreatePartitionedTopicWithTrailingWhitespaceIsRejected(regression for the original bug; creation is rejected and the trimmed
name still works)
pulsar-broker(broker-admin):PersistentTopicsTest.testCreateTopicWithSurroundingWhitespaceIsRejected(asserts 412 for persistent/non-persistent partitioned + non-partitioned)
pulsar-broker:BrokerServiceAutoTopicCreationTest.testAutoTopicCreationRejectsSurroundingWhitespace(auto-creation refused, no topic left behind)
pulsar-broker(broker-admin):ScalableTopicsListByPropertyTest.testCreateScalableTopicWithSurroundingWhitespaceIsRejected(asserts 412 for scalable topic creation)
./gradlew quickCheckplus the scoped tests above.Does this pull request potentially affect one of the following parts:
(
PreconditionFailedException) for names with leading/trailingwhitespace instead of persisting an unreachable topic.
leading/trailing whitespace that were previously accepted; this is a
behavioral correction of a bug. Callers that never sent whitespace
names are unaffected.