← Back to AESChat Benlafqih

Security Self-Audit

A straightforward look at my messaging app's weak points. It began as a C program meant to run locally on Linux: two machines connected over the network and exchanged messages, a bit like a tiny email service. Using a couple of coding agents, I ported it to the web as AESChat, and the port silently introduced real security bugs that weren't in the original. That's the whole point of this page: it shows that coding agents, if not guided carefully (and no, a "please review my app for security" won't cut it), can introduce serious vulnerabilities into a program that used to be sound. I left those bugs in while I audited them, documented every finding below, and reproduced the worst one in a live sandboxed demo. The critical bugs (F1 to F3) are now fixed (F1 and F2 in this web app, F3 in the C original), so the deployed app runs the patched code, while the remaining findings are tracked as open or accepted by design. Personal project, clearly educational. Snapshot: 17 Oct 2026.

▶ Live attack demo: fire the real XSS at the vulnerable code, next to the patched version. Self-contained sandbox — no chat, no network. The deployed app is patched.

Threat model

What we want to protect: message plaintext, the 256-bit room key, and who is talking to whom (metadata).

Who we are getting protected from: a network observer, the PeerJS signalling server, Google STUN, a malicious or curious peer (anyone with the link), anyone who obtains the invite link, someone with access to the device, a compromised CDN or page host.

Our core design: the key lives in the invite link. Whoever has the link can read the chat. So confidentiality comes down to two questions: Was the link shared safely? and is the page you loaded the real page? Most findings below are about that second question, because if attacker code runs in the page, the encryption protects nothing.

Legend

High can expose keys or plaintext Medium disruption, privacy, weakened defence Low hardening / hygiene
Open not addressed Planned accepted, fix scoped By design accepted limitation Fixed resolved

High severity

H1No Subresource Integrity on CDN scriptsHighOpen

The page loads PeerJS (unpkg) and qrcodejs (cdnjs) with no integrity hash. For a crypto app this is the biggest hole, if either CDN is compromised or tampered with in transit, its JavaScript runs inside the page with full access to crypto.subtle, the key, and every message ,at which point the encryption is irrelevant.

Fix: pin each script with an integrity="sha384-…" hash and crossorigin="anonymous", or self-host both files in the repo and drop the third parties entirely.
H2No Content-Security-PolicyHighOpen

The _headers file sets framing, MIME-sniffing, referrer and permissions protections, but no CSP ,the one header that turns "an XSS or a bad CDN" from game-over into blocked.

Fix: add a strict policy, e.g. default-src 'none' with script-src limited to self plus the two CDNs (ideally by hash), and a connect-src that still allows PeerJS and STUN. Must be tested against the live WebRTC flow.

Medium severity

M1Predictable peer ID & connection takeoverMediumPlanned

The host registers as aeschat-<roomId>-host, and the room ID is part of the link. When a new connection arrives, the host closes the current one and adopts the new peer. So anyone holding the link can connect to the host slot and kick the legitimate guest, or churn the connection ,a transport-level denial-of-service. They can't decrypt without the key, but they can disrupt and will see the handshake alias.

Fix: pin to the first peer, reject later connection attempts, and treat the peer as unverified until the safety number matches.
M2No forward secrecy (single long-lived key)MediumBy design

One random key per room, reused for the whole session and embedded in the link. If the key leaks later ,from browser history, a screenshot, the clipboard ,all previously captured ciphertext becomes decryptable. There is no ratchet or key rotation.

Fix (out of scope for now): perform an ECDH handshake over the DataChannel to derive a per-session key that never travels in the link, and re-key periodically. Documented here as a known limitation.
M3Key exposure on the local deviceMediumPlanned

The key sits in the URL fragment, so it also lands in the address bar, the browser history (each new room adds an entry), the QR code, and the clipboard on "Copy". None of this crosses the network, but anyone with the device ,or another app reading the clipboard ,can recover it.

Fix: strip the key from the visible URL with history.replaceState once connected; make clear in the UI that the link itself is the secret.
M4WebRTC exposes your IP to the peerMediumBy design

Inherent to WebRTC: the ICE candidate exchange reveals your local and public IP to whoever is on the other end, and to the signalling server. On this app, "the other end" is whoever has the link.

