mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
fix(mcp): close OAuth 2.1 resource-server gaps before the remote surface ships
Harden the remote MCP endpoint and the in-repo OAuth bridge against the adversarial review of the milestone branch. The two ship-blockers made the "read-only, revocable" promise false; the rest tightened the surface. Audience binding at the resource server: a token whose only grant is the new `health:read` scope is the MCP read credential. The REST edge now accepts it on safe (read) methods and refuses it on writes/deletes with 403, so a connector token can read over `/mcp` and GET routes but can never POST/DELETE the record despite a "read-only" consent. The `/mcp` resolver is unaffected. Revocable, theft-detectable refresh tokens: refresh artifacts now anchor to a persistent connection row. Revoking a connection stops every future refresh and kills its access tokens; a replayed (already-rotated) refresh is treated as theft and revokes the whole connection. Connections surface in settings as the unit a user can disconnect. The old access token is revoked on rotation. Also: fail closed on the OAuth/`/mcp` surface when no canonical origin is configured and mark the discovery documents non-cacheable; honour the global API kill-switch on `/mcp`, `/token`, `/authorize`, and `/register`; cap the CIMD and registration body reads while streaming instead of after; show the verified client origin on consent; validate CIMD redirect-URI schemes; assert same-origin on the consent POST; and add a daily prune for expired connector tokens. New migration 0195 adds the connection anchor and links access tokens to it.
This commit is contained in:
1 parent
06363771ee
commit
22eb9cba0d
39 files changed
+1826
-72
No files matched your search
@@ -21,7 +21,13 @@
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
// The surface fails closed without a pinned origin (M1); pin it for the suite.
|
||||
process.env.APP_URL = "http://localhost";
|
||||
|
||||
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
|
||||
vi.mock("@/lib/app-settings", () => ({
|
||||
isApiGloballyEnabled: vi.fn(async () => true),
|
||||
}));
|
||||
vi.mock("@/lib/auth/bearer", () => ({
|
||||
resolveBearerToken: vi.fn(),
|
||||
BearerAuthError: class BearerAuthError extends Error {},
|
||||
@@ -60,6 +66,7 @@ import { POST, GET } from "../route";
|
||||
import { resolveBearerToken } from "@/lib/auth/bearer";
|
||||
import { isModuleEnabled } from "@/lib/modules/gate";
|
||||
import { checkMcpRateLimit } from "@/lib/rate-limit";
|
||||
import { isApiGloballyEnabled } from "@/lib/app-settings";
|
||||
|
||||
const READ_TOOLS = [
|
||||
"get_correlations",
|
||||
@@ -102,6 +109,7 @@ beforeEach(() => {
|
||||
headersGet.mockReturnValue(null);
|
||||
// Default happy-path: module on, under budget.
|
||||
vi.mocked(isModuleEnabled).mockResolvedValue(true);
|
||||
vi.mocked(isApiGloballyEnabled).mockResolvedValue(true);
|
||||
vi.mocked(checkMcpRateLimit).mockResolvedValue({
|
||||
allowed: true,
|
||||
remaining: 119,
|
||||
@@ -174,6 +182,17 @@ describe("/mcp — Origin gate (DNS-rebinding defense, spec PR #1439)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("/mcp — operator kill-switch (M4)", () => {
|
||||
it("hides the surface (404) when the API is globally disabled", async () => {
|
||||
validToken();
|
||||
vi.mocked(isApiGloballyEnabled).mockResolvedValue(false);
|
||||
const res = await POST(postRpc(TOOLS_LIST));
|
||||
expect(res.status).toBe(404);
|
||||
// Fails closed before the token table is consulted.
|
||||
expect(resolveBearerToken).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("/mcp — 401 carries the RFC 9728 resource_metadata pointer", () => {
|
||||
it("includes resource_metadata in the WWW-Authenticate challenge", async () => {
|
||||
const res = await POST(
|
||||
|
||||
+14
-1
@@ -46,8 +46,12 @@ import { createMcpServer, resolveMcpAuthContext } from "@/lib/mcp";
|
||||
import { withBackgroundEvent } from "@/lib/logging/background";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { isModuleEnabled } from "@/lib/modules/gate";
|
||||
import { isApiGloballyEnabled } from "@/lib/app-settings";
|
||||
import { checkMcpRateLimit, rateLimitHeaders } from "@/lib/rate-limit";
|
||||
import { resolveBaseOrigin } from "@/lib/mcp/oauth/config";
|
||||
import {
|
||||
isMcpOriginConfigured,
|
||||
resolveBaseOrigin,
|
||||
} from "@/lib/mcp/oauth/config";
|
||||
import { wwwAuthenticateChallenge } from "@/lib/mcp/oauth/metadata";
|
||||
import type { ModuleKey } from "@/lib/modules/registry";
|
||||
|
||||
@@ -131,6 +135,15 @@ async function handleMcp(request: Request): Promise<Response> {
|
||||
return forbiddenOrigin();
|
||||
}
|
||||
|
||||
// 0a. KILL-SWITCH + ORIGIN CONFIG — M1 fails closed without a pinned
|
||||
// origin; M4 honours the operator's global API switch. Both answer the
|
||||
// same invisible 404 as the module-off posture so a disabled instance
|
||||
// leaks nothing.
|
||||
if (!isMcpOriginConfigured() || !(await isApiGloballyEnabled())) {
|
||||
annotate({ action: { name: "mcp.surface.unavailable" } });
|
||||
return moduleDisabled();
|
||||
}
|
||||
|
||||
// 1. AUTH — Bearer only; a cookie is never consulted (admin unreachable).
|
||||
const raw = readBearer(request);
|
||||
if (!raw) {
|
||||
|
||||
Reference in new issue
Block a user