fix(gateway): close oversized bot identify with code 4011 (#2497)

This commit is contained in:
Hampus
2026-09-06 15:25:19 +02:00
committed by GitHub
parent 31d7cb81d6
commit e828398e06
2 changed files with 49 additions and 0 deletions
@@ -137,6 +137,8 @@ check_user_session_limit(
{reply, start_reply(), state()}.
handle_user_session_limit_result({error, too_many_sessions}, Ctx) ->
{reply, {error, too_many_sessions}, maps:get(state, Ctx)};
handle_user_session_limit_result({error, sharding_required}, Ctx) ->
{reply, {error, sharding_required}, maps:get(state, Ctx)};
handle_user_session_limit_result(ok, Ctx) ->
UserId = maps:get(user_id, Ctx),
session_abuse_protection:increment_user_sessions(UserId),
@@ -0,0 +1,47 @@
%% SPDX-License-Identifier: AGPL-3.0-or-later
-module(session_manager_shard_start_tests).
-typing([eqwalizer]).
-include_lib("eunit/include/eunit.hrl").
build_test_state() ->
#{
sessions => #{},
identify_attempts => [],
pending_identifies => #{},
identify_workers => #{},
shard_index => 0
}.
guild_wire(GuildId) ->
#{<<"id">> => integer_to_binary(GuildId)}.
guild_id_for_shard(ShardId, NumShards, Offset) ->
((Offset * NumShards + ShardId) bsl 22) + 1.
bot_identify_data(GuildCount) ->
Guilds = [guild_wire(guild_id_for_shard(0, 1, I)) || I <- lists:seq(1, GuildCount)],
#{
<<"user">> => #{
<<"id">> => <<"123">>,
<<"username">> => <<"bot">>,
<<"discriminator">> => <<"0001">>,
<<"avatar">> => null,
<<"flags">> => 0,
<<"bot">> => true
},
<<"guilds">> => Guilds
}.
start_session(Data, IdentifyData) ->
session_manager_shard_start:build_and_start_session(
Data, IdentifyData, 1, self(), <<"session-id">>, #{}, build_test_state()
).
build_and_start_session_rejects_oversized_bot_with_sharding_required_test() ->
Data = bot_identify_data(2501),
?assertMatch(
{reply, {error, sharding_required}, _},
start_session(Data, #{properties => #{}, token => <<"token">>, presence => null})
).