* feat(auth): inject MCP_INSPECTOR_API_TOKEN into served index.html (#1378) Reloading the web client at the bare URL (no `?MCP_INSPECTOR_API_TOKEN=…` query string) with empty sessionStorage made every `/api/*` request 401 — the browser had no way to recover the backend's auth token. Embed the token into `index.html` on every page load so the browser no longer depends on the query string surviving navigation: - New shared helper `clients/web/server/inject-auth-token.ts` embeds `<script>window.__INSPECTOR_API_TOKEN__ = "…"</script>` (escaped against `</script>` injection; no-op when auth is dangerously omitted). - Dev: the Vite plugin injects via `transformIndexHtml`. - Prod: the Hono server injects on the `/` route. - `App.tsx` `getAuthToken()` now reads the injected global first, then the query string, then sessionStorage (both fallbacks preserved). - Shared global name lives in `INSPECTOR_API_TOKEN_GLOBAL` (`core/mcp/remote/constants.ts`). Tests: helper unit coverage + an integration test exercising the real prod server's `/` → `/api/*` flow (injected token authenticates; missing token 401s). AGENTS.md documents the token-recovery order. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(auth): inject token into prod SPA fallback + prime sessionStorage + no-store (#1378) Addresses code-review feedback on the token-injection PR: - Prod `server.ts` SPA deep-link fallback (e.g. `/oauth/callback`) previously served the raw index.html off disk via serveStatic, bypassing injection — so a bookmark/reload at a non-`/` route with empty sessionStorage would 401. Route the SPA fallback through the same `serveIndexHtml` (inject) helper; real static assets (paths with a dot) still serve verbatim. Dev already injected on every HTML serve via Vite `transformIndexHtml`. - `getAuthToken()` now persists the injected `window.__INSPECTOR_API_TOKEN__` to sessionStorage (not just the URL-param branch), priming the backstop for any later navigation that loses the global. - Injected HTML responses now send `Cache-Control: no-store`, so a page carrying a token isn't cached and served stale after a restart regenerates the token. Integration tests added: SPA fallback (`/oauth/callback`) carries the token, `Cache-Control: no-store` on injected HTML, real assets served verbatim, and unknown `/api` routes 404 rather than falling through to the HTML shell. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
e3b0b6a908
commit
881ba4b5e8
1 changed files with 372 additions and 29 deletions
|
|
@ -17,6 +17,7 @@ import open from "open";
|
|||
// spurious "could not resolve" warnings during build.
|
||||
import { createRemoteApp } from "../../../core/mcp/remote/node/server.ts";
|
||||
import { createSandboxController } from "./sandbox-controller.js";
|
||||
import { injectAuthToken } from "./inject-auth-token.js";
|
||||
import type { WebServerConfig } from "./web-server-config.js";
|
||||
import {
|
||||
webServerConfigToInitialPayload,
|
||||
|
|
@ -24,8 +25,20 @@ import {
|
|||
} from "./web-server-config.js";
|
||||
|
||||
export function honoMiddlewarePlugin(config: WebServerConfig): Plugin {
|
||||
// Resolved once `configureServer` runs (createRemoteApp generates a token
|
||||
// when none is supplied). Captured here so the `transformIndexHtml` hook —
|
||||
// which fires per index.html request, after `configureServer` — can embed it
|
||||
// into the served page. Stays "" when auth is dangerously omitted or under
|
||||
// Vitest (where `configureServer` returns early), making injection a no-op.
|
||||
let resolvedAuthToken = "";
|
||||
return {
|
||||
name: "hono-api-middleware",
|
||||
// Embed the API token into the dev-served index.html so a reload at the
|
||||
// bare URL (no `?MCP_INSPECTOR_API_TOKEN=…`) still authenticates. The
|
||||
// prod server applies the same injection in `server.ts`.
|
||||
transformIndexHtml(html) {
|
||||
return injectAuthToken(html, resolvedAuthToken);
|
||||
},
|
||||
// `apply: 'serve'` keeps the plugin out of `vite build`, but Vitest still
|
||||
// instantiates a Vite server in middleware mode (no HTTP server) for
|
||||
// transforms and invokes `configureServer` regardless. Returning early
|
||||
|
|
@ -67,6 +80,10 @@ export function honoMiddlewarePlugin(config: WebServerConfig): Plugin {
|
|||
initialConfig: webServerConfigToInitialPayload(config),
|
||||
});
|
||||
|
||||
// Expose the resolved token to `transformIndexHtml`. Left empty when
|
||||
// auth is dangerously omitted so the page carries no token global.
|
||||
resolvedAuthToken = config.dangerouslyOmitAuth ? "" : resolvedToken;
|
||||
|
||||
// Chain the API close (mcp.json watcher) and the sandbox into the
|
||||
// Vite server's close so dev-server restarts release both resources.
|
||||
const originalClose = server.close.bind(server);
|
||||
|
|
|
|||
Loading…
Reference in a new issue