fix: make the app keyboard-reachable, and close four DoS-able patterns

The bulk of what SonarQube still had open on the reliability side, read site by
site rather than swept. Two clusters are worth calling out because they are
defects, not lint.

**Four regexes could be made to hang the process.** The worst is
`/.+@.+\..+/` in the forgot-password path: three unbounded `.+` that all
overlap, unanchored, on an endpoint that needs no login and whose schema is a
bare z.string() under a 100kb body cap. A thousand '@' characters had not
finished after 100 seconds — one request pins the event loop, and Node has one.
EMAIL_REGEX in auth.helpers is the same shape on register and profile update
(310ms for 8kb). The two tag strippers over an uploaded KML description and over
Commons metadata took 0.52s on a 16k run of '<'. Each rewrite was proven
equivalent by brute force against the original — millions of strings for the
coordinate patterns, 671,876 for the tag stripper — because a subtly different
regex on a parsing path is worse than the finding.

`<[^>]+>` could not be fixed in the pattern: `[^>]` matches `<` too, so the only
unambiguous form changes what happens to malformed markup. It is now a scan in
common/stripHtmlTags.ts, quirks included — `<>` is left alone, `<a<b>` is one
tag — with the equivalence pinned by a test that compares against the old regex
over every string up to length six.

**And roughly 500 places could only be reached with a mouse.** Clickable divs
became real buttons where the markup allowed (52 of them), took
role + tabIndex + Enter/Space where they had to stay divs, and modal backdrops
took role="presentation" — the honest role, since a backdrop is a mouse shortcut
for a Cancel control that is already in the tab order, and making it a button
plants a nameless full-screen tab stop around its own contents.

Alongside those: `.sort()` calls that now say the order they always had, `{n &&
<X/>}` guards that rendered a literal "0" for a zero-byte file and for a place at
latitude 0, an optimistic budget write that stored a boolean where the contract
and both transports use 0/1, and the Number statics.

