@certrev/cert-block 1.2.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +230 -0
- package/README.md +135 -8
- package/dist/builder/card-contract.d.ts +143 -0
- package/dist/builder/card-contract.d.ts.map +1 -0
- package/dist/builder/card-contract.js +262 -0
- package/dist/builder/card-contract.js.map +1 -0
- package/dist/builder/card-report.d.ts +37 -0
- package/dist/builder/card-report.d.ts.map +1 -0
- package/dist/builder/card-report.js +43 -0
- package/dist/builder/card-report.js.map +1 -0
- package/dist/builder/cert-review-card.d.ts +7 -5
- package/dist/builder/cert-review-card.d.ts.map +1 -1
- package/dist/builder/cert-review-card.js +227 -18
- package/dist/builder/cert-review-card.js.map +1 -1
- package/dist/builder/contract-card.d.ts +53 -0
- package/dist/builder/contract-card.d.ts.map +1 -0
- package/dist/builder/contract-card.js +141 -0
- package/dist/builder/contract-card.js.map +1 -0
- package/dist/builder/delivered-face.d.ts +83 -0
- package/dist/builder/delivered-face.d.ts.map +1 -0
- package/dist/builder/delivered-face.js +135 -0
- package/dist/builder/delivered-face.js.map +1 -0
- package/dist/builder/delivery-runtime.d.ts +15 -1
- package/dist/builder/delivery-runtime.d.ts.map +1 -1
- package/dist/builder/delivery-runtime.js +26 -1
- package/dist/builder/delivery-runtime.js.map +1 -1
- package/dist/builder/index.d.ts +7 -1
- package/dist/builder/index.d.ts.map +1 -1
- package/dist/builder/index.js +8 -1
- package/dist/builder/index.js.map +1 -1
- package/dist/builder/ssr-seed.d.ts +31 -1
- package/dist/builder/ssr-seed.d.ts.map +1 -1
- package/dist/builder/ssr-seed.js +38 -4
- package/dist/builder/ssr-seed.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/reader/reader-css.generated.d.ts +1 -1
- package/dist/reader/reader-css.generated.d.ts.map +1 -1
- package/dist/reader/reader-css.generated.js +1 -1
- package/dist/reader/reader-css.generated.js.map +1 -1
- package/dist/reader/render.d.ts.map +1 -1
- package/dist/reader/render.js +71 -34
- package/dist/reader/render.js.map +1 -1
- package/dist/reader/tokens.d.ts +2 -0
- package/dist/reader/tokens.d.ts.map +1 -1
- package/dist/reader/tokens.js +28 -6
- package/dist/reader/tokens.js.map +1 -1
- package/dist/verify/get-verified-delivery.d.ts +56 -0
- package/dist/verify/get-verified-delivery.d.ts.map +1 -1
- package/dist/verify/get-verified-delivery.js +49 -4
- package/dist/verify/get-verified-delivery.js.map +1 -1
- package/dist/verify/key-set.d.ts +67 -0
- package/dist/verify/key-set.d.ts.map +1 -0
- package/dist/verify/key-set.js +190 -0
- package/dist/verify/key-set.js.map +1 -0
- package/package.json +115 -112
- package/src/builder/card-contract.ts +314 -0
- package/src/builder/card-report.ts +61 -0
- package/src/builder/cert-review-card.tsx +332 -26
- package/src/builder/contract-card.tsx +159 -0
- package/src/builder/delivered-face.ts +156 -0
- package/src/builder/delivery-runtime.ts +32 -1
- package/src/builder/index.tsx +16 -1
- package/src/builder/ssr-seed.ts +56 -4
- package/src/index.ts +12 -0
- package/src/reader/reader-css.generated.ts +1 -1
- package/src/reader/reader.css +297 -2
- package/src/reader/render.ts +87 -38
- package/src/reader/tokens.ts +29 -6
- package/src/verify/get-verified-delivery.ts +85 -4
- package/src/verify/key-set.ts +225 -0
|
@@ -25,16 +25,43 @@ import { type CertBlockMode, renderCertBlock } from '../components/render-cert-b
|
|
|
25
25
|
import { CERT_STRINGS_VERSION, type ResolvedBlockTheme } from '../components/render-def.js'
|
|
26
26
|
import { type CertPayload, renderVerdict } from '../contract/kernel.js'
|
|
27
27
|
import {
|
|
28
|
+
type DeliveredFace,
|
|
28
29
|
type DesignPin,
|
|
29
30
|
getVerifiedDelivery,
|
|
30
31
|
parseDesignPin,
|
|
31
32
|
readVerifiedDelivery,
|
|
32
33
|
type VerifiedDelivery,
|
|
33
34
|
} from '../verify/get-verified-delivery.js'
|
|
35
|
+
import {
|
|
36
|
+
acceptedCardBinding,
|
|
37
|
+
type CardBinding,
|
|
38
|
+
type CardDocumentV1,
|
|
39
|
+
type CardPartName,
|
|
40
|
+
cardDigestFor,
|
|
41
|
+
cardDigestMatches,
|
|
42
|
+
cardMatchesPlacement,
|
|
43
|
+
cardPartsFor,
|
|
44
|
+
parseCardDocument,
|
|
45
|
+
} from './card-contract.js'
|
|
34
46
|
import { CARD_READER_CSS } from './card-reader-css.js'
|
|
47
|
+
import {
|
|
48
|
+
type CardFallbackDetail,
|
|
49
|
+
type CardFallbackReason,
|
|
50
|
+
reportCardFallbackInBrowser,
|
|
51
|
+
reportCardFallbackOnServer,
|
|
52
|
+
} from './card-report.js'
|
|
35
53
|
import type { BuilderCertChromeData, CertChromeRenderDef } from './cert-chrome-data.js'
|
|
36
54
|
import { BUILDER_CERT_CHROME_KEYS } from './cert-chrome-data.js'
|
|
37
|
-
import {
|
|
55
|
+
import { ContractCardFace } from './contract-card.js'
|
|
56
|
+
import {
|
|
57
|
+
type DeliveredFaceHashes,
|
|
58
|
+
deliveredFaceDigest,
|
|
59
|
+
deliveredFaceHtml,
|
|
60
|
+
deliveredFaceMatches,
|
|
61
|
+
faceHashesFor,
|
|
62
|
+
showsWholeCard,
|
|
63
|
+
} from './delivered-face.js'
|
|
64
|
+
import { cardDeliveryRuntime, cardResolveKidFor } from './delivery-runtime.js'
|
|
38
65
|
import { envelopeCardHtml, envelopeCardInput, isReaderFaceDef } from './envelope-face.js'
|
|
39
66
|
import {
|
|
40
67
|
type FallbackFace,
|
|
@@ -91,11 +118,13 @@ export interface CertReviewCardProps extends Partial<BuilderCertChromeData> {
|
|
|
91
118
|
readonly context?: { readonly certrevDesignPin?: unknown } | null
|
|
92
119
|
} | null
|
|
93
120
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* a
|
|
121
|
+
* A Delivery read FROM THE BROWSER for a card the server did not render (a client-side navigation, Builder's editor).
|
|
122
|
+
* 2.0.0: ON by default (absent or `true`): it is the only way CertREV's server-rendered card exists after an in-app
|
|
123
|
+
* navigation, and it needs the Delivery API's origin (`https://portal.certrev.com`) in the page's `connect-src`, one
|
|
124
|
+
* of the four CSP sources 2.0 asks for. A read the policy blocks is a read that did not reach CertREV: the block's
|
|
125
|
+
* own options face, as 1.3.1 painted it (and a reported `delivery_unreachable`). `false` restores 1.2.0 to 1.3.1's
|
|
126
|
+
* behaviour: no request from the browser, and such a card paints the block's options face. Not a Builder input: a
|
|
127
|
+
* brand sets it in code, on a wrapper of this component.
|
|
99
128
|
*/
|
|
100
129
|
clientRead?: boolean
|
|
101
130
|
}
|
|
@@ -172,11 +201,25 @@ function suspendOn(key: string, start: () => Promise<VerifiedDelivery>): Verifie
|
|
|
172
201
|
pending = start().finally(() => inflightReads.delete(key))
|
|
173
202
|
inflightReads.set(key, pending)
|
|
174
203
|
}
|
|
175
|
-
|
|
204
|
+
return suspendUntil(pending)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** `use` where React has it (19), else the thrown promise (18). */
|
|
208
|
+
function suspendUntil<T>(pending: Promise<T>): T {
|
|
209
|
+
const use = (React as unknown as { use?: <U>(p: Promise<U>) => U }).use
|
|
176
210
|
if (typeof use === 'function') return use(pending)
|
|
177
211
|
throw pending
|
|
178
212
|
}
|
|
179
213
|
|
|
214
|
+
/**
|
|
215
|
+
* 1.3.0: a delivered face's two digests, suspending once while WebCrypto computes them (`faceHashesFor` memoizes them
|
|
216
|
+
* by the face's content, so every later render of the same face, server or browser, reads them at once).
|
|
217
|
+
*/
|
|
218
|
+
function suspendOnFaceHashes(face: DeliveredFace): DeliveredFaceHashes | null {
|
|
219
|
+
const memo = faceHashesFor(face)
|
|
220
|
+
return memo.settled ? memo.settled.hashes : suspendUntil(memo.pending)
|
|
221
|
+
}
|
|
222
|
+
|
|
180
223
|
/**
|
|
181
224
|
* 1.2.0-canary.6: the reader sidebar card is a right float that the article's copy flows around, as it does on a
|
|
182
225
|
* WordPress or Shopify page. On a Builder page the card sits in its own block, a flex item of the flex column that holds
|
|
@@ -225,6 +268,8 @@ function EnvelopeFace(props: {
|
|
|
225
268
|
readonly html: string
|
|
226
269
|
readonly defVersion: number | undefined
|
|
227
270
|
readonly reader?: boolean
|
|
271
|
+
/** 1.3.0: the server-rendered face the signed digest binds (`DeliveredEnvelopeFace`); never with `reader`. */
|
|
272
|
+
readonly delivered?: boolean
|
|
228
273
|
/** The envelope seed (`ssr-seed.ts`): what a browser hydrating this face paints from, with no read of its own. */
|
|
229
274
|
readonly seed: string
|
|
230
275
|
/** 1.2.0, reader faces: told which person's photo the browser could not load (`ReaderEnvelopeFace`). */
|
|
@@ -270,8 +315,12 @@ function EnvelopeFace(props: {
|
|
|
270
315
|
data-certrev-strings-version={CERT_STRINGS_VERSION}
|
|
271
316
|
data-certrev-def-version={props.defVersion}
|
|
272
317
|
{...{ [SEED_ATTRIBUTE]: props.seed }}
|
|
273
|
-
{...(props.reader
|
|
274
|
-
|
|
318
|
+
{...(props.reader
|
|
319
|
+
? { 'data-certrev-face': 'reader', style: { display: 'flow-root' } }
|
|
320
|
+
: props.delivered
|
|
321
|
+
? { 'data-certrev-face': 'delivered' }
|
|
322
|
+
: {})}
|
|
323
|
+
// biome-ignore lint/security/noDangerouslySetInnerHtml: renderCertBlock is the escaping-safe SSR string renderer; a delivered face paints only when its bytes hash to the signed faceDigest
|
|
275
324
|
dangerouslySetInnerHTML={{ __html: props.html }}
|
|
276
325
|
/>,
|
|
277
326
|
)
|
|
@@ -279,9 +328,13 @@ function EnvelopeFace(props: {
|
|
|
279
328
|
|
|
280
329
|
type PhotoPerson = 'expert' | 'author'
|
|
281
330
|
|
|
282
|
-
/**
|
|
331
|
+
/**
|
|
332
|
+
* Whose avatar a broken photo is. The banner's two bylines take the same face (1.3.1; the author's was the small
|
|
333
|
+
* `.certrev-r-face.sm` before), so the person is read from the byline: the author's carries the plain "Written by"
|
|
334
|
+
* role, the reviewer's "Reviewed by" (`.on`), and the sidebar's and the memo's reviewer has no role at all.
|
|
335
|
+
*/
|
|
283
336
|
function photoPerson(img: HTMLImageElement): PhotoPerson {
|
|
284
|
-
return img.
|
|
337
|
+
return img.closest('.certrev-r-by')?.querySelector('.certrev-r-role:not(.on)') ? 'author' : 'expert'
|
|
285
338
|
}
|
|
286
339
|
|
|
287
340
|
interface EnvelopeCardProps {
|
|
@@ -340,6 +393,51 @@ function ReaderEnvelopeFace(props: {
|
|
|
340
393
|
)
|
|
341
394
|
}
|
|
342
395
|
|
|
396
|
+
/**
|
|
397
|
+
* 1.3.0: the delivered face this paint may use, or undefined (paint what 1.2.1 paints). Only for an unpinned card on a
|
|
398
|
+
* block that shows the whole card, a digest the verified content signs for the def's version, and a face whose UTF-8
|
|
399
|
+
* bytes hash to it (`delivered-face.ts`). Suspends once while WebCrypto hashes a face it has not seen.
|
|
400
|
+
*/
|
|
401
|
+
function deliveredFaceFor(
|
|
402
|
+
payload: CertPayload,
|
|
403
|
+
renderDef: CertChromeRenderDef | null,
|
|
404
|
+
face: DeliveredFace | undefined,
|
|
405
|
+
props: EnvelopeCardProps,
|
|
406
|
+
/** 2.0.0: the hashes the read computed (`VerifiedDelivery.faceHashes`): then nothing suspends here. */
|
|
407
|
+
hashed?: DeliveredFaceHashes | null,
|
|
408
|
+
): DeliveredFace | undefined {
|
|
409
|
+
if (!face || props.design || !showsWholeCard(props.part)) return undefined
|
|
410
|
+
const digest = deliveredFaceDigest(payload.content, renderDef, face)
|
|
411
|
+
if (!digest) return undefined
|
|
412
|
+
const hashes = hashed !== undefined ? hashed : suspendOnFaceHashes(face)
|
|
413
|
+
return deliveredFaceMatches(hashes, digest) ? face : undefined
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* 1.3.0: the delivered face, in the card's own wrapper (the same attributes as any envelope face, and
|
|
418
|
+
* `data-certrev-face="delivered"`): the server's stylesheet, then its markup. `seed` is the record that names it.
|
|
419
|
+
*/
|
|
420
|
+
function DeliveredEnvelopeFace(props: {
|
|
421
|
+
readonly face: DeliveredFace
|
|
422
|
+
readonly defVersion: number
|
|
423
|
+
readonly seed: string
|
|
424
|
+
}) {
|
|
425
|
+
return <EnvelopeFace delivered html={deliveredFaceHtml(props.face)} defVersion={props.defVersion} seed={props.seed} />
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 1.3.0: what a card hydrating a seed that names a delivered face renders when the browser's own check of that face
|
|
430
|
+
* fails (no WebCrypto: an insecure context; or bytes that do not hash to the seed's digest). The server painted the
|
|
431
|
+
* delivered face there, and the browser must not render that markup, so this adopts the server's element as it stands
|
|
432
|
+
* (no attribute or innerHTML compared: `suppressHydrationWarning`; no children hydrated: `dangerouslySetInnerHTML`)
|
|
433
|
+
* and paints nothing of its own. The render right after hydration (at the browser's clock) is another component, so
|
|
434
|
+
* React replaces this element with the face 1.2.1 paints for the seed.
|
|
435
|
+
*/
|
|
436
|
+
function ServerFaceUntilHydrated() {
|
|
437
|
+
// biome-ignore lint/security/noDangerouslySetInnerHtml: an empty string; it only stops React hydrating the server's children
|
|
438
|
+
return <div suppressHydrationWarning dangerouslySetInnerHTML={{ __html: '' }} />
|
|
439
|
+
}
|
|
440
|
+
|
|
343
441
|
/** Paint a verified payload with its def; null when the def and content give no face. */
|
|
344
442
|
function paintEnvelope(
|
|
345
443
|
payload: CertPayload,
|
|
@@ -404,9 +502,110 @@ function NoFace(props: { readonly record: string }) {
|
|
|
404
502
|
return <div hidden {...{ [SEED_ATTRIBUTE]: props.record }} />
|
|
405
503
|
}
|
|
406
504
|
|
|
407
|
-
/**
|
|
408
|
-
|
|
409
|
-
|
|
505
|
+
/**
|
|
506
|
+
* 2.0.0: reports one fallback (`card-report.ts`) and renders nothing (no element, no marker: it changes no byte of the
|
|
507
|
+
* page). On a server it reports as it renders; in a browser, once it has mounted.
|
|
508
|
+
*/
|
|
509
|
+
function FallbackReport(props: { readonly detail: CardFallbackDetail; readonly server: boolean }) {
|
|
510
|
+
const { detail, server } = props
|
|
511
|
+
if (server) reportCardFallbackOnServer(detail)
|
|
512
|
+
React.useEffect(() => {
|
|
513
|
+
reportCardFallbackInBrowser(detail)
|
|
514
|
+
}, [detail])
|
|
515
|
+
return null
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** A face with its fallback report beside it. */
|
|
519
|
+
function withReport(
|
|
520
|
+
face: React.ReactElement | null,
|
|
521
|
+
reason: CardFallbackReason,
|
|
522
|
+
props: EnvelopeCardProps,
|
|
523
|
+
server: boolean,
|
|
524
|
+
certId?: string,
|
|
525
|
+
): React.ReactElement {
|
|
526
|
+
const detail: CardFallbackDetail = {
|
|
527
|
+
reason,
|
|
528
|
+
platform: 'builder',
|
|
529
|
+
externalId: props.externalId,
|
|
530
|
+
...(certId === undefined ? {} : { certId }),
|
|
531
|
+
}
|
|
532
|
+
return (
|
|
533
|
+
<>
|
|
534
|
+
{face}
|
|
535
|
+
<FallbackReport detail={detail} server={server} />
|
|
536
|
+
</>
|
|
537
|
+
)
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* 2.0.0: what a contract-1 (or unsupported-contract) placement prints, decided by the SIGNED binding (`card-contract.ts`):
|
|
542
|
+
* the card when the served document hashes to the signed digest and names this design version, certificate and
|
|
543
|
+
* placement; else nothing, and the reason. Suspends once while WebCrypto hashes a document it has not seen.
|
|
544
|
+
*/
|
|
545
|
+
type ContractOutcome =
|
|
546
|
+
| { readonly kind: 'card'; readonly card: CardDocumentV1; readonly names: CardPartName[] }
|
|
547
|
+
| { readonly kind: 'none'; readonly reason?: CardFallbackReason }
|
|
548
|
+
|
|
549
|
+
function contractOutcome(
|
|
550
|
+
binding: CardBinding | 'unsupported',
|
|
551
|
+
rawCard: unknown,
|
|
552
|
+
payload: CertPayload,
|
|
553
|
+
props: EnvelopeCardProps,
|
|
554
|
+
/** The digest the read computed (`VerifiedDelivery.cardDigest`): then nothing suspends here. */
|
|
555
|
+
digested?: string | null,
|
|
556
|
+
): ContractOutcome {
|
|
557
|
+
if (binding === 'unsupported') return { kind: 'none', reason: 'card_unsupported_contract' }
|
|
558
|
+
// CertREV's replica pin: the signed card was painted for one design version; another pin gets nothing.
|
|
559
|
+
if (props.design && props.design.version !== binding.defVersion) return { kind: 'none', reason: 'card_pin_mismatch' }
|
|
560
|
+
if (rawCard === undefined || rawCard === null) return { kind: 'none', reason: 'card_missing' }
|
|
561
|
+
const card = parseCardDocument(rawCard)
|
|
562
|
+
if (!card || !cardMatchesPlacement(card, binding, payload, props.externalId)) {
|
|
563
|
+
return { kind: 'none', reason: 'card_verify_failed' }
|
|
564
|
+
}
|
|
565
|
+
let digest: string | null
|
|
566
|
+
if (digested !== undefined) {
|
|
567
|
+
digest = digested
|
|
568
|
+
} else {
|
|
569
|
+
// A page's seed: the one suspension of this render (a render that read waits on nothing but its read).
|
|
570
|
+
const memo = cardDigestFor(card)
|
|
571
|
+
digest = memo.settled ? memo.settled.digest : suspendUntil(memo.pending)
|
|
572
|
+
}
|
|
573
|
+
if (!cardDigestMatches(digest, binding)) return { kind: 'none', reason: 'card_verify_failed' }
|
|
574
|
+
// A block with nothing of this design to print (a memo block of a memo-less card, or of a sidebar design) prints
|
|
575
|
+
// nothing, by design: not a fallback.
|
|
576
|
+
const names = cardPartsFor(card, props.part)
|
|
577
|
+
return names.length === 0 ? { kind: 'none' } : { kind: 'card', card, names }
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** The `'none'` record of a contract-1 placement, with its reason when it is a fallback. */
|
|
581
|
+
function contractNone(
|
|
582
|
+
props: EnvelopeCardProps,
|
|
583
|
+
reason: CardFallbackReason | undefined,
|
|
584
|
+
server: boolean,
|
|
585
|
+
certId: string,
|
|
586
|
+
): React.ReactElement {
|
|
587
|
+
const record = serializeFallback({
|
|
588
|
+
baseUrl: props.baseUrl,
|
|
589
|
+
externalId: props.externalId,
|
|
590
|
+
...(props.design ? { design: props.design } : {}),
|
|
591
|
+
face: 'none',
|
|
592
|
+
...(reason ? { reason } : {}),
|
|
593
|
+
})
|
|
594
|
+
const face = <NoFace record={record} />
|
|
595
|
+
return reason ? withReport(face, reason, props, server, certId) : face
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* What a card with no verified envelope paints: the options face when CertREV was not reached, else nothing. 2.0.0:
|
|
600
|
+
* the same faces and records as 1.3.1, and two of them are now reported: the options face painted because CertREV
|
|
601
|
+
* was not reached (`delivery_unreachable`), and nothing painted because the envelope's kid is unknown (`unknown_key`).
|
|
602
|
+
*/
|
|
603
|
+
function settleWithoutEnvelope(
|
|
604
|
+
props: EnvelopeCardProps,
|
|
605
|
+
verdict: { readonly unreachable: boolean; readonly reason?: string },
|
|
606
|
+
server: boolean,
|
|
607
|
+
): React.ReactElement {
|
|
608
|
+
const html = verdict.unreachable ? fallbackFace(props) : null
|
|
410
609
|
const face: FallbackFace = html === null ? 'none' : 'options'
|
|
411
610
|
const record = serializeFallback({
|
|
412
611
|
baseUrl: props.baseUrl,
|
|
@@ -414,7 +613,18 @@ function settleWithoutEnvelope(props: EnvelopeCardProps, unreachable: boolean):
|
|
|
414
613
|
...(props.design ? { design: props.design } : {}),
|
|
415
614
|
face,
|
|
416
615
|
})
|
|
417
|
-
|
|
616
|
+
if (html !== null) {
|
|
617
|
+
return withReport(
|
|
618
|
+
<OptionsFace block={props.block} html={html} record={record} />,
|
|
619
|
+
'delivery_unreachable',
|
|
620
|
+
props,
|
|
621
|
+
server,
|
|
622
|
+
)
|
|
623
|
+
}
|
|
624
|
+
const none = <NoFace record={record} />
|
|
625
|
+
// A block with no options to paint when CertREV was not reached is reported too (break-it B5).
|
|
626
|
+
if (verdict.unreachable) return withReport(none, 'delivery_unreachable', props, server)
|
|
627
|
+
return verdict.reason === 'unknown_key' ? withReport(none, 'unknown_key', props, server) : none
|
|
418
628
|
}
|
|
419
629
|
|
|
420
630
|
/**
|
|
@@ -435,14 +645,58 @@ function paintRecord(
|
|
|
435
645
|
return null
|
|
436
646
|
}
|
|
437
647
|
}
|
|
438
|
-
|
|
648
|
+
const { seed } = record
|
|
649
|
+
// 2.0.0: a seed whose signed binding names a contract prints the card it carries, only after the browser has
|
|
650
|
+
// checked it against that binding itself; otherwise nothing. Never a 1.3.1 face for such a placement.
|
|
651
|
+
const binding = acceptedCardBinding(seed.payload.content)
|
|
652
|
+
if (binding !== null) {
|
|
653
|
+
const outcome = contractOutcome(binding, seed.card, seed.payload, props)
|
|
654
|
+
if (outcome.kind === 'card')
|
|
655
|
+
return <ContractCardFace card={outcome.card} names={outcome.names} seed={record.raw} />
|
|
656
|
+
// Hydrating over the card the server printed, which the browser could not confirm (no WebCrypto): adopt the
|
|
657
|
+
// server's element; the render right after hydration prints nothing in its place.
|
|
658
|
+
if (!judge && outcome.reason === 'card_verify_failed' && seed.card !== undefined)
|
|
659
|
+
return <ServerFaceUntilHydrated />
|
|
660
|
+
// Nothing of this design for this block (a memo block of a sidebar card): nothing, and no record of its own.
|
|
661
|
+
if (outcome.reason === undefined) return null
|
|
662
|
+
return contractNone(props, outcome.reason, false, seed.payload.certId)
|
|
663
|
+
}
|
|
664
|
+
// 1.3.0: a seed that names a delivered face repaints it only after the browser hashes it against the seed
|
|
665
|
+
// payload's own digest; otherwise the seed paints what 1.2.1 paints for it.
|
|
666
|
+
const delivered = deliveredFaceFor(seed.payload, seed.renderDef, seed.face, props)
|
|
667
|
+
if (delivered && seed.renderDef) {
|
|
668
|
+
return <DeliveredEnvelopeFace face={delivered} defVersion={seed.renderDef.v} seed={record.raw} />
|
|
669
|
+
}
|
|
670
|
+
// Hydrating (not `judge`) over the delivered face the server painted, which the browser could not confirm.
|
|
671
|
+
if (seed.face && !judge) return <ServerFaceUntilHydrated />
|
|
672
|
+
return paintEnvelope(seed.payload, seed.renderDef, props, record.raw)
|
|
439
673
|
}
|
|
440
674
|
const html = record.fallback.face === 'options' ? fallbackFace(props) : null
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
675
|
+
if (html !== null) {
|
|
676
|
+
return withReport(
|
|
677
|
+
<OptionsFace block={props.block} html={html} record={record.raw} />,
|
|
678
|
+
'delivery_unreachable',
|
|
679
|
+
props,
|
|
680
|
+
false,
|
|
681
|
+
)
|
|
682
|
+
}
|
|
683
|
+
const none = <NoFace record={record.raw} />
|
|
684
|
+
// 2.0.0: a contract-1 placement's record names why it printed nothing; the browser reports it too.
|
|
685
|
+
const reason = record.fallback.reason
|
|
686
|
+
return isFallbackReason(reason) ? withReport(none, reason, props, false) : none
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
const FALLBACK_REASONS: readonly string[] = [
|
|
690
|
+
'card_missing',
|
|
691
|
+
'card_verify_failed',
|
|
692
|
+
'card_unsupported_contract',
|
|
693
|
+
'card_pin_mismatch',
|
|
694
|
+
'unknown_key',
|
|
695
|
+
'delivery_unreachable',
|
|
696
|
+
]
|
|
697
|
+
|
|
698
|
+
function isFallbackReason(value: unknown): value is CardFallbackReason {
|
|
699
|
+
return typeof value === 'string' && FALLBACK_REASONS.includes(value)
|
|
446
700
|
}
|
|
447
701
|
|
|
448
702
|
/** What a card the client mounts with no record and no `clientRead` paints: the options face, or nothing. */
|
|
@@ -469,8 +723,9 @@ function StreamedRecordCard(props: EnvelopeCardProps) {
|
|
|
469
723
|
pendingRecord,
|
|
470
724
|
)
|
|
471
725
|
if (record === 'pending') return null
|
|
472
|
-
if (record === 'done') return clientMountFace(props)
|
|
473
726
|
const context = { platform: 'builder', externalId: props.externalId, now: new Date(rt.now()) }
|
|
727
|
+
// 2.0.0: the page loaded with no record for this placement: with `clientRead` (the default), the one read.
|
|
728
|
+
if (record === 'done') return props.clientRead ? readAndPaint(props, context, true) : clientMountFace(props)
|
|
474
729
|
return paintRecord(record, props, context, true)
|
|
475
730
|
}
|
|
476
731
|
|
|
@@ -551,8 +806,9 @@ function EnvelopeCertReviewCard(props: EnvelopeCardProps) {
|
|
|
551
806
|
// 1.2.1, latched at mount: a client mount with no record and no `clientRead` while the page is still loading waits
|
|
552
807
|
// for a record the server may still be streaming, for the card's whole life (so it never swaps components when the
|
|
553
808
|
// record lands).
|
|
809
|
+
// 2.0.0: whatever `clientRead` says: a card that read instead of waiting would miss the record the server streams.
|
|
554
810
|
const [awaitsStream] = React.useState(
|
|
555
|
-
() => inBrowser && !hydrating && fromPage === undefined &&
|
|
811
|
+
() => inBrowser && !hydrating && fromPage === undefined && document.readyState === 'loading',
|
|
556
812
|
)
|
|
557
813
|
if (awaitsStream) return <StreamedRecordCard {...props} />
|
|
558
814
|
if (inBrowser) {
|
|
@@ -560,6 +816,19 @@ function EnvelopeCertReviewCard(props: EnvelopeCardProps) {
|
|
|
560
816
|
if (hydratedMount) return null
|
|
561
817
|
if (!props.clientRead) return clientMountFace(props)
|
|
562
818
|
}
|
|
819
|
+
return readAndPaint(props, context, inBrowser)
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* The Delivery read and its paint: a server render's, or a browser's (`clientRead`). Suspends on the read, and (1.3.0)
|
|
824
|
+
* on a delivered face's hashes or (2.0.0) on a card document's digest.
|
|
825
|
+
*/
|
|
826
|
+
function readAndPaint(
|
|
827
|
+
props: EnvelopeCardProps,
|
|
828
|
+
context: Parameters<typeof renderVerdict>[1],
|
|
829
|
+
inBrowser: boolean,
|
|
830
|
+
): React.ReactElement | null {
|
|
831
|
+
const rt = cardDeliveryRuntime()
|
|
563
832
|
const { design } = props
|
|
564
833
|
const read = {
|
|
565
834
|
baseUrl: props.baseUrl,
|
|
@@ -568,7 +837,7 @@ function EnvelopeCertReviewCard(props: EnvelopeCardProps) {
|
|
|
568
837
|
context,
|
|
569
838
|
cache: rt.cache,
|
|
570
839
|
design,
|
|
571
|
-
resolveKid: rt.
|
|
840
|
+
resolveKid: cardResolveKidFor(rt, props.baseUrl),
|
|
572
841
|
fetchImpl: rt.fetchImpl(),
|
|
573
842
|
timeoutMs: rt.timeoutMs,
|
|
574
843
|
}
|
|
@@ -576,7 +845,13 @@ function EnvelopeCertReviewCard(props: EnvelopeCardProps) {
|
|
|
576
845
|
? `${props.baseUrl}|${props.externalId}|design=${design.version}|${design.brand ?? ''}`
|
|
577
846
|
: `${props.baseUrl}|${props.externalId}`
|
|
578
847
|
const settled = readVerifiedDelivery(read) ?? suspendOn(key, () => getVerifiedDelivery(read))
|
|
579
|
-
if (settled.verdict.decision !== 'render')
|
|
848
|
+
if (settled.verdict.decision !== 'render') {
|
|
849
|
+
return settleWithoutEnvelope(
|
|
850
|
+
props,
|
|
851
|
+
{ unreachable: settled.unreachable === true, reason: settled.verdict.reason },
|
|
852
|
+
!inBrowser,
|
|
853
|
+
)
|
|
854
|
+
}
|
|
580
855
|
// The face is painted from the seed's payload (what the def hides is already out of it), so the server's HTML and
|
|
581
856
|
// the browser's, which paints from the seed, are the same bytes and neither holds a hidden field.
|
|
582
857
|
const seed = seedFor({
|
|
@@ -587,6 +862,37 @@ function EnvelopeCertReviewCard(props: EnvelopeCardProps) {
|
|
|
587
862
|
signature: settled.signature,
|
|
588
863
|
renderDef: settled.renderDef,
|
|
589
864
|
})
|
|
865
|
+
// 2.0.0: a placement whose signed binding names a contract prints CertREV's card, or nothing; never a face of its own.
|
|
866
|
+
const binding = acceptedCardBinding(seed.payload.content)
|
|
867
|
+
if (binding !== null) {
|
|
868
|
+
const outcome = contractOutcome(binding, settled.card, seed.payload, props, settled.cardDigest)
|
|
869
|
+
if (outcome.kind === 'card') {
|
|
870
|
+
return (
|
|
871
|
+
<ContractCardFace
|
|
872
|
+
card={outcome.card}
|
|
873
|
+
names={outcome.names}
|
|
874
|
+
seed={serializeSeed({ ...seed, card: outcome.card as unknown as Readonly<Record<string, unknown>> })}
|
|
875
|
+
/>
|
|
876
|
+
)
|
|
877
|
+
}
|
|
878
|
+
// Nothing of this design for this block, by design: nothing at all, and NO record. A record here would sit under the
|
|
879
|
+
// same placement as the block that printed the card (a split header and memo pair) and, read last on hydration,
|
|
880
|
+
// would take that block's card down (break-it B2).
|
|
881
|
+
if (outcome.reason === undefined) return null
|
|
882
|
+
return contractNone(props, outcome.reason, !inBrowser, seed.payload.certId)
|
|
883
|
+
}
|
|
884
|
+
// 1.3.0: the server-rendered face the Delivery API served beside the envelope, when its bytes hash to the digest the
|
|
885
|
+
// verified payload signs; the seed then carries it, so the browser can repaint (and re-check) the same face.
|
|
886
|
+
const delivered = deliveredFaceFor(seed.payload, seed.renderDef, settled.face, props, settled.faceHashes)
|
|
887
|
+
if (delivered && seed.renderDef) {
|
|
888
|
+
return (
|
|
889
|
+
<DeliveredEnvelopeFace
|
|
890
|
+
face={delivered}
|
|
891
|
+
defVersion={seed.renderDef.v}
|
|
892
|
+
seed={serializeSeed({ ...seed, face: delivered })}
|
|
893
|
+
/>
|
|
894
|
+
)
|
|
895
|
+
}
|
|
590
896
|
return paintEnvelope(seed.payload, seed.renderDef, props, serializeSeed(seed))
|
|
591
897
|
}
|
|
592
898
|
|
|
@@ -613,7 +919,7 @@ export function CertReviewCard(props: CertReviewCardProps) {
|
|
|
613
919
|
part={props.part}
|
|
614
920
|
design={design}
|
|
615
921
|
block={props}
|
|
616
|
-
clientRead={props.clientRead
|
|
922
|
+
clientRead={props.clientRead !== false}
|
|
617
923
|
/>
|
|
618
924
|
</Suspense>
|
|
619
925
|
)
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract-1 card face (2.0.0): one Builder block printing CertREV's server-rendered card verbatim.
|
|
3
|
+
*
|
|
4
|
+
* The block's wrapper holds, as its `dangerouslySetInnerHTML`, exactly `cardHtml(card, names)`: the block's part hosts
|
|
5
|
+
* in page order and, in the block holding the document's first part, the head fragment, both CertREV dialog hosts and
|
|
6
|
+
* the runtime's tag (`card-contract.ts`).
|
|
7
|
+
* Each host is a `<certrev-card>` with a declarative shadow root (`<template shadowrootmode="open">`), which the
|
|
8
|
+
* browser's HTML parser attaches as it reads the server's page: the card is complete with JavaScript off, and no page
|
|
9
|
+
* rule reaches inside it. React never reconciles the hosts (they are not React children), so the hosted runtime may
|
|
10
|
+
* remove a revoked placement's hosts without React noticing.
|
|
11
|
+
*
|
|
12
|
+
* What the parser does not do, the card does once it has mounted (`useCardRoots`):
|
|
13
|
+
* - ATTACH ROOTS the parser never saw. A card React inserts (an in-app navigation, Builder's editor, React's own
|
|
14
|
+
* re-render after a hydration error) gets its HTML through `innerHTML`, which leaves a declarative template inert.
|
|
15
|
+
* The wrapper is re-parsed with `setHTMLUnsafe`, which attaches declarative roots; a browser without it (or without
|
|
16
|
+
* declarative shadow DOM at all) gets each root attached from its template (`attachShadow` with the template's mode
|
|
17
|
+
* and options, then the template's content). A host that already has its root is left alone, so a hydrated card,
|
|
18
|
+
* whose roots the parser attached, is not touched.
|
|
19
|
+
* - LOAD THE RUNTIME a React-inserted `<script>` never runs: once per page, into `document.head`, with its SRI
|
|
20
|
+
* integrity and `crossorigin="anonymous"`, unless the runtime is already running (its global guard) or already
|
|
21
|
+
* injected. Only the block that prints the runtime's tag loads it (`cardCarriesAssets`). The server's HTML carries
|
|
22
|
+
* the same tag in that block's wrapper, which the parser runs; the runtime's init is idempotent, so it starts once.
|
|
23
|
+
* Both run in a layout effect, before the browser paints the inserted card, so a client-mounted card never shows an
|
|
24
|
+
* empty frame.
|
|
25
|
+
*
|
|
26
|
+
* ONE BUILDER RULE, for the sidebar card only. The card's rail is a right float that the article's copy flows around
|
|
27
|
+
* (the Appearance preview's layout, and every other CMS's). On a Builder page the block sits in Builder's own wrapper,
|
|
28
|
+
* a flex item of the flex column that holds the entry's blocks, and a flex item contains its floats: the block spans
|
|
29
|
+
* the column at the rail's height and no copy sits beside it. `BUILDER_CARD_FLOW_CSS` lets the float out of Builder's
|
|
30
|
+
* wrappers, as 1.2.0-canary.6 did for the reader face: in a column that holds a printed rail, above the card's own
|
|
31
|
+
* 820px breakpoint (below it the rail stacks as a block), the column lays its blocks out as blocks and still contains
|
|
32
|
+
* the float, and the rail's block and the card wrapper become plain blocks. It reaches nothing inside the card (the
|
|
33
|
+
* shadow root is out of its reach) and nothing on a page without a rail, or in a browser without `:has()`.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import * as React from 'react'
|
|
37
|
+
import { type CardDocumentV1, type CardPartName, cardCarriesAssets, cardHtml } from './card-contract.js'
|
|
38
|
+
import { SEED_ATTRIBUTE } from './ssr-seed.js'
|
|
39
|
+
|
|
40
|
+
/** The global the hosted runtime sets once it has started (`Symbol.for('certrev.card-runtime')` on `document`). */
|
|
41
|
+
const RUNTIME_GUARD = Symbol.for('certrev.card-runtime')
|
|
42
|
+
/** Marks the runtime tag this package injected into `document.head`. */
|
|
43
|
+
const INJECTED_RUNTIME_ATTRIBUTE = 'data-certrev-card-runtime'
|
|
44
|
+
|
|
45
|
+
/** A layout effect in a browser; nothing on a server (where React would only warn about it). */
|
|
46
|
+
const useBrowserLayoutEffect = typeof document === 'undefined' ? () => {} : React.useLayoutEffect
|
|
47
|
+
|
|
48
|
+
/** Whether this host still holds an unattached declarative root. */
|
|
49
|
+
function pendingTemplate(host: Element): HTMLTemplateElement | null {
|
|
50
|
+
if ((host as HTMLElement).shadowRoot) return null
|
|
51
|
+
for (const child of Array.from(host.children)) {
|
|
52
|
+
if (child instanceof HTMLTemplateElement && child.hasAttribute('shadowrootmode')) return child
|
|
53
|
+
}
|
|
54
|
+
return null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Attach one host's root from its template, as the parser would have: its mode, its options, its content. */
|
|
58
|
+
function attachFromTemplate(host: HTMLElement, template: HTMLTemplateElement): void {
|
|
59
|
+
const mode = template.getAttribute('shadowrootmode') === 'closed' ? 'closed' : 'open'
|
|
60
|
+
const init: ShadowRootInit & { clonable?: boolean; serializable?: boolean } = {
|
|
61
|
+
mode,
|
|
62
|
+
delegatesFocus: template.hasAttribute('shadowrootdelegatesfocus'),
|
|
63
|
+
...(template.hasAttribute('shadowrootclonable') ? { clonable: true } : {}),
|
|
64
|
+
...(template.hasAttribute('shadowrootserializable') ? { serializable: true } : {}),
|
|
65
|
+
}
|
|
66
|
+
const root = host.attachShadow(init)
|
|
67
|
+
root.appendChild(template.content)
|
|
68
|
+
template.remove()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Give every host in `wrapper` its declarative root: a no-op where the parser attached them all; else the wrapper is
|
|
73
|
+
* re-parsed from `html` with `setHTMLUnsafe` where the browser has it, or each pending root is attached from its
|
|
74
|
+
* template. Returns how many hosts were pending (for tests and the replica's evidence).
|
|
75
|
+
*/
|
|
76
|
+
export function attachCardRoots(wrapper: Element, html: string): number {
|
|
77
|
+
const pending = Array.from(wrapper.querySelectorAll('certrev-card')).filter((host) => pendingTemplate(host) !== null)
|
|
78
|
+
if (pending.length === 0) return 0
|
|
79
|
+
const parse = (wrapper as Element & { setHTMLUnsafe?: (html: string) => void }).setHTMLUnsafe
|
|
80
|
+
if (typeof parse === 'function') {
|
|
81
|
+
parse.call(wrapper, html)
|
|
82
|
+
// A browser whose setHTMLUnsafe predates declarative roots leaves templates behind: attach those by hand.
|
|
83
|
+
for (const host of Array.from(wrapper.querySelectorAll('certrev-card'))) {
|
|
84
|
+
const template = pendingTemplate(host)
|
|
85
|
+
if (template) attachFromTemplate(host as HTMLElement, template)
|
|
86
|
+
}
|
|
87
|
+
return pending.length
|
|
88
|
+
}
|
|
89
|
+
for (const host of pending) {
|
|
90
|
+
const template = pendingTemplate(host)
|
|
91
|
+
if (template) attachFromTemplate(host as HTMLElement, template)
|
|
92
|
+
}
|
|
93
|
+
return pending.length
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Load the document's runtime once per page: unless it already runs, or this package already injected it. */
|
|
97
|
+
export function injectCardRuntime(card: CardDocumentV1): void {
|
|
98
|
+
const runtime = card.assets.runtime
|
|
99
|
+
if (!runtime || typeof document === 'undefined' || !document.head) return
|
|
100
|
+
if ((document as unknown as Record<symbol, unknown>)[RUNTIME_GUARD]) return
|
|
101
|
+
for (const script of Array.from(document.head.querySelectorAll(`script[${INJECTED_RUNTIME_ATTRIBUTE}]`))) {
|
|
102
|
+
if (script.getAttribute('src') === runtime.src) return
|
|
103
|
+
}
|
|
104
|
+
const script = document.createElement('script')
|
|
105
|
+
// Attributes, not IDL properties: the markup is then exactly the server's tag, wherever a property is not reflected.
|
|
106
|
+
script.setAttribute('src', runtime.src)
|
|
107
|
+
script.setAttribute('integrity', runtime.integrity)
|
|
108
|
+
script.setAttribute('crossorigin', 'anonymous')
|
|
109
|
+
script.defer = true
|
|
110
|
+
script.setAttribute(INJECTED_RUNTIME_ATTRIBUTE, '')
|
|
111
|
+
document.head.appendChild(script)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Builder's wrappers around a printed rail let its float out (the header comment). */
|
|
115
|
+
export const BUILDER_CARD_FLOW_CSS =
|
|
116
|
+
'@media not all and (max-width: 820px){' +
|
|
117
|
+
'.builder-blocks:has(> .builder-block > [data-certrev-face="card"] > certrev-card[data-certrev-part="rail"]){display:flow-root !important}' +
|
|
118
|
+
'.builder-blocks > .builder-block:has(> [data-certrev-face="card"] > certrev-card[data-certrev-part="rail"]){display:block !important}' +
|
|
119
|
+
'.builder-blocks > .builder-block > [data-certrev-face="card"]:has(> certrev-card[data-certrev-part="rail"]){display:block !important}' +
|
|
120
|
+
'}'
|
|
121
|
+
|
|
122
|
+
export function ContractCardFace(props: {
|
|
123
|
+
readonly card: CardDocumentV1
|
|
124
|
+
readonly names: readonly CardPartName[]
|
|
125
|
+
/** The record that names this card (`ssr-seed.ts`): the browser repaints it from here with no read. */
|
|
126
|
+
readonly seed: string
|
|
127
|
+
}) {
|
|
128
|
+
const ref = React.useRef<HTMLDivElement>(null)
|
|
129
|
+
const html = cardHtml(props.card, props.names)
|
|
130
|
+
const { card } = props
|
|
131
|
+
const carriesAssets = cardCarriesAssets(card, props.names)
|
|
132
|
+
useBrowserLayoutEffect(() => {
|
|
133
|
+
const wrapper = ref.current
|
|
134
|
+
if (!wrapper) return
|
|
135
|
+
attachCardRoots(wrapper, html)
|
|
136
|
+
if (carriesAssets) injectCardRuntime(card)
|
|
137
|
+
}, [html, card, carriesAssets])
|
|
138
|
+
const wrapper = (
|
|
139
|
+
<div
|
|
140
|
+
ref={ref}
|
|
141
|
+
data-certrev-cert-chrome=""
|
|
142
|
+
data-certrev-delivery=""
|
|
143
|
+
data-certrev-face="card"
|
|
144
|
+
data-certrev-contract={String(props.card.contract)}
|
|
145
|
+
data-certrev-def-version={props.card.defVersion}
|
|
146
|
+
{...{ [SEED_ATTRIBUTE]: props.seed }}
|
|
147
|
+
// biome-ignore lint/security/noDangerouslySetInnerHtml: CertREV's card document, printed only after it hashes to the digest the verified envelope signs
|
|
148
|
+
dangerouslySetInnerHTML={{ __html: html }}
|
|
149
|
+
/>
|
|
150
|
+
)
|
|
151
|
+
if (!props.names.includes('rail')) return wrapper
|
|
152
|
+
return (
|
|
153
|
+
<>
|
|
154
|
+
{wrapper}
|
|
155
|
+
{/* biome-ignore lint/security/noDangerouslySetInnerHtml: a package constant; React would escape the `>` and `"` of its selectors */}
|
|
156
|
+
<style data-certrev-builder-flow="" dangerouslySetInnerHTML={{ __html: BUILDER_CARD_FLOW_CSS }} />
|
|
157
|
+
</>
|
|
158
|
+
)
|
|
159
|
+
}
|