@lariocpt/crust-site 0.0.0-stage → 0.4.2

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/src/fence.ts ADDED
@@ -0,0 +1,593 @@
1
+ // The document fence (plan §1 "value-range fence"): a post-render scan over
2
+ // (final markup, merged value ranges) that turns the fence rules into build
3
+ // errors. Enforced by this file, NOT by inspecting the template literal —
4
+ // `html\`<scr${"i"}pt>\`` proves the literal cannot be trusted.
5
+ //
6
+ // ALL scans are single-pass linear-time state machines (plan, risks: "the
7
+ // fence is a build-time DoS surface — no backtracking regexes"). The
8
+ // structure pass walks the document once with an explicit character loop;
9
+ // every follow-up check is O(n + m log m) over the recorded regions.
10
+ //
11
+ // Soundness note: interpolated values are ALWAYS escaped by html.ts, so a
12
+ // value can never contribute a raw `<`, `>`, `&`, `"`, or `'` to the final
13
+ // document. Every structural boundary the scanner finds (`<`, `<!--`,
14
+ // attribute quotes) therefore comes from LITERAL template text — the scanner
15
+ // parses exactly what a browser would parse, and the tracked ranges mark the
16
+ // (escaped) value text the fence rules must police.
17
+
18
+ import type { Diagnostic, DiagnosticCode } from "./diagnostics";
19
+ import { isDiagnostic } from "./diagnostics";
20
+ import { URL_ATTRIBUTES, type UrlPolicyOptions, validateUrl } from "./url";
21
+
22
+ /** A half-open character range [start, end) in the final markup. */
23
+ export type Range = readonly [start: number, end: number];
24
+
25
+ /** An attribute as the scanner sees it in the final document. */
26
+ interface ParsedAttribute {
27
+ /** Literal text (kept as-is; checks compare it lowercased). */
28
+ name: string;
29
+ nameStart: number;
30
+ nameEnd: number;
31
+ /** Raw value text (between the quotes, or the unquoted run); absent when the attribute is value-less. */
32
+ value?: string;
33
+ valueStart?: number;
34
+ valueEnd?: number;
35
+ /** The value existed but carried no quotes. */
36
+ unquoted: boolean;
37
+ /** The opening quote was never closed (value runs to end of document). */
38
+ unterminated: boolean;
39
+ }
40
+
41
+ /** An opening tag as the scanner sees it. */
42
+ interface ParsedTag {
43
+ /** Lowercased tag name. */
44
+ name: string;
45
+ nameStart: number;
46
+ nameEnd: number;
47
+ selfClosing: boolean;
48
+ attributes: ParsedAttribute[];
49
+ }
50
+
51
+ interface CommentRegion {
52
+ start: number;
53
+ end: number;
54
+ terminated: boolean;
55
+ }
56
+
57
+ interface RawBody {
58
+ kind: "script" | "style";
59
+ start: number;
60
+ end: number;
61
+ tagName: string;
62
+ }
63
+
64
+ interface UnterminatedQuote {
65
+ tagName: string;
66
+ attrName: string;
67
+ }
68
+
69
+ interface ScanResult {
70
+ tags: ParsedTag[];
71
+ comments: CommentRegion[];
72
+ /** `<!...>` (doctype) and `<?...>` (bogus comment) regions — inert text, not comments. */
73
+ declarations: { start: number; end: number }[];
74
+ rawBodies: RawBody[];
75
+ unterminatedQuotes: UnterminatedQuote[];
76
+ }
77
+
78
+ function isWs(ch: string): boolean {
79
+ return ch === " " || ch === "\t" || ch === "\n" || ch === "\f" || ch === "\r";
80
+ }
81
+
82
+ function isAsciiLetter(ch: string): boolean {
83
+ const c = ch.charCodeAt(0);
84
+ return (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a);
85
+ }
86
+
87
+ /** Case-insensitive `markup.startsWith(needle, at)` for an already-lowercased ASCII needle. */
88
+ function startsWithLower(markup: string, needle: string, at: number): boolean {
89
+ if (at + needle.length > markup.length) return false;
90
+ for (let c = 0; c < needle.length; c++) {
91
+ let ch = markup.charCodeAt(at + c);
92
+ if (ch >= 0x41 && ch <= 0x5a) ch += 32;
93
+ if (ch !== needle.charCodeAt(c)) return false;
94
+ }
95
+ return true;
96
+ }
97
+
98
+ /**
99
+ * True iff [s, e) intersects any tracked range. `ranges` must be sorted by
100
+ * start and non-overlapping (the invariant html.ts maintains), which makes
101
+ * the ends strictly increasing and binary-searchable. A zero-length probe
102
+ * never intersects (nothing was emitted, so nothing can cross a boundary).
103
+ */
104
+ export function rangesOverlap(ranges: readonly Range[], s: number, e: number): boolean {
105
+ if (e <= s) return false;
106
+ let lo = 0;
107
+ let hi = ranges.length;
108
+ while (lo < hi) {
109
+ const mid = (lo + hi) >> 1;
110
+ if (ranges[mid][1] <= s) lo = mid + 1;
111
+ else hi = mid;
112
+ }
113
+ return lo < ranges.length && ranges[lo][0] < e;
114
+ }
115
+
116
+ function parseOpenTag(markup: string, start: number): { tag: ParsedTag; next: number } {
117
+ const n = markup.length;
118
+ let i = start + 1; // past '<'
119
+ const nameStart = i;
120
+ while (i < n && isAsciiLetter(markup[i])) i++;
121
+ const nameEnd = i;
122
+ const tag: ParsedTag = {
123
+ name: markup.slice(nameStart, nameEnd).toLowerCase(),
124
+ nameStart,
125
+ nameEnd,
126
+ selfClosing: false,
127
+ attributes: [],
128
+ };
129
+
130
+ for (;;) {
131
+ while (i < n && isWs(markup[i])) i++;
132
+ if (i >= n) break; // tag runs to EOF; HTML emits it as-is
133
+ const c = markup[i];
134
+ if (c === ">") {
135
+ i++;
136
+ break;
137
+ }
138
+ if (c === "/") {
139
+ // The self-closing marker. For script/style the RAW-TEXT phase that
140
+ // follows ignores it (HTML: <script/> is NOT self-closed) — that is
141
+ // handled by the caller.
142
+ tag.selfClosing = true;
143
+ i++;
144
+ continue;
145
+ }
146
+ // Attribute name: runs until '=', ws, '/' or '>'. (A '"' that appears
147
+ // here is a name character, matching the HTML tokenizer's attribute-name
148
+ // state — <a "quoted"> parses a single attribute named `"quoted`.)
149
+ const aNameStart = i;
150
+ while (i < n && markup[i] !== "=" && markup[i] !== ">" && markup[i] !== "/" && !isWs(markup[i]))
151
+ i++;
152
+ const aNameEnd = i;
153
+ if (aNameEnd === aNameStart) {
154
+ // A character that cannot start a name in our stop-set logic; consume
155
+ // it to keep the machine moving (unreachable for well-formed markup).
156
+ i++;
157
+ continue;
158
+ }
159
+ const attr: ParsedAttribute = {
160
+ name: markup.slice(aNameStart, aNameEnd),
161
+ nameStart: aNameStart,
162
+ nameEnd: aNameEnd,
163
+ unquoted: false,
164
+ unterminated: false,
165
+ };
166
+ while (i < n && isWs(markup[i])) i++;
167
+ if (i < n && markup[i] === "=") {
168
+ i++;
169
+ while (i < n && isWs(markup[i])) i++;
170
+ if (i < n && (markup[i] === '"' || markup[i] === "'")) {
171
+ const quote = markup[i];
172
+ const vStart = i + 1;
173
+ const vEnd = markup.indexOf(quote, vStart);
174
+ if (vEnd === -1) {
175
+ // Unclosed quote: the HTML tokenizer consumes the REST OF THE
176
+ // DOCUMENT as this value. Policy: fence.unterminated (the document
177
+ // is rejected; see the header of this file and the fence tests).
178
+ attr.unterminated = true;
179
+ attr.value = markup.slice(vStart);
180
+ attr.valueStart = vStart;
181
+ attr.valueEnd = n;
182
+ i = n;
183
+ } else {
184
+ attr.value = markup.slice(vStart, vEnd);
185
+ attr.valueStart = vStart;
186
+ attr.valueEnd = vEnd;
187
+ i = vEnd + 1;
188
+ }
189
+ } else if (i >= n || markup[i] === ">") {
190
+ // '=' immediately followed by end-of-tag/EOF: the attribute is value-less.
191
+ } else {
192
+ const vStart = i;
193
+ while (i < n && !isWs(markup[i]) && markup[i] !== ">") i++;
194
+ attr.value = markup.slice(vStart, i);
195
+ attr.valueStart = vStart;
196
+ attr.valueEnd = i;
197
+ attr.unquoted = true;
198
+ }
199
+ }
200
+ // A value-less attribute (`<a href>`): no value to check. An empty URL
201
+ // resolves to the current page — same-origin by construction.
202
+ tag.attributes.push(attr);
203
+ }
204
+
205
+ return { tag, next: i };
206
+ }
207
+
208
+ /**
209
+ * One linear pass over the whole document. Records every opening tag with its
210
+ * attributes, every comment region, every inert declaration, and the raw-text
211
+ * bodies of script/style elements.
212
+ */
213
+ function scanDocument(markup: string): ScanResult {
214
+ const result: ScanResult = {
215
+ tags: [],
216
+ comments: [],
217
+ declarations: [],
218
+ rawBodies: [],
219
+ unterminatedQuotes: [],
220
+ };
221
+ const n = markup.length;
222
+ let i = 0;
223
+
224
+ while (i < n) {
225
+ if (markup[i] !== "<") {
226
+ i++;
227
+ continue;
228
+ }
229
+ // markup[i] === '<'
230
+ if (markup.startsWith("<!--", i)) {
231
+ // Comment close positions per the HTML tokenizer, all linear:
232
+ // - `<!-->` — comment-start state sees '>' → closed empty comment
233
+ // (the "incorrect-comment-syntax" parse error; the '>' is NOT data)
234
+ // - `<!--->` — comment-start-dash state sees '>' → closed empty comment
235
+ // - otherwise the first `-->` OR `--!>` (comment end / end-bang states)
236
+ // The old `indexOf("-->", i+4)` rule disagreed with browsers on all
237
+ // three shapes: it ran the comment to EOF, so a value AFTER a closed
238
+ // `<!-->`/`<!--->`/`--!>` comment was falsely fenced as comment data.
239
+ let end = -1;
240
+ if (markup[i + 4] === ">") end = i + 5;
241
+ else if (markup[i + 4] === "-" && markup[i + 5] === ">") end = i + 6;
242
+ else {
243
+ for (let k = i + 4; ; ) {
244
+ const d = markup.indexOf("--", k);
245
+ if (d === -1) break;
246
+ if (markup[d + 2] === ">") {
247
+ end = d + 3;
248
+ break;
249
+ }
250
+ if (markup[d + 2] === "!" && markup[d + 3] === ">") {
251
+ end = d + 4;
252
+ break;
253
+ }
254
+ // advance by 1, not 2: `--->` closes at the SECOND dash pair
255
+ k = d + 1;
256
+ }
257
+ }
258
+ result.comments.push({ start: i, end: end === -1 ? n : end, terminated: end !== -1 });
259
+ i = end === -1 ? n : end;
260
+ continue;
261
+ }
262
+ if (markup.startsWith("<!", i) || markup.startsWith("<?", i)) {
263
+ // Doctype or bogus comment: inert until the first '>'. No attributes
264
+ // are parsed here (a bogus comment has none; a doctype's quirks are
265
+ // not a fence concern). Values cannot produce '<!' (escaped), so a
266
+ // tracked range can never land inside this region.
267
+ const close = markup.indexOf(">", i + 2);
268
+ const end = close === -1 ? n : close + 1;
269
+ result.declarations.push({ start: i, end });
270
+ i = end;
271
+ continue;
272
+ }
273
+ if (markup.startsWith("</", i)) {
274
+ // Closing tag: consume to the matching '>' (or EOF). Closing tag names
275
+ // are NOT fence-checked — the rule is about tag-OPEN contexts, and a
276
+ // bogus closing tag is inert.
277
+ let j = i + 2;
278
+ while (j < n && markup[j] !== ">") j++;
279
+ i = j < n ? j + 1 : n;
280
+ continue;
281
+ }
282
+ if (i + 1 < n && isAsciiLetter(markup[i + 1])) {
283
+ const { tag, next } = parseOpenTag(markup, i);
284
+ result.tags.push(tag);
285
+ for (const attr of tag.attributes) {
286
+ if (attr.unterminated)
287
+ result.unterminatedQuotes.push({ tagName: tag.name, attrName: attr.name });
288
+ }
289
+ if (tag.name === "script" || tag.name === "style") {
290
+ // RAW TEXT element: the body runs to the matching end tag, and the
291
+ // self-closing '/' is ignored (HTML5: <script src="x"/> still opens a
292
+ // raw-text element — everything up to </script> is its body).
293
+ const endTag = `</${tag.name}`;
294
+ const endTagLen = endTag.length;
295
+ let j = next;
296
+ let bodyEnd = n;
297
+ let nextI = n;
298
+ while (j < n) {
299
+ // Case-INSENSITIVE: `</SCRIPT>` closes `<script>` in every browser.
300
+ // A case-sensitive match would run the body to EOF — a false
301
+ // script-body on `<SCRIPT src=x></SCRIPT>…`, and (worse) the rest
302
+ // of the document would never be scanned.
303
+ if (startsWithLower(markup, endTag, j)) {
304
+ const after = j + endTagLen;
305
+ if (after >= n) {
306
+ // End tag truncated at EOF: the element never closes, so the
307
+ // body is everything to EOF (the tokenizer agrees).
308
+ bodyEnd = n;
309
+ nextI = n;
310
+ break;
311
+ }
312
+ if (isWs(markup[after]) || markup[after] === ">") {
313
+ // The body ends where the END TAG BEGINS (j); the end tag's
314
+ // own text is not body content.
315
+ bodyEnd = j;
316
+ let k = after;
317
+ while (k < n && markup[k] !== ">") k++;
318
+ nextI = k < n ? k + 1 : n;
319
+ break;
320
+ }
321
+ // `</scriptx` etc. — the name does not match exactly; keep
322
+ // scanning (the text is body content).
323
+ }
324
+ j++;
325
+ }
326
+ result.rawBodies.push({
327
+ kind: tag.name as "script" | "style",
328
+ start: next,
329
+ end: bodyEnd,
330
+ tagName: tag.name,
331
+ });
332
+ i = nextI;
333
+ continue;
334
+ }
335
+ i = next;
336
+ continue;
337
+ }
338
+ // '<' followed by anything else (digit, punctuation) is literal text in
339
+ // HTML (e.g. "a < b").
340
+ i++;
341
+ }
342
+
343
+ return result;
344
+ }
345
+
346
+ const NAMED_ENTITIES: Record<string, string> = {
347
+ amp: "&",
348
+ lt: "<",
349
+ gt: ">",
350
+ quot: '"',
351
+ apos: "'",
352
+ };
353
+
354
+ function isHexDigit(ch: string): boolean {
355
+ const c = ch.charCodeAt(0);
356
+ return (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x46) || (c >= 0x61 && c <= 0x66);
357
+ }
358
+
359
+ /**
360
+ * Decode the HTML entity references that html.ts's escaper produces (plus
361
+ * numeric references and &apos;), used to recover the URL a BROWSER would
362
+ * interpret from a quoted attribute value. Unknown or malformed references
363
+ * pass through as literal text — conservative: a surviving `&#` then trips
364
+ * the url.entity-escape reject downstream, it can never hide a scheme.
365
+ * Single pass, linear.
366
+ */
367
+ export function decodeEntities(text: string): string {
368
+ let out = "";
369
+ let i = 0;
370
+ const n = text.length;
371
+ while (i < n) {
372
+ const amp = text.indexOf("&", i);
373
+ if (amp === -1) {
374
+ out += text.slice(i);
375
+ break;
376
+ }
377
+ out += text.slice(i, amp);
378
+ i = amp; // position of '&'
379
+ let j = i + 1;
380
+ let decoded: string | undefined;
381
+ if (j < n && text[j] === "#") {
382
+ j++;
383
+ let radix = 10;
384
+ if (j < n && (text[j] === "x" || text[j] === "X")) {
385
+ radix = 16;
386
+ j++;
387
+ }
388
+ const d0 = j;
389
+ while (
390
+ j < n &&
391
+ (radix === 16
392
+ ? isHexDigit(text[j])
393
+ : text.charCodeAt(j) >= 0x30 && text.charCodeAt(j) <= 0x39)
394
+ )
395
+ j++;
396
+ if (j > d0 && j < n && text[j] === ";") {
397
+ const code = Number.parseInt(text.slice(d0, j), radix);
398
+ if (Number.isFinite(code) && code >= 1 && code <= 0x10ffff) {
399
+ decoded = String.fromCodePoint(code);
400
+ j++;
401
+ }
402
+ }
403
+ } else {
404
+ const d0 = j;
405
+ while (j < n && isAsciiLetter(text[j])) j++;
406
+ if (j > d0 && j < n && text[j] === ";") {
407
+ const mapped = NAMED_ENTITIES[text.slice(d0, j).toLowerCase()];
408
+ if (mapped !== undefined) {
409
+ decoded = mapped;
410
+ j++;
411
+ }
412
+ }
413
+ }
414
+ if (decoded !== undefined) {
415
+ out += decoded;
416
+ i = j;
417
+ } else {
418
+ out += "&";
419
+ i++;
420
+ }
421
+ }
422
+ return out;
423
+ }
424
+
425
+ function fenceDiagnostic(
426
+ code: DiagnosticCode,
427
+ message: string,
428
+ extra?: Partial<Diagnostic>,
429
+ ): Diagnostic {
430
+ return { code, severity: "error", sourcePath: "", message, ...extra };
431
+ }
432
+
433
+ function isUrlAttribute(name: string): boolean {
434
+ for (const attr of URL_ATTRIBUTES) {
435
+ if (attr === name) return true;
436
+ }
437
+ return false;
438
+ }
439
+
440
+ /**
441
+ * Run the fence over a rendered document. Returns every violation (empty =
442
+ * clean); `finalizeDocument` wraps a non-empty result in one BuildError.
443
+ *
444
+ * Checks (all O(n + m log m), m = tracked ranges):
445
+ * - fence.script-body / fence.style-body — non-whitespace body of a
446
+ * script/style element (whitespace-only bodies pass: `<script src=x>\n</script>`)
447
+ * - fence.comment-value — a tracked value range inside a `<!-- ... -->`
448
+ * region (nothing is escapable inside a comment; `-->` injection)
449
+ * - fence.dynamic-tag-name / fence.dynamic-attr-name — a tag-open or
450
+ * attribute-name position covered by a tracked value range
451
+ * - fence.event-handler — any attribute matching /^on/i (the RENDERED name;
452
+ * a dynamic `onclick` value renders the literal text and is caught here
453
+ * as well as by fence.dynamic-attr-name)
454
+ * - fence.ping-attr / fence.srcdoc-attr — the attributes are banned outright
455
+ * - fence.unquoted-attribute — a present value without quotes
456
+ * - fence.unterminated — an opening quote never closed (the rest of the
457
+ * document is consumed by that value; the document is rejected)
458
+ * - fence.url-attribute — a URL-bearing attribute whose value region
459
+ * intersects a tracked range: the ENTIRE decoded value is re-run through
460
+ * validateUrl (a mixed literal+value attribute must resolve safely as a
461
+ * whole). Literal (untracked) values are trusted author markup and are
462
+ * NOT url-checked — documented policy, see the fence tests.
463
+ */
464
+ export function runFence(
465
+ markup: string,
466
+ ranges: readonly Range[],
467
+ opts: UrlPolicyOptions,
468
+ ): Diagnostic[] {
469
+ const scan = scanDocument(markup);
470
+ const diags: Diagnostic[] = [];
471
+
472
+ for (const body of scan.rawBodies) {
473
+ let hasNonWs = false;
474
+ for (let m = body.start; m < body.end; m++) {
475
+ if (!isWs(markup[m])) {
476
+ hasNonWs = true;
477
+ break;
478
+ }
479
+ }
480
+ if (hasNonWs) {
481
+ diags.push(
482
+ fenceDiagnostic(
483
+ body.kind === "script" ? "fence.script-body" : "fence.style-body",
484
+ `<${body.kind}> at offset ${body.start} has an inline body; v1 ships all JS/CSS as hashed assets — use script()/stylesheet()`,
485
+ ),
486
+ );
487
+ }
488
+ }
489
+
490
+ // Comment/value overlap in ONE merge pass: ranges are sorted+disjoint
491
+ // (html.ts invariant) and comments are recorded in start order and disjoint
492
+ // (the scan is sequential), so a moving cursor is linear. The naive
493
+ // range×comment double loop is quadratic — a build-time DoS surface.
494
+ let ci = 0;
495
+ for (const range of ranges) {
496
+ while (ci < scan.comments.length && scan.comments[ci].end <= range[0]) ci++;
497
+ if (ci < scan.comments.length && scan.comments[ci].start < range[1]) {
498
+ diags.push(
499
+ fenceDiagnostic(
500
+ "fence.comment-value",
501
+ `a template value at offset ${range[0]} lands inside a <!-- ... --> comment; nothing is escapable inside a comment (a value can close it with '-->')`,
502
+ ),
503
+ );
504
+ }
505
+ }
506
+
507
+ for (const tag of scan.tags) {
508
+ if (rangesOverlap(ranges, tag.nameStart, tag.nameEnd)) {
509
+ diags.push(
510
+ fenceDiagnostic(
511
+ "fence.dynamic-tag-name",
512
+ `the tag name at offset ${tag.nameStart} contains a template value; tag names must be static`,
513
+ ),
514
+ );
515
+ }
516
+ for (const attr of tag.attributes) {
517
+ const name = attr.name.toLowerCase();
518
+ if (rangesOverlap(ranges, attr.nameStart, attr.nameEnd)) {
519
+ diags.push(
520
+ fenceDiagnostic(
521
+ "fence.dynamic-attr-name",
522
+ `the attribute name at offset ${attr.nameStart} (in <${tag.name}>) contains a template value; attribute names must be static`,
523
+ ),
524
+ );
525
+ }
526
+ if (name.startsWith("on")) {
527
+ diags.push(
528
+ fenceDiagnostic(
529
+ "fence.event-handler",
530
+ `attribute "${attr.name}" on <${tag.name}> is an event handler; inline handlers are banned (v1 CSP is script-src 'self')`,
531
+ ),
532
+ );
533
+ }
534
+ if (name === "ping") {
535
+ diags.push(
536
+ fenceDiagnostic(
537
+ "fence.ping-attr",
538
+ `attribute "ping" on <${tag.name}> is banned (it fires requests at click time)`,
539
+ ),
540
+ );
541
+ }
542
+ if (name === "srcdoc") {
543
+ diags.push(
544
+ fenceDiagnostic(
545
+ "fence.srcdoc-attr",
546
+ `attribute "srcdoc" on <${tag.name}> is banned (an iframe document is a second, unfenced HTML universe)`,
547
+ ),
548
+ );
549
+ }
550
+ if (attr.unquoted && attr.value !== undefined) {
551
+ diags.push(
552
+ fenceDiagnostic(
553
+ "fence.unquoted-attribute",
554
+ `attribute "${attr.name}" on <${tag.name}> has an unquoted value; attribute values must be enclosed in "..." or '...'`,
555
+ ),
556
+ );
557
+ }
558
+ if (attr.unterminated) {
559
+ diags.push(
560
+ fenceDiagnostic(
561
+ "fence.unterminated",
562
+ `attribute "${attr.name}" on <${tag.name}> opens a quoted value that is never closed; the rest of the document was consumed by it`,
563
+ ),
564
+ );
565
+ }
566
+ if (
567
+ attr.value !== undefined &&
568
+ attr.valueStart !== undefined &&
569
+ attr.valueEnd !== undefined &&
570
+ isUrlAttribute(name) &&
571
+ rangesOverlap(ranges, attr.valueStart, attr.valueEnd)
572
+ ) {
573
+ const decoded = decodeEntities(attr.value);
574
+ try {
575
+ validateUrl(decoded, opts);
576
+ } catch (d) {
577
+ if (isDiagnostic(d)) {
578
+ diags.push(
579
+ fenceDiagnostic(
580
+ "fence.url-attribute",
581
+ `attribute "${attr.name}" on <${tag.name}> carries a template value that fails the URL policy (${d.code}): ${d.message}`,
582
+ ),
583
+ );
584
+ } else {
585
+ throw d;
586
+ }
587
+ }
588
+ }
589
+ }
590
+ }
591
+
592
+ return diags;
593
+ }