Site builder: security & data-loss hardening + unified asset picker + audit backlog #3

Merged
jknapp merged 61 commits from builder-hardening-2026-07 into main 2026-07-13 01:13:28 +00:00
6 changed files with 106 additions and 9 deletions
Showing only changes of commit 7179287087 - Show all commits
@@ -0,0 +1,13 @@
import { describe, test, expect } from 'vitest';
import { HtmlBlock } from './HtmlBlock';
const toHtml = (HtmlBlock as any).toHtml;
describe('HtmlBlock.toHtml sanitizes raw code (A4.1)', () => {
test('strips <script> and on-handlers from exported output', () => {
const { html } = toHtml({ code: '<script>alert(1)</script><p onclick="x">hi</p>' }, '');
expect(html).not.toContain('<script');
expect(html).not.toContain('onclick');
expect(html).toContain('<p>hi</p>');
});
});
+3 -2
View File
@@ -140,6 +140,7 @@ HtmlBlock.craft = {
/* ---------- HTML export ---------- */
(HtmlBlock as any).toHtml = (props: HtmlBlockProps, _childrenHtml: string) => {
// Output the raw code as-is
return { html: props.code || '' };
// Run through the same DOMPurify config used for the live editor preview
// so exported pages can't carry <script>/on*= payloads either.
return { html: purifyHtml(props.code || '') };
};
@@ -0,0 +1,28 @@
import { describe, test, expect } from 'vitest';
import { Countdown } from './Countdown';
const toHtml = (Countdown as any).toHtml;
describe('Countdown.toHtml validates targetDate before inline-script injection (A4.2)', () => {
test('malicious targetDate cannot break out of the new Date(...) call', () => {
const { html } = toHtml({ targetDate: '2026-01-01");alert(1)//' }, '');
expect(html).not.toContain('alert(');
expect(html).not.toContain('");');
});
test('valid date is JSON-encoded into the script', () => {
const { html } = toHtml({ targetDate: '2026-01-01' }, '');
expect(html).toContain('new Date("2026-01-01")');
});
test('valid date+time is preserved', () => {
const { html } = toHtml({ targetDate: '2026-01-01T12:30:00' }, '');
expect(html).toContain('new Date("2026-01-01T12:30:00")');
});
test('invalid/empty targetDate falls back safely (no injected literal)', () => {
const { html } = toHtml({ targetDate: 'not-a-date' }, '');
expect(html).not.toContain('not-a-date');
expect(html).toMatch(/new Date\(\)\.getTime\(\)|new Date\(Date\.now\(\)\)\.getTime\(\)/);
});
});
+9 -1
View File
@@ -284,6 +284,14 @@ Countdown.craft = {
// Generate a unique ID for this countdown instance
const uid = 'cd_' + Math.random().toString(36).slice(2, 8);
// Only accept a strict date/datetime shape before it's embedded in the
// inline <script>; anything else falls back to "now" instead of letting
// arbitrary text (e.g. `");alert(1)//`) break out of the new Date(...) call.
const VALID_DATE_RE = /^\d{4}-\d{2}-\d{2}([T ][0-9:.\-+Z]*)?$/;
const dateExpr = typeof targetDate === 'string' && VALID_DATE_RE.test(targetDate)
? `new Date(${JSON.stringify(targetDate)})`
: 'new Date()';
return {
html: `<section${idAttr}${sectionStyle ? ` style="${sectionStyle}"` : ''}>
${headingHtml}
@@ -295,7 +303,7 @@ Countdown.craft = {
</div>
<script>
(function(){
var target = new Date("${targetDate}").getTime();
var target = ${dateExpr}.getTime();
function pad(n){ return String(n).padStart(2,'0'); }
function update(){
var diff = target - Date.now();
@@ -0,0 +1,33 @@
import { describe, test, expect } from 'vitest';
import { Gallery } from './Gallery';
const toHtml = (Gallery as any).toHtml;
describe('Gallery.toHtml lightbox uses a delegated listener, not per-item onclick (A4.3)', () => {
test('no per-item inline onclick with interpolated src', () => {
const { html } = toHtml({ images: [{ src: '/a.jpg', alt: 'a' }], lightbox: true }, '');
expect(html).not.toMatch(/onclick="[^"]*_open\(/);
expect(html).toContain('data-lb-src="/a.jpg"');
});
test('a single-quote in src cannot break the handler (no per-item onclick at all)', () => {
const { html } = toHtml({ images: [{ src: "/a'.jpg", alt: 'a' }], lightbox: true }, '');
// no per-item onclick handler exists at all (delegated listener only)
expect(html).not.toMatch(/onclick="[^"]*_open\(/);
// the quote in src is entity-escaped in the data attribute, not raw
expect(html).toContain('data-lb-src="/a&#39;.jpg"');
expect(html).not.toContain(`data-lb-src="/a'.jpg"`);
});
test('emits exactly one delegated click listener', () => {
const { html } = toHtml({ images: [{ src: '/a.jpg' }, { src: '/b.jpg' }], lightbox: true }, '');
const matches = html.match(/addEventListener\(['"]click['"]/g) || [];
expect(matches.length).toBe(1);
});
test('lightbox=false: no data-lb-src, no script', () => {
const { html } = toHtml({ images: [{ src: '/a.jpg' }], lightbox: false }, '');
expect(html).not.toContain('data-lb-src');
expect(html).not.toContain('<script>');
});
});
+20 -6
View File
@@ -1,7 +1,7 @@
import React, { CSSProperties } from 'react';
import { useNode, UserComponent } from '@craftjs/core';
import { cssPropsToString } from '../../utils/style-helpers';
import { escapeHtml, escapeAttr } from '../../utils/escape';
import { escapeHtml, escapeAttr, safeUrl } from '../../utils/escape';
interface GalleryImage {
src: string;
@@ -293,28 +293,42 @@ Gallery.craft = {
const caption = img.caption
? `<div style="position:absolute;bottom:0;left:0;right:0;padding:8px 12px;background:linear-gradient(transparent,rgba(0,0,0,0.7));color:#ffffff;font-size:12px;border-bottom-left-radius:8px;border-bottom-right-radius:8px">${escapeHtml(img.caption)}</div>`
: '';
const clickAttr = lightbox ? ` onclick="${galleryId}_open('${escapeAttr(img.src)}')" style="cursor:pointer;position:relative;overflow:hidden;border-radius:8px"` : ' style="position:relative;overflow:hidden;border-radius:8px"';
return `<div${clickAttr}>
<img src="${escapeAttr(img.src)}" alt="${escapeAttr(img.alt)}" style="width:100%;height:200px;object-fit:cover;display:block;border-radius:8px;background-color:#f1f5f9" />
// Lightbox items carry the image URL as a data attribute rather than an
// inline onclick with an interpolated src -- a single delegated click
// listener below reads it, so a src containing a quote can't break out
// of a per-item event-handler string.
const lbAttr = lightbox ? ` data-lb-src="${escapeAttr(safeUrl(img.src || ''))}"` : '';
const itemStyle = lightbox ? 'cursor:pointer;position:relative;overflow:hidden;border-radius:8px' : 'position:relative;overflow:hidden;border-radius:8px';
return `<div${lbAttr} style="${itemStyle}">
<img src="${escapeAttr(safeUrl(img.src || ''))}" alt="${escapeAttr(img.alt)}" style="width:100%;height:200px;object-fit:cover;display:block;border-radius:8px;background-color:#f1f5f9" />
${caption}
</div>`;
}).join('\n ');
let lightboxHtml = '';
let gridIdAttr = '';
if (lightbox) {
gridIdAttr = ` id="${galleryId}_grid"`;
lightboxHtml = `
<div id="${galleryId}_overlay" onclick="${galleryId}_close()" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.9);z-index:9999;justify-content:center;align-items:center;cursor:pointer">
<img id="${galleryId}_img" src="" alt="" style="max-width:90%;max-height:90%;object-fit:contain;border-radius:8px" />
</div>
<script>
function ${galleryId}_open(src){var o=document.getElementById('${galleryId}_overlay');document.getElementById('${galleryId}_img').src=src;o.style.display='flex';}
function ${galleryId}_close(){document.getElementById('${galleryId}_overlay').style.display='none';}
document.getElementById('${galleryId}_grid').addEventListener('click', function(e){
var t = e.target.closest('[data-lb-src]');
if(!t) return;
var src = t.getAttribute('data-lb-src');
var o = document.getElementById('${galleryId}_overlay');
document.getElementById('${galleryId}_img').src = src;
o.style.display = 'flex';
});
</script>`;
}
return {
html: `<section${sectionStyle ? ` style="${sectionStyle}"` : ''}>
<div style="max-width:1100px;margin:0 auto;display:grid;grid-template-columns:repeat(${columns},1fr);gap:${gap}">
<div${gridIdAttr} style="max-width:1100px;margin:0 auto;display:grid;grid-template-columns:repeat(${columns},1fr);gap:${gap}">
${items}
</div>${lightboxHtml}
</section>`,