mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* refactor(relay): extract shared relay helpers to server/_shared/relay.ts Consolidates 8 duplicate relay helper implementations into a single canonical source. Fixes bugs found across copies: - aviation/_shared.ts: _extra param was silently ignored (now spreads) - military/list-military-flights.ts: WS_RELAY_URL + '/opensky' bypassed wss:// → https:// conversion (URL would be invalid for REST requests) - research/list-tech-events.ts, news/list-feed-digest.ts: missing Authorization: Bearer header (now added via shared helper) - market/_shared.ts: same missing Bearer + no Accept: application/json api/_relay.js unchanged (edge functions cannot import server/ TS); canonical version comment added. * fix(relay): correct aviation re-export path and remove unused CHROME_UA in military * fix(relay): import relay helpers in aviation/_shared.ts for local use * fix(relay): skip Authorization: Bearer when RELAY_AUTH_HEADER=Authorization When RELAY_AUTH_HEADER=Authorization, getRelayHeaders() was setting both headers['authorization'] = secret (the direct key) and headers.Authorization = 'Bearer secret' (different JS case, same HTTP header). Undici merges both into authorization: "secret, Bearer secret". The relay's getRelaySecretFromRequest() reads this as the raw secret and the direct compare against RELAY_SHARED_SECRET fails, rejecting the request. Guard: only add the Authorization: Bearer header when relayHeader !== 'authorization'. This fixes the regression introduced by PR C for market/news/research handlers that now use the shared helper instead of their old secret-only variants.
27 lines
1.1 KiB
TypeScript
27 lines
1.1 KiB
TypeScript
import { CHROME_UA } from './constants';
|
|
|
|
export function getRelayBaseUrl(): string | null {
|
|
const relayUrl = process.env.WS_RELAY_URL;
|
|
if (!relayUrl) return null;
|
|
return relayUrl.replace(/^ws(s?):\/\//, 'http$1://').replace(/\/$/, '');
|
|
}
|
|
|
|
export function getRelayHeaders(extra: Record<string, string> = {}): Record<string, string> {
|
|
const headers: Record<string, string> = {
|
|
Accept: 'application/json',
|
|
'User-Agent': CHROME_UA,
|
|
...extra,
|
|
};
|
|
const relaySecret = process.env.RELAY_SHARED_SECRET;
|
|
if (!relaySecret) return headers;
|
|
const relayHeader = (process.env.RELAY_AUTH_HEADER || 'x-relay-key').toLowerCase();
|
|
headers[relayHeader] = relaySecret;
|
|
// Only add a separate Authorization: Bearer header when relayHeader is not 'authorization'.
|
|
// If RELAY_AUTH_HEADER=Authorization, both keys normalize to the same HTTP header and
|
|
// Undici merges them into "secret, Bearer secret", which breaks the relay's direct-compare check.
|
|
if (relayHeader !== 'authorization') {
|
|
headers.Authorization = `Bearer ${relaySecret}`;
|
|
}
|
|
return headers;
|
|
}
|