Mitigation: route through a TURN relay (iceTransportPolicy: 'relay') to hide IPs ,at the cost of running and paying for TURN. Stated as a privacy fact rather than fixed.
M5Man-in-the-middle check is manual and un-nudgedMediumPlanned

The only defence against talking to the wrong peer is the safety number, compared out of band ,and nothing in the UI prompts users to compare it. If the link is intercepted before first contact, an attacker could be the peer and neither side would notice.

Fix: surface the safety number prominently on connect and ask both sides to confirm it matches before the composer unlocks.

Low severity

L1BroadcastChannel echoes packets to same-origin tabsLowPlanned

Every packet is also posted to a same-origin BroadcastChannel, even during a live WebRTC connection. Any other tab or script on the same origin in the same browser can observe (ciphertext) or inject frames. Low risk ,same user, same origin ,but an unnecessary surface.

Fix: use the local channel only in the explicit offline/local-test mode, not alongside a live connection.
L2No input limits → UI denial of serviceLowPlanned

The peer's alias and message text aren't length- or rate-limited. A malicious peer can send a huge alias or flood messages to bloat or freeze the interface. Content is rendered with textContent, so this is not an injection bug ,only cosmetic/availability.

Fix: cap alias and message length and throttle inbound rendering.
L3"Bot verification" is cosmeticLowBy design

The human-check is a styled checkbox that ungates the button after a fake delay. It is not a CAPTCHA and stops no automation ,and it can't be real without a backend to validate a token. Kept intentionally as a joke, and never labelled "verification" in any claim about the app.

Decision: keep as a gag; do not present it as a security control.
L4Repo files served as public assetsLowPlanned

Because the whole repo root is published, wrangler.toml, README.md and LICENSE are publicly fetchable. Harmless here (nothing secret), but untidy.

Fix: move the web files under a public/ directory and point the assets config there.
L5Malformed invite link stalls silentlyLowPlanned

A crafted #key= that isn't valid base64 makes key import throw. The join handler awaits the session with no error handling, so a bad or corrupt link makes the UI stall silently instead of explaining what went wrong. Robustness, not a breach.

Fix: validate the room/key shape, wrap session start in try/catch, and show a clear "invalid or corrupt invite link" message.
L6Peer-controlled message idLowPlanned

An incoming message's id is taken from the peer and used as a DOM element id. It is never evaluated (not an injection), but an odd or colliding id could confuse later lookups. Harmless today.

Fix: regenerate or sanitize the id locally instead of trusting the peer's.

Injection surface (reviewed)

Every classic injection sink was checked: innerHTML (only clearing or static strings), and no eval, new Function, document.write, string-based timers or insertAdjacentHTML. All peer- and user-controlled data (messages, aliases, packet fields, status) is written with textContent, so it can't be parsed as HTML; the QR code receives a self-generated URL. After the inspector fix (F1), there is no classic DOM-XSS or JS-injection path. The dominant remaining "inject code into the page" risk is therefore H1 (third-party scripts with no integrity check) backed by the absence of H2 (CSP) ,which is exactly why those two sit at the top of the list.

Found & fixed in this review

F1DOM-XSS in the packet inspectorHighFixed

The wire inspector built its rows with innerHTML, interpolating packet fields that, for inbound packets, come straight from the remote peer. A crafted iv/ciphertext/tag could inject markup and run script in the other person's page. Rebuilt to construct nodes with textContent, so peer-controlled data can never be parsed as HTML.

▶ Try the live demo — fire the real payload at the vulnerable code next to the patched version, and see how you'd attack it. (Sandboxed to that page; the deployed app is patched.)

F2Overstated security claimsLowFixed

"Zero-knowledge", "never touches a server" and "$0 forever" were corrected across the README and the in-app copy to the honest model: peer-to-peer messages, but a signalling server and STUN for setup, and "anyone with the link can read it".

F3Stack buffer overflow (C predecessor)HighFixed

In the original C socket chat this app grew from, decrypt_msg wrote recovered plaintext into an undersized stack buffer. Fixed with length-bounded crypto buffers. Tracked in that project's own audit.

What holds up

Priority order

Self-audit of an educational project by Ahmed Benlafqih. This is a living document; open findings are tracked, not hidden. Snapshot 17 Oct 2026.