Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🎚️ fix: Apply librechat.yaml Rate Limits to Route Limiters (#16432)

Marco Beretta50ce28957f0f
diff --git a/api/server/experimental.js b/api/server/experimental.js
index 276beb8ed0..8ce1fd4749 100644
--- a/api/server/experimental.js
+++ b/api/server/experimental.js
@@ -85,7 +85,6 @@ const { getAppConfig } = require('./services/Config');
const staticCache = require('./utils/staticCache');
const optionalJwtAuth = require('./middleware/optionalJwtAuth');
const noIndex = require('./middleware/noIndex');
-const routes = require('./routes');
const agentEventMethods = require('~/models');
/** Route admin file-config MIME patterns through a linear-time engine (ReDoS-safe) on upload. */
@@ -517,6 +516,10 @@ if (cluster.isMaster) {
await updateInterfacePerms({ appConfig, getRoleByName, updateAccessPermissions });
});
+ /* Route modules build their rate limiters as they load, so they load only after the
+ * startup checks have applied `rateLimits` from librechat.yaml. */
+ const routes = require('./routes');
+
/** Load index.html for SPA serving */
const indexPath = path.join(appConfig.paths.dist, 'index.html');
let indexHTML = fs.readFileSync(indexPath, 'utf8');
diff --git a/api/server/index.js b/api/server/index.js
index fa3412b0f7..8477c3b57e 100644
--- a/api/server/index.js
+++ b/api/server/index.js
@@ -87,7 +87,6 @@ const createSpaFallback = require('./utils/fallback');
const { getAppConfig } = require('./services/Config');
const staticCache = require('./utils/staticCache');
const noIndex = require('./middleware/noIndex');
-const routes = require('./routes');
const agentEventMethods = require('~/models');
/** Route admin file-config MIME patterns through a linear-time engine (ReDoS-safe) on upload. */
@@ -273,6 +272,10 @@ const startServer = async () => {
await updateInterfacePermissions({ appConfig, getRoleByName, updateAccessPermissions });
});
+ /* Route modules build their rate limiters as they load, so they load only after the
+ * startup checks have applied `rateLimits` from librechat.yaml. */
+ const routes = require('./routes');
+
const indexPath = path.join(appConfig.paths.dist, 'index.html');
let indexHTML = fs.readFileSync(indexPath, 'utf8');
diff --git a/e2e/config/librechat.e2e.yaml b/e2e/config/librechat.e2e.yaml
index ac380839b7..832c49345a 100644
--- a/e2e/config/librechat.e2e.yaml
+++ b/e2e/config/librechat.e2e.yaml
@@ -28,6 +28,15 @@ memory:
personalize: true
tokenLimit: 10000
+# Budgets that exist only here, not in the environment, so
+# yaml-rate-limits.spec.ts proves librechat.yaml reaches the limiters that route
+# modules build while loading. No other spec calls the speech routes.
+rateLimits:
+ stt:
+ userMax: 2
+ tts:
+ userMax: 2
+
mcpSettings:
# Deliberately excludes 127.0.0.1, so the URL-based `e2e-http` server below is blocked
# at boot (stored as inspectionFailed). mcp-allowlist-override.spec.ts adds that origin
diff --git a/e2e/specs/mock/scenarios/yaml-rate-limits.spec.ts b/e2e/specs/mock/scenarios/yaml-rate-limits.spec.ts
new file mode 100644
index 0000000000..b5655980f3
--- /dev/null
+++ b/e2e/specs/mock/scenarios/yaml-rate-limits.spec.ts
@@ -0,0 +1,75 @@
+import { randomUUID } from 'crypto';
+import { expect, test } from '@playwright/test';
+import type { APIRequestContext } from '@playwright/test';
+import cleanupUser from '../../../setup/cleanupUser';
+import type { User } from '../../../types';
+
+/**
+ * `rateLimits` in librechat.yaml is applied during startup, and the speech routes
+ * build their limiters when their module loads. The harness sets `userMax: 2` for
+ * `stt` and `tts` in the yaml only, against a built-in default of 50, so the third
+ * request from a fresh user is refused only when the yaml budget reached the
+ * limiter.
+ */
+
+const YAML_USER_MAX = 2;
+
+async function registerAndLogin(request: APIRequestContext, user: User): Promise<string> {
+ const registerResponse = await request.post('/api/auth/register', {
+ data: {
+ email: user.email,
+ name: user.name,
+ password: user.password,
+ confirm_password: user.password,
+ },
+ });
+ expect(registerResponse.ok()).toBeTruthy();
+
+ const loginResponse = await request.post('/api/auth/login', {
+ data: { email: user.email, password: user.password },
+ });
+ expect(loginResponse.ok()).toBeTruthy();
+ const { token } = (await loginResponse.json()) as { token?: string };
+ if (!token) {
+ throw new Error('Expected login response to include a bearer token');
+ }
+ return token;
+}
+
+test.describe('rate limits from librechat.yaml', () => {
+ test('the speech routes enforce the yaml budget @scenario:yaml-rate-limits-reach-speech-limiters', async ({
+ request,
+ }) => {
+ const user: User = {
+ email: `yaml-rate-limits-${randomUUID()}@example.com`,
+ name: 'Yaml Rate Limits',
+ password: 'securepassword123',
+ };
+
+ try {
+ const token = await registerAndLogin(request, user);
+ const headers = { Authorization: `Bearer ${token}` };
+
+ for (const lane of ['stt', 'tts'] as const) {
+ const statuses: number[] = [];
+ let body: { message?: string } = {};
+ for (let attempt = 0; attempt <= YAML_USER_MAX; attempt++) {
+ const response = await request.post(`/api/files/speech/${lane}`, {
+ headers,
+ data: { input: 'hello' },
+ });
+ statuses.push(response.status());
+ if (response.status() === 429) {
+ body = (await response.json()) as { message?: string };
+ }
+ }
+
+ expect(statuses.slice(0, YAML_USER_MAX)).not.toContain(429);
+ expect(statuses[YAML_USER_MAX]).toBe(429);
+ expect(body.message).toBe(`Too many ${lane.toUpperCase()} requests. Try again later`);
+ }
+ } finally {
+ await cleanupUser(user);
+ }
+ });
+});