mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-10 16:04:04 +00:00
feat(multiplayer): Phase 3 task 3.4 - input validation, rate limiting, disconnect policy
MatchSim._recv_input now validates before decoding (§3.1 steps 2-3): per-peer rolling-1s rate limiting (packet count AND byte budget, dropping over-budget packets and disconnecting after 3 consecutive over-budget seconds), and framing validation (redundancy count and payload size checked against NetCodec's own layout before unpack_input ever runs, disconnecting after 20 malformed packets). Framing has to be validated explicitly rather than relying on decode failure: StreamPeerBuffer silently zero-fills past EOF instead of erroring, a finding from Phase 2's adversarial review. networked_match.gd's _on_input_received now rejects any seq claiming to be more than 20 ticks ahead of the current server tick (§3.1 step 4) and counts (rather than silently ignoring) input from a peer with no slot, for observability. Verified with two new permanent regression tests (networked_match_smoke.gd --role=client-abuse-malformed / client-abuse-flood) that call MatchSim._recv_input directly with garbage bytes and a legitimate-but- too-frequent flood, respectively, bypassing the honest client encoder entirely - the same thing a hostile custom client sending raw ENet packets would look like. Both confirm real disconnection, not just that the server tolerates the abuse. Two bugs surfaced by getting these tests to actually pass cleanly: a GDScript lambda-capture-by-value mistake in the tests themselves (a plain `var disconnected := false` mutated inside a signal-handler lambda never became visible to the enclosing function - fixed by capturing a single-element Array instead, which is captured by reference); and a narrow real race where NetworkManager's own ping/pong reply could target a peer that a concurrent abuse-triggered disconnect had just removed from the same poll() batch, now guarded. (Passing disconnect_peer's `force` parameter as an attempted fix for a related one-off benign error was tried and reverted - it made Godot's own peer-list bookkeeping inconsistent, producing hundreds of errors instead of one; verified empirically rather than assumed.) Full regression suite, including the net-sim-latency milestone gate, re-run clean.
This commit is contained in:
@@ -174,6 +174,16 @@ func _ping(client_send_ms: int) -> void:
|
||||
# RTT sample and the offset estimate instead of adding to them.
|
||||
var server_now := Time.get_ticks_msec()
|
||||
var sender_id := multiplayer.get_remote_sender_id()
|
||||
# A single poll() call can process several queued RPCs from the same
|
||||
# peer in one batch — an earlier one in that same batch (e.g. task 3.4's
|
||||
# abuse-triggered disconnect_peer(..., now=true), which removes the
|
||||
# peer immediately rather than waiting for an acknowledged disconnect)
|
||||
# can leave this ping's sender no longer a valid peer by the time its
|
||||
# own turn in the batch comes up. NetSim's inactive/passthrough path
|
||||
# (the common case — no CLI flags) dispatches immediately with no
|
||||
# validation of its own, so check here rather than relying on it.
|
||||
if sender_id not in multiplayer.get_peers():
|
||||
return
|
||||
NetSim.send(func() -> void: _pong.rpc_id(sender_id, client_send_ms, server_now), sender_id)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user