Engineering note · AuraRisk
Fixing stored XSS in AuraRisk's map popups
- Project
- AuraRisk — Personal project
- Issue
- Part of #16 Secure community report creation endpoint, and #26 Implement Automated Tests and CI Pipelines
- My role
- Author of PR #45
- Stack
- TypeScript · React · Vite · MapLibre GL
- Status
- Open, in review
The problem
AuraRisk lets people submit community reports about flooding, each with a short free-text note. The web map shows these reports as markers, and each marker opens a popup.
The popup was built with MapLibre's Popup.setHTML(), with the user's note placed straight into the HTML string. That is a stored cross-site scripting bug. Whatever markup someone put in a note would be parsed as HTML by the browser of anyone who opened that popup, and markup can carry script.
Separately, npm audit flagged a critical advisory in the maplibre-gl version we used (4.7.1), GHSA-jrc7-96c5-q579, a sanitiser bypass. The mobile toolchain also had a critical advisory in shell-quote.
What I did
- Popups from DOM nodes. A small
reportPopup()function builds the popup withdocument.createElementand sets the category and note withtextContent. The popup is attached withsetDOMContent(). Text set this way is never parsed as markup. - Looked for the same pattern elsewhere. I searched web, mobile and shared code for
setHTML,innerHTML,dangerouslySetInnerHTMLand similar. I found no other raw-HTML insertion. - maplibre-gl 4.7.1 to 6.13.0. This needed two code changes. v6 has no default export, so the import became
import * as maplibregl. And v6 finds its web worker relative to its own module file, which Vite moves, so the map drew no tiles. I import the worker with?urland register it withsetWorkerUrl(). - Non-breaking audit fixes in the web and mobile apps, including the
shell-quoteadvisory.
The core of the popup change is short:
const note = document.createElement("div");
note.textContent = r.note;
box.append(title, note);
new maplibregl.Popup({ offset: 25 }).setDOMContent(box);
How I checked it
The web build passes, and the production npm audit --omit=dev reports 0 vulnerabilities. Mobile tsc and jest pass.
For the popup, I ran headless Chromium against both the production build and the dev server. A fake API returned a report whose note contained HTML, including an image tag with an event handler. I checked that the map canvas rendered and tiles loaded, with no console errors, so the worker was loading. The marker rendered and its popup opened. The note appeared as literal text: no element was created from it and its script never ran.
Some advisories remain because they need major upgrades. The web app has 7, all in Tailwind 3 build and dev tooling. The mobile app has 58 in the Expo SDK 57, React Native, Metro and Jest toolchain, which needs an Expo SDK 58 upgrade. I listed these in the PR rather than leaving them implied.
What I learned
The bug was one template string. Anything a user typed should reach the page as text unless there is a clear reason to treat it as HTML, and a map library's popup is no exception.
The upgrade taught me to check behaviour, not just the build. The production build succeeded while leaving out the worker file entirely, and the only symptom was an empty map. One deploy note follows from this: the host must serve .mjs as text/javascript, or browsers will refuse the module worker. The PR is open and in review.