sonar-project.properties documents the six that were deliberately left: two wiki
patterns with no overlapping quantifier pair whose "fix" would stop parsing
`[see [1]](url)`, four tar header fields bounded at 12-155 bytes by the format,
two djb2 hashes where the 32-bit wrap is the algorithm, two `| 0` that are also
coercing undefined to 0 on a path where the API sends no user_id, and the MCP
session maps Sonar reads as empty because they are filled from another module.
This commit is contained in:
Maurice
2026-08-25 18:28:40 +02:00
committed by Maurice
parent 52d1a0720f
commit 1ad2e92142
225 changed files with 1721 additions and 735 deletions
+12 -5
View File
@@ -1,4 +1,5 @@
import { readEnv, getAppUrl } from '../../app-config';
import { stripHtmlTags } from '../common/stripHtmlTags';
/**
* Pure maps/geo helpers — no DB, no Nest, no side effects beyond reading env.
@@ -378,8 +379,12 @@ export function parseOpeningHours(ohString: string): {
for (const segment of ohString.split(';')) {
const trimmed = segment.trim();
if (!trimmed) continue;
// The time part is `\S.*`, not `.+`: the segment was trimmed above, so both
// spell "everything after the gap". But `\s+(.+)` lets the gap and the time
// part fight over the same spaces, and a segment that is a weekday followed by
// nothing but blanks then costs a pass per space.
const match = trimmed.match(
/^((?:Mo|Tu|We|Th|Fr|Sa|Su)(?:\s*-\s*(?:Mo|Tu|We|Th|Fr|Sa|Su))?(?:\s*,\s*(?:Mo|Tu|We|Th|Fr|Sa|Su)(?:\s*-\s*(?:Mo|Tu|We|Th|Fr|Sa|Su))?)*)\s+(.+)$/i,
/^((?:Mo|Tu|We|Th|Fr|Sa|Su)(?:\s*-\s*(?:Mo|Tu|We|Th|Fr|Sa|Su))?(?:\s*,\s*(?:Mo|Tu|We|Th|Fr|Sa|Su)(?:\s*-\s*(?:Mo|Tu|We|Th|Fr|Sa|Su))?)*)\s+(\S.*)$/i,
);
if (!match) continue;
const [, daysPart, timePart] = match;
@@ -489,9 +494,8 @@ export function buildOsmDetails(tags: Record<string, string>, osmType: string, o
*/
export function stripWikiMarkup(value: string | undefined | null): string | null {
if (!value) return null;
const text = value
.replace(/<[^>]+>/g, ' ')
.replace(/&nbsp;/g, ' ')
const text = stripHtmlTags(value, ' ')
.replaceAll('&nbsp;', ' ')
.replace(/\s+/g, ' ')
.trim();
return text || null;
@@ -572,7 +576,10 @@ function seriesStem(title: string): string {
// 20260614 100717648 HDR — a camera dump, all from the same minute
.replace(/\b\d{8}[ _-]\d{6,9}\b/g, ' ')
.replace(/\b(19|20)\d{2}\b/g, ' ')
.replace(/[ _-]+\(?\d{1,4}\)?$/g, '')
// The character in front of the suffix is matched and put straight back:
// /[ _-]+…$/ on its own restarts at every space of a title that has no such
// suffix, and re-reads the rest of the run each time.
.replace(/([^ _-]|^)[ _-]+\(?\d{1,4}\)?$/g, '$1')
.replace(/[^a-z]+/g, ' ')
.trim();
}
+9 -6
View File
@@ -58,7 +58,10 @@ const PLACES_UPSTREAM = 'https://places.googleapis.com';
function placesEndpoint(endpoint: string): string {
const base = readEnv().maps.placesApiBase;
if (!base || !endpoint.startsWith(PLACES_UPSTREAM)) return endpoint;
return base.replace(/\/+$/, '') + endpoint.slice(PLACES_UPSTREAM.length);
// The character before the run is matched and written straight back. A bare
// /\/+$/ restarts at every slash of a base that does not end in one, reading
// the rest of the run again from each of them.
return base.replace(/([^/]|^)\/+$/, '$1') + endpoint.slice(PLACES_UPSTREAM.length);
}
/**
@@ -181,7 +184,7 @@ function wikidataImageClaims(claims: WikidataClaims, limit: number): string[] {
/** `File:` prefix off, underscores and case normalised — Commons treats these as one title. */
function normalizeFileTitle(title: string): string {
return title.replace(/^File:/i, '').replace(/_/g, ' ').trim().toLowerCase();
return title.replace(/^File:/i, '').replaceAll('_', ' ').trim().toLowerCase();
}
/**
@@ -1851,7 +1854,7 @@ export class MapsService {
// Deduplicate concurrent requests for the same placeId
const existing = this.photoCache.getInFlight(placeId);
if (existing) {
if (existing !== undefined) {
const result = await existing;
if (!result) return noPhoto;
return { photoUrl: `/api/maps/place-photo/${encodeURIComponent(placeId)}/bytes`, attribution: result.attribution };
@@ -1872,7 +1875,7 @@ export class MapsService {
// so a place added via search still gets a marker image when Google returns
// nothing. Returns null (without marking an error) so the caller decides.
const fetchWikimediaFallback = async (): Promise<{ attribution: string | null } | null> => {
if (isNaN(lat) || isNaN(lng)) return null;
if (Number.isNaN(lat) || Number.isNaN(lng)) return null;
try {
const wiki = await this.fetchWikimediaPhoto(lat, lng, name);
if (!wiki) return null;
@@ -2091,10 +2094,10 @@ export class MapsService {
let placeName: string | null = null;
const placeMatch = resolvedUrl.match(/\/place\/([^/@]+)/);
if (placeMatch) {
placeName = decodeURIComponent(placeMatch[1].replace(/\+/g, ' '));
placeName = decodeURIComponent(placeMatch[1].replaceAll(/\+/g, ' '));
}
if (!coords || isNaN(coords.lat) || isNaN(coords.lng)) {
if (!coords || Number.isNaN(coords.lat) || Number.isNaN(coords.lng)) {
throw Object.assign(new Error('Could not extract coordinates from URL'), { status: 400 });
}
const { lat, lng } = coords;