← Back to the audit Benlafqih

Live demo: the XSS the AI port introduced (F1)

This page intentionally runs the vulnerable inspector code next to the patched one so you can fire a real cross-site-scripting payload and watch the difference. It's a self-contained sandbox — no chat, no network, no peers — so nothing you do here can reach anyone. The live app at index.html only ships the patched version.

⚠️ Deliberately vulnerable code lives on this page only. Don't copy the left-hand pattern into anything real.

Try it

In AESChat, the wire inspector logged every incoming packet by dropping its fields into the DOM. The iv field comes straight from the remote peer. Below is what a malicious peer could put in that field. Hit Render packet and watch the two panes.

Vulnerable — uses innerHTML

The original ported code. The field is parsed as HTML.
No packet yet.

Patched — uses textContent

The fix. The field is shown as inert text.
No packet yet.

How you'd actually attack it

In the real app the inspector renders on every inbound packet, even with the panel closed. So the attack needs no clicks from the victim:

  1. Get into a room with the target — trivial here, since anyone with the invite link is a valid peer.
  2. Instead of a normal encrypted message, send a packet whose iv (or ciphertext/tag) is a payload like the one above.
  3. On the victim's side, logPacket builds the row with innerHTML, the browser parses your <img>, the bogus src fails, and onerror runs your JavaScript in their page.

From there, "end-to-end encrypted" is meaningless: your code is already inside the page, so it can read the AES key out of memory, grab every decrypted message, change what the victim sees, or quietly send messages as them. A realistic payload wouldn't reveal a banner — it would exfiltrate the key and stay silent.

Why a coding agent produced this

Building a row with a template string and innerHTML is the shortest path to working UI, and it looks completely fine in a demo: you type a normal message, the inspector shows it, done. The trap is the data source — those fields arrive from an untrusted peer, not from you. Nothing in the happy path reveals it, and "please review my app for security" won't either unless someone traces where each value actually comes from.

The fix (what the patched pane does)

// Vulnerable: peer data parsed as HTML
entry.innerHTML = `IV: <span class="packet-val">${packet.iv}</span>`;

// Patched: peer data can only ever be text
const val = document.createElement('span');
val.className = 'packet-val';
val.textContent = packet.iv;   // never parsed as markup

The rule: untrusted data goes in with textContent / createElement, never interpolated into innerHTML. A Content-Security-Policy (finding H2) would have blocked the inline onerror too — defence in depth.

Educational demo for the AESChat security self-audit. The vulnerable code is confined to this page; the deployed app is patched. By Ahmed Benlafqih.