repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(the-system): close Phase 2 review bypasses

cfb2f8d776fc4f766b0b8d2e0ff93d117ceea481

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/the-system/__tests__/documents.test.ts

Modified
122 + it("rejects duplicate config keys", () =>
123 + assert.throws(
124 + () =>
125 + parseSystemConfig(
126 + '{"prefix":"PX","prefix":"AB","schemaVersion":1,"styleVersion":1}',
127 + ),
128 + (error: unknown) =>
129 + error instanceof SystemDocumentError &&
130 + error.code === "config.duplicate-key",
131 + ));
122 133 it.each([
123 134 ["malformed JSON", "{"],
238 249 ] as const)("parses exact metadata %s", (json, expected) =>
239 250 assert.deepEqual(parseDocumentMetadata(shell(json), "PX"), expected));
252 + it("wraps malformed metadata JSON", () => {
253 + assert.throws(
254 + () =>
255 + parseDocumentMetadata(
256 + shell('{"id":"PX-SPEC-001","type":"spec","\\uZZZZ":true}'),
257 + "PX",
258 + ),
259 + (error: unknown) =>
260 + error instanceof SystemDocumentError &&
261 + error.code === "metadata.invalid-json",
262 + );
263 + });
265 + it("ignores metadata blocks inside HTML comments", () => {
266 + const html = `${shell('{"id":"PX-SPEC-001","type":"spec"}')}<!-- <script type="application/json" id="system-meta">{}</script> -->`;
267 + assert.deepEqual(parseDocumentMetadata(html, "PX"), {
268 + id: "PX-SPEC-001",
269 + type: "spec",
270 + });
271 + });
241 273 it.each([
242 274 ["missing block", '<!doctype html><html lang="en"></html>'],
401 433 validateAuthoredHtml("doc.html", html).some((item) => item.code === code),
404 - it("allows remote HTTPS navigation anchors", () => {
436 + it("ignores shell and forbidden tags inside HTML comments", () => {
405 437 const html = shell(
406 438 '{"id":"PX-SPEC-001","type":"spec"}',
407 - '<header><h1>X</h1></header><main><section><a href="https://example.com/reference">Reference</a></section></main>',
439 + "<!-- <header><main><section><iframe></iframe></section></main></header> -->",
409 - assert.deepEqual(validateAuthoredHtml("doc.html", html), []);
441 + const codes = validateAuthoredHtml("doc.html", html).map(
442 + ({ code }) => code,
443 + );
444 + assert.ok(codes.includes("document.semantic-shell"));
445 + assert.ok(!codes.includes("document.forbidden-tag"));
446 + });
448 + it.each([
449 + ["missing head close", "</head>"],
450 + ["missing body close", "</body>"],
451 + ["missing html close", "</html>"],
452 + ["missing header close", "</header>"],
453 + ["missing main close", "</main>"],
454 + ["missing section close", "</section>"],
455 + ] as const)("requires a complete shell: %s", (_name, removed) => {
456 + const findings = validateAuthoredHtml(
457 + "doc.html",
458 + shell('{"id":"PX-SPEC-001","type":"spec"}').replace(removed, ""),
459 + );
460 + assert.ok(
461 + findings.some(
462 + ({ code }) =>
463 + code === "document.structure" || code === "document.semantic-shell",
464 + ),
465 + );
466 + });
468 + it.each([
470 + "unquoted executable URL",
471 + "<a href=javascript:alert(1)>X</a>",
472 + "document.executable-url",
473 + ],
475 + "encoded executable URL with control whitespace",
476 + '<a href="java&#x09;script&colon;alert(1)">X</a>',
477 + "document.executable-url",
478 + ],
480 + "unquoted remote src",
481 + "<img src=https://example.com/a.png>",
482 + "document.remote-resource",
483 + ],
485 + "encoded remote srcset",
486 + '<img srcset="/local.png 1x, https&#58;//example.com/a.png 2x">',
487 + "document.remote-resource",
488 + ],
490 + "remote poster",
491 + '<video poster="//example.com/poster.png"></video>',
492 + "document.remote-resource",
493 + ],
495 + "resource-bearing href",
496 + '<link rel="preload" href="https://example.com/font.woff2">',
497 + "document.remote-resource",
498 + ],
499 + ] as const)("rejects %s", (_name, markup, code) => {
500 + const html = shell(
501 + '{"id":"PX-SPEC-001","type":"spec"}',
502 + `<header><h1>X</h1></header><main><section>${markup}</section></main>`,
503 + );
504 + assert.ok(
505 + validateAuthoredHtml("doc.html", html).some(
506 + (finding) => finding.code === code,
507 + ),
508 + );
509 + });
511 + it("allows quoted and unquoted remote HTTPS navigation anchors", () => {
512 + for (const anchor of [
513 + '<a href="https://example.com/reference">Reference</a>',
514 + "<a href=https://example.com/reference>Reference</a>",
515 + ]) {
516 + const html = shell(
517 + '{"id":"PX-SPEC-001","type":"spec"}',
518 + `<header><h1>X</h1></header><main><section>${anchor}</section></main>`,
519 + );
520 + assert.deepEqual(validateAuthoredHtml("doc.html", html), []);
461 573 assert.deepEqual(result.diagnostics, []);
464 - it("sorts stable duplicate, filename, dangling, and forbidden-edge diagnostics", () => {
576 + it("excludes HTML- and filename-invalid documents from graph edges", () => {
577 + const invalidSpec = source(
578 + "specs",
579 + "PX-SPEC-009",
580 + "bad-html",
581 + '{"id":"PX-SPEC-009","type":"spec"}',
582 + );
583 + invalidSpec.html = invalidSpec.html.replace(
584 + "<header>",
585 + "<!-- <header> -->",
586 + );
587 + const invalidPlan = source(
588 + "plans",
589 + "PX-PLAN-002",
590 + "invalid-source",
591 + '{"id":"PX-PLAN-002","type":"plan","spec":"PX-SPEC-404"}',
592 + );
593 + invalidPlan.html = invalidPlan.html.replace("<main>", "");
594 + const invalidFilenamePlan = source(
595 + "plans",
596 + "PX-PLAN-004",
597 + "invalid-filename-source",
598 + '{"id":"PX-PLAN-003","type":"plan","spec":"PX-SPEC-405"}',
599 + );
600 + const result = indexDocuments(
602 + invalidSpec,
603 + source(
604 + "specs",
605 + "PX-SPEC-002",
606 + "wrong-name",
607 + '{"id":"PX-SPEC-010","type":"spec"}',
608 + ),
609 + source(
610 + "plans",
611 + "PX-PLAN-001",
612 + "references-invalid-html",
613 + '{"id":"PX-PLAN-001","type":"plan","spec":"PX-SPEC-009"}',
614 + ),
615 + source(
616 + "plans",
617 + "PX-PLAN-005",
618 + "references-invalid-filename",
619 + '{"id":"PX-PLAN-005","type":"plan","spec":"PX-SPEC-010"}',
620 + ),
621 + invalidPlan,
622 + invalidFilenamePlan,
623 + ],
624 + "PX",
625 + );
626 + const invalidSourcePaths = new Set(
627 + result.documents
628 + .filter(({ metadata }) =>
629 + ["PX-PLAN-002", "PX-PLAN-003"].includes(metadata.id),
631 + .map(({ path }) => path),
632 + );
633 + assert.ok(
634 + result.diagnostics.some(
635 + ({ code, path }) =>
636 + code === "document.filename" && path.includes("PX-SPEC-002"),
637 + ),
638 + );
639 + assert.ok(
640 + result.diagnostics.some(
641 + ({ code, path }) =>
642 + code === "document.semantic-shell" && path.includes("PX-SPEC-009"),
643 + ),
644 + );
645 + for (const planId of ["PX-PLAN-001", "PX-PLAN-005"])
646 + assert.ok(
647 + result.diagnostics.some(
648 + ({ code, path }) =>
649 + code === "graph.dangling-reference" && path.includes(planId),
650 + ),
651 + );
652 + for (const path of invalidSourcePaths)
653 + assert.ok(
654 + result.diagnostics.some(
655 + (item) => item.path === path && item.code.startsWith("document."),
656 + ),
657 + );
658 + assert.ok(
659 + !result.diagnostics.some(
660 + ({ code, path }) =>
661 + code.startsWith("graph.") && invalidSourcePaths.has(path),
662 + ),
663 + );
664 + });
666 + it("sorts stable filename, dangling, and forbidden-edge diagnostics", () => {
465 667 const result = indexDocuments(
467 669 source(
521 723 .map(({ code }) => code),
523 725 assert.ok(
524 - result.diagnostics.some(
525 - (item) =>
526 - item.code === "graph.duplicate-id" &&
527 - item.message.includes("PX-SPEC-001"),
528 - ),
726 + !result.diagnostics.some((item) => item.code === "graph.duplicate-id"),
530 728 assert.ok(
531 729 result.diagnostics.some((item) => item.code === "document.filename"),

extensions/the-system/documents.ts

Modified
164 164 } catch {
165 165 throw new SystemDocumentError("config.invalid-json", "Invalid config JSON");
167 + const duplicates = duplicateTopLevelKeys(source);
168 + if (duplicates.length)
169 + throw new SystemDocumentError(
170 + "config.duplicate-key",
171 + `Duplicate config keys: ${duplicates.join(", ")}`,
172 + );
167 173 const value = objectValue(parsed, "Config");
168 174 exactKeys(value, ["prefix", "schemaVersion", "styleVersion"]);
169 175 if (typeof value.prefix !== "string" || !PREFIX.test(value.prefix)) {
305 311 return false;
314 +type HtmlTag = Readonly<{
315 + name: string;
316 + closing: boolean;
317 + start: number;
318 + end: number;
319 + attributes: ReadonlyMap<string, string | undefined>;
320 +}>;
322 +const withoutHtmlComments = (html: string) =>
323 + html.replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) =>
324 + comment.replace(/[^\r\n]/g, " "),
325 + );
327 +const scanAttributes = (
328 + source: string,
329 +): ReadonlyMap<string, string | undefined> => {
330 + const attributes = new Map<string, string | undefined>();
331 + let index = 0;
332 + while (index < source.length) {
333 + while (/\s|\//.test(source[index] ?? "")) index += 1;
334 + const start = index;
335 + while (!/[\s=/>]/.test(source[index] ?? ">")) index += 1;
336 + if (start === index) break;
337 + const name = source.slice(start, index).toLowerCase();
338 + while (/\s/.test(source[index] ?? "")) index += 1;
339 + let value: string | undefined;
340 + if (source[index] === "=") {
341 + index += 1;
342 + while (/\s/.test(source[index] ?? "")) index += 1;
343 + const quote =
344 + source[index] === '"' || source[index] === "'"
345 + ? source[index++]
346 + : undefined;
347 + const valueStart = index;
348 + if (quote)
349 + while (index < source.length && source[index] !== quote) index += 1;
350 + else
351 + while (index < source.length && !/[\s>]/.test(source[index]))
352 + index += 1;
353 + value = source.slice(valueStart, index);
354 + if (quote && source[index] === quote) index += 1;
356 + if (!attributes.has(name)) attributes.set(name, value);
358 + return attributes;
361 +const scanHtmlTags = (html: string): readonly HtmlTag[] => {
362 + const source = withoutHtmlComments(html);
363 + const tags: HtmlTag[] = [];
364 + let index = 0;
365 + while (index < source.length) {
366 + const start = source.indexOf("<", index);
367 + if (start < 0) break;
368 + let cursor = start + 1;
369 + const closing = source[cursor] === "/";
370 + if (closing) cursor += 1;
371 + while (/\s/.test(source[cursor] ?? "")) cursor += 1;
372 + const nameStart = cursor;
373 + while (/[A-Za-z0-9:-]/.test(source[cursor] ?? "")) cursor += 1;
374 + if (cursor === nameStart) {
375 + index = start + 1;
376 + continue;
378 + const name = source.slice(nameStart, cursor).toLowerCase();
379 + let quote: string | undefined;
380 + let end = cursor;
381 + for (; end < source.length; end += 1) {
382 + const character = source[end];
383 + if (quote) {
384 + if (character === quote) quote = undefined;
385 + } else if (character === '"' || character === "'") quote = character;
386 + else if (character === ">") break;
388 + if (end >= source.length) break;
389 + tags.push({
390 + name,
391 + closing,
392 + start,
393 + end: end + 1,
394 + attributes: closing
395 + ? new Map()
396 + : scanAttributes(source.slice(cursor, end)),
397 + });
398 + index = end + 1;
400 + return tags;
403 +const namedEntities: Readonly<Record<string, string>> = {
404 + amp: "&",
405 + apos: "'",
406 + colon: ":",
407 + gt: ">",
408 + lt: "<",
409 + newline: "\n",
410 + quot: '"',
411 + tab: "\t",
413 +const decodeBasicEntities = (value: string) =>
414 + value.replace(
415 + /&(?:#(\d+)|#x([\da-f]+)|([a-z][a-z\d]+));?/gi,
416 + (entity, decimal: string, hexadecimal: string, named: string) => {
417 + if (named) return namedEntities[named.toLowerCase()] ?? entity;
418 + const point = Number.parseInt(decimal ?? hexadecimal, decimal ? 10 : 16);
419 + try {
420 + return String.fromCodePoint(point);
421 + } catch {
422 + return "�";
424 + },
425 + );
426 +const attributeValue = (tag: HtmlTag, name: string) => {
427 + const value = tag.attributes.get(name);
428 + return value === undefined ? undefined : decodeBasicEntities(value);
430 +const normalizedUrl = (value: string) =>
431 + decodeBasicEntities(value)
432 + .replace(/[\u0000-\u001f\u007f-\u009f]/g, "")
433 + .trim();
435 +const scriptBlocks = (html: string, tags = scanHtmlTags(html)) => {
436 + const blocks: { tag: HtmlTag; content: string }[] = [];
437 + for (let index = 0; index < tags.length; index += 1) {
438 + const tag = tags[index];
439 + if (tag.name !== "script" || tag.closing) continue;
440 + const close = tags
441 + .slice(index + 1)
442 + .find((item) => item.name === "script" && item.closing);
443 + if (close) blocks.push({ tag, content: html.slice(tag.end, close.start) });
445 + return blocks;
308 448 const metadataScripts = (html: string) =>
309 - [...html.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi)].filter(
310 - (match) =>
311 - /\bid\s*=\s*(["'])system-meta\1/i.test(match[1]) &&
312 - /\btype\s*=\s*(["'])application\/json\1/i.test(match[1]),
449 + scriptBlocks(html).filter(
450 + ({ tag }) =>
451 + attributeValue(tag, "id")?.toLowerCase() === "system-meta" &&
452 + attributeValue(tag, "type")?.toLowerCase() === "application/json",
315 455 export function parseDocumentMetadata(
322 462 "metadata.block",
323 463 `Expected exactly one system-meta block; received ${blocks.length}`,
325 - const json = blocks[0][2].trim();
326 - const duplicates = duplicateTopLevelKeys(json);
327 - if (duplicates.length)
328 - throw new SystemDocumentError(
329 - "metadata.duplicate-key",
330 - `Duplicate metadata keys: ${duplicates.join(", ")}`,
331 - );
465 + const json = blocks[0].content.trim();
332 466 let parsed: unknown;
467 + let duplicates: string[];
334 469 parsed = JSON.parse(json);
470 + duplicates = duplicateTopLevelKeys(json);
335 471 } catch {
336 472 throw new SystemDocumentError(
337 473 "metadata.invalid-json",
338 474 "Invalid system metadata JSON",
477 + if (duplicates.length)
478 + throw new SystemDocumentError(
479 + "metadata.duplicate-key",
480 + `Duplicate metadata keys: ${duplicates.join(", ")}`,
481 + );
341 482 const value = objectValue(parsed, "Metadata");
342 483 if (typeof value.id !== "string" || typeof value.type !== "string")
343 484 throw new SystemDocumentError(
444 585 path: string,
445 586 message: string,
446 587 ): Diagnostic => ({ code, path, message });
447 -const count = (source: string, expression: RegExp) =>
448 - [...source.matchAll(expression)].length;
588 +const tagsNamed = (tags: readonly HtmlTag[], name: string, closing = false) =>
589 + tags.filter((tag) => tag.name === name && tag.closing === closing);
590 +const isExecutableUrl = (value: string) =>
591 + /^(?:javascript|vbscript|data):/i.test(normalizedUrl(value));
592 +const isRemoteUrl = (value: string) =>
593 + /^(?:https?:|\/\/)/i.test(normalizedUrl(value));
594 +const srcsetUrls = (value: string) =>
595 + decodeBasicEntities(value)
596 + .split(",")
597 + .map((candidate) => candidate.trim().split(/\s+/)[0] ?? "");
598 +const srcsetHasRemoteUrl = (value: string) =>
599 + srcsetUrls(value).some(isRemoteUrl);
450 601 export function validateAuthoredHtml(
451 602 filename: string,
452 603 html: string,
453 604 ): readonly Diagnostic[] {
454 605 const findings: Diagnostic[] = [];
455 - const requireOne = (code: string, expression: RegExp, message: string) => {
456 - if (count(html, expression) !== 1)
457 - findings.push(diagnostic(code, filename, message));
606 + const cleanHtml = withoutHtmlComments(html);
607 + const tags = scanHtmlTags(html);
608 + const opening = (name: string) => tagsNamed(tags, name);
609 + const closing = (name: string) => tagsNamed(tags, name, true);
610 + const add = (code: string, message: string) =>
611 + findings.push(diagnostic(code, filename, message));
613 + if (!/^<!doctype html>/i.test(cleanHtml.trimStart()))
614 + add("document.doctype", "Document must begin with HTML doctype");
616 + const htmlOpen = opening("html");
617 + const htmlClose = closing("html");
618 + const headOpen = opening("head");
619 + const headClose = closing("head");
620 + const bodyOpen = opening("body");
621 + const bodyClose = closing("body");
622 + if (
623 + htmlOpen.length !== 1 ||
624 + htmlClose.length !== 1 ||
625 + headOpen.length !== 1 ||
626 + headClose.length !== 1 ||
627 + bodyOpen.length !== 1 ||
628 + bodyClose.length !== 1 ||
629 + !(htmlOpen[0]?.start < headOpen[0]?.start) ||
630 + !(headOpen[0]?.start < headClose[0]?.start) ||
631 + !(headClose[0]?.start < bodyOpen[0]?.start) ||
632 + !(bodyOpen[0]?.start < bodyClose[0]?.start) ||
633 + !(bodyClose[0]?.start < htmlClose[0]?.start)
635 + add(
636 + "document.structure",
637 + "Document requires one complete html, head, and body structure",
638 + );
640 + if (
641 + htmlOpen.length !== 1 ||
642 + attributeValue(htmlOpen[0], "lang")?.toLowerCase() !== "en"
644 + add("document.language", "Document requires one html lang=en root");
646 + const meta = opening("meta");
647 + if (
648 + meta.filter(
649 + (tag) => attributeValue(tag, "charset")?.toLowerCase() === "utf-8",
650 + ).length !== 1
652 + add("document.charset", "Document requires one UTF-8 charset meta");
653 + if (
654 + meta.filter(
655 + (tag) =>
656 + attributeValue(tag, "name")?.toLowerCase() === "viewport" &&
657 + /^width=device-width,\s*initial-scale=1$/i.test(
658 + attributeValue(tag, "content") ?? "",
659 + ),
660 + ).length !== 1
662 + add("document.viewport", "Document requires one viewport meta");
664 + const titleOpen = opening("title");
665 + const titleClose = closing("title");
666 + if (
667 + titleOpen.length !== 1 ||
668 + titleClose.length !== 1 ||
669 + titleOpen[0].end >= titleClose[0].start ||
670 + !html.slice(titleOpen[0].end, titleClose[0].start).trim()
672 + add("document.title", "Document requires one nonempty title");
674 + const stylesheets = opening("link").filter((tag) =>
675 + (attributeValue(tag, "rel") ?? "")
676 + .toLowerCase()
677 + .split(/\s+/)
678 + .includes("stylesheet"),
679 + );
680 + if (
681 + stylesheets.length !== 1 ||
682 + attributeValue(stylesheets[0], "href") !== "../system.css"
684 + add(
685 + "document.stylesheet",
686 + "Document requires exactly one ../system.css stylesheet",
687 + );
689 + const semantic = {
690 + header: { open: opening("header"), close: closing("header") },
691 + main: { open: opening("main"), close: closing("main") },
692 + section: { open: opening("section"), close: closing("section") },
459 - if (!/^<!doctype html>/i.test(html.trimStart()))
460 - findings.push(
461 - diagnostic(
462 - "document.doctype",
463 - filename,
464 - "Document must begin with HTML doctype",
465 - ),
466 - );
467 - requireOne(
468 - "document.language",
469 - /<html\b[^>]*\blang\s*=\s*(["'])en\1[^>]*>/gi,
470 - "Document requires one html lang=en root",
471 - );
472 - requireOne(
473 - "document.charset",
474 - /<meta\b[^>]*\bcharset\s*=\s*(["'])?utf-8\1[^>]*>/gi,
475 - "Document requires one UTF-8 charset meta",
476 - );
477 - requireOne(
478 - "document.viewport",
479 - /<meta\b[^>]*\bname\s*=\s*(["'])viewport\1[^>]*\bcontent\s*=\s*(["'])width=device-width,\s*initial-scale=1\2[^>]*>/gi,
480 - "Document requires one viewport meta",
481 - );
482 - requireOne(
483 - "document.title",
484 - /<title>\s*[^<\s][^<]*<\/title>/gi,
485 - "Document requires one nonempty title",
486 - );
487 - requireOne(
488 - "document.stylesheet",
489 - /<link\b(?=[^>]*\brel\s*=\s*(["'])stylesheet\1)(?=[^>]*\bhref\s*=\s*(["'])\.\.\/system\.css\2)[^>]*>/gi,
490 - "Document requires exactly ../system.css",
491 - );
492 - if (count(html, /<link\b[^>]*\brel\s*=\s*(["'])stylesheet\1[^>]*>/gi) !== 1)
493 - findings.push(
494 - diagnostic(
495 - "document.stylesheet",
496 - filename,
497 - "Additional or missing stylesheets are forbidden",
498 - ),
499 - );
500 - for (const tag of ["header", "main", "section"] as const)
501 - requireOne(
694 + for (const name of ["header", "main", "section"] as const) {
695 + const pair = semantic[name];
696 + if (
697 + pair.open.length !== 1 ||
698 + pair.close.length !== 1 ||
699 + pair.open[0].start >= pair.close[0].start
701 + add(
702 + "document.semantic-shell",
703 + `Document requires one complete ${name} element`,
704 + );
706 + if (
707 + bodyOpen.length === 1 &&
708 + bodyClose.length === 1 &&
709 + semantic.header.open.length === 1 &&
710 + semantic.header.close.length === 1 &&
711 + semantic.main.open.length === 1 &&
712 + semantic.main.close.length === 1 &&
713 + semantic.section.open.length === 1 &&
714 + semantic.section.close.length === 1 &&
715 + !(
716 + bodyOpen[0].start < semantic.header.open[0].start &&
717 + semantic.header.close[0].start < semantic.main.open[0].start &&
718 + semantic.main.open[0].start < semantic.section.open[0].start &&
719 + semantic.section.close[0].start < semantic.main.close[0].start &&
720 + semantic.main.close[0].start < bodyClose[0].start
723 + add(
502 724 "document.semantic-shell",
503 - new RegExp(`<${tag}\\b`, "gi"),
504 - `Document requires one ${tag} element`,
505 - );
506 - if (/<\/?(?:iframe|object|embed)\b/i.test(html))
507 - findings.push(
508 - diagnostic(
509 - "document.forbidden-tag",
510 - filename,
511 - "iframe, object, and embed are forbidden",
512 - ),
513 - );
514 - if (/<meta\b[^>]*\bhttp-equiv\s*=\s*(["'])refresh\1/i.test(html))
515 - findings.push(
516 - diagnostic(
517 - "document.meta-refresh",
518 - filename,
519 - "Meta refresh is forbidden",
520 - ),
521 - );
522 - if (/\son[a-z]+\s*=/i.test(html))
523 - findings.push(
524 - diagnostic(
525 - "document.event-handler",
526 - filename,
527 - "Event-handler attributes are forbidden",
528 - ),
725 + "Document requires header then main with one contained section",
728 + if (tags.some((tag) => ["iframe", "object", "embed"].includes(tag.name)))
729 + add("document.forbidden-tag", "iframe, object, and embed are forbidden");
531 - /\b(?:href|src|action|formaction|xlink:href)\s*=\s*(["'])\s*(?:javascript|vbscript|data):/i.test(
532 - html,
731 + meta.some(
732 + (tag) => attributeValue(tag, "http-equiv")?.toLowerCase() === "refresh",
535 - findings.push(
536 - diagnostic(
537 - "document.executable-url",
538 - filename,
539 - "Executable URL schemes are forbidden",
540 - ),
541 - );
542 - if (/<script\b[^>]*\bsrc\s*=/i.test(html))
543 - findings.push(
544 - diagnostic(
545 - "document.external-script",
546 - filename,
547 - "External script sources are forbidden",
548 - ),
549 - );
735 + add("document.meta-refresh", "Meta refresh is forbidden");
551 - /<(?:img|audio|video|source|track)\b[^>]*\bsrc\s*=\s*(["'])https?:/i.test(
552 - html,
737 + tags.some(
738 + (tag) =>
739 + !tag.closing &&
740 + [...tag.attributes].some(([name]) => /^on[a-z]+$/i.test(name)),
555 - findings.push(
556 - diagnostic(
557 - "document.remote-resource",
558 - filename,
559 - "Remote media resources are forbidden",
560 - ),
743 + add("document.event-handler", "Event-handler attributes are forbidden");
745 + const urlAttributes = new Set([
746 + "href",
747 + "src",
748 + "srcset",
749 + "poster",
750 + "action",
751 + "formaction",
752 + "xlink:href",
753 + ]);
754 + let executableUrl = false;
755 + let remoteResource = false;
756 + for (const tag of tags) {
757 + if (tag.closing) continue;
758 + for (const [name, rawValue] of tag.attributes) {
759 + if (!urlAttributes.has(name) || rawValue === undefined) continue;
760 + if (
761 + isExecutableUrl(rawValue) ||
762 + (name === "srcset" && srcsetUrls(rawValue).some(isExecutableUrl))
764 + executableUrl = true;
765 + if (
766 + ((name === "src" || name === "poster") && isRemoteUrl(rawValue)) ||
767 + (name === "srcset" && srcsetHasRemoteUrl(rawValue)) ||
768 + ((name === "href" || name === "xlink:href") &&
769 + tag.name !== "a" &&
770 + tag.name !== "area" &&
771 + isRemoteUrl(rawValue))
773 + remoteResource = true;
776 + if (executableUrl)
777 + add("document.executable-url", "Executable URL schemes are forbidden");
778 + if (remoteResource)
779 + add("document.remote-resource", "Remote runtime resources are forbidden");
780 + if (opening("script").some((tag) => tag.attributes.has("src")))
781 + add("document.external-script", "External script sources are forbidden");
782 + if (/(?:@import|url\s*\(\s*["']?\s*(?:https?:|data:))/i.test(cleanHtml))
783 + add(
784 + "document.css-resource",
785 + "CSS imports and remote/data URLs are forbidden",
562 - if (/(?:@import|url\s*\(\s*["']?\s*(?:https?:|data:))/i.test(html))
563 - findings.push(
564 - diagnostic(
565 - "document.css-resource",
566 - filename,
567 - "CSS imports and remote/data URLs are forbidden",
568 - ),
569 - );
570 - const executable = [
571 - ...html.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi),
572 - ]
788 + const executable = scriptBlocks(html, tags)
573 789 .filter(
574 - (match) => !/\btype\s*=\s*(["'])application\/json\1/i.test(match[1]),
790 + ({ tag }) =>
791 + attributeValue(tag, "type")?.toLowerCase() !== "application/json",
576 - .map((match) => match[2])
793 + .map(({ content }) => content)
577 794 .join("\n");
579 796 /\b(?:fetch|XMLHttpRequest|WebSocket|EventSource|importScripts|localStorage|sessionStorage|indexedDB)\b/.test(
582 799 /\bimport\s*\(/.test(executable)
584 - findings.push(
585 - diagnostic(
586 - "document.active-api",
587 - filename,
588 - "Network, import, and browser storage APIs are forbidden",
589 - ),
801 + add(
802 + "document.active-api",
803 + "Network, import, and browser storage APIs are forbidden",
591 805 return findings.sort(compareDiagnostics);
629 843 prefix: string,
630 844 ): DocumentIndex {
631 845 const documents: DocumentRecord[] = [];
846 + const graphEligible: DocumentRecord[] = [];
632 847 const diagnostics: Diagnostic[] = [];
633 848 for (const item of [...sources].sort((left, right) =>
634 849 left.path.localeCompare(right.path),
636 - diagnostics.push(...validateAuthoredHtml(item.path, item.html));
851 + const htmlDiagnostics = validateAuthoredHtml(item.path, item.html);
852 + diagnostics.push(...htmlDiagnostics);
638 854 const metadata = parseDocumentMetadata(item.html, prefix);
855 + let filenameValid = true;
640 857 validateFilename(item.path, metadata, prefix);
641 858 } catch (error) {
859 + filenameValid = false;
642 860 diagnostics.push(
643 861 diagnostic("document.filename", item.path, (error as Error).message),
646 - documents.push({ path: item.path, metadata });
864 + const record = { path: item.path, metadata };
865 + documents.push(record);
866 + if (filenameValid && htmlDiagnostics.length === 0)
867 + graphEligible.push(record);
647 868 } catch (error) {
648 869 const value = error as SystemDocumentError;
649 870 diagnostics.push(
658 879 const groups = new Map<string, DocumentRecord[]>();
659 - for (const document of documents)
880 + for (const document of graphEligible)
660 881 groups.set(document.metadata.id, [
661 882 ...(groups.get(document.metadata.id) ?? []),
662 883 document,
679 900 .filter(([, records]) => records.length === 1)
680 901 .map(([id, records]) => [id, records[0]]),
682 - for (const document of documents) {
903 + for (const document of graphEligible) {
683 904 for (const [targetId, expectedType] of references(document.metadata)) {
684 905 const referencedType = parseDocumentId(targetId, prefix).type;
685 906 const target = unique.get(targetId);