feat(http): trustProxy + ctx.protocol/ctx.ip from socket or X-Forwarded-*
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+49
-6
No files matched your search
+19
-1
@@ -16,7 +16,7 @@ export interface RequestLimits {
|
||||
headersTimeoutMs?: number; // default 10_000
|
||||
keepAliveTimeoutMs?: number; // default 5_000
|
||||
}
|
||||
export interface HttpOptions { limits?: RequestLimits; streams?: Set<() => void>; tls?: TlsOptions }
|
||||
export interface HttpOptions { limits?: RequestLimits; streams?: Set<() => void>; tls?: TlsOptions; trustProxy?: boolean }
|
||||
|
||||
export type RouteHandler = (req: {
|
||||
method: string; url: string;
|
||||
@@ -24,6 +24,8 @@ export type RouteHandler = (req: {
|
||||
params: Record<string, string>;
|
||||
query: Record<string, string>;
|
||||
body: unknown;
|
||||
protocol: 'http' | 'https';
|
||||
ip: string;
|
||||
}) => Promise<PipelineResult>;
|
||||
|
||||
export interface RouteDef { method: string; pattern: string; transport: Transport; handler: RouteHandler }
|
||||
@@ -82,6 +84,19 @@ async function readBody(req: http.IncomingMessage, maxBodyBytes: number): Promis
|
||||
return raw === '' ? undefined : raw;
|
||||
}
|
||||
|
||||
function firstToken(v: string | string[] | undefined): string | undefined {
|
||||
const s = Array.isArray(v) ? v[0] : v;
|
||||
return s?.split(',')[0].trim();
|
||||
}
|
||||
function deriveSecure(req: http.IncomingMessage, trustProxy: boolean): boolean {
|
||||
if (trustProxy) { const p = firstToken(req.headers['x-forwarded-proto']); if (p) return p === 'https'; }
|
||||
return (req.socket as any).encrypted === true;
|
||||
}
|
||||
function deriveIp(req: http.IncomingMessage, trustProxy: boolean): string {
|
||||
if (trustProxy) { const f = firstToken(req.headers['x-forwarded-for']); if (f) return f; }
|
||||
return req.socket.remoteAddress ?? '';
|
||||
}
|
||||
|
||||
function pickEncoder(transport: Transport, accept: string): StreamEncoder {
|
||||
if (transport === 'sse') return sseEncoder;
|
||||
if (transport === 'ndjson') return ndjsonEncoder;
|
||||
@@ -218,11 +233,14 @@ export function createHttpServer(
|
||||
} else if (raw !== undefined && ct.includes('application/x-www-form-urlencoded')) {
|
||||
body = Object.fromEntries(new URLSearchParams(raw));
|
||||
}
|
||||
const trustProxy = opts?.trustProxy ?? false;
|
||||
const secure = deriveSecure(req, trustProxy);
|
||||
let result: PipelineResult;
|
||||
try {
|
||||
result = await matched.def.handler({
|
||||
method: req.method ?? 'GET', url, headers: req.headers,
|
||||
params: matched.params, query: parseQuery(url), body,
|
||||
protocol: secure ? 'https' : 'http', ip: deriveIp(req, trustProxy),
|
||||
});
|
||||
} catch (error) {
|
||||
result = errorToResponse(error);
|
||||
|
||||
Reference in new issue
Block a user