H5: Security review, and fixes for connection-slot exhaustion

docs/SECURITY_REVIEW.md lists the scope, nine findings and what was done.

- High, fixed: a peer that connects and stops answering kept its ENet slot
  for up to 15 s after being turned away, so one address could fill every
  slot. Flood refusals now disconnect at once (reason in the disconnect
  data, which NetClient now honours for REASON_BUSY), peers told to leave
  get 1 s to acknowledge, and when slots run short the longest-waiting
  silent guest makes room.
- Medium, mitigated: ENet holds half-open slots for spoofed CONNECTs that
  the script never sees. The host now has 512 slots (NetServer.peer_slots).
- Medium, fixed: SourceLimiter, handshakes per source and the list
  server's per-address cap count an IPv6 /64 as one source.
- Low, fixed: CommandLog refuses a header with non-numeric fields instead
  of raising errors.
- tools/godot.sh unsets DISPLAY and WAYLAND_DISPLAY so the engine's error
  alerts can never reach the host desktop.
- A test fixture's 10.0.0.9 is now the documentation address 192.0.2.9.

Tests: 4 new handshake tests with real non-acknowledging ENet peers,
test_source_limiter (3), a CommandLog type test. make check, make fuzz,
make client-check, make discovery-check and make soak-selfcheck pass.

