A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.
TypeScript
harden: add resource limit in app.ts (CWE-770) (#103)
Anupam Medirattac911000c81b8
diff --git a/.env.example b/.env.exampleindex 4d8e9a1..4ffae83 100644--- a/.env.example+++ b/.env.example@@ -8,6 +8,9 @@ PUBLIC_API_URL=http://localhost:8787ALLOWED_ORIGINS=http://localhost:8081,http://127.0.0.1:8081DATA_DIR=.openmuseTASK_WORKER_ENABLED=true+# Rate limiting keys on the socket address. Set true only behind a reverse proxy that+# overwrites X-Forwarded-For; otherwise callers can spoof it.+# TRUST_PROXY=false# General delegated work: select AGENT_BACKEND=model and a provider/model.# MODEL=openai/your-model-iddiff --git a/apps/server/src/app.ts b/apps/server/src/app.tsindex f216dab..258d7a2 100644--- a/apps/server/src/app.ts+++ b/apps/server/src/app.ts@@ -24,6 +24,7 @@ import { AgentService } from "./engine/service.ts";import { AppError } from "./errors.ts";import { Files } from "./files.ts";import { GoogleAuth } from "./google-auth.ts";+import { rateLimit } from "./rate-limit.ts";import { WorkspaceService } from "./workspace.ts";export async function createApp(@@ -74,6 +75,7 @@ export async function createApp(onError: (c) => c.json({ error: "Request is too large; PDFs must be 10 MB or smaller" }, 413),}),);+ app.use("/api/*", rateLimit(Boolean(config.trustProxy)));app.onError((error, c) => {if (error instanceof z.ZodError)return c.json({ error: error.issues.map((i) => i.message).join("; ") }, 422);diff --git a/apps/server/src/config.ts b/apps/server/src/config.tsindex 55a70bd..d34a909 100644--- a/apps/server/src/config.ts+++ b/apps/server/src/config.ts@@ -58,6 +58,7 @@ export interface Config {computerE2bTemplate?: string;e2bApiKey?: string;allowedOrigins: string[];+ trustProxy?: boolean;}export type ComputerProvider = "docker" | "e2b-desktop";@@ -165,6 +166,9 @@ export function readConfig(): Config {allowedOrigins: (process.env.ALLOWED_ORIGINS ?? "http://localhost:8081,http://127.0.0.1:8081").split(","),+ // Only trust X-Forwarded-For/X-Real-IP when the deployment is known to sit+ // behind a proxy that sets them; otherwise a direct caller can spoof them.+ trustProxy: process.env.TRUST_PROXY === "true",};if (mode === "live" &&diff --git a/apps/server/src/rate-limit.ts b/apps/server/src/rate-limit.tsnew file mode 100644index 0000000..95c1f5f--- /dev/null+++ b/apps/server/src/rate-limit.ts@@ -0,0 +1,90 @@+import { getConnInfo } from "@hono/node-server/conninfo";+import type { Context } from "hono";+import { AppError } from "./errors.ts";++interface RateLimitEntry {+ count: number;+ expiresAt: number;+}++export interface RateLimitStoreOptions {+ windowMs?: number;+ maxRequests?: number;+ maxEntries?: number;+}++export function createRateLimitStore(options: RateLimitStoreOptions = {}) {+ const windowMs = options.windowMs ?? 60_000;+ const maxRequests = options.maxRequests ?? 120;+ const maxEntries = options.maxEntries ?? 10_000;+ const entries = new Map<string, RateLimitEntry>();++ function cleanupExpired(now: number) {+ for (const [key, entry] of entries) {+ if (entry.expiresAt <= now) entries.delete(key);+ }+ }++ function take(key: string, now: number): boolean {+ let entry = entries.get(key);+ if (entry && entry.expiresAt <= now) entry = undefined;+ if (!entry) {+ if (entries.size >= maxEntries) {+ cleanupExpired(now);+ if (entries.size >= maxEntries) return false;+ }+ entry = { count: 0, expiresAt: now + windowMs };+ entries.set(key, entry);+ }+ entry.count += 1;+ return entry.count <= maxRequests;+ }++ return { take, size: () => entries.size };+}++// This limiter runs before authentication, so the key must never be derived from+// caller-supplied credentials (e.g. Authorization): any string would mint a new bucket.+export function resolveClientKey(input: {+ trustProxy: boolean;+ forwardedFor?: string;+ realIp?: string;+ connectionAddress?: string;+}): string {+ if (input.trustProxy) {+ const forwarded = input.forwardedFor?.split(",")[0]?.trim();+ if (forwarded) return `proxy:${forwarded}`;+ if (input.realIp) return `proxy:${input.realIp}`;+ }+ return `conn:${input.connectionAddress ?? "unknown"}`;+}++// getConnInfo reads the raw Node socket, which only exists behind a real+// @hono/node-server listener; in-memory `app.request()` calls (as used in+// tests) have no socket, so this must degrade rather than throw.+function connectionAddress(c: Context): string | undefined {+ try {+ return getConnInfo(c).remote.address;+ } catch {+ return undefined;+ }+}++export function rateLimit(+ trustProxy: boolean,+ options: RateLimitStoreOptions & { getAddress?: (c: Context) => string | undefined } = {},+) {+ const store = createRateLimitStore(options);+ const getAddress = options.getAddress ?? connectionAddress;+ return async (c: Context, next: () => Promise<void>) => {+ const key = resolveClientKey({+ trustProxy,+ forwardedFor: c.req.header("x-forwarded-for"),+ realIp: c.req.header("x-real-ip"),+ connectionAddress: getAddress(c),+ });+ if (!store.take(key, Date.now()))+ throw new AppError("Too many requests. Try again in a minute.", 429);+ await next();+ };+}diff --git a/tests/rate-limit.test.ts b/tests/rate-limit.test.tsnew file mode 100644index 0000000..30ed787--- /dev/null+++ b/tests/rate-limit.test.ts@@ -0,0 +1,162 @@+import assert from "node:assert/strict";+import { mkdtemp, rm } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { after, before, test } from "node:test";+import { Hono } from "hono";+import { createApp } from "../apps/server/src/app.ts";+import type { Config } from "../apps/server/src/config.ts";+import { createStore, type Store } from "../apps/server/src/db.ts";+import {+ createRateLimitStore,+ rateLimit,+ resolveClientKey,+} from "../apps/server/src/rate-limit.ts";++test("resolveClientKey ignores forwarding headers unless trustProxy is enabled", () => {+ const untrustedA = resolveClientKey({+ trustProxy: false,+ forwardedFor: "1.2.3.4",+ connectionAddress: "10.0.0.1",+ });+ const untrustedB = resolveClientKey({+ trustProxy: false,+ forwardedFor: "5.6.7.8",+ connectionAddress: "10.0.0.1",+ });+ assert.equal(untrustedA, untrustedB, "spoofed X-Forwarded-For must not change the bucket");++ const trustedA = resolveClientKey({ trustProxy: true, forwardedFor: "1.2.3.4, 10.0.0.1" });+ const trustedB = resolveClientKey({ trustProxy: true, forwardedFor: "5.6.7.8, 10.0.0.1" });+ assert.notEqual(trustedA, trustedB, "a trusted proxy header should pick the first (client) hop");++ assert.equal(resolveClientKey({ trustProxy: true, realIp: "1.2.3.4" }), "proxy:1.2.3.4");+});++test("resolveClientKey falls back to a connection-derived key with no headers", () => {+ assert.equal(resolveClientKey({ trustProxy: false }), "conn:unknown");+ assert.equal(+ resolveClientKey({ trustProxy: false, connectionAddress: "127.0.0.1" }),+ "conn:127.0.0.1",+ );+});++test("createRateLimitStore enforces the request limit and resets after the window", () => {+ const store = createRateLimitStore({ windowMs: 1000, maxRequests: 2, maxEntries: 10 });+ assert.equal(store.take("a", 0), true);+ assert.equal(store.take("a", 0), true);+ assert.equal(store.take("a", 0), false, "third request within the window must be rejected");+ assert.equal(store.take("a", 1001), true, "a new window should reset the counter");+});++test("createRateLimitStore caps total entries and evicts expired ones to make room", () => {+ const store = createRateLimitStore({ windowMs: 1000, maxRequests: 5, maxEntries: 2 });+ assert.equal(store.take("a", 0), true);+ assert.equal(store.take("b", 0), true);+ assert.equal(store.size(), 2);+ assert.equal(store.take("c", 500), false, "a brand-new key must be rejected once at capacity");+ assert.equal(store.size(), 2, "the map must never exceed maxEntries");++ // Once "a" and "b" expire, capacity should be reclaimed for a new key.+ assert.equal(store.take("c", 2000), true);+ assert.equal(store.size(), 1);+});++let db: Store, app: Awaited<ReturnType<typeof createApp>>["app"], config: Config, directory: string;+before(async () => {+ directory = await mkdtemp(join(tmpdir(), "openmuse-ratelimit-"));+ db = await createStore();+ config = {+ mode: "sample",+ port: 8787,+ host: "127.0.0.1",+ publicUrl: "http://localhost:8787",+ dataDir: directory,+ agentBackend: "sample",+ intelligenceApiKey: "test-project-key-never-sent",+ googleRedirectUri: "http://localhost:8787/api/google/callback",+ allowedOrigins: ["http://localhost:8081"],+ };+ ({ app } = await createApp(db, config));+});+after(async () => {+ await db.close();+ await rm(directory, { recursive: true, force: true });+});++test("session token rotation does not create new pre-auth buckets", async () => {+ let lastStatus = 200;+ for (let i = 0; i < 121; i++) {+ const token = i % 2 ? "rotated-token" : `rotated-token-${i}`;+ const response = await app.request("/api/workspace", {+ headers: { Authorization: `Bearer ${token}` },+ });+ lastStatus = response.status;+ }+ assert.equal(lastStatus, 429, "rotating tokens from one connection must share one bucket");+});++test("unauthenticated requests cannot multiply buckets by spoofing X-Forwarded-For", async () => {+ let lastStatus = 200;+ for (let i = 0; i < 121; i++) {+ const response = await app.request("/api/health", {+ headers: { "X-Forwarded-For": `10.0.0.${i % 255}` },+ });+ lastStatus = response.status;+ }+ assert.equal(+ lastStatus,+ 429,+ "spoofed per-request X-Forwarded-For values must not create independent buckets",+ );+});++function limitedApp(options: Parameters<typeof rateLimit>[1]) {+ const app = new Hono();+ app.onError((error, c) =>+ c.json({ error: error.message }, "status" in error ? (error.status as 429) : 500),+ );+ app.use("/api/*", rateLimit(false, options));+ app.get("/api/ping", (c) => c.text("ok"));+ return app;+}++test("spoofed bearer tokens from one connection share a single bucket", async () => {+ let address = "203.0.113.1";+ const app = limitedApp({ maxRequests: 3, maxEntries: 5, getAddress: () => address });+ const statuses: number[] = [];+ for (let i = 0; i < 20; i++) {+ const response = await app.request("/api/ping", {+ headers: { Authorization: `Bearer fake-${i}` },+ });+ statuses.push(response.status);+ }+ assert.deepEqual(statuses.slice(0, 4), [200, 200, 200, 429]);+ assert.ok(statuses.slice(3).every((status) => status === 429));++ // Fake tokens did not consume the shared capacity: other connections still get in.+ address = "203.0.113.2";+ const other = await app.request("/api/ping", { headers: { Authorization: "Bearer fake-x" } });+ assert.equal(other.status, 200);+});++test("capacity exhaustion fails closed and recovers once entries expire", async () => {+ const addresses = ["10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4"];+ let current = 0;+ const app = limitedApp({ maxEntries: 3, windowMs: 60_000, getAddress: () => addresses[current] });+ const statuses: number[] = [];+ for (current = 0; current < 4; current++) {+ statuses.push((await app.request("/api/ping")).status);+ }+ assert.deepEqual(statuses, [200, 200, 200, 429], "the fourth connection is rejected");+ current = 0;+ assert.equal((await app.request("/api/ping")).status, 200, "existing entries keep working");+});++test("capacity exhaustion reclaims expired entries", () => {+ const store = createRateLimitStore({ windowMs: 1000, maxEntries: 3 });+ for (const key of ["conn:a", "conn:b", "conn:c"]) assert.equal(store.take(key, 0), true);+ assert.equal(store.take("conn:d", 500), false);+ assert.equal(store.take("conn:d", 1500), true);+ assert.equal(store.size(), 1);+});