[deep-review][duplication] D8: Loopback host check duplicated in 3 places with subtly different rules
Summary
The notion "is this URL/IP a loopback?" is implemented three times across the codebase, each with slightly different semantics.
Where
src/OpenClaw.Shared/Mcp/McpHttpServer.cs:340-358—IsHostAllowed: accepts127.0.0.1[:port],localhost[:port],[::1]:portexactly. Used as the Host-header allowlist for DNS-rebinding mitigation.src/OpenClaw.Shared/Mcp/McpHttpServer.cs:136-140—IPAddress.IsLoopback(RemoteEndPoint)— checks the connecting socket, not a string.src/OpenClaw.Shared/LocalGatewayUrlClassifier.cs—IsLocalGatewayUrl: a broader check includinglocalhost,127.*,0.0.0.0, and WSL-typical addresses. Used to mark a gateway record asIsLocal.
Plus inline checks in:
src/OpenClaw.Tray.WinUI/Helpers/GatewayChatUrlBuilder.cs— when deciding if a chat URL needs TLS warning (related to C9).src/OpenClaw.WinNode.Cli/Program.cs:157-168— off-loopback refusal when an explicit--mcp-urlpoints off127.0.0.1.
Why it matters
Each check answers a slightly different question:
- "Is the bound socket loopback?" (security-critical for MCP)
- "Is the user-provided URL loopback-ish?" (UX hint for the chat URL)
- "Is this a local-WSL-style gateway?" (classification for the gateway registry)
These really are different questions, so consolidating into a single boolean would lose information. But the underlying parsing (extract host, compare to literal 127.0.0.1 / localhost / [::1]) is duplicated.
Proposed fix
src/OpenClaw.Shared/Net/LoopbackClassifier.cs:
public static class LoopbackClassifier
{
public static bool IsLoopbackHostString(string host); // 127.0.0.1, localhost, ::1
public static bool IsLoopbackEndpoint(IPEndPoint? ep);
public static bool IsLocalGatewayUrl(string url); // existing semantics
public static bool IsPrivateNetworkUrl(string url); // 192.168/10/172.16-31/fc00::/7
}
Each existing caller picks the function it wants. The shared parsing (extract host, IPv6 brackets, port stripping) happens once.
Side benefit: when implementing C9 (TLS warning for non-loopback ws://), use IsPrivateNetworkUrl from this helper.
Severity
Low — duplication; becomes High-priority when C9 lands (to keep the local-net allowlist consistent).
Related
- C9 (TLS warning for non-loopback
ws://).
🤖 This is an automated response from Repo Assist.
Verified the three sites against the codebase. Each one does answer a genuinely different question:
| Site | Question asked | Note |
|---|---|---|
McpHttpServer:IsHostAllowed |
"Is the Host header ≈ 127.0.0.1/localhost/[::1]?" |
DNS-rebinding defence — strict allowlist |
McpHttpServer:IPAddress.IsLoopback |
"Is the connecting socket loopback?" | Socket-layer check, no string parsing |
LocalGatewayUrlClassifier:IsLocalGatewayUrl |
"Is this gateway URL local/WSL-ish?" | Broader: covers 0.0.0.0, WSL IPs, 127.* |
GatewayChatUrlBuilder:uri.IsLoopback |
"Should the chat URL skip the TLS hint?" | Uri.IsLoopback — correctly delegates to the BCL |
The dangerous duplication is really only between IsHostAllowed and any future expansion of C9 (TLS warning for ws:// gateways). A LoopbackClassifier helper as proposed would:
- Make
IsHostAllowedtestable in isolation (currently only tested viaMcpHttpServerintegration tests). - Give C9 a single host-string parser to extend with
IsPrivateNetworkUrl. - Centralise the IPv6-bracket-strip logic (
[::1]:port→::1) which is re-implemented at each site.
The uri.IsLoopback call in GatewayChatUrlBuilder is idiomatic and should stay as-is — the BCL already gets this right.
Generated by 🌈 Repo Assist, see workflow run. Learn more.
Generated by 🌈 Repo Assist, see workflow run. Learn more.
To install this agentic workflow, run
gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a
Closing during the post-refactor deep-review cleanup. Loopback host classification is consolidated into #372 for a fresh bounded audit. This keeps the signal while reducing stale one-off issue noise.