/** * Reader comments in the renderer (RFC-0007 rollout step 4): the section's HTML (escaped plain * text, `https:`-only links with `rel="ugc nofollow noopener noreferrer"`, two levels, the plain * form with its honeypot, the notes, closed/hidden/off/preview states — invariant C7), the page * loader on the Node runtime (cache tag, `?comments=`, Turnstile CSP), and `SiteComments` * answering the endpoints on SQLite (published entries only, same origin, the honeypot, replays, * Turnstile's `siteverify` hostname/action checks, comments off without a key). */ import { mkdtemp, readFile, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { type Comment, deriveCommentKeys, ipHmac, issueChallenge, newCommentsId, type PreparedSubmission, type RenderPage, randomSalt, rateLimitKey, solveChallenge, } from "@cloudhal/comments"; import { SqlCommentStore } from "@cloudhal/comments/sql"; import type { ContentEntry, Site } from "@cloudhal/core-contracts"; import { SiteGraphStore, seedStarterSite } from "@cloudhal/core-ts"; import { openLocalStore } from "@cloudhal/core-ts/local"; import { MemoryBlobStore, type SqlDriver } from "@cloudhal/storage"; import { BetterSqlite3Driver } from "@cloudhal/storage/sqlite"; import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { SiteComments, usableCommentsKey } from "../src/comments.ts"; import { commentStrings, commentsHtml } from "../src/comments-html.ts"; import { localCommentsKey } from "../src/comments-node.ts"; import { type CommentsView, commentsJsonLd, commentsPageParam, loadComments, } from "../src/comments-view.ts"; import { SITE_CSP, turnstileCsp } from "../src/csp.ts"; import { SnapshotSource } from "../src/source.ts"; const KEY = "renderer-comments-test-key-not-a-secret-00"; const editor = { kind: "human" as const, id: "user_editor01", name: "Ayşe" }; const RLO = String.fromCodePoint(0x202e); function comment(o: Partial & { id: string }): Comment { return { schemaVersion: 1, siteId: "site_render01", nodeId: "entry_render01", source: "native", author: { kind: "guest", name: "Mehmet" }, body: { format: "text", text: "Hello" }, status: "approved", labels: [], createdAt: "2026-10-12T10:00:00.000Z", rev: 1, ...o, } as Comment; } function view(o: Partial = {}): CommentsView { return { nodeId: "entry_render01", state: "open", path: "/posts/ferries", locale: "en", protection: "pow", turnstileSiteKey: null, allowWithoutScript: true, script: "/_cloudhal/c.0123abcd.js", notFound: false, page: { items: [], total: 0, page: 1, pageCount: 1 }, ...o, }; } describe("the comments section's HTML (C7)", () => { const top = comment({ id: "cmt_top0000001", author: { kind: "guest", name: ` Eve${"&"}` }, body: { format: "text", text: `See https://example.org/a?b=1&c=2 and javascript:alert(1) and http://plain.example\n\n`, }, }); const staff = comment({ id: "cmt_staff00001", parentId: "cmt_top0000001", author: { kind: "staff", personId: "user_editor01", name: "Ayşe", badge: "author" }, body: { format: "text", text: "Thanks!" }, createdAt: "2026-10-12T11:00:00.000Z", }); const nested = comment({ id: "cmt_nested0001", parentId: "cmt_top0000001", inReplyToId: "cmt_staff00001", author: { kind: "erased" }, body: { format: "text", text: `Spoof ${RLO}gnp.exe` }, createdAt: "2026-10-12T12:00:00.000Z", }); const page: RenderPage = { items: [ { comment: top, replies: [staff, nested] }, { removed: { id: "cmt_gone000001", createdAt: "2026-10-12T09:00:00.000Z" }, replies: [comment({ id: "cmt_orphan0001", parentId: "cmt_gone000001" })], }, ], total: 4, page: 1, pageCount: 2, }; it("escapes names and text, links only https: URLs, and keeps two levels", () => { const html = commentsHtml(view({ page })); expect(html).not.toContain("alert"); expect(html).toContain("<img src=x onerror=alert(1)> Eve&"); expect(html).toContain( '', ); expect(html).not.toContain('href="javascript:'); expect(html).not.toContain('href="http://plain.example'); // two visible levels: replies (including a reply to a reply) sit under the top comment expect(html).toMatch( /
  • .*
    1. Author'); expect(html).toContain( 'in reply to Ayşe', ); expect(html).toContain(">Anonymous"); expect(html).toContain(''); expect(html).toContain("This comment was removed."); expect(html).toContain('

      4 comments

      '); expect(html).toContain('href="/posts/ferries?comments=2#comments" data-more'); // no third-party resource; the only script is the first-party module expect(html).not.toMatch(/(?:src|href)="https?:\/\/(?!example\.org)/); expect(html.match(/'); // Reply buttons wait for the module; every comment has a report form expect(html).toContain('data-reply="cmt_top0000001" data-name="<img'); expect(html).toMatch(/data-reply="cmt_top0000001"[^>]* hidden>/); expect(html.match(/action="\/__cloudhal\/comments\/report"/g)).toHaveLength(4); expect(html).toContain('data-protection="pow"'); }); it("closed threads keep comments and say so; hidden and off render nothing; previews a placeholder", () => { const closed = commentsHtml(view({ page, state: "closed" })); expect(closed).toContain("Comments are closed."); expect(closed).not.toContain("comment-form"); expect(closed).not.toContain(" { const html = commentsHtml(view({ locale: "tr", allowWithoutScript: false })); expect(html).toContain("Yorumlar"); expect(html).toContain("Yorumu gönder"); expect(html).toContain("