Ahmed Benlafqih

All projects

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.

Try it onlineSource code

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, introduction

Architecture

How AESChat connects two browsersBrowser A creates the room and generates a 256-bit AES-GCM key with the Web Crypto API. The key goes into the invite link's fragment, after the # sign, which browsers don't send to servers; whoever has the link can read the chat. Browser B opens the link and imports the key. PeerJS signalling and Google STUN set up the connection: they see IP addresses and peer IDs, never the key. Messages then travel directly between the two browsers over a WebRTC DataChannel, each one encrypted with AES-256-GCM as an IV, a ciphertext and a tag.Browser Acreates the room,generates the keyBrowser Bopens the link,imports the keyWebRTC{ iv, ciphertext, tag }PeerJS signalling + Google STUNConnection setup only. Sees IPs and peer IDs,never the key.…/#room=…&key=…Invite link: the key rides after the #,which browsers don't send to servers.Whoever has the link can read the chat.Sent from A to B over another channel
Messages travel directly between the browsers. The signalling server and STUN only set up the connection. The key never leaves the invite link, which is why whoever has the link can read the chat.Drawn from js/app.js, js/p2p.js and js/crypto.js at commit 8219c3e

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.

js/crypto.js, lines 108–123

  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:

js/app.js, lines 226–229

  // 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.html, finding F1; vuln-demo.html; git show a75560a:js/app.js

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.

Findings of the self-audit, as published on 17 October 2026
IDFindingSeverityStatus
H1CDN scripts (PeerJS, qrcodejs) loaded without Subresource IntegrityHighOpen
H2No Content-Security-PolicyHighOpen
M1Predictable host peer ID: anyone with the link can take the host’s connection slotMediumPlanned
M2No forward secrecy: one key per room, for the whole sessionMediumBy design
M3Key exposed on the device: address bar, history, QR code, clipboardMediumPlanned
M4WebRTC reveals each side’s IP address to the otherMediumBy design
M5Nothing prompts users to compare the safety numberMediumPlanned
L1–L6Hardening: local channel left on, no input limits, cosmetic bot check, repository files served, bad links stall silently, peer-chosen message IDsLowPlanned or by design
F1DOM-XSS in the packet inspectorHighFixed
F2Overstated claims in the app copyLowFixed
F3Stack buffer overflow in the C originalHighFixed

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

Checks run for this write-up, 19 October 2026
CheckResult
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 siteX-Frame-Options: DENY, nosniff, Referrer-Policy: no-referrer, Permissions-Policy; no CSP, as H2 says
wrangler.toml on the live siteServed, 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 number13 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.