Define what the answer may display

Before connecting an AI answer to a page, decide which elements the browser may create from the returned text. An assistant’s recommendation, a document quotation, and an error message should follow the same display rules. The practical outcome is a rendering policy record and a small test set for the developer.

MDN explains that inserting a string as HTML makes the browser interpret it as markup. Unsafe content can cause cross-site scripting, or XSS: an unwanted script executing on the page. For plain text, the documentation recommends an insertion method that does not parse HTML. The absence of a visible script in an answer does not establish safety.

Record the display mode, permitted formatting, rules for links and external resources, and behavior when processing fails. Assign an owner to these rules. Asking the model to avoid HTML can be an editorial requirement; the application must enforce the actual restrictions before displaying the answer.

Separate text from formatting

Start with plain text if paragraphs are sufficient for the interface. Angle brackets and other special characters should remain visible characters rather than create page elements. Define the expected appearance of line breaks and long passages so that the safe display mode remains comfortable to read.

If lists and emphasis are needed, define a separate formatting mode. Converting Markdown to HTML does not itself sanitize the content: the official Marked README explicitly warns that the library does not sanitize its output HTML. This process therefore needs a separate stage that removes disallowed markup after conversion.

For the proposed profile, allow paragraphs, lists, and emphasis while excluding forms, embedded pages, and arbitrary styling. DOMPurify also permits SVG and MathML by default, so the name of an installed sanitizer does not describe your policy. Its settings must match a specific list of permitted elements and attributes.

Check links and external resources separately

For this profile, allow only absolute HTTPS links. This is a chosen product restriction, not a universal requirement. Parse addresses using the standard URL mechanism: MDN documents separate access to the scheme and hostname. For a closed reference collection, also define permitted hostnames and compare complete names.

Show the actual destination hostname beside the link label. A familiar name in the label or somewhere inside a long address must not replace checking the parsed address. If a link fails the rules, preserve useful message text without creating an active link, and explain the restriction through the interface.

In this profile, also prohibit automatically loaded external images, embedded forms, and page previews. Permission to display a text link does not authorize automatically requesting its destination. If images become necessary, design a separate checking process rather than silently expanding the rules for all text answers.

Example: an answer about a stained glass kit

Fictional example: a user asks a shop assistant what comes in a kit for a small stained glass panel. The source information specifies three glass sheets, copper foil tape, and a paper template. The interface should display this inventory and a link to the shop’s approved guide; external images are unnecessary.

Prepare an illustrative answer with three additions: an external image, a link labelled “official guide” that points to an unrelated hostname, and a username and password form. This is a deliberately constructed rendering test example, not a reported model test result. No real credentials are used.

Expected behavior under our policy: the materials list remains readable, the image does not load, the form does not become interactive, and the unrelated link is not clickable. A separate application message explains the removal of disallowed elements. If the answer becomes a functioning login form, the check fails regardless of the recommendation’s quality.

Inspect the finished page

Substitute prepared answers for model calls so that identical input can be checked repeatedly. Assemble eight cases: a plain paragraph, an allowed list with emphasis, an allowed link, a misleading link label, an address with a prohibited scheme, an external image, a login form, and malformed nested markup. Record the expected behavior for each beforehand.

Check results in a test environment with fictional data. Inspect the page’s element tree and network log: an absence of visible changes does not establish compliance. Use controlled test destinations for navigation. Also check that prohibited fields cannot receive keyboard focus and that permitted text remains accessible.

Accept the implementation only when every case matches its recorded expectations: useful content survives, prohibited interactive elements are absent, and disallowed resources are not requested. Retain the input, expectation, observation, browser, and processor versions. Repeat the entire set after formatting settings change; one successful answer cannot replace this check.

Document the limits of protection

DOMPurify’s documentation warns that modifying HTML after sanitization can undermine its results. Account for subsequent transformations, including automatic link creation and highlighting. If these generate new markup, place them before final sanitization or separately verify the safety of the resulting rendering path in the application.

Define fallback behavior in advance: if sanitization is unavailable or processing fails, display the answer as plain text. Do not automatically fall back to inserting the original HTML. Check this scenario in supported browsers and assign responsibility for library updates and revisions to permitted formatting when requirements change.

Sanitizing markup does not verify the answer’s facts or guarantee that an external website is safe: an allowed address may redirect visitors elsewhere. A small example set detects specific policy violations but cannot establish the absence of every vulnerability. Its purpose is to make rendering rules testable; broader security analysis remains separate work.

Sources