Sitesmith capture: CSS judged via CSSOM (no regex), single URL gate (no % / dot segments / backslash), all resources inlined through a bounded no-credentials fetch, CSP img-src data: only; bypass-class proof tests (unit + Firefox/Chromium)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-01 10:37:56 -08:00
1 parent c4f45541a3
commit f0b3dd1dba
7 files changed
+523 -93

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
import type { SendResult } from '../../types/sitesmith';
import type { CaptureResult } from '../../utils/self-review-capture';
/** Instruction for the one follow-up job. The render rides in the mockup slot, so say plainly what it is. */
export const REVIEW_MESSAGE =
'The attached image is a screenshot of the page as it is built right now. It is NOT a mockup to copy. ' +
'Review it like a careful designer: look for text that is hard to read, cramped or uneven spacing, misaligned or ' +
'broken layout, images that are cropped badly, missing icons, and sections that look generic. ' +
'Fix only real problems with a small patch; if the page already looks right, answer with a short "ask"-free patch with no ops and say it looks good. ' +
'Do not redesign the page and do not change the wording.';
export interface SelfReviewDeps {
/** Published-style HTML of the current page (untrusted; the capture sanitises it). */
composeHtml: () => string;
/** `assets/<file>` -> same-origin URL, for images in the capture. */
assetUrls: Record<string, string>;
allowUrls: string[];
capture: (html: string, o: { allowUrls: string[]; timeoutMs: number }) => Promise<CaptureResult>;
upload: (file: File) => Promise<{ ok: true; id: string } | { ok: false; message: string }>;
/** Start the follow-up job and wait for it. */
review: (message: string, imageId: string) => Promise<SendResult>;
/** Apply the review result (contrast repair included). */
applyResult: (res: SendResult) => Promise<{ ok: boolean; message?: string; summary?: string }>;
removeImage: (id: string) => Promise<unknown>;
resolveAssetUrls: (html: string, urls: Record<string, string>) => string;
}
export type SelfReviewOutcome =
| { status: 'applied'; note: string }
| { status: 'looks-good'; note: string }
| { status: 'skipped'; note: string };
const skip = (why: string): SelfReviewOutcome => ({ status: 'skipped', note: `Self-review skipped: ${why}. Your page was built as normal.` });
/** One review round, never more. Never throws; any failure is a non-blocking note. */
export async function runSelfReview(d: SelfReviewDeps): Promise<SelfReviewOutcome> {
let imageId: string | null = null;
try {
let html: string;
try { html = d.resolveAssetUrls(d.composeHtml(), d.assetUrls); } catch { return skip('the page could not be rendered'); }
const cap = await d.capture(html, { allowUrls: d.allowUrls, timeoutMs: 20000 });
if (!cap.ok) return skip(cap.reason);
const file = new File([cap.blob], 'self-review-render.jpg', { type: 'image/jpeg' });
const up = await d.upload(file);
if (!up.ok) return skip(up.message || 'the screenshot could not be uploaded');
imageId = up.id;
const res = await d.review(REVIEW_MESSAGE, imageId);
if (!res.ok) return skip(res.message || 'the review request failed');
if (res.response.type === 'ask') return { status: 'looks-good', note: 'Self-review: Sitesmith had no changes to make.' };
if (res.response.type === 'patch' && res.response.ops.length === 0) return { status: 'looks-good', note: 'Self-review: Sitesmith looked at the finished page and found nothing to fix.' };
const applied = await d.applyResult(res);
if (!applied.ok) return skip(applied.message || 'the fixes could not be applied');
const extra = applied.summary ? ` ${applied.summary}.` : '';
return { status: 'applied', note: `Self-review: Sitesmith looked at the finished page and made fixes.${extra} Use Undo to go back.` };
} catch (e) {
return skip(e instanceof Error && e.message ? e.message.slice(0, 100) : 'something went wrong');
} finally {
// The render is private scratch, not a keepsake: it would otherwise eat the per-site image quota.
if (imageId) { try { await d.removeImage(imageId); } catch { /* best effort */ } }
}
}