Skip to content

Commit c8a66d2

Browse files
authored
fix(wingbits): clamp bbox coords to valid geographic ranges before API call (koala73#2453)
Map projections can send lamin/lamax outside [-90,90] (e.g. a zoomed-out viewport). The isFinite validation passes these through, but Wingbits rejects la < -90 with a Zod too_small error (HTTP 400). Clamp all four bbox values to valid lat/lon ranges before computing centerLat/centerLon so the API payload is always valid regardless of what the client sends.
1 parent 6b4dadf commit c8a66d2

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

scripts/ais-relay.cjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,10 +3421,16 @@ async function handleWingbitsTrackRequest(req, res) {
34213421
JSON.stringify({ error: 'Invalid bbox params: must be finite numbers', positions: [] }));
34223422
}
34233423

3424-
const centerLat = (lamin + lamax) / 2;
3425-
const centerLon = (lomin + lomax) / 2;
3426-
const widthNm = Math.min(Math.abs(lomax - lomin) * 60 * Math.cos(centerLat * Math.PI / 180), WINGBITS_MAX_BOX_NM);
3427-
const heightNm = Math.min(Math.abs(lamax - lamin) * 60, WINGBITS_MAX_BOX_NM);
3424+
// Clamp bbox to valid geographic ranges before computing center.
3425+
// Map projections can produce slightly out-of-range values; Wingbits rejects la outside [-90,90].
3426+
const clampedLamin = Math.max(-90, Math.min(90, lamin));
3427+
const clampedLamax = Math.max(-90, Math.min(90, lamax));
3428+
const clampedLomin = Math.max(-180, Math.min(180, lomin));
3429+
const clampedLomax = Math.max(-180, Math.min(180, lomax));
3430+
const centerLat = (clampedLamin + clampedLamax) / 2;
3431+
const centerLon = (clampedLomin + clampedLomax) / 2;
3432+
const widthNm = Math.min(Math.abs(clampedLomax - clampedLomin) * 60 * Math.cos(centerLat * Math.PI / 180), WINGBITS_MAX_BOX_NM);
3433+
const heightNm = Math.min(Math.abs(clampedLamax - clampedLamin) * 60, WINGBITS_MAX_BOX_NM);
34283434
const areas = [{ alias: 'viewport', by: 'box', la: centerLat, lo: centerLon, w: widthNm, h: heightNm, unit: 'nm' }];
34293435

34303436
try {

0 commit comments

Comments
 (0)