Repository navigation
v7.8.1
Websocket guard follow-ups: whitelist parity, explicit-config factory, close-code contract, state.client_ip (v7.8.1)
- Added -
make_guard_websocket(config: SecurityConfig, redis_handler: RedisManager | None = None)returns aDepends-ready WebSocket dependency bound to an explicitSecurityConfig, for a route on an app that never callsapp.add_middleware(SecurityMiddleware, config=...);guard_websocketis unchanged and still resolves its config from the registered middleware, raisingRuntimeErrorwithout one. Both entry points share one private check coroutine, so every rule applies to both. - Added -
guardexports module-level(code, reason)constants for the five WebSocket close outcomesguard_websocket/make_guard_websocketcan emit (WS_CLOSE_IP_BANNED,WS_CLOSE_IP_NOT_ALLOWED,WS_CLOSE_RATE_LIMIT_EXCEEDED,WS_CLOSE_CLIENT_ADDRESS_UNKNOWN,WS_CLOSE_SECURITY_CHECK_FAILED, collected inWS_CLOSE_REASONS), documented as a stable contract indocs/tutorial/websockets.mdthat a close handler can key logging on. - Added -
websocket.state.client_ipis now set to the resolved client identity (including"unknown") immediately after resolution, before any check runs, so a route handler can read it afteraccept(). - Fixed -
guard_websocketandmake_guard_websocketnow skip the rate-limit check for a client that passes a configured globalwhitelist, mirroring guard-core's HTTP rule (RateLimitCheck.checkreturns early once a request is resolved as whitelisted); previously a whitelisted WebSocket client could still be rate-limited. - Fixed - The websocket rate-limit check no longer constructs a
RedisManageritself.RedisManageris a process-wide singleton; constructing one with a differentSecurityConfigrebinds its connection for every other caller in the process, soguard_websocketpreviously risked overwriting the HTTP pipeline's own Redis manager, andmake_guard_websocketon an app withoutSecurityMiddlewarecould construct an uninitialized manager and raiseGuardRedisErroron every connect.guard_websocketnow resolves the middleware's already-initialized manager instead of constructing one;make_guard_websockettakes an optionalredis_handlerparameter and falls back to the in-memory store (logging one warning at dependency creation) whenenable_redis=Trueand none is given.
What's Changed
Full Changelog: 7.8.0...7.8.1