Files
HealthLog/src/lib/cycle/custom-symptoms.ts
T
Marc-André Bombeck 1a95739561 fix(crypto): log swallowed decrypt on undecryptable read-path fields
The cycle/illness/records note DTOs, the biomarker context, and the custom
mood/cycle labels each degraded an undecryptable value to a blank/generic
fallback with no signal — a key-rotation gap read as 'left blank' and the
catch was silent. Keep the fail-soft read posture (one bad row never 500s
the list) but emit a wide-event on each swallowed decrypt so a systemic
key gap surfaces. The visible per-field 'unreadable' marker in the UI is
the deferred remainder.
2026-07-09 00:21:16 +02:00

68 lines
2.5 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { encrypt, decrypt } from "@/lib/crypto";
import { getEvent } from "@/lib/logging/context";
import { CUSTOM_SYMPTOM_KEY_PREFIX } from "@/lib/cycle/custom-symptoms-shared";
/**
* v1.15.1 — per-user custom cycle-symptom helpers (the v1.13 custom-mood-tag
* precedent, mirrored 1:1).
*
* Custom symptoms live in the same `cycle_symptoms` table as the seeded
* catalogue, distinguished by a non-null `userId`. Their key carries a
* reserved `custom:` prefix so it never collides with a bare catalogue slug,
* and the write path (`day-log-write.ts:resolveSymptomIds`) already resolves a
* key against the catalogue rows (userId NULL) PLUS the caller's own rows —
* so a `custom:` key routes to the user's symptom by construction. The label
* is held AES-256-GCM-encrypted at rest (`labelEncrypted`): a symptom name is
* intent-revealing free text, the highest-sensitivity category in the
* post-Dobbs threat model.
*
* This module carries the SERVER-ONLY helpers (uuid mint + label crypto). The
* client-safe constants + Zod schemas live in `custom-symptoms-shared.ts` and
* are re-exported here so existing server import sites need no change.
*/
export {
CUSTOM_SYMPTOM_KEY_PREFIX,
CUSTOM_SYMPTOM_CATEGORY_KEY,
CUSTOM_SYMPTOM_CATEGORY_ID,
MAX_CUSTOM_SYMPTOMS_PER_USER,
CUSTOM_SYMPTOM_ICON_ALLOWLIST,
isCustomSymptomKey,
createCustomSymptomSchema,
updateCustomSymptomSchema,
} from "@/lib/cycle/custom-symptoms-shared";
/** Mint a fresh, collision-proof custom-symptom key. */
export function mintCustomSymptomKey(): string {
return `${CUSTOM_SYMPTOM_KEY_PREFIX}${randomUUID()}`;
}
/** Encrypt a custom label for storage in `label_encrypted`. */
export function encryptCustomLabel(label: string): string {
return encrypt(label);
}
/**
* Decrypt a stored custom label. Returns null on a missing or corrupt
* ciphertext so one bad row never throws the whole effective-set read.
*/
export function decryptCustomLabel(encoded: string | null): string | null {
if (!encoded) return null;
try {
return decrypt(encoded);
} catch (err) {
// A custom label WAS stored but is undecryptable — fail soft (the caller
// falls back to the generic i18n label) but log it (F-CRYPTO-5) so the loss
// of a user-renamed symptom is not entirely silent.
getEvent()?.addWarning(
`cycle custom-label decrypt failed: ${
err instanceof Error ? err.message : String(err)
}`,
);
return null;
}
}