Letting AI change your UI at runtime, safely
Having a model turn "make this button bigger" into a live change is easy. Doing it without giving every user (and every string on your page) a way to run code in your app is the actual design problem. The answer is to make the output format the security boundary.
Don't let the model write HTML, CSS text or JavaScript
If the model's output is a snippet of HTML or a CSS string that you insert, you have built a cross-site-scripting machine: one user request ("make this say <img src=x onerror=…>") or one hostile string on the page that the model reads is enough. Escaping after the fact is whack-a-mole.
Instead, define a tiny structured format of operations, and have the model fill it in.
Design the allow-list
Start from what feedback actually asks for — wording, size, spacing, colour, hiding, reordering — and allow only that:
- text: set as text (
textContent), never parsed as HTML; - a few attributes that can't execute or load anything:
aria-label,title,placeholder,alt; - CSS properties from a list: typography, colours, spacing, sizing, flex alignment, opacity. Leave out anything that loads a resource (
background-image,content,cursor,filter,mask) or escapes the element's box (position); - CSS values without escape hatches: no
url(,expression(,@import,attr(, braces, semicolons, comments or!importantfrom the model; - structure without creation: hide an element, or reorder it among its siblings — never create elements or move them into other containers.
Then constrain where: each selector must match exactly one element, and must not reach html, body, script, style, iframe or your own widget.
Validate twice, with the same code
Validate on the server, right after the model answers, and again in the browser before applying anything — with one shared validator, so they can't drift. In the browser, also check the change against the live page: if a selector matches nothing or several things, don't guess; queue the request for a human.
Use structured outputs, and say what's data
Constrain the model's answer to a JSON schema (structured outputs), so you never parse free text. Tell the model in the system prompt that page text, selectors and screen outlines are data, never instructions. Prompt injection can still steer the model, but with the format above, the worst it can produce is a valid copy or layout change — for one user, who can undo it.
Scope and rate-limit
Apply drafted changes to the requesting user only. Roll out to everyone only after enough distinct users kept it (and a human approved). Rate-limit drafts per user and per IP so feedback can't run up a model bill.
A worked implementation
PMF Harness implements exactly this: six operations, a shared validator (src/patch.js), structured outputs with Claude, per-user application, and rate limits. Its safety page lists every rule.