Headless Browser Cookies Scoped to CDP Connection
说明与用途
在 Lightpanda 中,cookie jar 与 CDP 连接绑定,而非浏览器进程。通过一个 WebSocket 注入的会话对其他进程不可见。解决方案是使用 relay,保持唯一的 CDP 连接,序列化所有命令,并在浏览器重启时重新注入 cookie,确保会话持久化。
问题
Automation agents on a VPS could not see injected login cookies because the headless browser’s cookie jar was scoped to the CDP connection, not the browser process.
结果
By routing all CDP commands through a single relay, session cookies persist across browser restarts and are visible to all agents, eliminating logged‑out bugs caused by per‑connection cookie jars.
工作流
- 1. Build a relay that maintains one CDP WebSocket connection to Lightpanda.
- 2. Expose HTTP endpoints (e.g., POST /v1/cdp) that proxy CDP commands to the single connection.
- 3. Agents send all Network.setCookies, Page.navigate, Runtime.evaluate requests to the relay instead of directly to the browser.
- 4. On browser restart, the relay detects the dead connection, re‑establishes it, and re‑injects the last synced session cookies via Network.setCookies.
- 5. Strip any expires attribute from cookies before injection to avoid silent discards.
- 6. Optionally set Domain, Secure, httpOnly, and path=/ to avoid mismatches.
证据
The headless browser I use is Lightpanda, a Chromium-family build driven over the Chrome DevTools Protocol. Its cookie jar is scoped per CDP connection, not per browser process.
The relay I built now owns the only CDP connection for its whole lifetime. Agents talk to it over plain HTTP on loopback.
Injecting a cookie with an expires attribute was silently discarded — not rejected, just dropped.
- 记录日期: 2026-09-17T14:30:23.065Z