Closes #58

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 02:36:36 -05:00
co-authored by Claude Opus 5.5
parent e3c0d2814c
commit 9b5064d7a8
14 changed files with 515 additions and 23 deletions
+94
View File
@@ -389,3 +389,97 @@ func test_a_graceful_shutdown_reaches_clients_on_a_lossy_link() -> void:
assert_eq(client.close_reason, NetProtocol.REASON_SHUTDOWN, "%s was told the server stopped" % client.player_name)
for relay: UdpRelay in relays:
relay.close()
## H5: a peer that connects and then never answers again: it doesn't say hello, and
## doesn't acknowledge being turned away, so ENet alone would keep its slot for seconds.
func _silent(silent: Array[ENetConnection]) -> void:
var host: ENetConnection = ENetConnection.new()
assert_eq(host.create_host(1, NetProtocol.CHANNEL_COUNT), OK)
host.connect_to_host(HOST, server.port(), NetProtocol.CHANNEL_COUNT)
silent.append(host)
var connected: Array[bool] = [false]
_pump(func() -> bool:
var event: Array = host.service(0)
if event[0] == ENetConnection.EVENT_CONNECT:
connected[0] = true
return connected[0], 2000)
# The acknowledgement goes out on the next service; send it, then never again.
host.flush()
var before: int = server.guest_count() + _events("refused").size() + server.evicted
_pump(func() -> bool: return server.guest_count() + _events("refused").size() + server.evicted > before, 1000)
func _destroy(silent: Array[ENetConnection]) -> void:
for host: ENetConnection in silent:
host.destroy()
func test_a_flood_from_one_address_cannot_hold_the_slots() -> void:
server.close()
server.max_players = 2
server.peer_slots = 0
server.connect_limiter = SourceLimiter.new(3, 60000)
server.max_guests_per_address = 3
server.handshake_timeout_msec = 60000
assert_eq(server.listen(0, HOST), OK)
# Ten slots: three silent guests, then peers turned away for flooding.
var silent: Array[ENetConnection] = []
for _index: int in 9:
_silent(silent)
assert_eq(server.guest_count(), 3)
assert_eq(server.leaving_count(), 0, "peers turned away for flooding don't keep a slot")
assert_eq(_events("refused").size(), 6)
# The flood has passed; a real player still finds a slot.
server.connect_limiter = SourceLimiter.new(32, 10000)
server.max_guests_per_address = 8
var client: NetClient = _client("After")
assert_true(_pump(func() -> bool: return client.state == NetClient.JOINED or client.state == NetClient.CLOSED, 3000))
assert_eq(client.state, NetClient.JOINED)
_destroy(silent)
func test_silent_guests_make_room_for_a_newcomer() -> void:
server.close()
server.max_players = 2
server.peer_slots = 0
server.max_guests_per_address = 64
server.handshake_timeout_msec = 60000
assert_eq(server.listen(0, HOST), OK)
# More silent guests than the ten slots: the oldest make way, one slot stays free.
var silent: Array[ENetConnection] = []
for _index: int in 12:
_silent(silent)
assert_true(server.guest_count() <= 9, "a slot is kept free, %d guests" % server.guest_count())
assert_true(server.evicted >= 3)
var client: NetClient = _client("Newcomer")
assert_true(_pump(func() -> bool: return client.state == NetClient.JOINED or client.state == NetClient.CLOSED, 3000))
assert_eq(client.state, NetClient.JOINED)
assert_eq(server.player_count(), 1)
_destroy(silent)
func test_a_peer_that_never_acknowledges_leaving_is_cut_off() -> void:
server.close()
server.max_players = 2
server.handshake_timeout_msec = 100
server.leave_grace_msec = 300
assert_eq(server.listen(0, HOST), OK)
var silent: Array[ENetConnection] = []
_silent(silent)
assert_true(_pump(func() -> bool: return server.leaving_count() == 1, 2000), "the silent guest was told to go")
assert_true(_pump(func() -> bool: return server.leaving_count() == 0, 2000), "and cut off after its grace")
assert_eq(server.evicted, 1)
_destroy(silent)
## H5: a client turned away for flooding learns why from the disconnect alone.
func test_busy_reaches_the_client_without_a_reject_message() -> void:
assert_true(NetProtocol.is_server_reason(NetProtocol.REASON_BUSY))
assert_false(NetProtocol.is_server_reason(NetProtocol.REASON_NONE))
assert_false(NetProtocol.is_server_reason(NetProtocol.REASON_LOST))
server.connect_limiter = SourceLimiter.new(1, 60000)
var first: NetClient = _client("First")
var second: NetClient = _client("Second")
assert_true(_pump(func() -> bool: return second.state == NetClient.CLOSED and first.state == NetClient.JOINED))
assert_eq(second.close_reason, NetProtocol.REASON_BUSY)
+35
View File
@@ -0,0 +1,35 @@
extends TestCase
## H5: the rate limiter counts sources, and an IPv6 sender counts with its whole /64.
func test_sources_group_ipv6_by_its_64_and_keep_ipv4_whole() -> void:
assert_eq(SourceLimiter.source_of("192.0.2.7"), "192.0.2.7")
assert_eq(SourceLimiter.source_of("::ffff:192.0.2.7"), "192.0.2.7", "IPv4-mapped counts as the IPv4 address")
assert_eq(SourceLimiter.source_of("2001:db8:0:1::5"), "2001:db8:0:1::/64")
assert_eq(SourceLimiter.source_of("2001:0DB8:0000:0001:ffff:1:2:3"), "2001:db8:0:1::/64")
assert_eq(SourceLimiter.source_of("2001:db8::1"), "2001:db8:0:0::/64")
assert_eq(SourceLimiter.source_of("::1"), "0:0:0:0::/64")
assert_eq(SourceLimiter.source_of("fe80::1%eth0"), "fe80:0:0:0::/64")
assert_eq(SourceLimiter.source_of("2001:db8:1:2:3:4:192.0.2.1"), "2001:db8:1:2::/64")
assert_ne(SourceLimiter.source_of("2001:db8:0:1::5"), SourceLimiter.source_of("2001:db8:0:2::5"))
# Not an address: left as it is, never an error.
for odd: String in ["", "x", "1:2:3", "1::2::3", "zzzz::1", "12345::1"]:
assert_eq(SourceLimiter.source_of(odd), odd)
func test_one_ipv6_64_shares_one_allowance() -> void:
var limiter: SourceLimiter = SourceLimiter.new(3, 1000, 16)
for index: int in 3:
assert_true(limiter.allow("2001:db8:0:1::%x" % (index + 1), 0))
assert_false(limiter.allow("2001:db8:0:1::ffff", 0), "a fourth address in the same /64 is over the limit")
assert_true(limiter.allow("2001:db8:0:2::1", 0), "the next /64 has its own allowance")
assert_eq(limiter.tracked(), 2)
func test_rotating_addresses_in_one_64_cannot_fill_the_table() -> void:
var limiter: SourceLimiter = SourceLimiter.new(1000000, 1000, 4)
for index: int in 5000:
limiter.allow("2001:db8:0:1:%x::1" % index, 0)
assert_eq(limiter.tracked(), 1)
assert_true(limiter.allow("192.0.2.1", 0), "others are still let in")
+1
View File
@@ -0,0 +1 @@
uid://wnkqkkalmafs
+31
View File
@@ -103,3 +103,34 @@ func test_bad_bytes_are_refused_without_errors() -> void:
var short: PackedByteArray = good.duplicate()
short.encode_u32(8 + head_size, CommandLog.RECORD_SIZE * 40)
assert_null(CommandLog.from_bytes(short))
## H5: a header whose fields are the wrong kind of JSON is refused, never an error.
func test_a_header_with_the_wrong_types_is_refused_without_errors() -> void:
var good: PackedByteArray = _log().to_bytes()
var head_size: int = good.decode_u32(4)
var head: Dictionary = JSON.parse_string(good.slice(8, 8 + head_size).get_string_from_utf8())
var records: PackedByteArray = good.slice(8 + head_size)
var bad_rule: Dictionary = head.duplicate(true)
bad_rule["rules"]["max_players"] = [1, 2]
var bad_winner: Dictionary = head.duplicate(true)
bad_winner["winners"] = [{"x": 1}]
var bad_tick: Dictionary = head.duplicate(true)
bad_tick["final_tick"] = {"x": 1}
for changed: Dictionary in [bad_rule, bad_winner]:
assert_null(CommandLog.from_bytes(_with_head(changed, records)))
var read: CommandLog = CommandLog.from_bytes(_with_head(bad_tick, records))
assert_not_null(read, "a bad final tick alone still leaves a usable log")
assert_eq(read.final_tick, 0)
assert_eq(read.count(), 3)
func _with_head(head: Dictionary, records: PackedByteArray) -> PackedByteArray:
var text: PackedByteArray = JSON.stringify(head).to_utf8_buffer()
var out: StreamPeerBuffer = StreamPeerBuffer.new()
out.big_endian = false
out.put_data(CommandLog.MAGIC.to_ascii_buffer())
out.put_u32(text.size())
out.put_data(text)
out.put_data(records)
return out.data_array
+1 -1
View File
@@ -159,7 +159,7 @@ func test_practice_and_join_say_what_was_asked() -> void:
assert_eq(GameSettings.load_from(PATH).last_server, "127.0.0.1:24999")
# A found server, chosen from the list.
menus.show_servers([
{"name": "Old", "players": 9, "max_players": 24, "address": "10.0.0.9", "port": 24650, "source": "lan", "compatible": false},
{"name": "Old", "players": 9, "max_players": 24, "address": "192.0.2.9", "port": 24650, "source": "lan", "compatible": false},
{"name": "Harbour", "players": 3, "max_players": 24, "address": "127.0.0.1", "port": 24651, "source": "lan", "compatible": true},
] as Array[Dictionary])
menus._servers.select(0)