Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -1,14 +1,22 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
|
||||
import { getTrustedProxyClientIp } from "@hivegrid/foundation/trusted-client-ip";
|
||||
|
||||
import { McpHttpError } from "./errors.js";
|
||||
|
||||
export const MCP_MAX_BODY_BYTES = 1_000_000;
|
||||
|
||||
export async function readJsonBody(req: IncomingMessage): Promise<unknown> {
|
||||
const contentLength = parseContentLength(req.headers["content-length"]);
|
||||
if (contentLength !== null && contentLength > MCP_MAX_BODY_BYTES) {
|
||||
throw new McpHttpError(413, "payload_too_large", "MCP request body is too large.");
|
||||
}
|
||||
const chunks: Buffer[] = [];
|
||||
let size = 0;
|
||||
for await (const chunk of req) {
|
||||
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
||||
size += buffer.byteLength;
|
||||
if (size > 1_000_000) {
|
||||
if (size > MCP_MAX_BODY_BYTES) {
|
||||
throw new McpHttpError(413, "payload_too_large", "MCP request body is too large.");
|
||||
}
|
||||
chunks.push(buffer);
|
||||
@@ -23,6 +31,12 @@ export async function readJsonBody(req: IncomingMessage): Promise<unknown> {
|
||||
}
|
||||
}
|
||||
|
||||
function parseContentLength(value: string | undefined): number | null {
|
||||
if (!value || !/^\d+$/.test(value.trim())) return null;
|
||||
const parsed = Number(value);
|
||||
return Number.isSafeInteger(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
export function writeJson(res: ServerResponse, status: number, payload: unknown, headers: Record<string, string> = {}): void {
|
||||
if (res.headersSent) {
|
||||
return;
|
||||
@@ -34,21 +48,13 @@ export function writeJson(res: ServerResponse, status: number, payload: unknown,
|
||||
res.end(JSON.stringify(payload));
|
||||
}
|
||||
|
||||
// Trusted, proxy-set headers only. The leftmost X-Forwarded-For value is
|
||||
// client-controlled, so keying a rate limit on it would let an attacker rotate
|
||||
// their apparent IP. In this estate the MCP host sits behind Cloudflare, which
|
||||
// sets CF-Connecting-IP and overwrites any client-supplied value.
|
||||
const TRUSTED_IP_HEADERS = ["cf-connecting-ip", "true-client-ip", "x-real-ip"] as const;
|
||||
|
||||
export function getClientIp(req: IncomingMessage): string {
|
||||
for (const headerName of TRUSTED_IP_HEADERS) {
|
||||
const value = req.headers[headerName];
|
||||
const candidate = (Array.isArray(value) ? value[0] : value)?.trim();
|
||||
if (candidate) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return req.socket.remoteAddress ?? "unknown";
|
||||
return (
|
||||
getTrustedProxyClientIp((name) => {
|
||||
const value = req.headers[name];
|
||||
return Array.isArray(value) ? value.at(-1) : value;
|
||||
}, req.socket.remoteAddress) ?? "unknown"
|
||||
);
|
||||
}
|
||||
|
||||
export function getHeader(req: IncomingMessage, name: string): string | undefined {
|
||||
|
||||
Reference in new issue
Block a user