mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
merge: bring the MCP server onto the v1.23 base
Combine the remote MCP server (stdio + remote /mcp + OAuth 2.1 + read tools + deep-value reads + a confirmed, audience-bound write surface) with the v1.23 account-security line (MFA, known/trusted devices, free-text note encryption). - schema: keep every v1.23 User relation (MFA, known/trusted devices) alongside the McpOAuthConnection relation; the MeasurementSource enum carries the MCP value. - api-handler: the MCP audience guard (a token scoped to health:read/health:write is bound to /mcp and refused a REST write) and the step-up MFA helpers coexist. - maintenance queues: register the MCP token-cleanup sweep next to the note-encryption backfill and key-rotation queues; shift it to 04:00 so the two daily discovery ticks do not collide. - migrations: renumber the MCP migrations to 0206/0207 so they follow the v1.23 chain (0199-0205). - MCP mood writes route the note through the v1.23 at-rest cipher.
This commit is contained in:
commit
fb986bc09d
89 files changed
+10276
-109
No files matched your search
@@ -0,0 +1,41 @@
|
||||
-- MCP Phase 3 security fix (H2) — revocable, theft-detectable refresh chains.
|
||||
--
|
||||
-- The remote MCP OAuth bridge minted stateless `hlrt_` refresh tokens that
|
||||
-- referenced no server row: they could not be revoked (the settings "revoke"
|
||||
-- only killed the 60-minute access token) and a replayed, already-rotated
|
||||
-- refresh token was not treated as theft. This migration adds the persistent
|
||||
-- anchor the refresh artifact lacked.
|
||||
--
|
||||
-- `mcp_oauth_connections` is the revocable unit: one row per connector
|
||||
-- connection that requested `offline_access`. `current_jti` is the only refresh
|
||||
-- `jti` valid at any moment — every rotation advances it, so a presented `jti`
|
||||
-- that is not `current_jti` is a replay (theft signal) that revokes the row.
|
||||
-- `revoked_at` is the kill switch the settings card flips; revoking the row
|
||||
-- stops every future refresh.
|
||||
CREATE TABLE "mcp_oauth_connections" (
|
||||
"id" TEXT NOT NULL,
|
||||
"user_id" TEXT NOT NULL,
|
||||
"client_id" TEXT NOT NULL,
|
||||
"client_name" TEXT NOT NULL,
|
||||
"scope" TEXT NOT NULL,
|
||||
"resource" TEXT NOT NULL,
|
||||
"current_jti" TEXT NOT NULL,
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"last_used_at" TIMESTAMP(3),
|
||||
"revoked_at" TIMESTAMP(3),
|
||||
|
||||
CONSTRAINT "mcp_oauth_connections_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE INDEX "mcp_oauth_connections_user_id_idx" ON "mcp_oauth_connections"("user_id");
|
||||
|
||||
ALTER TABLE "mcp_oauth_connections" ADD CONSTRAINT "mcp_oauth_connections_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- Link each OAuth-minted access token back to its connection so a connection
|
||||
-- revoke kills every access row it issued, and so the connector token list can
|
||||
-- exclude the transient access rows (they would otherwise flood it — M2).
|
||||
ALTER TABLE "api_tokens" ADD COLUMN "mcp_connection_id" TEXT;
|
||||
|
||||
CREATE INDEX "api_tokens_mcp_connection_id_idx" ON "api_tokens"("mcp_connection_id");
|
||||
|
||||
ALTER TABLE "api_tokens" ADD CONSTRAINT "api_tokens_mcp_connection_id_fkey" FOREIGN KEY ("mcp_connection_id") REFERENCES "mcp_oauth_connections"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- MCP write provenance.
|
||||
--
|
||||
-- The remote MCP endpoint gains a confirmed write surface (logging a
|
||||
-- measurement / mood). Rows it writes carry their own `MeasurementSource`
|
||||
-- value so they are distinguishable from MANUAL / TELEGRAM provenance and
|
||||
-- get an isolated idempotency namespace `(userId, type, MCP, externalId)`.
|
||||
--
|
||||
-- Purely-additive enum extension; no row touched. `ADD VALUE IF NOT EXISTS`
|
||||
-- makes a rerun safe. The new value is NOT used elsewhere in this migration,
|
||||
-- so it is safe to extend the enum in the same step (Postgres only forbids
|
||||
-- USING a freshly-added value in the same transaction). Mirrors the 0189
|
||||
-- source-extension that added TELEGRAM. The enum is `@@map`-ed to the
|
||||
-- snake_case type name `measurement_source`.
|
||||
ALTER TYPE "measurement_source" ADD VALUE IF NOT EXISTS 'MCP';
|
||||
+52
-1
@@ -584,6 +584,9 @@ model User {
|
||||
medications Medication[]
|
||||
intakeEvents MedicationIntakeEvent[]
|
||||
apiTokens ApiToken[]
|
||||
// v1.24 — MCP remote OAuth 2.1 connections (1:N). Each row is a granted
|
||||
// authorization for an MCP client, audience-bound to health:read/health:write.
|
||||
mcpOAuthConnections McpOAuthConnection[]
|
||||
refreshTokens RefreshToken[]
|
||||
auditLogs AuditLog[]
|
||||
withingsConnection WithingsConnection?
|
||||
@@ -1209,6 +1212,15 @@ enum MeasurementSource {
|
||||
// (server-resolved only); iOS DTO mirrors this spelling so the
|
||||
// /api/sync/changes delta feed decodes the rows.
|
||||
TELEGRAM
|
||||
// v1.22.0 — measurements logged through the remote MCP write surface. A
|
||||
// distinct provenance from MANUAL/TELEGRAM: the row was confirmed and
|
||||
// written by an external assistant over the `/mcp` wire under a
|
||||
// `health:write`-scoped token, never the cookie/Bearer client write path.
|
||||
// Its own value gives the MCP writes an isolated idempotency namespace
|
||||
// `(userId, type, MCP, externalId)`. The client batch + single-POST
|
||||
// surfaces still reject it (server-resolved only); iOS DTO mirrors this
|
||||
// spelling so the /api/sync/changes delta feed decodes the rows.
|
||||
MCP
|
||||
|
||||
@@map("measurement_source")
|
||||
}
|
||||
@@ -2550,13 +2562,52 @@ model ApiToken {
|
||||
expiresAt DateTime? @map("expires_at")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
revoked Boolean @default(false)
|
||||
// The MCP OAuth connection this access token belongs to, when it was minted
|
||||
// through the `/api/mcp/oauth` bridge. Null for manually-minted tokens and
|
||||
// native-client tokens. Lets a connection revoke kill every access row it
|
||||
// ever issued (H2), and lets the connector token list exclude the transient
|
||||
// 60-minute access rows so it does not flood (M2).
|
||||
mcpConnectionId String? @map("mcp_connection_id")
|
||||
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
mcpConnection McpOAuthConnection? @relation(fields: [mcpConnectionId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@index([userId])
|
||||
@@index([mcpConnectionId])
|
||||
@@map("api_tokens")
|
||||
}
|
||||
|
||||
// MCP OAuth connection — the revocable, theft-detectable anchor for a remote
|
||||
// connector's refresh-token chain (Phase 3 security fix, H2).
|
||||
//
|
||||
// The stateless `hlrt_` refresh artifact carries this row's `id` (`cid`) plus
|
||||
// the rotation `jti`; the row is the persistent server state the artifact alone
|
||||
// lacks. `currentJti` is the only refresh `jti` valid for the connection at any
|
||||
// moment — every rotation advances it. A presented refresh whose `jti` is not
|
||||
// `currentJti` is a replay of an already-rotated token (theft signal) and
|
||||
// revokes the connection (reuse-detection family revocation, mirroring
|
||||
// `refresh-token.ts`). `revokedAt` is the kill switch the settings card flips,
|
||||
// so revoking a connection stops every future refresh and (via the
|
||||
// `ApiToken.mcpConnectionId` back-link) kills its outstanding access tokens.
|
||||
model McpOAuthConnection {
|
||||
id String @id @default(cuid())
|
||||
userId String @map("user_id")
|
||||
clientId String @map("client_id")
|
||||
clientName String @map("client_name")
|
||||
scope String
|
||||
resource String
|
||||
currentJti String @map("current_jti")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
lastUsedAt DateTime? @map("last_used_at")
|
||||
revokedAt DateTime? @map("revoked_at")
|
||||
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
accessTokens ApiToken[]
|
||||
|
||||
@@index([userId])
|
||||
@@map("mcp_oauth_connections")
|
||||
}
|
||||
|
||||
// v1.15.20 — registration invite tokens. When the operator has disabled
|
||||
// open registration (`AppSettings.registrationEnabled = false`), a valid,
|
||||
// unexpired invite token still admits a signup. The raw `hlv_<hex>` token
|
||||
|
||||
Reference in new issue
Block a user