Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(gateway): reject unknown rpc methods instead of crashing (#2238)

Hampus2dd35c0d6ebe
diff --git a/fluxer_api/src/api/infrastructure/GatewayService.ts b/fluxer_api/src/api/infrastructure/GatewayService.ts
index 00f39b7d7..32a115fdf 100644
--- a/fluxer_api/src/api/infrastructure/GatewayService.ts
+++ b/fluxer_api/src/api/infrastructure/GatewayService.ts
@@ -281,6 +281,8 @@ export class GatewayService {
private readonly MAX_BATCH_CONCURRENCY = 50;
private readonly PENDING_REQUEST_TIMEOUT_MS = ms('30 seconds');
private readonly AUTH_CONTEXT_FALLBACK_MS = ms('5 minutes');
+ private readonly BADGE_COUNTS_FALLBACK_MS = ms('5 minutes');
+ private badgeCountsUnsupportedUntil = 0;
constructor() {
this.rpcClient = GatewayRpcClient.getInstance();
@@ -703,15 +705,33 @@ export class GatewayService {
}
async invalidatePushBadgeCounts({userIds}: InvalidatePushBadgeCountsParams): Promise<void> {
+ if (Date.now() < this.badgeCountsUnsupportedUntil) {
+ await this.invalidatePushBadgeCountsIndividually(userIds);
+ return;
+ }
const batches: Array<Array<UserID>> = [];
for (let index = 0; index < userIds.length; index += PUSH_BADGE_COUNT_BATCH_SIZE) {
batches.push(userIds.slice(index, index + PUSH_BADGE_COUNT_BATCH_SIZE));
}
- await Promise.all(
- batches.map((batch) =>
- this.call('push.invalidate_badge_counts', {user_ids: batch.map((userId) => userId.toString())}),
- ),
- );
+ try {
+ await Promise.all(
+ batches.map((batch) =>
+ this.call('push.invalidate_badge_counts', {user_ids: batch.map((userId) => userId.toString())}),
+ ),
+ );
+ } catch (error) {
+ const transformedError = this.transformGatewayError(error);
+ if (!this.isAuthContextUnsupportedError(transformedError)) {
+ throw transformedError;
+ }
+ this.badgeCountsUnsupportedUntil = Date.now() + this.BADGE_COUNTS_FALLBACK_MS;
+ Logger.warn({error}, '[gateway-rpc] push.invalidate_badge_counts unavailable, falling back to per-user calls');
+ await this.invalidatePushBadgeCountsIndividually(userIds);
+ }
+ }
+
+ private async invalidatePushBadgeCountsIndividually(userIds: ReadonlyArray<UserID>): Promise<void> {
+ await Promise.all(userIds.map((userId) => this.invalidatePushBadgeCount({userId})));
}
async invalidatePushSubscriptions({userId}: InvalidatePushSubscriptionsParams): Promise<void> {
diff --git a/fluxer_gateway/src/gateway/gateway_rpc_misc.erl b/fluxer_gateway/src/gateway/gateway_rpc_misc.erl
index 1142040e9..5f3682fef 100644
--- a/fluxer_gateway/src/gateway/gateway_rpc_misc.erl
+++ b/fluxer_gateway/src/gateway/gateway_rpc_misc.erl
@@ -33,7 +33,9 @@ execute_method(<<"process.voice_state_counts">>, _Params) ->
gateway_rpc_misc_presence:collect_and_aggregate_voice_state_counts(ActiveNodes);
execute_method(<<"process.active_voice_rooms">>, _Params) ->
ActiveNodes = gateway_node_router:active_nodes(),
- gateway_rpc_misc_push:collect_and_aggregate_active_voice_rooms(ActiveNodes).
+ gateway_rpc_misc_push:collect_and_aggregate_active_voice_rooms(ActiveNodes);
+execute_method(Method, _Params) ->
+ gateway_rpc_error:raise(<<"Unknown method: ", Method/binary>>).
-spec get_local_node_id() -> binary().
get_local_node_id() ->
diff --git a/fluxer_gateway/src/gateway/gateway_rpc_presence.erl b/fluxer_gateway/src/gateway/gateway_rpc_presence.erl
index d44bca7c0..ddb16ec49 100644
--- a/fluxer_gateway/src/gateway/gateway_rpc_presence.erl
+++ b/fluxer_gateway/src/gateway/gateway_rpc_presence.erl
@@ -20,7 +20,9 @@ execute_method(<<"presence.terminate_all_sessions">>, P) -> handle_terminate_all
execute_method(<<"presence.has_active">>, P) -> handle_has_active(P);
execute_method(<<"presence.add_temporary_guild">>, P) -> handle_add_temp_guild(P);
execute_method(<<"presence.remove_temporary_guild">>, P) -> handle_remove_temp_guild(P);
-execute_method(<<"presence.sync_group_dm_recipients">>, P) -> handle_sync_dm_recipients(P).
+execute_method(<<"presence.sync_group_dm_recipients">>, P) -> handle_sync_dm_recipients(P);
+execute_method(Method, _Params) ->
+ gateway_rpc_error:raise(<<"Unknown method: ", Method/binary>>).
-spec handle_dispatch(map()) -> true.
handle_dispatch(#{<<"user_id">> := UserIdBin, <<"event">> := Event, <<"data">> := Data}) ->
diff --git a/fluxer_gateway/src/gateway/gateway_rpc_push.erl b/fluxer_gateway/src/gateway/gateway_rpc_push.erl
index 1e2f79e27..58bcbb358 100644
--- a/fluxer_gateway/src/gateway/gateway_rpc_push.erl
+++ b/fluxer_gateway/src/gateway/gateway_rpc_push.erl
@@ -17,7 +17,9 @@ execute_method(<<"push.invalidate_badge_counts">>, P) ->
execute_method(<<"push.clear_channel_notifications">>, P) ->
do_clear_channel_notifications(P);
execute_method(<<"push.invalidate_subscriptions">>, P) ->
- do_invalidate_subscriptions(P).
+ do_invalidate_subscriptions(P);
+execute_method(Method, _Params) ->
+ gateway_rpc_error:raise(<<"Unknown method: ", Method/binary>>).
-spec do_sync_user_guild_settings(map()) -> true.
do_sync_user_guild_settings(#{