A request with no client in its ASGI scope skips the entire pipeline, and nothing is logged
Description
BypassHandler.handle_passthrough (guard_core/core/bypass/handler.py:18-20):
if not request.client_host:
response = await call_next(request)
return await self.context.response_factory.apply_modifier(response)client is optional in the ASGI spec. When it is absent, every check is skipped - bans, rate limiting, IP lists, detection - with no log record, no telemetry event, and no way for the operator to notice. fail_secure=True, the default, does not cover this path.
Details
Servers and adapters that omit client: AWS Lambda and Azure Functions ASGI adapters, Unix-domain-socket transports, several test harnesses, and any custom ASGI shim that forwards a scope it built by hand. None of these are exotic - a Lambda deployment is a normal way to run FastAPI.
The failure is quiet in both directions. Guard logs Security pipeline initialized with 14 checks at startup exactly as it does in a working deployment, and a /_guard/status probe reports everything ready.
PoC
class DropClient: # what a serverless adapter hands the app
def __init__(self, inner): self.inner = inner
async def __call__(self, scope, receive, send):
if scope["type"] == "http":
scope = dict(scope); scope.pop("client", None)
return await self.inner(scope, receive, send)Same app, rate_limit=2/60s, once with the scope intact and once through DropClient:
rate limit 2/60s, scope['client'] present : [200, 200, 429, 429, 429]
rate limit 2/60s, scope['client'] absent : [200, 200, 200, 200, 200]
SQLi probe (limit 100), client present : 400
SQLi probe (limit 100), client absent : 200
log records naming the missing client : 0Full script attached as poc_missing_client.py.
<details> <summary><code>poc_missing_client.py</code></summary>
"""A request with no `client` in its ASGI scope skips every check, and logs nothing.
uv run python docs/upstream-guard-core/followups/poc_missing_client.py
`BypassHandler.handle_passthrough` (guard_core/core/bypass/handler.py:18-20)
returns the downstream response as soon as `request.client_host` is falsy. The
ASGI spec makes `client` optional, so the trigger is a deployment detail, not an
attack: serverless adapters, Unix-socket transports and some test harnesses omit it.
"""
import logging
from importlib.metadata import version
from fastapi import FastAPI
from guard.middleware import SecurityMiddleware
from guard_core.models import SecurityConfig
from starlette.testclient import TestClient
CAPTURED: list[str] = []
class Capture(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
CAPTURED.append(f"{record.levelname} {record.name} {record.getMessage()}")
logging.getLogger().addHandler(Capture())
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger("guard_core").setLevel(logging.DEBUG)
def app(rate_limit: int = 2) -> FastAPI:
application = FastAPI()
application.add_middleware(
SecurityMiddleware,
config=SecurityConfig(enable_redis=False, rate_limit=rate_limit, rate_limit_window=60),
)
@application.get("/")
async def root():
return {"ok": True}
return application
class DropClient:
"""What a serverless adapter or a Unix-socket transport hands the app."""
def __init__(self, inner):
self.inner = inner
async def __call__(self, scope, receive, send):
if scope["type"] == "http":
scope = dict(scope)
scope.pop("client", None)
return await self.inner(scope, receive, send)
print(f"guard-core {version('guard-core')} / fastapi-guard {version('fastapi-guard')}")
with TestClient(app(), client=("203.0.113.9", 1)) as client:
with_client = [client.get("/").status_code for _ in range(5)]
with TestClient(app(rate_limit=100), client=("203.0.113.8", 1)) as client:
sqli_with = client.get("/", params={"q": "' OR 1=1 --"}).status_code
CAPTURED.clear()
with TestClient(DropClient(app())) as client:
without_client = [client.get("/").status_code for _ in range(5)]
with TestClient(DropClient(app(rate_limit=100))) as client:
sqli_without = client.get("/", params={"q": "' OR 1=1 --"}).status_code
print(f" rate limit 2/60s, scope['client'] present : {with_client}")
print(f" rate limit 2/60s, scope['client'] absent : {without_client}")
print(f" SQLi probe (limit 100), client present : {sqli_with}")
print(f" SQLi probe (limit 100), client absent : {sqli_without}")
mentions = [line for line in CAPTURED if "client" in line.lower() and "scope" in line.lower()]
print(f" log records naming the missing client : {len(mentions)}")
print(" guard reports itself as enabled and initialised throughout; nothing in the")
print(" logs distinguishes this from a deployment where every check ran and passed.")</details>
Impact
An application deployed behind an adapter that does not populate client runs with guard-core installed, configured, reporting healthy, and enforcing nothing. The operator has no signal. This is the failure mode that is worst to discover during an incident, because the dashboard says the control is on.
Suggested fix
Treat a missing client as the identity "unknown" and run the pipeline against it - detection and the IP lists work unchanged, and rate limiting degrades to a shared bucket, which is the right answer when every request genuinely is indistinguishable. At minimum, log once at WARNING on the first such request and honour fail_secure on this path. Silently passing through is the one option that should not be available.
Fixed in 3.14.0
Fixed in guard-core 3.14.0 (PyPI), merged in #82 (500d4ef6). A request with no client address is rejected (403) under fail_secure=True, the default, with a one-time warning naming the fix; under fail_secure=False it runs as the unknown identity, which is allowed only when no global whitelist, whitelist_countries, route ip_whitelist or route whitelist_countries is configured and can never match a blacklist or country block. A new "unix" token in trusted_proxies makes a peer-less connection a trusted hop so X-Forwarded-For resolves over Unix sockets. UNKNOWN_CLIENT_IDENTITY is exported from guard_core.utils. fastapi-guard 7.8.0 adopts the rule at the ASGI boundary.