mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
fix: close the backtracking patterns SonarCloud's profile turns up
The move to SonarCloud brought a wider rule set with it, and 23 more regexes that can be made to hang on a crafted near-match. 21 are fixed here, each proven equivalent to the pattern it replaces by brute force rather than by reading. Two of them are reachable across users, not just by yourself. A reservation endpoint name is free text any trip member types, stored and then read back in every other member's browser while the map draws its labels, and the pattern over it takes 2.3 seconds on a 32k paste - so one member could freeze the map for the whole trip. The mobile transport sheet reads the same field. Both now scan instead of backtrack, and both were matched against the desktop twin the earlier sweep had already fixed rather than given a third spelling. The markdown link stripper needed the same treatment as the tag stripper before it: the label class cannot be disambiguated in the pattern, because it matches an opening bracket as well, and narrowing it changes what "[a[b](c)" produces. It is a scan now, with all four of the regex's quirks intact. Also here: the two encoding warnings the scanner reported. A decorative comment line in JourneyDetailPage.test.tsx had picked up a replacement character somewhere along the way, and calendar.service.test.ts asserts on one deliberately - that one is now written as a U+FFFD escape, so the file no longer carries the raw bytes while the test says exactly what it said before. Seven findings were deliberately not changed and are flagged in SonarCloud with the reasoning. Five are false positives - patterns with no overlapping quantifier pair, where the only cure would change how nested brackets parse. Two are marked accepted rather than dismissed, because they are real: the readme check's heading pattern has a proven linear replacement that cannot land until the registry's copy of the same script can move with it, and `export let JWT_SECRET` is what makes an admin's key rotation take effect in-process instead of silently waiting for a restart.
This commit is contained in:
@@ -119,14 +119,47 @@ export default function MAdminGitHubPanel({ isPrerelease = false }: { isPrerelea
|
||||
}
|
||||
|
||||
const escapeHtml = (str: string) => str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
|
||||
// `[label](url)` → anchor, exactly as /\[([^\]]+)\]\(([^)]+)\)/g did. Written as a
|
||||
// scan because that pattern backtracks from both ends: `[^\]]` matches `[` and
|
||||
// `[^)]` matches `(`, so a run of either made the engine retry the whole match from
|
||||
// every one of them, reading to the end each time. Release bodies come off the
|
||||
// GitHub API. Both parts are one-or-more, so `[](url)` and `[label]()` stay plain
|
||||
// text; the label ends at the first `]`, the url at the first `)`.
|
||||
const linkify = (text: string) => {
|
||||
let out = ''
|
||||
let cursor = 0
|
||||
let search = 0
|
||||
for (;;) {
|
||||
const open = text.indexOf('[', search)
|
||||
if (open === -1) break
|
||||
const close = text.indexOf(']', open + 1)
|
||||
if (close === -1) break // no `]` left: no later `[` can match either
|
||||
// Every `[` up to `close` shares that first `]`, so they fail together.
|
||||
if (close === open + 1 || text[close + 1] !== '(') {
|
||||
search = close + 1
|
||||
continue
|
||||
}
|
||||
const end = text.indexOf(')', close + 2)
|
||||
if (end === -1) break // no `)` left: no later `(` can close either
|
||||
if (end === close + 2) {
|
||||
search = close + 1
|
||||
continue
|
||||
}
|
||||
const label = text.slice(open + 1, close)
|
||||
const url = text.slice(close + 2, end)
|
||||
const safeUrl = url.startsWith('http://') || url.startsWith('https://') ? url : '#'
|
||||
out += `${text.slice(cursor, open)}<a href="${escapeHtml(safeUrl)}" target="_blank" rel="noopener noreferrer" style="color:var(--m-st-info);text-decoration:underline">${label}</a>`
|
||||
cursor = end + 1
|
||||
search = end + 1
|
||||
}
|
||||
return out + text.slice(cursor)
|
||||
}
|
||||
const inlineFormat = (text: string) => {
|
||||
return escapeHtml(text)
|
||||
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
|
||||
.replace(/`(.+?)`/g, '<code style="font-size:11px;padding:1px 4px;border-radius:4px;background:var(--m-ic)">$1</code>')
|
||||
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_, label, url) => {
|
||||
const safeUrl = url.startsWith('http://') || url.startsWith('https://') ? url : '#'
|
||||
return `<a href="${escapeHtml(safeUrl)}" target="_blank" rel="noopener noreferrer" style="color:var(--m-st-info);text-decoration:underline">${label}</a>`
|
||||
})
|
||||
return linkify(
|
||||
escapeHtml(text)
|
||||
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
|
||||
.replace(/`(.+?)`/g, '<code style="font-size:11px;padding:1px 4px;border-radius:4px;background:var(--m-ic)">$1</code>')
|
||||
)
|
||||
}
|
||||
|
||||
for (const line of lines) {
|
||||
|
||||
@@ -315,7 +315,12 @@ export default function MJourneyEntrySheet({
|
||||
}
|
||||
|
||||
const addTag = () => {
|
||||
const value = tagInput.trim().replace(/,+$/, '')
|
||||
// Trailing commas dropped by a scan, not /,+$/: an unanchored ,+ before $ has to
|
||||
// retry from every comma in the run, so a pasted string of them freezes the tab.
|
||||
const trimmed = tagInput.trim()
|
||||
let end = trimmed.length
|
||||
while (end > 0 && trimmed[end - 1] === ',') end--
|
||||
const value = trimmed.slice(0, end)
|
||||
if (!value) return
|
||||
if (!tags.includes(value)) setTags(prev => [...prev, value])
|
||||
setTagInput('')
|
||||
|
||||
@@ -54,9 +54,16 @@ function endpointFromAirport(a: Airport, role: 'from' | 'to' | 'stop', sequence:
|
||||
function endpointFromLocation(l: LocationPoint, role: 'from' | 'to' | 'stop', sequence: number, date: string | null, time: string | null): Omit<ReservationEndpoint, 'id' | 'reservation_id'> {
|
||||
return { role, sequence, name: l.name, code: null, lat: l.lat, lng: l.lng, timezone: null, local_date: date, local_time: time }
|
||||
}
|
||||
// "Paris Charles de Gaulle (CDG)" → "Paris Charles de Gaulle", the same trim-plus-
|
||||
// anchored-test the desktop TransportModal uses instead of /\s*\([A-Z]{3}\)\s*$/,
|
||||
// because that leading \s* backtracks over every space in a long name.
|
||||
function stripAirportCode(name: string): string {
|
||||
const trimmed = name.trimEnd()
|
||||
return /\([A-Z]{3}\)$/.test(trimmed) ? trimmed.slice(0, -5).trimEnd() : name
|
||||
}
|
||||
function airportFromEndpoint(e: ReservationEndpoint | undefined): Airport | null {
|
||||
if (!e || !e.code) return null
|
||||
return { iata: e.code, icao: null, name: e.name, city: e.name.replace(/\s*\([A-Z]{3}\)\s*$/, ''), country: '', lat: e.lat, lng: e.lng, tz: e.timezone || '' }
|
||||
return { iata: e.code, icao: null, name: e.name, city: stripAirportCode(e.name), country: '', lat: e.lat, lng: e.lng, tz: e.timezone || '' }
|
||||
}
|
||||
function locationFromEndpoint(e: ReservationEndpoint | undefined): LocationPoint | null {
|
||||
if (!e) return null
|
||||
|
||||
Reference in New Issue
Block a user