mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +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:
@@ -1688,8 +1688,8 @@ function VersionPickerDialog({ plugin, versions, failed, busy, t, locale, onPick
|
||||
t: T; locale: string; onPick: (version: string) => void; onClose: () => void
|
||||
}) {
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4" onClick={onClose}>
|
||||
<div className="bg-surface-card border border-edge rounded-2xl w-full max-w-md max-h-[80vh] overflow-auto shadow-modal" onClick={e => e.stopPropagation()}>
|
||||
<div role="presentation" className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4" onClick={onClose}>
|
||||
<div role="presentation" className="bg-surface-card border border-edge rounded-2xl w-full max-w-md max-h-[80vh] overflow-auto shadow-modal" onClick={e => e.stopPropagation()}>
|
||||
<div className="flex items-center justify-between px-4 py-3.5 border-b border-edge-secondary">
|
||||
<h3 className="text-[15px] font-semibold text-content truncate">{t('admin.plugins.versionPickerTitle', { name: plugin.name })}</h3>
|
||||
<button type="button" onClick={onClose} className="w-8 h-8 grid place-items-center rounded-lg text-content-faint hover:text-content hover:bg-surface-tertiary transition-colors shrink-0"><X size={16} /></button>
|
||||
|
||||
@@ -101,8 +101,8 @@ describe('DevNotificationsPanel', () => {
|
||||
render(<><ToastContainer /><DevNotificationsPanel /></>);
|
||||
await screen.findByText('Type Testing');
|
||||
|
||||
// Fire the click but do not await — handler never resolves so sending stays true
|
||||
void user.click(screen.getByText('Simple → Me').closest('button')!);
|
||||
// The request handler never resolves, so sending stays true after the click settles
|
||||
await user.click(screen.getByText('Simple → Me').closest('button')!);
|
||||
|
||||
await waitFor(() => {
|
||||
const buttons = screen.getAllByRole('button');
|
||||
|
||||
@@ -3,12 +3,13 @@
|
||||
* Handles: bold, italic, headings, links, images, blockquotes, code, lists, hr.
|
||||
*/
|
||||
export function stripMarkdown(md: string): string {
|
||||
return md
|
||||
const withoutImages = md
|
||||
.replace(/^#{1,6}\s+/gm, '') // headings
|
||||
// the alt text is a run that cannot contain "](", so it ends on the first one
|
||||
// instead of being retried against every later "](". Same matches, no backtracking
|
||||
.replace(/!\[(?:(?!\]\().)*\]\(.*?\)/g, '') // images
|
||||
.replace(/\[([^\]]*)\]\(.*?\)/g, '$1') // links → text
|
||||
|
||||
return stripLinks(withoutImages) // links → text
|
||||
.replace(/(`{3}[\s\S]*?`{3})/g, '') // code blocks
|
||||
.replace(/`([^`]+)`/g, '$1') // inline code
|
||||
.replace(/\*\*(.+?)\*\*/g, '$1') // bold **
|
||||
@@ -24,3 +25,68 @@ export function stripMarkdown(md: string): string {
|
||||
.replace(/\n/g, ' ') // remaining newlines → spaces
|
||||
.trim()
|
||||
}
|
||||
|
||||
/**
|
||||
* `.` matches every character except a line terminator, which is exactly the set a link
|
||||
* target could be built from back when this was `.*?`. Testing a single character against
|
||||
* it spells that out without hand-listing \n, \r and the two Unicode separators.
|
||||
*/
|
||||
const notALineTerminator = /./
|
||||
|
||||
/**
|
||||
* Replace `[label](target)` with its label, exactly as `/\[([^\]]*)\]\(.*?\)/g` did.
|
||||
*
|
||||
* Written as a scan rather than a regex because that pattern backtracks: `[^\]]` matches
|
||||
* `[` too, so on a run of `[` with no `]` after it the engine retries the whole
|
||||
* label-and-target scan from every one of them and reads to the end each time. Measured
|
||||
* on 32k of `[x`: 300ms, quadrupling with every doubling. The subject is a journal
|
||||
* entry's story text, which any contributor to the journal can write and every reader's
|
||||
* browser then renders a preview of.
|
||||
*
|
||||
* The semantics are the regex's, quirks included:
|
||||
* - the label runs to the FIRST `]` and may itself contain `[`, so `[a[b](c)` keeps
|
||||
* `a[b` — narrowing the label to `[^[\]]` would have been a behaviour change;
|
||||
* - the label may be empty, since `[^\]]*` is zero-or-more, so `[](c)` is a link;
|
||||
* - `(` has to sit directly after that `]`;
|
||||
* - the target ends at the first `)`, and `.` cannot cross a line terminator, so a `(`
|
||||
* whose first `)` only turns up on a later line is not a link at all.
|
||||
*/
|
||||
function stripLinks(input: string): string {
|
||||
let out = ''
|
||||
let cursor = 0 // everything before this has already been copied into `out`
|
||||
let search = 0 // where the next `[` is looked for
|
||||
// First `)`-or-line-terminator at or after the target we last examined. Targets are
|
||||
// examined left to right, so this only ever moves forward — which is what stops a
|
||||
// document full of `[a](` from being re-read once per candidate.
|
||||
let stop = 0
|
||||
|
||||
for (;;) {
|
||||
const open = input.indexOf('[', search)
|
||||
if (open === -1) break
|
||||
|
||||
const close = input.indexOf(']', open + 1)
|
||||
// No `]` left anywhere: neither this `[` nor any after it can match. Rediscovering
|
||||
// that from every `[` in turn is what the regex spent quadratic time on.
|
||||
if (close === -1) break
|
||||
|
||||
// Every `[` between `open` and `close` has that same first `]`, so the verdict on
|
||||
// what follows it is the verdict for all of them — skip the lot when it fails.
|
||||
if (input[close + 1] !== '(') {
|
||||
search = close + 1
|
||||
continue
|
||||
}
|
||||
|
||||
if (stop < close + 2) stop = close + 2
|
||||
while (stop < input.length && input[stop] !== ')' && notALineTerminator.test(input[stop])) stop++
|
||||
if (input[stop] !== ')') {
|
||||
search = close + 1
|
||||
continue
|
||||
}
|
||||
|
||||
out += input.slice(cursor, open) + input.slice(open + 1, close)
|
||||
cursor = stop + 1
|
||||
search = stop + 1
|
||||
}
|
||||
|
||||
return out + input.slice(cursor)
|
||||
}
|
||||
|
||||
@@ -64,8 +64,25 @@ function endpointIcon(type: TransportType, label: string | null): L.DivIcon {
|
||||
|
||||
function toRad(d: number) { return d * Math.PI / 180 }
|
||||
|
||||
// "Wien Hbf (Vienna)" → "Wien Hbf". Done as a scan, not /\s*\([^)]*\)/g: the leading
|
||||
// \s* overlaps the engine's own restart-at-every-position scan, so a name that is a
|
||||
// long run of spaces with no "(" after it backtracks quadratically (1.0s at 32k), and
|
||||
// so does a run of "(" with no ")". Same matches: the whitespace directly before a "("
|
||||
// that has a ")" after it goes too, and an unclosed "(" is left alone.
|
||||
function cleanName(name: string): string {
|
||||
return name.replace(/\s*\([^)]*\)/g, '').trim()
|
||||
let out = ''
|
||||
let cursor = 0
|
||||
for (;;) {
|
||||
const open = name.indexOf('(', cursor)
|
||||
if (open === -1) break
|
||||
const close = name.indexOf(')', open + 1)
|
||||
if (close === -1) break
|
||||
let start = open
|
||||
while (start > cursor && /\s/.test(name[start - 1])) start--
|
||||
out += name.slice(cursor, start)
|
||||
cursor = close + 1
|
||||
}
|
||||
return (out + name.slice(cursor)).trim()
|
||||
}
|
||||
|
||||
function haversineKm(a: [number, number], b: [number, number]): number {
|
||||
|
||||
@@ -94,7 +94,26 @@ function computeDuration(from: ReservationEndpoint, to: ReservationEndpoint, fal
|
||||
return h > 0 ? `${h}h ${m}m` : `${m}m`
|
||||
}
|
||||
|
||||
const cleanName = (name: string) => name.replace(/\s*\([^)]*\)/g, '').trim()
|
||||
// "Wien Hbf (Vienna)" → "Wien Hbf". Done as a scan, not /\s*\([^)]*\)/g: the leading
|
||||
// \s* overlaps the engine's own restart-at-every-position scan, so a name that is a
|
||||
// long run of spaces with no "(" after it backtracks quadratically (1.2s at 40k).
|
||||
// Same matches: the whitespace directly before a "(" that has a ")" after it goes
|
||||
// too, and an unclosed "(" is left alone.
|
||||
const cleanName = (name: string) => {
|
||||
let out = ''
|
||||
let cursor = 0
|
||||
for (;;) {
|
||||
const open = name.indexOf('(', cursor)
|
||||
if (open === -1) break
|
||||
const close = name.indexOf(')', open + 1)
|
||||
if (close === -1) break
|
||||
let start = open
|
||||
while (start > cursor && /\s/.test(name[start - 1])) start--
|
||||
out += name.slice(cursor, start)
|
||||
cursor = close + 1
|
||||
}
|
||||
return (out + name.slice(cursor)).trim()
|
||||
}
|
||||
|
||||
// ── item building ─────────────────────────────────────────────────────────
|
||||
interface TransportItem {
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function CustomTimePicker({ value, onChange, placeholder = '00:00
|
||||
|
||||
// `m` falls back to NaN because a value with no colon ('' while the field is
|
||||
// empty) splits into a single part, and an absent minute has to read as unset.
|
||||
const [h, m = NaN] = (parseMeridiemTime(value) ?? value ?? '').split(':').map(Number)
|
||||
const [h, m = Number.NaN] = (parseMeridiemTime(value) ?? value ?? '').split(':').map(Number)
|
||||
const hour = Number.isNaN(h) ? null : h
|
||||
const minute = Number.isNaN(m) ? null : m
|
||||
|
||||
|
||||
Reference in New Issue
Block a user