AESChat, an encrypted browser chat ported with AI agents, then audited
Personal project · 2026 · online
A personal browser chat encrypted with AES-256-GCM, ported from my C server with AI coding agents, then audited: 13 findings published, an XSS fixed.
In short
- What it is
- A static web app in which two browsers chat directly over WebRTC. Each message is encrypted with AES-256-GCM through the Web Crypto API, under a key generated in the browser and carried in the invite link.
- Why
- To bring the C socket chat online without running a server, and to show, through a published self-audit, that AI coding agents can introduce security bugs into a program when they aren't guided carefully.
- My role
- Personal project. I ported my C chat to the web with AI coding agents, then published a security self-audit of the result.
- Domains and tech
- DevSecOps, Software engineering. JavaScript (ES modules, no build step), Web Crypto API, WebRTC through PeerJS, Google STUN, Cloudflare Workers Static Assets.
- Status
- Personal project: live; educational, not audited messaging; two high findings open. Live at aeschat.benlafqih.com, its HTML pages and main scripts match the repository's current commit (checked on 19 October 2026). Self-audit: 13 findings, the two high ones still open (no Subresource Integrity, no CSP); 3 issues fixed: a DOM-XSS, overstated copy, and an overflow in the C original.
- Period
- Apr 2026 – present
Context and problem
AESChat started from my C socket server, whose last stage is a chat encrypted with AES-256-GCM. That program needs a machine running the server. The README sets the goal for the web version: no server to keep running, only static files and a peer-to-peer connection between the two browsers.
I started the port around April 2026, paused, and came back to it; the public history begins on 17 October 2026. The port was done with AI coding agents, and it introduced security bugs that weren’t in the C version. That is the point of the project’s own security page: an agent that isn’t guided carefully can make a program less safe, and asking it to “review my app for security” doesn’t fix that.
README.md, lines 15–21; security.html, introductionArchitecture
The host’s browser generates a 256-bit AES-GCM key, exports it as URL-safe Base64 and writes it into the page’s fragment, #room=…&key=…. The fragment is the part of a URL that browsers don’t send to servers. The guest opens the link and imports the key from it.
The connection goes through PeerJS: the host registers as aeschat-<room>-host, and three Google STUN servers help each browser find a route to the other. From then on, packets go over a WebRTC DataChannel. Every packet is also posted on a BroadcastChannel, which lets two tabs of the same browser talk with no network, for local testing.
The site is static files on Cloudflare Workers Static Assets, with a _headers file that sets four security headers.
Implementation
Encryption
crypto.js uses the browser’s Web Crypto API only, with no library. Each message gets a fresh 12-byte IV from getRandomValues, and the 16-byte tag is kept separate so the packet inspector can show it. The parameters match the C version’s crypto.h.
async encrypt(plaintext, key) {
// 12-byte random IV as per NIST SP 800-38D recommendations
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encoder = new TextEncoder();
const data = encoder.encode(plaintext);
// Web Crypto API encrypt returns ciphertext concatenated with 16-byte tag
const cipherResult = await window.crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: iv,
tagLength: 128, // 16 bytes
},
key,
data
);
Decryption rebuilds ciphertext plus tag and lets crypto.subtle.decrypt check the tag; a modified packet or the wrong key throws instead of returning text.
Safety number
Both sides show a safety number: a number derived from the SHA-256 of the key, shown as four groups of five digits, to compare by another channel. Because it comes from the shared key and not from identity keys, it confirms that both browsers hold the same key. It doesn’t say who the other person is.
How an AI agent put an XSS in it
The bug was in the packet inspector, the panel that shows each packet’s IV, ciphertext and tag. The ported version built its rows with innerHTML, inserting the packet fields as they arrived. For an inbound packet, those fields come from the other peer, so a crafted IV or tag could inject markup and run script in the other person’s page. In an encryption app that is the worst case: script running in the page can read the key and every message.
The fix rebuilds each row with createElement and textContent, and drops any field that isn’t a string, so peer data is never parsed as HTML:
// Inbound packets come straight from the remote peer, so their fields are
// untrusted. Build the entry with textContent (never innerHTML) so a crafted
// IV/ciphertext/tag can't inject markup or script into this page.
const str = (v) => (typeof v === 'string' ? v : '');
The vulnerable version isn’t in the public history: the first commit with the app code already has the fix. To show the bug anyway, the repository has a demo page that runs the old innerHTML code next to the fixed one, on a payload the visitor types in. The same review found a stack buffer overflow in the C original, fixed there.
Security considerations
The security page starts from a threat model. What it protects: message plaintext, the room key, and who talks to whom. Who it considers: a network observer, the signalling server, STUN, a curious peer, anyone who gets the link, someone with the device, and a compromised CDN or host. Its core assumption: the link is the secret, so most findings are about whether the page you loaded is the real one.
| ID | Finding | Severity | Status |
|---|---|---|---|
| H1 | CDN scripts (PeerJS, qrcodejs) loaded without Subresource Integrity | High | Open |
| H2 | No Content-Security-Policy | High | Open |
| M1 | Predictable host peer ID: anyone with the link can take the host’s connection slot | Medium | Planned |
| M2 | No forward secrecy: one key per room, for the whole session | Medium | By design |
| M3 | Key exposed on the device: address bar, history, QR code, clipboard | Medium | Planned |
| M4 | WebRTC reveals each side’s IP address to the other | Medium | By design |
| M5 | Nothing prompts users to compare the safety number | Medium | Planned |
| L1–L6 | Hardening: local channel left on, no input limits, cosmetic bot check, repository files served, bad links stall silently, peer-chosen message IDs | Low | Planned or by design |
| F1 | DOM-XSS in the packet inspector | High | Fixed |
| F2 | Overstated claims in the app copy | Low | Fixed |
| F3 | Stack buffer overflow in the C original | High | Fixed |
The page also records a sweep of every injection sink: innerHTML is only used to clear elements or write fixed strings, and there is no eval, new Function, document.write or insertAdjacentHTML. With F1 fixed, the main way to run foreign code in the page is H1, made worse by H2.
Testing and validation
| Check | Result |
|---|---|
Files served by aeschat.benlafqih.com against commit 8219c3e (the three HTML pages, app.js, crypto.js and p2p.js) | Identical |
| Response headers of the live site | X-Frame-Options: DENY, nosniff, Referrer-Policy: no-referrer, Permissions-Policy; no CSP, as H2 says |
wrangler.toml on the live site | Served, as L4 says |
13 tests of crypto.js in Node 22: key size, export and import, round trips (empty, Unicode, 10,000 characters), packet layout, 1,000 unique IVs, modified ciphertext, tag and IV, truncated tag, wrong key, safety number | 13 of 13 pass |
The crypto tests were written for this write-up and aren’t in the repository yet. A chat between a laptop and a phone over the live deployment worked on 17 October 2026 (my test); the browser-to-browser connection hasn’t been tested by any script.
Limitations
- It isn’t secure messaging. The README says so: an educational project, not an audited product. The two high findings are open.
- The link is the key. Anyone who sees the link, in a chat log, a screenshot or the browser history, can read the conversation, and with no forward secrecy that includes traffic captured earlier.
- The signalling and STUN servers see metadata: IP addresses and peer IDs.
- The README is behind the audit. It still says “$0.00 Forever”, describes a Cloudflare Pages deployment instead of Workers, and calls the session key ephemeral, though the key stays in the browser history.
Possible next steps
Pin the two CDN scripts with integrity hashes, or host them in the repository (H1). Add a strict Content-Security-Policy and test it against the live WebRTC flow (H2). Then replace the key in the link with a key agreed between the two browsers through an ECDH exchange over the DataChannel, which would also give each session its own key.