mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-21 14:36:31 +00:00
1ad2e92142
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.
252 lines
8.5 KiB
TypeScript
252 lines
8.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { safeFetch } from '../../utils/ssrfGuard';
|
|
|
|
/**
|
|
* Thin HTTP client for the AirTrail REST API (github.com/johanohly/AirTrail).
|
|
* This is the ONLY place that talks to a user's AirTrail instance.
|
|
*
|
|
* Verified against AirTrail source:
|
|
* - Auth: `Authorization: Bearer <key>`; a key maps to exactly one user.
|
|
* - GET /api/flight/list — defaults to scope=mine. We NEVER send a scope
|
|
* param so the key only ever returns its owner's own flights (isolation
|
|
* holds even if an admin key is pasted).
|
|
* - GET /api/flight/get/{id}
|
|
* - POST /api/flight/save — `id` present => update, else create. The
|
|
* passenger list is required (>=1). An entry with userId '<USER_ID>' is
|
|
* attributed to the key owner server-side, so we never need the caller's
|
|
* AirTrail user id.
|
|
* - AirTrail 3.12.0 renamed that list from `seats` to `passengers` and moved
|
|
* flightReason from the flight onto each passenger, with no alias on either
|
|
* side (verified against running 3.11.1 and 3.12.0 instances: each returns
|
|
* its own key and null for the other). TREK reads and writes both shapes so
|
|
* one build works against either version.
|
|
* - There is no webhook and no updated_at on a flight, so change detection is
|
|
* snapshot-hash based (see airtrailSync).
|
|
*/
|
|
|
|
const TIMEOUT_MS = 12000;
|
|
|
|
export interface AirtrailCreds {
|
|
/** Instance origin without a trailing /api. */
|
|
baseUrl: string;
|
|
apiKey: string;
|
|
allowInsecureTls: boolean;
|
|
}
|
|
|
|
export class AirtrailAuthError extends Error {
|
|
constructor(message = 'AirTrail rejected the API key') {
|
|
super(message);
|
|
this.name = 'AirtrailAuthError';
|
|
}
|
|
}
|
|
|
|
export class AirtrailRequestError extends Error {
|
|
status?: number;
|
|
constructor(message: string, status?: number) {
|
|
super(message);
|
|
this.name = 'AirtrailRequestError';
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
export interface AirtrailAirport {
|
|
id: number;
|
|
icao: string | null;
|
|
iata: string | null;
|
|
name: string | null;
|
|
lat: number | null;
|
|
lon: number | null;
|
|
tz: string | null;
|
|
country: string | null;
|
|
}
|
|
|
|
export interface AirtrailSeat {
|
|
userId: string | null;
|
|
guestName: string | null;
|
|
seat: string | null;
|
|
seatNumber: string | null;
|
|
seatClass: string | null;
|
|
/** Per-passenger since 3.12.0; absent on older instances. */
|
|
flightReason?: string | null;
|
|
}
|
|
|
|
/** Airline/aircraft come back as joined objects (not bare codes) on a flight. */
|
|
export interface AirtrailNamedCode {
|
|
id?: number;
|
|
icao?: string | null;
|
|
iata?: string | null;
|
|
name?: string | null;
|
|
}
|
|
|
|
/** A flight as returned by list/get (the fields TREK consumes). */
|
|
export interface AirtrailFlightRaw {
|
|
id: number;
|
|
from: AirtrailAirport | null;
|
|
to: AirtrailAirport | null;
|
|
date: string | null;
|
|
datePrecision: string | null;
|
|
departure: string | null;
|
|
arrival: string | null;
|
|
departureScheduled: string | null;
|
|
arrivalScheduled: string | null;
|
|
airline: AirtrailNamedCode | null;
|
|
flightNumber: string | null;
|
|
aircraft: AirtrailNamedCode | null;
|
|
aircraftReg: string | null;
|
|
/** Flight-level up to 3.11.x; gone in 3.12.0, where it sits on the passenger. */
|
|
flightReason?: string | null;
|
|
note: string | null;
|
|
/** 3.11.x and older. */
|
|
seats?: AirtrailSeat[];
|
|
/** 3.12.0 and newer. */
|
|
passengers?: AirtrailSeat[];
|
|
}
|
|
|
|
/**
|
|
* The passenger list under whichever name this instance uses, and the entry
|
|
* belonging to the key owner (the one carrying a userId).
|
|
*/
|
|
export function flightPassengers(raw: Pick<AirtrailFlightRaw, 'seats' | 'passengers'>): AirtrailSeat[] {
|
|
return raw.passengers ?? raw.seats ?? [];
|
|
}
|
|
|
|
export function ownPassenger(raw: Pick<AirtrailFlightRaw, 'seats' | 'passengers'>): AirtrailSeat | undefined {
|
|
const people = flightPassengers(raw);
|
|
return people.find((s) => s.userId) ?? people[0];
|
|
}
|
|
|
|
/** Write shape accepted by POST /flight/save (airports/airline/aircraft as codes). */
|
|
export interface AirtrailSavePayload {
|
|
id?: number;
|
|
from: string;
|
|
to: string;
|
|
departure: string | null;
|
|
departureTime?: string | null;
|
|
arrival?: string | null;
|
|
arrivalTime?: string | null;
|
|
departureScheduled?: string | null;
|
|
departureScheduledTime?: string | null;
|
|
arrivalScheduled?: string | null;
|
|
arrivalScheduledTime?: string | null;
|
|
datePrecision?: string;
|
|
airline?: string | null;
|
|
flightNumber?: string | null;
|
|
aircraft?: string | null;
|
|
aircraftReg?: string | null;
|
|
flightReason?: string | null;
|
|
note?: string | null;
|
|
/**
|
|
* Sent under both names on purpose. Neither schema is strict, so each version
|
|
* validates the key it knows and strips the other one.
|
|
*/
|
|
seats: AirtrailPassengerWrite[];
|
|
passengers: AirtrailPassengerWrite[];
|
|
}
|
|
|
|
export interface AirtrailPassengerWrite {
|
|
userId: string | null;
|
|
guestName: string | null;
|
|
seat: string | null;
|
|
seatNumber: string | null;
|
|
seatClass: string | null;
|
|
flightReason?: string | null;
|
|
}
|
|
|
|
function apiBase(baseUrl: string): string {
|
|
// Tolerate a pasted trailing slash or '/api' suffix so we never build '/api/api'.
|
|
// The lookbehind matches only the first slash of the trailing run. Without it the
|
|
// engine retries from every slash, which is quadratic on a slash-heavy value.
|
|
const origin = baseUrl.trim().replace(/(?<!\/)\/+$/, '').replace(/\/api$/i, '');
|
|
return origin + '/api';
|
|
}
|
|
|
|
/**
|
|
* Parse a response as JSON, but turn the cryptic "Unexpected token '<'" that a
|
|
* misconfigured URL produces (AirTrail serving its SPA / an auth-proxy login
|
|
* page) into an actionable message.
|
|
*/
|
|
async function parseJson<T>(resp: Response): Promise<T> {
|
|
const text = await resp.text();
|
|
try {
|
|
return JSON.parse(text) as T;
|
|
} catch {
|
|
throw new AirtrailRequestError(
|
|
'AirTrail returned a non-JSON response. Check the URL is your AirTrail base URL (e.g. https://airtrail.example.com, without /api) and that the instance is reachable without a separate login.',
|
|
);
|
|
}
|
|
}
|
|
|
|
async function request(creds: AirtrailCreds, path: string, init: RequestInit): Promise<Response> {
|
|
const url = apiBase(creds.baseUrl) + path;
|
|
let resp: Response;
|
|
try {
|
|
resp = await safeFetch(
|
|
url,
|
|
{
|
|
...init,
|
|
headers: {
|
|
Authorization: `Bearer ${creds.apiKey}`,
|
|
Accept: 'application/json',
|
|
...(init.headers || {}),
|
|
},
|
|
signal: AbortSignal.timeout(TIMEOUT_MS) as any,
|
|
},
|
|
{ rejectUnauthorized: !creds.allowInsecureTls },
|
|
);
|
|
} catch (err: unknown) {
|
|
throw new AirtrailRequestError(err instanceof Error ? err.message : 'Could not reach AirTrail');
|
|
}
|
|
if (resp.status === 401 || resp.status === 403) {
|
|
throw new AirtrailAuthError();
|
|
}
|
|
return resp;
|
|
}
|
|
|
|
|
|
/**
|
|
* The AirTrail HTTP surface, as a provider.
|
|
*
|
|
* It has no constructor dependencies: `safeFetch` is the SSRF guard, a plain
|
|
* util that fifteen other nest services import directly. The error classes stay
|
|
* exported classes rather than becoming anything injectable, because four call
|
|
* sites branch on `instanceof` and on `err.status === 400`.
|
|
*/
|
|
@Injectable()
|
|
export class AirtrailClient {
|
|
async listFlights(creds: AirtrailCreds): Promise<AirtrailFlightRaw[]> {
|
|
const resp = await request(creds, '/flight/list', { method: 'GET' });
|
|
if (!resp.ok) throw new AirtrailRequestError(`AirTrail list failed (HTTP ${resp.status})`, resp.status);
|
|
const data = await parseJson<{ flights?: AirtrailFlightRaw[] }>(resp);
|
|
return data.flights ?? [];
|
|
}
|
|
|
|
async getFlight(creds: AirtrailCreds, id: number): Promise<AirtrailFlightRaw | null> {
|
|
const resp = await request(creds, `/flight/get/${id}`, { method: 'GET' });
|
|
if (resp.status === 404) return null;
|
|
if (!resp.ok) throw new AirtrailRequestError(`AirTrail get failed (HTTP ${resp.status})`, resp.status);
|
|
const data = await parseJson<{ flight?: AirtrailFlightRaw }>(resp);
|
|
return data.flight ?? null;
|
|
}
|
|
|
|
async saveFlight(creds: AirtrailCreds, payload: AirtrailSavePayload): Promise<{ id?: number }> {
|
|
const resp = await request(creds, '/flight/save', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
if (!resp.ok) {
|
|
let msg = `AirTrail save failed (HTTP ${resp.status})`;
|
|
try {
|
|
const body = (await resp.json()) as { message?: string; errors?: unknown };
|
|
if (body?.message) msg = body.message;
|
|
else if (body?.errors) msg = JSON.stringify(body.errors);
|
|
} catch {
|
|
/* keep the generic message */
|
|
}
|
|
throw new AirtrailRequestError(msg, resp.status);
|
|
}
|
|
const data = await parseJson<{ id?: number }>(resp);
|
|
return { id: data.id };
|
|
}
|
|
}
|