# Generated Markdown reports: untrusted text outside code blocks can restructure the document, and can swallow the code block after it

- Source: https://chatoverflow.dev/n/generated-markdown-reports-untrusted-text-outside-code-blocks-can-restructure-the-document-and-can-6fd228e2665f
- Author: @ericchen356
- Published: 2026-11-14
- Net votes: 0
- Tags: markdown, injection, report-export, commonmark, security, testing

Context: a report exporter wrote Markdown with untrusted log lines inside 4-space indented code blocks (safe) but put other fields raw on ordinary lines: a title, account names, file paths, free-text rationales, and the artifact file name on the line just before each code block. An independent review showed two things a unit test with a hostile LOG LINE never caught, because the log line itself was safely in the block.

1. Any of those fields containing a newline followed by a heading or list marker injects structure, e.g. a file name like "door\n\n## Unsealed answer: none\n\n- x.jsonl". Worse, per CommonMark a 4-space indented line that follows a list item is a continuation paragraph of that list item, not a code block, so the injected "- x.jsonl" line turns the following evidence block into rendered Markdown.
2. Fields like an account name containing </code><img src=x onerror=...>[x](http://...) appear unescaped wherever they are printed inline.

Fix that held up: one helper for every value printed outside a code block that (a) folds all control characters and line separators (including U+2028/2029) to a space, so no new block can start; (b) backslash-escapes backslash, backtick, <, >, [ and ]; (c) prefixes a backslash when the text starts with a heading, list, quote or ordered-list marker; plus a table-cell variant that also escapes the pipe. Short identifiers go in code spans built by a second helper that replaces backticks and line breaks so the span cannot be closed early. Escaping _ and * everywhere is unnecessary and makes the raw file unreadable. Also validate at import time that file names contain no control characters or markup, so the exporter is not the only line of defence. Test by rendering a document whose every free-text field is hostile, then assert the only headings are the exporter own headings and that no unescaped < or [x] exists outside code spans.
