{"success":true,"name":"render-user-content-as-text-never-as-html-close-the-xss-door","description":"Displaying user-typed content (names, comments, bios) on any page","category":"ai-brimind","path":"ai-brimind/render-user-content-as-text-never-as-html-close-the-xss-door/SKILL.md","file":"SKILL.md","content":"---\nname: render-user-content-as-text-never-as-html-close-the-xss-door\ndescription: \"Displaying user-typed content (names, comments, bios) on any page\"\n---\n\n# Render User Content as Text, Never as HTML: Close the XSS Door\n\n## When to use\nYou are writing frontend code that displays content a user typed (a name, comment, message, bio, review), and that content is about to be inserted as HTML — via dangerouslySetInnerHTML, innerHTML, or server-side string templating — instead of as plain text.\n\n## Example request\nA comment containing <script>...</script> is rendered with innerHTML → the script runs in every visitor's browser, can steal their session, and acts as them; this is XSS (cross-site scripting), the classic web attack. Content rendered normally in React/JSX as {comment.text} → React escapes it automatically and the script shows as harmless text; the framework's default IS the protection, danger begins where you opt out. dangerouslySetInnerHTML used to show rich text from users → the name literally warns you; if rich text is truly needed, a sanitizer library (DOMPurify) filtering to safe tags is the named solution, not raw trust. Attacker-typed content stored fine in the DB → storage is not the crime scene; the injection fires at RENDER time, in the victim's browser — which is why the display layer owns this defense.\n\n## Instructions\nAct as a frontend developer who assumes at least one user will eventually type <script> or an onerror-loaded image tag into every text field you display — because automated tools do exactly that against every form on the internet, and the victim of the resulting script is not you, it is every other user who views the page. For any spot where user-originated content reaches the screen, do this: (1) Point at the exact render line and classify it: framework default text rendering (JSX {value}, standard template binding — SAFE, the framework escapes it), or raw HTML insertion (dangerouslySetInnerHTML, innerHTML, hand-concatenated HTML strings — DANGER lane). Everything in the danger lane must justify itself now. (2) For the overwhelmingly common case — the content is just text (names, comments, titles) — use the safe default and you are done; never reach for innerHTML because styling felt easier, styling is CSS's job on an element whose TEXT is the user content. (3) If the feature genuinely requires user-authored rich content (formatted posts, a WYSIWYG editor), name the need and use the solved tool: sanitize with a maintained library like DOMPurify configured to an allow-list of tags you actually want (bold, lists, links) — never a hand-written regex strip, which is the classic broken wheel here. Sanitize at render time or at the border, but KNOW which, and be consistent. (4) Extend the same suspicion to sneaky sinks: user content placed into href (a link of javascript:... executes), into style, or into attributes — validate URLs are http(s) before rendering them as links. (5) Test it like an attacker for thirty seconds: save a record whose text is <img src=x onerror=alert(1)> and view the page — an alert means the door is open; the literal text on screen means it is closed. Only move on once every render of user content either uses the framework's escaping default or passes through a named sanitizer with an allow-list — and you have watched your test payload appear as harmless text with your own eyes.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #212\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}