Skip to content

Commit

Permalink
handling invalid presence event data in subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Apr 15, 2024
1 parent 40b6bb6 commit 45e793e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
19 changes: 18 additions & 1 deletion pubnub/lib/src/subscribe/envelope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,26 @@ class PresenceEvent {
.map((uuid) => UUID(uuid))
.toList();

static bool isValidPresenceEvent(Envelope envelope) {
final payload = envelope.payload;
if (payload is! Map) return false;

final action = payload['action'];
final occupancy = payload['occupancy'];

if (action is! String ||
PresenceActionExtension.fromString(action) == PresenceAction.unknown) {
return false;
}
if (occupancy is! int) {
return false;
}
return true;
}

PresenceEvent.fromEnvelope(this.envelope)
: action = PresenceActionExtension.fromString(
envelope.payload['action'] as String?),
envelope.payload['action'] as String),
uuid = envelope.payload['uuid'] != null
? UUID(envelope.payload['uuid'])
: null,
Expand Down
7 changes: 4 additions & 3 deletions pubnub/lib/src/subscribe/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ class Subscription {
/// Will only emit when [withPresence] is true.
Stream<PresenceEvent> get presence => _envelopesController.stream
.where((envelope) =>
presenceChannels.contains(envelope.channel) ||
presenceChannels.contains(envelope.subscriptionPattern) ||
presenceChannelGroups.contains(envelope.subscriptionPattern))
PresenceEvent.isValidPresenceEvent(envelope) &&
(presenceChannels.contains(envelope.channel) ||
presenceChannels.contains(envelope.subscriptionPattern) ||
presenceChannelGroups.contains(envelope.subscriptionPattern)))
.map<PresenceEvent>((envelope) => PresenceEvent.fromEnvelope(envelope));

final Completer<void> _cancelCompleter = Completer();
Expand Down

0 comments on commit 45e793e

Please sign in to comment.