-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLiveKitChatMessagesBusShould.cs
More file actions
273 lines (222 loc) · 11 KB
/
Copy pathLiveKitChatMessagesBusShould.cs
File metadata and controls
273 lines (222 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using DCL.Chat.History;
using DCL.Communities;
using DCL.FeatureFlags;
using DCL.Friends.UserBlocking;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.Multiplayer.Connections.Messaging;
using DCL.Multiplayer.Connections.Messaging.Hubs;
using DCL.Multiplayer.Connections.Messaging.Pipe;
using DCL.Multiplayer.Connections.RoomHubs;
using DCL.Multiplayer.Connections.Rooms;
using DCL.Profiles;
using DCL.SceneBannedUsers;
using DCL.Web3.Identities;
using Decentraland.Kernel.Comms.Rfc4;
using Global.AppArgs;
using Google.Protobuf;
using LiveKit.Internal.FFIClients.Pools;
using NSubstitute;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading;
using ChatMessage = DCL.Chat.History.ChatMessage;
using ChatPacket = Decentraland.Kernel.Comms.Rfc4.Chat;
namespace DCL.Chat.MessageBus.Tests
{
/// <summary>
/// Regression coverage for SEC-029: <c>Chat.ForwardedFrom</c> is a server-only stamp, so it must
/// only be honored when the authenticated LiveKit sender is the trusted message router. Otherwise
/// any co-located peer can impersonate a wallet and rotate the value to defeat ban, block, dedup
/// and rate-limiting, all of which key on the resolved sender.
/// </summary>
[TestFixture]
public class LiveKitChatMessagesBusShould
{
private const string ROUTING_USER = "message-router-dev-0";
private const string PEER_WALLET = "0x1111111111111111111111111111111111111111";
private const string VICTIM_WALLET = "0x2222222222222222222222222222222222222222";
private const string COMMUNITY_TOPIC = "community:1234";
private FakeMessagePipesHub pipesHub = null!;
private IMultiPool multiPool = null!;
private IUserBlockingCache userBlockingCache = null!;
private IWeb3IdentityCache identityCache = null!;
private List<ChatMessage> received = null!;
private LiveKitChatMessagesBus? bus;
[SetUp]
public void SetUp()
{
FeatureFlagsConfiguration.Reset();
OfficialWalletsHelper.Reset();
FeaturesRegistry.Reset();
CommunitiesFeatureAccess.Reset();
RoomMetadataCurrentScene.Reset();
IAppArgs appArgs = Substitute.For<IAppArgs>();
identityCache = Substitute.For<IWeb3IdentityCache>();
FeatureFlagsConfiguration.Initialize(new FeatureFlagsConfiguration(FeatureFlagsResultDto.Empty));
OfficialWalletsHelper.Initialize(new OfficialWalletsHelper());
FeaturesRegistry.Initialize(new FeaturesRegistry(appArgs, false));
CommunitiesFeatureAccess.Initialize(new CommunitiesFeatureAccess(identityCache, appArgs, CancellationToken.None));
RoomMetadataCurrentScene.InitializeTest();
pipesHub = new FakeMessagePipesHub();
multiPool = Substitute.For<IMultiPool>();
userBlockingCache = Substitute.For<IUserBlockingCache>();
received = new List<ChatMessage>();
}
[TearDown]
public void TearDown()
{
bus?.Dispose();
bus = null;
RoomMetadataCurrentScene.Reset();
CommunitiesFeatureAccess.Reset();
FeaturesRegistry.Reset();
OfficialWalletsHelper.Reset();
FeatureFlagsConfiguration.Reset();
}
[Test]
public void AttributeNearbyMessageToAuthenticatedSenderIgnoringForwardedFrom()
{
// Arrange — Island and Scene are peer-to-peer pipes, so no router is ever in their path.
CreateBus();
// Act — a peer forges the author of its own nearby message.
DeliverNearby(fromWallet: PEER_WALLET, forwardedFrom: VICTIM_WALLET, timestamp: 1d);
// Assert
Assert.That(received.Count, Is.EqualTo(1));
Assert.That(received[0].SenderWalletAddress, Is.EqualTo(PEER_WALLET));
}
[Test]
public void IgnoreForwardedFromWhenChatPipeSenderIsNotTheMessageRouter()
{
// Arrange
CreateBus();
// Act — the same forgery on the pipe that does have a legitimate router path.
DeliverOnChatPipe(fromWallet: PEER_WALLET, forwardedFrom: VICTIM_WALLET, timestamp: 1d);
// Assert
Assert.That(received.Count, Is.EqualTo(1));
Assert.That(received[0].SenderWalletAddress, Is.EqualTo(PEER_WALLET));
}
[Test]
public void HonorForwardedFromWhenChatPipeSenderIsTheMessageRouter()
{
// Arrange — a genuinely relayed community message arrives AS the router participant.
// Requires the Communities shape to be included, which it is in the Editor.
CreateBus();
// Act
DeliverOnChatPipe(fromWallet: ROUTING_USER, forwardedFrom: VICTIM_WALLET, timestamp: 1d);
// Assert — real forwarding still resolves to the original sender.
Assert.That(received.Count, Is.EqualTo(1));
Assert.That(received[0].SenderWalletAddress, Is.EqualTo(VICTIM_WALLET));
}
[Test]
public void FallBackToRouterIdentityWhenForwardedFromIsMalformed()
{
// Arrange
CreateBus();
// Act — a malformed or rogue value from the router itself.
DeliverOnChatPipe(fromWallet: ROUTING_USER, forwardedFrom: "not-a-wallet-address", timestamp: 1d);
// Assert
Assert.That(received.Count, Is.EqualTo(1));
Assert.That(received[0].SenderWalletAddress, Is.EqualTo(ROUTING_USER));
}
[Test]
public void NotGrantFreshDedupSlotsWhenNearbyForwardedFromRotates()
{
// Arrange — dedup keys on (sender, timestamp), so a rotating author used to hand one peer
// an unlimited supply of unused keys at a single timestamp.
CreateBus();
// Act
DeliverNearby(PEER_WALLET, forwardedFrom: VICTIM_WALLET, timestamp: 1d);
DeliverNearby(PEER_WALLET, forwardedFrom: "0x3333333333333333333333333333333333333333", timestamp: 1d);
DeliverNearby(PEER_WALLET, forwardedFrom: "0x4444444444444444444444444444444444444444", timestamp: 1d);
// Assert — all three collapse onto the one authenticated wallet.
Assert.That(received.Count, Is.EqualTo(1));
}
[Test]
public void DeduplicateAcrossNearbyPipesWhenForwardedFromDiffers()
{
// Arrange — the same message reaches the client over both nearby pipes.
CreateBus();
// Act — a forged author on the second copy used to defeat the cross-pipe dedup.
DeliverNearby(PEER_WALLET, forwardedFrom: null, timestamp: 1d);
Deliver(pipesHub.Scene, PEER_WALLET, VICTIM_WALLET, 1d, RoomSource.Gatekeeper, string.Empty);
// Assert
Assert.That(received.Count, Is.EqualTo(1));
}
[Test]
public void KeepNearbyMessageOfBlockedPeerHiddenWhenForwardedFromRotates()
{
// Arrange — the local user blocks the peer and hides its messages.
userBlockingCache.HideChatMessages.Returns(true);
userBlockingCache.UserIsBlocked(PEER_WALLET).Returns(true);
CreateBus();
// Act — the blocked peer claims to be somebody the local user has not blocked.
DeliverNearby(PEER_WALLET, forwardedFrom: VICTIM_WALLET, timestamp: 1d);
// Assert
Assert.That(received, Is.Empty);
}
[Test]
public void AttributeNearbyMessageToAuthenticatedSenderWhenForwardedFromIsAbsent()
{
// Arrange
CreateBus();
// Act
DeliverNearby(PEER_WALLET, forwardedFrom: null, timestamp: 1d);
// Assert
Assert.That(received.Count, Is.EqualTo(1));
Assert.That(received[0].SenderWalletAddress, Is.EqualTo(PEER_WALLET));
}
// ── Test helpers ─────────────────────────────────────────
private void CreateBus()
{
var messageFactory = new ChatMessageFactory(Substitute.For<IProfileCache>(), identityCache);
// Zone resolves the routing user to "message-router-dev-0".
var newBus = new LiveKitChatMessagesBus(pipesHub,
messageFactory,
userBlockingCache,
DecentralandEnvironment.Zone,
identityCache,
Substitute.For<IRoomHub>());
newBus.MessageAdded += (_, _, message) => received.Add(message);
bus = newBus;
}
private void DeliverNearby(string fromWallet, string? forwardedFrom, double timestamp) =>
Deliver(pipesHub.Island, fromWallet, forwardedFrom, timestamp, RoomSource.Island, string.Empty);
private void DeliverOnChatPipe(string fromWallet, string? forwardedFrom, double timestamp) =>
Deliver(pipesHub.Chat, fromWallet, forwardedFrom, timestamp, RoomSource.Island, COMMUNITY_TOPIC);
private void Deliver(FakeMessagePipe pipe, string fromWallet, string? forwardedFrom, double timestamp,
RoomSource roomSource, string topic)
{
var payload = new ChatPacket { Message = "hello", Timestamp = timestamp };
if (forwardedFrom != null)
payload.ForwardedFrom = forwardedFrom;
pipe.Deliver(Packet.MessageOneofCase.Chat,
new ReceivedMessage<ChatPacket>(payload, new Packet(), fromWallet, multiPool, roomSource, topic));
}
private sealed class FakeMessagePipesHub : IMessagePipesHub
{
public readonly FakeMessagePipe Island = new ();
public readonly FakeMessagePipe Scene = new ();
public readonly FakeMessagePipe Chat = new ();
public IMessagePipe ScenePipe() => Scene;
public IMessagePipe IslandPipe() => Island;
public IMessagePipe ChatPipe() => Chat;
public void Dispose() { }
}
private sealed class FakeMessagePipe : IMessagePipe
{
private readonly Dictionary<Packet.MessageOneofCase, object> handlers = new ();
public MessageWrap<T> NewMessage<T>(string topic = "") where T: class, IMessage, new() =>
throw new NotSupportedException("Receive-only fake");
public void Subscribe<T>(Packet.MessageOneofCase ofCase, Action<ReceivedMessage<T>> onMessageReceived,
IMessagePipe.ThreadStrict threadStrict = IMessagePipe.ThreadStrict.MainThreadOnly) where T: class, IMessage, new() =>
handlers[ofCase] = onMessageReceived;
public void Deliver<T>(Packet.MessageOneofCase ofCase, ReceivedMessage<T> message) where T: class, IMessage, new()
{
if (handlers.TryGetValue(ofCase, out object? handler))
((Action<ReceivedMessage<T>>)handler).Invoke(message);
}
public void Dispose() { }
}
}
}