From 9aba952b35b2da2604bdbe31ee6e84a8cdbb4833 Mon Sep 17 00:00:00 2001 From: Flegma Date: Tue, 24 Nov 2026 21:56:39 +0100 Subject: [PATCH] fix(player-management): kick refused players with a reason instead of the empty ticket loop Without a server password the access gate stripped the Steam ticket, which loops the client on reconnect and holds a slot. The player is now let in and kicked with the access list reason before the sanctions answer, cannot chat meanwhile, and a connect under the id of a player already here changes nothing. On CounterStrikeSharp the reason is shown in chat and center text before a delayed kick. The password refusal is unchanged; a banned rejoin is refused at the door only on a password server. --- apps/player-management-css/src/ConnectGate.cs | 55 ++++++++------ .../src/PlayerManagementPlugin.cs | 71 ++++++++++++------ apps/player-management-sw/src/ConnectGate.cs | 27 ++++--- .../src/PlayerManagementPlugin.cs | 45 +++++++----- .../test/ConnectRefusalTests.cs | 72 ++++++++++++------- .../test/ServerAccessBookTests.cs | 25 +++++++ .../FiveStack.Utilities/ConnectRefusal.cs | 3 +- .../FiveStack.Utilities/ServerAccessBook.cs | 24 +++++++ 8 files changed, 222 insertions(+), 100 deletions(-) diff --git a/apps/player-management-css/src/ConnectGate.cs b/apps/player-management-css/src/ConnectGate.cs index 291abb4d..356802ba 100644 --- a/apps/player-management-css/src/ConnectGate.cs +++ b/apps/player-management-css/src/ConnectGate.cs @@ -111,18 +111,42 @@ private HookResult OnConnectClient(DynamicHook hook) ); // A player this server kicked for a ban, still banned: refused - // here rather than loading in to be kicked again. - if (_rejoin.Refuse(steamId, _book, DateTimeOffset.UtcNow)) + // here on a password server. Without one, Enforce kicks them again + // (an empty ticket loops). + if ( + _rejoin.Refuse(steamId, _book, DateTimeOffset.UtcNow) + && ConnectRefusal.ByPassword(ConVar.Find("sv_password")?.StringValue) + ) { _loop?.Request(); Logger.LogInformation("refusing banned player {steamId} at connect", steamId); - Refuse(hook); + hook.SetParam(5, RefusedPasswordBuffer); return HookResult.Continue; } eServerAccess access = _access.Decide(steamId.ToString()); + if (steamId != 0) + { + // Its own guard: a failure here must not skip the password swap. + try + { + _access.Connecting( + steamId.ToString(), + access == eServerAccess.Denied, + Humans().Any(player => + SteamIdOf(player) == steamId + && player.Connected == PlayerConnectedState.Connected + ) + ); + } + catch (Exception error) + { + Logger.LogError(error, "access bookkeeping failed for {steamId}", steamId); + } + } + if (access is eServerAccess.Unknown or eServerAccess.Open) { return HookResult.Continue; @@ -139,9 +163,15 @@ private HookResult OnConnectClient(DynamicHook hook) password != nint.Zero ); - if (access == eServerAccess.Denied) + // Without a password there is no clean refusal at the door (an + // empty ticket loops), so a refused player is let in and kicked + // with the reason by Enforce. + if ( + access == eServerAccess.Denied + && ConnectRefusal.ByPassword(ConVar.Find("sv_password")?.StringValue) + ) { - Refuse(hook); + hook.SetParam(5, RefusedPasswordBuffer); } else if (password != nint.Zero) { @@ -156,21 +186,6 @@ private HookResult OnConnectClient(DynamicHook hook) return HookResult.Continue; } - // A wrong password on a password server, where the engine refuses it as - // one; elsewhere no auth ticket, which the engine refuses too. - private static void Refuse(DynamicHook hook) - { - if (ConnectRefusal.ByPassword(ConVar.Find("sv_password")?.StringValue)) - { - hook.SetParam(5, RefusedPasswordBuffer); - } - else - { - hook.SetParam(6, 0); - hook.SetParam(7, 0); - } - } - // Read at connect time: the owner can change sv_password over RCON while // the server hibernates, when nothing on a tick would notice. private nint ServerPassword() diff --git a/apps/player-management-css/src/PlayerManagementPlugin.cs b/apps/player-management-css/src/PlayerManagementPlugin.cs index 13af9c62..12886c13 100644 --- a/apps/player-management-css/src/PlayerManagementPlugin.cs +++ b/apps/player-management-css/src/PlayerManagementPlugin.cs @@ -5,6 +5,7 @@ using CounterStrikeSharp.API.Core.Translations; using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Cvars; +using CounterStrikeSharp.API.Modules.Utils; using CounterStrikeSharp.API.ValveConstants.Protobuf; using FiveStack.Entities.PlayerManagement; using FiveStack.Enums; @@ -336,6 +337,48 @@ private void Enforce(List humans) { ulong steamId = SteamIdOf(player); + // Before the awaiting skip: a refused player must not play while + // the sanctions answer is still out. CounterStrikeSharp disconnects + // with a reason code only, so the reason is shown once the player + // is in game and the kick follows a few seconds later. + if (_access.IsDenied(steamId.ToString())) + { + if (player.Connected == PlayerConnectedState.Connected && _kicked.Add(steamId)) + { + Logger.LogInformation( + "kicking {name} ({steamId}): not on the server's access list", + player.PlayerName, + steamId + ); + + string reason = _access.KickReason( + Localizer.ForPlayer(player, "access.denied") + ); + player.ChangeTeam(CsTeam.Spectator); + player.PrintToChat(reason); + player.PrintToCenter(reason); + + AddTimer( + RefusedKickDelaySeconds, + () => + { + if ( + player.IsValid + && SteamIdOf(player) == steamId + && _access.IsDenied(steamId.ToString()) + ) + { + player.Disconnect( + NetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED + ); + } + } + ); + } + + continue; + } + if (_book.IsAwaiting(steamId.ToString())) { continue; @@ -365,27 +408,6 @@ private void Enforce(List humans) continue; } - // CounterStrikeSharp disconnects with a reason code only, so the - // text goes to chat on the way out. - if (_access.IsDenied(steamId.ToString())) - { - if (_kicked.Add(steamId)) - { - Logger.LogInformation( - "kicking {name} ({steamId}): not on the server's access list", - player.PlayerName, - steamId - ); - - player.PrintToChat( - _access.KickReason(Localizer.ForPlayer(player, "access.denied")) - ); - player.Disconnect(NetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED); - } - - continue; - } - // Re-asserted rather than set once: the engine resets voice flags // across a reconnect and a map change. if (state.IsMuted && !player.VoiceFlags.HasFlag(VoiceFlags.Muted)) @@ -982,6 +1004,11 @@ private HookResult OnChat(CCSPlayerController? player, CommandInfo info) return HookResult.Continue; } + if (_access.IsDenied(SteamIdOf(player).ToString())) + { + return HookResult.Stop; + } + SanctionState state = _book.StateFor(SteamIdOf(player).ToString(), DateTimeOffset.UtcNow); if (!state.IsGagged) @@ -1012,6 +1039,8 @@ private void Tell(CCSPlayerController player, string key, PlayerSanction sanctio ); } + private const float RefusedKickDelaySeconds = 4f; + private static List Humans() { return Utilities.GetPlayers().Where(IsHuman).ToList(); diff --git a/apps/player-management-sw/src/ConnectGate.cs b/apps/player-management-sw/src/ConnectGate.cs index 12d2cc09..36c19824 100644 --- a/apps/player-management-sw/src/ConnectGate.cs +++ b/apps/player-management-sw/src/ConnectGate.cs @@ -120,21 +120,9 @@ private void InstallConnectGate() ); } - if (access == eServerAccess.Denied) - { - return next()( - server, - name, - address, - netInfo, - connectMsg, - password, - nint.Zero, - 0, - isLowViolence - ); - } - + // Without a password there is no clean refusal at the door (an + // empty ticket loops), so the player is let in and kicked + // with the reason by Enforce. return next()( server, name, @@ -199,6 +187,15 @@ int authTicketLength ulong steamId = ServerAccessBook.TicketSteamId(authTicket, authTicketLength); eServerAccess access = _access.Decide(steamId.ToString()); + if (steamId != 0) + { + _access.Connecting( + steamId.ToString(), + access == eServerAccess.Denied, + Humans().Any(player => SteamIdOf(player) == steamId) + ); + } + if (access is eServerAccess.Unknown or eServerAccess.Open) { return (access, nint.Zero); diff --git a/apps/player-management-sw/src/PlayerManagementPlugin.cs b/apps/player-management-sw/src/PlayerManagementPlugin.cs index 6f20c26f..31e7dfc6 100644 --- a/apps/player-management-sw/src/PlayerManagementPlugin.cs +++ b/apps/player-management-sw/src/PlayerManagementPlugin.cs @@ -430,6 +430,27 @@ private void Enforce(List humans) { ulong steamId = SteamIdOf(player); + // Before the awaiting skip: a refused player must not play while + // the sanctions answer is still out. + if (_access.IsDenied(steamId.ToString())) + { + if (_kicked.Add(steamId)) + { + _logger.LogInformation( + "kicking {name} ({steamId}): not on the server's access list", + player.Name, + steamId + ); + + player.Kick( + _access.KickReason(LocalizerFor(player)["access.denied"]), + ENetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED + ); + } + + continue; + } + if (_book.IsAwaiting(steamId.ToString())) { continue; @@ -462,25 +483,6 @@ private void Enforce(List humans) continue; } - if (_access.IsDenied(steamId.ToString())) - { - if (_kicked.Add(steamId)) - { - _logger.LogInformation( - "kicking {name} ({steamId}): not on the server's access list", - player.Name, - steamId - ); - - player.Kick( - _access.KickReason(LocalizerFor(player)["access.denied"]), - ENetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED - ); - } - - continue; - } - // Re-asserted rather than set once: the engine resets voice flags // across a reconnect and a map change. if (state.IsMuted && !player.VoiceFlags.HasFlag(VoiceFlagValue.Muted)) @@ -1146,6 +1148,11 @@ private HookResult OnChat(int playerId) return HookResult.Continue; } + if (_access.IsDenied(SteamIdOf(player).ToString())) + { + return HookResult.Stop; + } + SanctionState state = _book.StateFor(SteamIdOf(player).ToString(), DateTimeOffset.UtcNow); if (!state.IsGagged) diff --git a/apps/player-management-sw/test/ConnectRefusalTests.cs b/apps/player-management-sw/test/ConnectRefusalTests.cs index 02e8fffd..fec5b75d 100644 --- a/apps/player-management-sw/test/ConnectRefusalTests.cs +++ b/apps/player-management-sw/test/ConnectRefusalTests.cs @@ -48,45 +48,69 @@ public void TheRefusedPasswordIsOneNobodyCouldSet() Assert.Contains(ConnectRefusal.Password, character => char.IsControl(character)); } - private static void RefusesWithThePasswordBeforeStrippingTheTicket(string hook, string byPassword, string stripped) + // Without a password the Swiftly gate lets the client in and kicks it + // with the reason, so it never strips the ticket. + [Fact] + public void TheSwiftlyAccessGateRefusesWithThePasswordAndOtherwiseMarksTheKick() { - Match password = Regex.Match(hook, byPassword); - Match ticket = Regex.Match(hook, stripped); + string hook = Code("player-management-sw", "ConnectGate.cs"); - Assert.True(password.Success, "the refused password path is gone or changed"); - Assert.True(ticket.Success, "the empty ticket path is gone or changed"); - Assert.True(password.Index < ticket.Index); Assert.Matches( - @"RefusedPasswordBuffer = Marshal\.StringToCoTaskMemUTF8\(\s*ConnectRefusal\.Password\s*\)", + @"if\s*\(\s*access == eServerAccess\.Denied\s*&&\s*ConnectRefusal\.ByPassword\(\s*Core\.ConVar\.Find\(""sv_password""\)\?\.Value\s*\)\s*\)\s*\{\s*return next\(\)\(\s*server,\s*name,\s*address,\s*netInfo,\s*connectMsg,\s*RefusedPasswordBuffer,\s*authTicket,\s*authTicketLength,\s*isLowViolence\s*\);", hook ); - } - - [Fact] - public void TheSwiftlyAccessGateRefusesWithThePasswordAndOnlyStripsTheTicketWithoutOne() - { - RefusesWithThePasswordBeforeStrippingTheTicket( - Code("player-management-sw", "ConnectGate.cs"), - @"if\s*\(\s*access == eServerAccess\.Denied\s*&&\s*ConnectRefusal\.ByPassword\(\s*Core\.ConVar\.Find\(""sv_password""\)\?\.Value\s*\)\s*\)\s*\{\s*return next\(\)\(\s*server,\s*name,\s*address,\s*netInfo,\s*connectMsg,\s*RefusedPasswordBuffer,\s*authTicket,\s*authTicketLength,\s*isLowViolence\s*\);", - @"if\s*\(access == eServerAccess\.Denied\)\s*\{\s*return next\(\)\(\s*server,\s*name,\s*address,\s*netInfo,\s*connectMsg,\s*password,\s*nint\.Zero,\s*0," + Assert.DoesNotMatch(@"nint\.Zero,\s*0,\s*isLowViolence", hook); + Assert.Matches( + @"_access\.Connecting\(\s*steamId\.ToString\(\),\s*access == eServerAccess\.Denied,", + hook ); } [Fact] - public void TheCounterStrikeSharpAccessGateRefusesWithThePasswordAndOnlyStripsTheTicketWithoutOne() + public void TheCounterStrikeSharpAccessGateRefusesWithThePasswordAndOtherwiseMarksTheKick() { string hook = Code("player-management-css", "ConnectGate.cs"); - RefusesWithThePasswordBeforeStrippingTheTicket( - hook, - @"if\s*\(\s*ConnectRefusal\.ByPassword\(ConVar\.Find\(""sv_password""\)\?\.StringValue\)\s*\)\s*\{\s*hook\.SetParam\(5, RefusedPasswordBuffer\);\s*\}", - @"else\s*\{\s*hook\.SetParam\(6, 0\);\s*hook\.SetParam\(7, 0\);" + Assert.Matches( + @"if\s*\(\s*access == eServerAccess\.Denied\s*&&\s*ConnectRefusal\.ByPassword\(ConVar\.Find\(""sv_password""\)\?\.StringValue\)\s*\)\s*\{\s*hook\.SetParam\(5, RefusedPasswordBuffer\);\s*\}", + hook + ); + Assert.Matches( + @"RefusedPasswordBuffer = Marshal\.StringToCoTaskMemUTF8\(\s*ConnectRefusal\.Password\s*\)", + hook ); - // A denied player and a banned one coming back are refused the same way. - Assert.Matches(@"if \(access == eServerAccess\.Denied\)\s*\{\s*Refuse\(hook\);", hook); + Assert.DoesNotMatch(@"hook\.SetParam\(\s*[67]\s*,", hook); Assert.Matches( - @"if \(_rejoin\.Refuse\(steamId, _book, DateTimeOffset\.UtcNow\)\)\s*\{[\s\S]*?Refuse\(hook\);", + @"_access\.Connecting\(\s*steamId\.ToString\(\),\s*access == eServerAccess\.Denied,\s*Humans\(\)\.Any\(", hook ); + // A banned player coming back is refused the same way, only on a password server. + Assert.Matches( + @"_rejoin\.Refuse\(steamId, _book, DateTimeOffset\.UtcNow\)\s*&&\s*ConnectRefusal\.ByPassword\(ConVar\.Find\(""sv_password""\)\?\.StringValue\)\s*\)\s*\{[\s\S]*?hook\.SetParam\(5, RefusedPasswordBuffer\);", + hook + ); + } + + // A refused player is kicked even while the sanctions answer is out, and + // says nothing on the way. + [Theory] + [InlineData("player-management-sw")] + [InlineData("player-management-css")] + public void ARefusedPlayerIsKickedBeforeTheSanctionsAnswerAndCannotChat(string app) + { + string plugin = Code(app, "PlayerManagementPlugin.cs"); + + int enforce = plugin.IndexOf("private void Enforce(", StringComparison.Ordinal); + Assert.True(enforce >= 0, "Enforce is gone or renamed"); + + int denied = plugin.IndexOf("_access.IsDenied(steamId.ToString())", enforce, StringComparison.Ordinal); + int awaiting = plugin.IndexOf("_book.IsAwaiting(steamId.ToString())", enforce, StringComparison.Ordinal); + Assert.True(denied >= 0 && awaiting >= 0); + Assert.True(denied < awaiting, "the access kick must come before the awaiting skip"); + + Assert.Matches( + @"if\s*\(_access\.IsDenied\(SteamIdOf\(player\)\.ToString\(\)\)\)\s*\{\s*return HookResult\.Stop;\s*\}\s*SanctionState state = _book\.StateFor\(", + plugin + ); } } diff --git a/apps/player-management-sw/test/ServerAccessBookTests.cs b/apps/player-management-sw/test/ServerAccessBookTests.cs index ef8bf01f..211a4270 100644 --- a/apps/player-management-sw/test/ServerAccessBookTests.cs +++ b/apps/player-management-sw/test/ServerAccessBookTests.cs @@ -107,6 +107,31 @@ public void ADenialCoversOnlyThePlayersThePanelWasAskedAbout() Assert.False(book.IsDenied("2")); } + [Fact] + public void TheConnectGatesVerdictMarksAndClearsADenial() + { + ServerAccessBook book = new(); + + book.Connecting("1", true, false); + Assert.True(book.IsDenied("1")); + + book.Connecting("1", false, false); + Assert.False(book.IsDenied("1")); + } + + [Fact] + public void AConnectUnderTheIdOfAPlayerAlreadyHereChangesNothing() + { + ServerAccessBook book = new(); + + book.Connecting("1", true, true); + Assert.False(book.IsDenied("1")); + + book.Answered(["2"], ["2"], null); + book.Connecting("2", false, true); + Assert.True(book.IsDenied("2")); + } + [Fact] public void LeavingForgetsADenial() { diff --git a/shared/dotnet/FiveStack.Utilities/ConnectRefusal.cs b/shared/dotnet/FiveStack.Utilities/ConnectRefusal.cs index 6d2d1ce0..3cc4ade1 100644 --- a/shared/dotnet/FiveStack.Utilities/ConnectRefusal.cs +++ b/shared/dotnet/FiveStack.Utilities/ConnectRefusal.cs @@ -5,7 +5,8 @@ namespace FiveStack.Utilities; // was full. With a server password the client is handed this one instead, and // the engine refuses it once. It holds a control character, which no password // can. The engine reads an empty sv_password or "none" as no password at all, -// and then this would let the client in, so those keep the empty ticket. +// and then this would let the client in, so without one the client is let in +// and kicked with a reason once it is in game. public static class ConnectRefusal { public const string Password = "\u0001refused"; diff --git a/shared/dotnet/FiveStack.Utilities/ServerAccessBook.cs b/shared/dotnet/FiveStack.Utilities/ServerAccessBook.cs index 2483d75f..7b9da32f 100644 --- a/shared/dotnet/FiveStack.Utilities/ServerAccessBook.cs +++ b/shared/dotnet/FiveStack.Utilities/ServerAccessBook.cs @@ -93,6 +93,30 @@ public void Answered(IEnumerable queried, IEnumerable denied, st } } + // The connect gate's own verdict, so a player let in only to be kicked + // is kicked with the reason even while the panel cannot be reached. A + // connect under the id of a player already here (a stale list, or a + // claimed id) must not get that player kicked. + public void Connecting(string steamId, bool denied, bool alreadyHere) + { + if (alreadyHere) + { + return; + } + + lock (_lock) + { + if (denied) + { + _denied.Add(steamId); + } + else + { + _denied.Remove(steamId); + } + } + } + // A denial is only good for the visit it was given on: a player removed // and later re-added must not be kicked on the way back in by a stale one. public void Left(string steamId)