Skip to content

fix: harden local GraphServer with loopback bind, token auth, CORS, and kernel sessions - #66

Merged
cqian23 merged 1 commit into
mainfrom
fix/graph-server-security
Nov 30, 2026
Merged

cqian23 merged 1 commit into
mainfrom
fix/graph-server-security

Conversation

@cqian23

@cqian23 cqian23 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Hardens the local Jupyter GraphServer against unauthenticated network/cross-origin access and arbitrary database selector / query injection:

  1. Loopback-only bind (127.0.0.1): Bind ThreadedTCPServer to 127.0.0.1 instead of "" (0.0.0.0), and inherit GraphServerHandler from http.server.BaseHTTPRequestHandler (returning 404 for unknown paths instead of serving files from the working directory).
  2. Strict loopback CORS + preflight: Remove Access-Control-Allow-Origin: *. Only echo Origin with Vary: Origin when the origin is an explicit loopback origin (localhost, 127.0.0.1, or [::1]), and handle OPTIONS preflight (204 for loopback origins, 403 otherwise). Same-origin /proxy/<port> environments (such as Vertex AI Workbench) continue working without requiring CORS headers.
  3. Per-session authentication token (X-Graph-Server-Token): Generate a random 256-bit token on GraphServer.init(), pass it from the kernel into the rendered visualization HTML (generate_visualization_html(..., token=...) with an optional default "" to preserve compatibility with bigquery-magics), send it from frontend/src/graph-server.js, and require a matching token (via hmac.compare_digest) on every GET/POST endpoint (returning 401 otherwise). Rebuilt third_party/index.js.
  4. Kernel-side session registry: Register (selector, graph, query) on the Python kernel side keyed by an opaque session_id in magics.py:visualize(). GraphServerHandler (and the Colab callbacks) resolve the kernel-side session by session_id and ignore any client-supplied selector or query in request payloads.

Testing

  • Python unit tests (pytest tests/): added regression tests in tests/server_test.py and tests/node_expansion_test.py covering loopback binding, 401 rejection for unauthenticated requests, CORS preflight / origin enforcement, 404 on unknown GET paths, and rejection/ignoring of forged body selectors and queries.
  • Frontend unit tests (cd frontend && npm run test:unit): 10/10 suites, 165/165 tests passing (including new token-header unit tests in frontend/tests/unit/graph-server.test.ts).
  • bigquery-magics compatibility tests (test_graph_server.py, test_bigquery.py): 130 passed, 10 skipped.
  • End-to-end Jupyter Notebook 7 + Chrome test (%%spanner_graph --mock): verified OPTIONS preflight (204, loopback ACAO), authenticated POST /post_query with X-Graph-Server-Token and session_id (200), and graph canvas rendering with zero console errors.

@cqian23
cqian23 merged commit 3734396 into main Nov 30, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant