From db4da33bc6ba403d674bc55d6932156c768ff0fc Mon Sep 17 00:00:00 2001 From: Rainier <74943233+Rmosser@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:51:29 +0800 Subject: [PATCH] gateway: a LazyCat box's X-HC-* headers go on only to a provider whose PassHeaders names them (#5987) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On a LazyCat box (懒猫微服) every app sits behind the box's ingress, lzc-ingress, which puts on each request it passes the app who sent it: the box account (X-HC-User-ID), its role (X-HC-User-Role), what sent it (X-HC-Source), the device (X-HC-Device-ID, -PeerID and -Version) and when it signed in (X-HC-Login-Time); the box's docs name X-HC-User-Groups and X-HC-User-Ticket too. A request signed in to the box gets them on the app's public paths as well, where a package serves the gateway's client routes. The native route, the Codex passthrough and relay to the ChatGPT backend (copyHeaders) and /mcp/ passed them all on, so a vendor, the ChatGPT backend or a remote MCP server was told the box account's name and role and the device's ids. proxyIdentity takes any X-Hc-* as what a proxy says of the caller, as it takes Tailscale-*, so nativeHeaderDropped, copyHeaders and the MCP proxy's header loop skip them. lzc-ingress's X-Forwarded-By was dropped already (X-Forwarded-*). What 7d196b679 checked and left stays as it is: a provider's PassHeaders passes on the names it lists, these too, as the user chose them (gateway-routing.md now says so); Codex's allow-listed codexHeader, the Codex websocket's headers (taken from a request magpie built) and passOnCaller are as they were. What the ingress puts there was read off a probe app on a box (lzcos 1.6.2+fix.4) that echoes its request headers: a browser signed in to the box got X-HC-User-ID, X-HC-User-Role ADMIN, X-HC-Source client, X-HC-Device-ID, -PeerID, -Version and X-HC-Login-Time, on a public path the same as on /. Tests: TestNativeAnyDropsProxyIdentity and TestPassthroughDropsProxyIdentity send ten X-HC-* headers and X-Forwarded-By beside the twelve they sent, and check that none arrive and X-Custom still does. Run (macOS 27.2, go1.26.8 darwin/arm64), at this tree, on 6c3f113ce: gofmt clean; go vet, GOOS=linux -tags nogui go vet and GOOS=windows go vet of internal/gateway clean; go build -tags nogui ok; both tests ok. The next commit adds a test to internal/gateway and changes nothing else of it; the whole package ran there: ok. Broken by hand: with the X-Hc- prefix out of proxyIdentity, both are red: the native vendor, the ChatGPT backend and the MCP server were each told all ten X-HC-* headers. Not tried: a real vendor call through a box; a provider whose PassHeaders names an X-HC-* header. Co-Authored-By: Claude Opus 5.5 --- docs/subsystems/gateway-routing.md | 7 +++++-- internal/gateway/native.go | 11 +++++++---- internal/gateway/proxy_identity_test.go | 8 +++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 656de9cf6..caf796cab 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -1132,11 +1132,14 @@ OpenAI shape can't carry. GET, POST, PUT, PATCH and DELETE `proxyIdentity`: every name lanGuard reads as forwarded (`forwardHeaders`: `Forwarded`, `X-Forwarded-For`, `X-Real-IP`, `Cf-Connecting-IP`, `True-Client-IP`, `Tailscale-User-Login`), any - `X-Forwarded-*`, `Cf-*` and `Tailscale-*` (Tailscale Serve's user - login, name and picture), `Via`, `X-Client-IP` and + `X-Forwarded-*`, `Cf-*`, `Tailscale-*` (Tailscale Serve's user + login, name and picture) and `X-HC-*` (a LazyCat box's ingress: the + box account signed in, its role and device), `Via`, `X-Client-IP` and `X-Original-Forwarded-For`. The Codex passthrough and relay to the ChatGPT backend (`copyHeaders`) and the MCP proxy drop the same; see `TestNativeAnyDropsProxyIdentity` and `TestPassthroughDropsProxyIdentity`. + A provider's `PassHeaders` still passes on the names it lists, these + too: the user chose them. `Sign` puts in the provider's key as it takes one, and magpie's User-Agent goes in (`stampUA`). The answer's hop headers, `Set-Cookie`, `Alt-Svc` and HSTS don't come back (`nativeAnswerDropped`). diff --git a/internal/gateway/native.go b/internal/gateway/native.go index 00250a08f..cb2cf92e9 100644 --- a/internal/gateway/native.go +++ b/internal/gateway/native.go @@ -313,9 +313,12 @@ func nativeHeaderDropped(k string) bool { // proxyIdentity says k is what a proxy or tunnel in front of magpie says // of the caller: its address (every forwardHeaders entry, X-Forwarded-*, // Cloudflare's Cf-*) or who it is (Tailscale Serve's Tailscale-User-Login, -// the user's email, -Name and -Profile-Pic). It is for magpie to judge a -// request by (proxied), never for a vendor or a server magpie passes the -// request on to. +// the user's email, -Name and -Profile-Pic; a LazyCat box's X-HC-*, the +// box account a signed-in browser or app is, its role, its device and +// when it signed in, which the box's ingress adds to every request it +// passes an app, the gateway's client routes too). It is for magpie to +// judge a request by (proxied), never for a vendor or a server magpie +// passes the request on to. func proxyIdentity(k string) bool { k = http.CanonicalHeaderKey(k) if slices.ContainsFunc(forwardHeaders, func(h string) bool { return http.CanonicalHeaderKey(h) == k }) { @@ -325,7 +328,7 @@ func proxyIdentity(k string) bool { case "Forwarded", "Via", "X-Real-Ip", "True-Client-Ip", "X-Client-Ip", "X-Original-Forwarded-For": return true } - return strings.HasPrefix(k, "X-Forwarded-") || strings.HasPrefix(k, "Cf-") || strings.HasPrefix(k, "Tailscale-") + return strings.HasPrefix(k, "X-Forwarded-") || strings.HasPrefix(k, "Cf-") || strings.HasPrefix(k, "Tailscale-") || strings.HasPrefix(k, "X-Hc-") } // nativeAnswerDropped are the vendor's answer headers that don't come diff --git a/internal/gateway/proxy_identity_test.go b/internal/gateway/proxy_identity_test.go index 7334e0997..c21ce45c6 100644 --- a/internal/gateway/proxy_identity_test.go +++ b/internal/gateway/proxy_identity_test.go @@ -14,11 +14,17 @@ import ( // identityHeaders are what a proxy or tunnel in front of magpie says of // the caller: where it is, and with Tailscale Serve who (the user's -// email, name and picture). +// email, name and picture). A LazyCat box's ingress (lzc-ingress, in +// front of every app on the box) says who too: the box account signed +// in, its role and groups, the device and the sign-in's ticket, as the +// box's own docs and a request to a probe app on a box (lzcos 1.6.2) +// name them. var identityHeaders = []string{ "Forwarded", "X-Forwarded-For", "X-Forwarded-Host", "X-Forwarded-Proto", "X-Real-IP", "Via", "Cf-Connecting-IP", "Cf-Ipcountry", "True-Client-IP", "Tailscale-User-Login", "Tailscale-User-Name", "Tailscale-User-Profile-Pic", + "X-Forwarded-By", "X-HC-User-ID", "X-HC-User-Role", "X-HC-User-Groups", "X-HC-User-Ticket", "X-HC-Source", + "X-HC-Login-Time", "X-HC-Device-ID", "X-HC-Device-PeerID", "X-HC-Device-Version", "X-HC-Original-Host", } // identityHeadersOn is a request's headers: every identity header, and