Skip to content

fix(protocol): reject content topics that do not start with a slash - #1322

Merged
igor-sirotin merged 1 commit into
logos-messaging:masterfrom
yucazer0:fix/content-topic-leading-slash
Oct 1, 2026
Merged

igor-sirotin merged 1 commit into
logos-messaging:masterfrom
yucazer0:fix/content-topic-leading-slash

Conversation

@yucazer0

Copy link
Copy Markdown
Contributor

Description

StringToContentTopic splits the string on / and only reads the parts after the first one:

p := strings.Split(s, "/")
switch len(p) {
case 5:
	...
	return ContentTopic{
		ApplicationName:    p[1],
		ApplicationVersion: p[2],
		...

p[0] is never checked, so a string with text before the leading slash is accepted and that text is silently dropped:

StringToContentTopic("waku/2/test/proto/extra")
  -> ContentTopic{ApplicationName: "2", ApplicationVersion: "test", ContentTopicName: "proto", Encoding: "extra"}

ContentTopic.String() then returns /2/test/proto/extra, which is not what was passed in, and GetShardFromContentTopic derives the shard from an application name and version that were never intended, so the message ends up on a different pubsub topic.

topic_test.go already covers this intent:

_, err = StringToContentTopic("waku/1/a/b")
require.Error(t, err, ErrInvalidFormat)

but that string is rejected for having 4 parts rather than for the missing slash, so the 5 and 6 part cases are not caught.

This also explains three test content topics in the repository that are missing the leading slash and are only accepted because of this behaviour:

  • waku/v2/protocol/lightpush/waku_lightpush_test.go: "0/test/1/testTopic/proto"
  • waku/v2/protocol/filter/filter_test.go: "0/test/1/testTopic/proto" and "0/test/1/testTopic1/proto"

They read as generation-0 topics but currently parse as /test/1/testTopic/proto with the 0 dropped. They are corrected here, since they would otherwise fail with this change. The derived shard is unaffected, because it only depends on the application name and version, which stay the same either way.

Changes

  • StringToContentTopic rejects strings that do not start with /
  • Added TestStringToContentTopicRequiresLeadingSlash
  • Corrected the three test content topics that were missing the leading slash

Tests

go test ./waku/v2/protocol/ ./waku/v2/protocol/lightpush/ -count=1

TestStringToContentTopicRequiresLeadingSlash fails on master:

Error: Target error should be in err chain:
       expected: "invalid content topic format"
       in chain:

and passes with the fix. waku/v2/protocol and waku/v2/protocol/lightpush both pass.

Note: TestFilterSuite/TestUnsubscribeErrorHandling fails with and without this change in my environment, so it looks unrelated to this fix.

@igor-sirotin
igor-sirotin force-pushed the fix/content-topic-leading-slash branch from 8db2fd4 to f92776d Compare October 1, 2026 13:25
@igor-sirotin
igor-sirotin requested a balanced review from Copilot October 1, 2026 13:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The validation is correct, focused, and adequately covered by tests.

Review effort: Balanced
Findings: None

What changed in this PR

Enforces canonical leading slashes for parsed content topics and updates affected tests.

Changes:

  • Rejects malformed content topics missing /.
  • Adds regression coverage.
  • Corrects existing test fixtures.
File Description
waku/​v2/​protocol/​content_topic.go Validates the leading slash.
waku/​v2/​protocol/​topic_test.go Adds regression tests.
waku/​v2/​protocol/​lightpush/​waku_lightpush_test.go Corrects a test topic.
waku/​v2/​protocol/​filter/​filter_test.go Corrects filter test topics.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

StringToContentTopic splits on / and only reads the parts after the first
one, so a string with text before the leading slash is accepted and that
text is silently dropped. waku/2/test/proto/extra parses as
/2/test/proto/extra, and GetShardFromContentTopic then derives the shard
from the wrong application name and version.

topic_test.go already covers this intent with waku/1/a/b, but that string
is rejected for having too few parts rather than for the missing slash.

Require the leading slash, and correct three test content topics that were
missing it and were only accepted because of this.
@igor-sirotin
igor-sirotin force-pushed the fix/content-topic-leading-slash branch from f92776d to 026f7c5 Compare October 1, 2026 13:54
@igor-sirotin
igor-sirotin merged commit b0ddae4 into logos-messaging:master Oct 1, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants