mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
feat(insights): seal the cached AI insights payload at rest
users.insights_cached_text held the model's briefing, readings and recommendations about the account in the clear. Migration 0411 adds insights_cached_text_encrypted (AES-256-GCM, binary layout, purpose-bound label) and a partial discovery index; the free-text encryption backfill seals the existing rows one at a time, fail-closed, carrying updatedAt and guarded on the value it read. Every reader, writer and clear goes through cached-text-cipher.ts, and a guard keeps the column names there. The column joins the encrypted-column registry and the key-rotation script, stays out of backups as operational, and is reset by the data wipe.
This commit is contained in:
1 parent
eae2138f3e
commit
00dfc5a0c0
37 files changed
+709
-64
No files matched your search
@@ -8,6 +8,7 @@ import { prisma } from "@/lib/db";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { getClientIp, apiSuccess, apiError } from "@/lib/api-response";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { INSIGHTS_CACHE_CLEARED } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
/**
|
||||
* v1.4.7.1: Device-code flow polling endpoint.
|
||||
@@ -81,7 +82,7 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
codexConnectedAt: new Date(),
|
||||
codexConnectionStatus: "connected",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
...INSIGHTS_CACHE_CLEARED,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import { prisma } from "@/lib/db";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { apiSuccess, getClientIp } from "@/lib/api-response";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { INSIGHTS_CACHE_CLEARED } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
export const DELETE = apiHandler(async (request: NextRequest) => {
|
||||
const { user } = await requireAuth();
|
||||
@@ -21,7 +22,7 @@ export const DELETE = apiHandler(async (request: NextRequest) => {
|
||||
codexConnectedAt: null,
|
||||
codexConnectionStatus: "disconnected",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
...INSIGHTS_CACHE_CLEARED,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -227,6 +227,7 @@ describe("about-me module gate", () => {
|
||||
data: {
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -69,6 +69,7 @@ import {
|
||||
aboutMeAiSections,
|
||||
type StoredHealthProfileAiSection,
|
||||
} from "@/lib/validations/health-profile-facts";
|
||||
import { INSIGHTS_CACHE_CLEARED } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
const PUT_RATE_LIMIT = 30;
|
||||
const PUT_WINDOW_MS = 60_000;
|
||||
@@ -319,7 +320,7 @@ export const PUT = apiHandler(async (req: Request) => {
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
...INSIGHTS_CACHE_CLEARED,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -175,6 +175,10 @@ import {
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { buildUserPrompt } from "@/lib/ai/prompts/insight-system-prompt";
|
||||
import { AiUnavailableError } from "@/lib/ai/capabilities/refusal";
|
||||
import { openInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// The cached payload is sealed at rest, so its writes need a key.
|
||||
vi.stubEnv("ENCRYPTION_KEY", "c".repeat(64));
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
@@ -455,7 +459,7 @@ describe("POST /api/insights/generate — cache write (v1.16.8)", () => {
|
||||
const args = vi.mocked(prisma.user.updateMany).mock.calls[0][0] as {
|
||||
data: Record<string, unknown>;
|
||||
};
|
||||
expect(args.data.insightsCachedText).toEqual(expect.any(String));
|
||||
expect(openInsightsCachedText(args.data)).toEqual(expect.any(String));
|
||||
// SHA-256 hex digest of the compacted feature snapshot.
|
||||
expect(args.data.insightsSnapshotHash).toMatch(/^[0-9a-f]{64}$/);
|
||||
// The cache row records the language it was written in, so a reader
|
||||
|
||||
@@ -110,6 +110,11 @@ import {
|
||||
enqueueStatusRefillForUser,
|
||||
} from "@/lib/insights/comprehensive-generate";
|
||||
import { cachedPayloadCarriesBriefing } from "@/lib/insights/briefing-payload";
|
||||
import {
|
||||
INSIGHTS_CACHE_SELECT,
|
||||
insightsCacheWrite,
|
||||
openInsightsCachedText,
|
||||
} from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -176,12 +181,13 @@ export const GET = apiHandler(async (request: NextRequest) => {
|
||||
where: { id: userId },
|
||||
select: {
|
||||
insightsCachedAt: true,
|
||||
insightsCachedText: true,
|
||||
...INSIGHTS_CACHE_SELECT,
|
||||
insightsCachedLocale: true,
|
||||
locale: true,
|
||||
timezone: true,
|
||||
},
|
||||
});
|
||||
const cachedText = openInsightsCachedText(dbUser);
|
||||
|
||||
// Resolve the locale the caller is actually reading (cookie /
|
||||
// Accept-Language fall-back when `User.locale` is unset) and validate it
|
||||
@@ -211,7 +217,7 @@ export const GET = apiHandler(async (request: NextRequest) => {
|
||||
// text was generated: `insightsCachedAt` also moves when an unchanged
|
||||
// warm only re-stamps yesterday's text.
|
||||
const generatedToday = generatedOnLocalToday(
|
||||
readBriefingGeneratedAt(dbUser?.insightsCachedText ?? null),
|
||||
readBriefingGeneratedAt(cachedText),
|
||||
timezone,
|
||||
new Date(),
|
||||
);
|
||||
@@ -267,9 +273,9 @@ export const GET = apiHandler(async (request: NextRequest) => {
|
||||
revalidating = true;
|
||||
}
|
||||
|
||||
if (dbUser?.insightsCachedText && !languageMismatch) {
|
||||
if (cachedText && !languageMismatch) {
|
||||
try {
|
||||
const cached = JSON.parse(dbUser.insightsCachedText);
|
||||
const cached = JSON.parse(cachedText);
|
||||
const legacyPayload = isLegacyInsightPayload(cached);
|
||||
annotate({
|
||||
action: { name: "insights.generate.read" },
|
||||
@@ -355,7 +361,7 @@ export const POST = apiHandler((request: NextRequest) =>
|
||||
select: {
|
||||
insightsPrivacyMode: true,
|
||||
insightsCachedAt: true,
|
||||
insightsCachedText: true,
|
||||
...INSIGHTS_CACHE_SELECT,
|
||||
insightsCachedLocale: true,
|
||||
timezone: true,
|
||||
// v1.4.36 W3 T3 — per-user opt-out list mirroring the Coach
|
||||
@@ -378,6 +384,7 @@ export const POST = apiHandler((request: NextRequest) =>
|
||||
glucoseUnit: true,
|
||||
},
|
||||
});
|
||||
const cachedText = openInsightsCachedText(dbUser);
|
||||
const modeAtRead = dbUser?.insightsPrivacyMode ?? "aggregated";
|
||||
// v1.34 — the commit guard below (line ~966) matches on these exact
|
||||
// columns instead of the broad `updatedAt` timestamp. Every User write
|
||||
@@ -439,12 +446,12 @@ export const POST = apiHandler((request: NextRequest) =>
|
||||
if (
|
||||
!forceRefresh &&
|
||||
dbUser?.insightsCachedAt &&
|
||||
dbUser.insightsCachedText &&
|
||||
cachedText &&
|
||||
cachedLocaleMatches &&
|
||||
Date.now() - dbUser.insightsCachedAt.getTime() < 24 * 60 * 60 * 1000
|
||||
) {
|
||||
try {
|
||||
const cached = JSON.parse(dbUser.insightsCachedText);
|
||||
const cached = JSON.parse(cachedText);
|
||||
// v1.4.16 B5c: detect legacy payloads (pre-rationale) so the UI
|
||||
// can show a "regenerate for new explainability features" CTA.
|
||||
// We don't auto-regenerate — that would burn rate-limit tokens
|
||||
@@ -454,9 +461,9 @@ export const POST = apiHandler((request: NextRequest) =>
|
||||
// a briefingless cache, it does not short-circuit a provider-backed
|
||||
// account, and degrades to the cached payload on a 429 below.
|
||||
const carriesBriefing =
|
||||
cachedPayloadCarriesBriefing(dbUser.insightsCachedText) &&
|
||||
cachedPayloadCarriesBriefing(cachedText) &&
|
||||
generatedOnLocalToday(
|
||||
readBriefingGeneratedAt(dbUser.insightsCachedText),
|
||||
readBriefingGeneratedAt(cachedText),
|
||||
postTimezone,
|
||||
new Date(),
|
||||
);
|
||||
@@ -1042,12 +1049,9 @@ export const POST = apiHandler((request: NextRequest) =>
|
||||
// state still fires exactly when the user really has no briefing.
|
||||
if (ungrounded.length > 0 && insights && typeof insights === "object") {
|
||||
let fallbackBriefing: unknown = null;
|
||||
if (dbUser?.insightsCachedText) {
|
||||
if (cachedText) {
|
||||
try {
|
||||
const prev = JSON.parse(dbUser.insightsCachedText) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
const prev = JSON.parse(cachedText) as Record<string, unknown>;
|
||||
fallbackBriefing = prev.dailyBriefing ?? null;
|
||||
} catch {
|
||||
// Unparseable previous payload — nothing safe to stand in.
|
||||
@@ -1109,7 +1113,7 @@ export const POST = apiHandler((request: NextRequest) =>
|
||||
},
|
||||
data: {
|
||||
insightsCachedAt: new Date(),
|
||||
insightsCachedText: JSON.stringify(stored),
|
||||
...insightsCacheWrite(JSON.stringify(stored)),
|
||||
insightsCachedLocale: locale,
|
||||
insightsSnapshotHash: hashInsightSnapshot({
|
||||
features: compactFeatures,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { NextRequest } from "next/server";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { isCodexOAuthConfigured } from "@/lib/ai/codex-oauth";
|
||||
import { INSIGHTS_CACHE_CLEARED } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
export const GET = apiHandler(async () => {
|
||||
const { user } = await requireAuth();
|
||||
@@ -76,7 +77,7 @@ export const PUT = apiHandler(async (request: NextRequest) => {
|
||||
}
|
||||
data.insightsPrivacyMode = mode;
|
||||
data.insightsCachedAt = null;
|
||||
data.insightsCachedText = null;
|
||||
Object.assign(data, INSIGHTS_CACHE_CLEARED);
|
||||
data.insightsCachedLocale = null;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user