feat: rate-limit connections and cap concurrent sessions (#56) - #62
Merged
Conversation
Closes #56 (deferred from the security audit). The accept loop spawned a RemoteSession (two threads) per connection with no limit, so a connection flood is a resource-exhaustion DoS. It also undercut the auth (#41) and setPlayer (#51) brute-force lockouts, which close only the current connection after 3 bad guesses: an attacker just reconnected for another 3 with no cooldown. Adds admission control applied on the raw socket BEFORE a RemoteSession is built: a concurrent-session cap (max-sessions, default 100) reusing the existing session-list size, and a per-IP fixed-window connection-rate limit (max-connections-per-minute, default 60) in a new ConnectionRateLimiter. Refused sockets are closed without spawning threads. Both default-on, both disablable with 0. Mutation-guard verified: neutering the rate check lets the over-limit connection through; weakening the session-cap comparison flips the boundary test. 133 tests (6 new). Config, README, SECURITY.md, CHANGELOG updated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #56 — the connection-rate-limiting item deferred from the security audit.
Summary
The accept loop (
ServerListenerThread) spawned aRemoteSession— two threads — per connection with no limit, so a connection flood is a resource-exhaustion DoS. It also undercut theauth(#41) andsetPlayer(#51) brute-force lockouts: those close only the current connection after 3 bad guesses, so an attacker just reconnected for another 3 with no cooldown.Adds admission control on the raw socket, before a
RemoteSessionis built (so refused connections never spawn threads):max-sessions(default 100) — concurrent-session cap, reusing the existing session-list size.0= unlimited.max-connections-per-minute(default 60) — per-IP fixed-window rate limit in a newConnectionRateLimiter(O(1), tiny memory, prunes under an IP-spoof flood).0= disabled.Refused sockets are closed immediately. Defaults are classroom-friendly (100 concurrent, 60 conn/min/IP) yet close the flood and reconnect-to-brute-force loopholes.
Test plan
./mvnw test— 133 tests (6 new), 1 pre-existing skipConnectionRateLimiterTest(4): up-to-limit-then-refuse, window reset, per-IP independence, 0-disablesConnectionAdmissionTest(2): session-cap boundary at the configured max, andadmit()refusing the 61st connection from one IP while a different IP still passes<to<=flips the boundary testhttps://claude.ai/code/session_01AcK3SgXRTVzAKBdPZw72sr