Designing Output Escaping for AI-Generated Reports
Learn how to display AI-generated report text safely in HTML, with context-aware escaping and a test for literal characters.
Treat AI-generated report text as untrusted input when a browser displays it. Place plain text into a text node or use an HTML template that escapes variable values by default. Decide separately how to handle links, attributes, or rich text because each changes where the value is interpreted. OWASP documents the distinction between those output contexts OWASP cross-site scripting prevention guidance.
Keep the report format and the report text separate
Imagine an internal report template with a fixed heading and a paragraph named summary. The model is allowed to supply the summary’s words. The application owns the heading, the paragraph element, and where the paragraph appears. Pass the model output as a value to the template instead of building an HTML fragment by joining strings.
In a browser interface, a safe pattern for a plain summary is to set the existing element’s textContent. Avoid placing untrusted output in innerHTML to preserve formatting. If the report needs bold labels, the template can render its own known labels beside the model’s text.
Test with a harmless string that could be parsed as markup
For a synthetic fixture, have the model response contain A < B & B > C and the literal text <em>review</em>. The reader should see all characters, including both angle brackets around each tag name. The page should contain no additional emphasis element originating from that string.
Inspect the rendered report and its element tree. If the word “review” is italicized and the angle brackets are gone, the program interpreted the input as markup. If the screen displays the literal string < in place of the first less-than sign, investigate whether the application escaped it twice. Include both checks in the regression test.
Review each place that uses the same value
The same report may show the summary in an HTML page, an email preview, and a downloadable file. Follow the value to each destination. A safe HTML text node does not validate a destination URL, a query parameter, or a spreadsheet cell. Use a destination-specific check when the application reuses the value.
If the report truly needs rich formatting from an external source, decide which tags and attributes the application will allow and use a maintained sanitizer configured for that purpose. Test the sanitizer separately from plain-text escaping. Keep default escaping enabled for ordinary text fields.
Record the acceptance check
Have a reviewer open the actual report route using the synthetic input. Confirm that the text remains readable, that the template has the same element structure as a normal report, and that the generated value cannot add a link or control. Repeat the test for each rendering route that shares the report data. Keep the fixture with the template so a future formatting change runs through the same check.