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,4 +1,6 @@
|
||||
import { createFixedWindowRateLimiter, type FixedWindowDecision, type FixedWindowRateLimiter, type SharedStore } from "@hivegrid/shared-store";
|
||||
import { createHmac } from "node:crypto";
|
||||
|
||||
import { createFixedWindowRateLimiter, type FixedWindowRateLimiter, type SharedStore } from "@hivegrid/shared-store";
|
||||
|
||||
import { getMcpSharedStore } from "./shared-store.js";
|
||||
|
||||
@@ -43,18 +45,23 @@ export type McpRateLimitBlocked = {
|
||||
|
||||
export type McpRateLimitResult = McpRateLimitAllowed | McpRateLimitBlocked;
|
||||
|
||||
export async function checkMcpRateLimit(input: { apiKeyId: string; tenantId: string; weight?: number }): Promise<McpRateLimitResult> {
|
||||
export async function checkMcpRateLimit(input: { apiKeyId: string; tenantId: string; bucketKeySecret: string | undefined; weight?: number }): Promise<McpRateLimitResult> {
|
||||
const { key, tenant } = getLimiters();
|
||||
// Consume one token per JSON-RPC operation the request carries (weight), not
|
||||
// one per HTTP request — otherwise a batch bypasses the per-key/per-tenant cap.
|
||||
const weight = Math.max(1, Math.min(input.weight ?? 1, MCP_MAX_BATCH_OPERATIONS));
|
||||
const [keyDecision, tenantDecision] = await Promise.all([
|
||||
consumeWeighted(key, input.apiKeyId, weight),
|
||||
consumeWeighted(tenant, input.tenantId, weight),
|
||||
key.consume(hashBucketKey(input.bucketKeySecret, "credential", input.apiKeyId), weight),
|
||||
tenant.consume(hashBucketKey(input.bucketKeySecret, "tenant", input.tenantId), weight),
|
||||
]);
|
||||
const blockedScope = !keyDecision.allowed ? "key" : !tenantDecision.allowed ? "tenant" : null;
|
||||
const decision = blockedScope === "key" ? keyDecision : tenantDecision;
|
||||
const limit = blockedScope === "key" ? MCP_KEY_RATE_LIMIT_PER_HOUR : blockedScope === "tenant" ? MCP_TENANT_RATE_LIMIT_PER_HOUR : Math.min(MCP_KEY_RATE_LIMIT_PER_HOUR, MCP_TENANT_RATE_LIMIT_PER_HOUR);
|
||||
const limit =
|
||||
blockedScope === "key"
|
||||
? MCP_KEY_RATE_LIMIT_PER_HOUR
|
||||
: blockedScope === "tenant"
|
||||
? MCP_TENANT_RATE_LIMIT_PER_HOUR
|
||||
: Math.min(MCP_KEY_RATE_LIMIT_PER_HOUR, MCP_TENANT_RATE_LIMIT_PER_HOUR);
|
||||
const headers = createRateLimitHeaders({
|
||||
limit,
|
||||
remaining: Math.min(keyDecision.remaining, tenantDecision.remaining),
|
||||
@@ -77,9 +84,9 @@ export async function checkMcpRateLimit(input: { apiKeyId: string; tenantId: str
|
||||
|
||||
// Pre-auth per-IP shield, run before the token lookups so invalid/absent
|
||||
// tokens cannot force unbounded DB work. Keyed on the trusted client IP.
|
||||
export async function checkMcpPreAuthIpRateLimit(ip: string): Promise<McpRateLimitResult> {
|
||||
export async function checkMcpPreAuthIpRateLimit(ip: string, bucketKeySecret: string | undefined): Promise<McpRateLimitResult> {
|
||||
const { ip: limiter } = getLimiters();
|
||||
const decision = await limiter.consume(ip);
|
||||
const decision = await limiter.consume(hashBucketKey(bucketKeySecret, "client", ip));
|
||||
const headers = createRateLimitHeaders({
|
||||
limit: MCP_PREAUTH_IP_RATE_LIMIT_PER_MINUTE,
|
||||
remaining: decision.remaining,
|
||||
@@ -98,6 +105,14 @@ export async function checkMcpPreAuthIpRateLimit(ip: string): Promise<McpRateLim
|
||||
return { allowed: true, headers };
|
||||
}
|
||||
|
||||
function hashBucketKey(secret: string | undefined, scope: "client" | "credential" | "tenant", value: string): string {
|
||||
const normalizedSecret = secret?.trim() ?? "";
|
||||
if (Buffer.byteLength(normalizedSecret, "utf8") < 32) {
|
||||
throw new Error("MCP rate-limit bucket secret is unavailable.");
|
||||
}
|
||||
return createHmac("sha256", normalizedSecret).update(`v1\0${scope}\0${value}`).digest("hex");
|
||||
}
|
||||
|
||||
export function __setMcpRateLimitStoreForTests(store: SharedStore): void {
|
||||
limiters = null;
|
||||
storeFactory = () => store;
|
||||
@@ -108,17 +123,6 @@ export function __resetMcpRateLimitStoreForTests(): void {
|
||||
storeFactory = () => getMcpSharedStore();
|
||||
}
|
||||
|
||||
// Consume `weight` tokens from a fixed-window limiter. The underlying store
|
||||
// increments atomically, so the last decision reflects the cumulative count;
|
||||
// `allowed` is false once the window budget is exceeded by any of the tokens.
|
||||
async function consumeWeighted(limiter: FixedWindowRateLimiter, key: string, weight: number): Promise<FixedWindowDecision> {
|
||||
let decision = await limiter.consume(key);
|
||||
for (let i = 1; i < weight; i += 1) {
|
||||
decision = await limiter.consume(key);
|
||||
}
|
||||
return decision;
|
||||
}
|
||||
|
||||
function getLimiters(): Limiters {
|
||||
if (limiters) {
|
||||
return limiters;
|
||||
|
||||
Reference in new issue
Block a user