import { Cell, computed, type Confidential, handler, lift, NAME, pattern, Stream, type TrustedActionWrite, UI, Writable, } from "commonfabric"; const TRUSTED_HEALTH_DISCLOSURE_SURFACE = "TrustedHealthDisclosureSurface"; const TRUSTED_REVEAL_HEALTH_DATA_ACTION = "TrustedRevealHealthData"; const TRUSTED_CONCEAL_HEALTH_DATA_ACTION = "TrustedConcealHealthData"; const HEALTH_RECORD_CONFIDENTIALITY = { type: "https://commonfabric.org/cfc/atom/Resource", class: "SensitiveHealthRecord", subject: "did:example:patient", } as const; type TrustedHealthDisclosureInput = { content: Writable< Confidential >; revealSensitive: Writable; }; type DirectHealthRenderInput = { content: Writable< Confidential >; }; type LabelledContentArgument = { id: string; content: string; }; export type TrustedHealthDisclosureOutput = { [NAME]: string; [UI]: unknown; revealSensitive: TrustedActionWrite< boolean, typeof setRevealSensitive, typeof TRUSTED_REVEAL_HEALTH_DATA_ACTION, typeof TRUSTED_HEALTH_DISCLOSURE_SURFACE >; reveal: Stream; conceal: Stream; }; export type RenderPolicyDemoOutput = { [NAME]: string; [UI]: unknown; revealSensitive: TrustedActionWrite< boolean, typeof setRevealSensitive, typeof TRUSTED_REVEAL_HEALTH_DATA_ACTION, typeof TRUSTED_HEALTH_DISCLOSURE_SURFACE >; reveal: Stream; conceal: Stream; }; export const setRevealSensitive = handler< unknown, { revealSensitive: Writable; next: boolean } >((_, { revealSensitive, next }) => { revealSensitive.set(next); }); const makeConfidentialHealthText = lift< LabelledContentArgument, Writable< Confidential > >((input) => Cell.for< Confidential >(input.id).set( input.content as Confidential< string, readonly [typeof HEALTH_RECORD_CONFIDENTIALITY] >, ) ); export const UntrustedDirectHealthRender = pattern< DirectHealthRenderInput, { [NAME]: string; [UI]: unknown } >(({ content }) => ({ [NAME]: "Untrusted direct health render", [UI]: ( Untrusted direct render attempt This section intentionally has no trusted declassification surface, so the content below should stay hidden.
{content}
), })); export const TrustedHealthDisclosureSurface = pattern< TrustedHealthDisclosureInput, TrustedHealthDisclosureOutput >(({ content, revealSensitive }) => { const reveal = setRevealSensitive({ revealSensitive, next: true }); const conceal = setRevealSensitive({ revealSensitive, next: false }); const buttonLabel = computed(() => revealSensitive.get() ? "Hide sensitive health data" : "Show sensitive health data" ); const revealState = computed(() => revealSensitive.get() ? "Reveal enabled" : "Reveal disabled" ); const trustedContentStyle = computed(() => ({ display: revealSensitive.get() ? "block" : "none", })); const trustedPlaceholderStyle = computed(() => ({ display: revealSensitive.get() ? "none" : "block", })); return { [NAME]: "Trusted health disclosure surface", [UI]: ( Trusted shoulder-surfing control This trusted surface explains that confidential health data is about to be rendered. The render boundary only declassifies the matching label after this control is switched on. {buttonLabel} Reset to private {revealState} Trusted render region
Content hidden by policy
{content}
Label attached to the protected value
), revealSensitive, reveal, conceal, }; }); export default pattern(() => { const healthContent = makeConfidentialHealthText({ id: "cfc-render-policy-demo-health-content", content: "Sensitive health data: migraine treatment plan includes medication review.", }); const healthContentRender: Writable< Confidential > = healthContent as never; const revealSensitive = new Writable(false); const trustedDisclosure = TrustedHealthDisclosureSurface({ content: healthContentRender, revealSensitive, }); const rawAttempt = UntrustedDirectHealthRender({ content: healthContentRender, }); return { [NAME]: "CFC render policy demo", [UI]: ( Render-time confidentiality The page below tries to render the same confidential health cell twice. The raw host attempt stays blocked by the default-low render boundary; the trusted surface can reveal it only inside its own disclosure region. {rawAttempt} {trustedDisclosure} ), revealSensitive: trustedDisclosure.revealSensitive, reveal: trustedDisclosure.reveal, conceal: trustedDisclosure.conceal, }; });