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
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🧹 fix: Stop Cache Audit From Logging Cache Keys and Values (#16995)
Marco Berettaf7a2ed1f9708
diff --git a/api/cache/getLogStores.js b/api/cache/getLogStores.jsindex a9916f3b4b..f977e017b7 100644--- a/api/cache/getLogStores.js+++ b/api/cache/getLogStores.js@@ -1,4 +1,5 @@const { Keyv } = require('keyv');+const { logger } = require('@librechat/data-schemas');const { Time, CacheKeys, ViolationTypes } = require('librechat-data-provider');const {logFile,@@ -9,6 +10,8 @@ const {violationCache,userPrincipalsCache,registerShutdownTask,+ auditCacheStores,+ formatMemoryUsage,} = require('@librechat/api');/** No-op store for cache namespaces that are deliberately disabled. */@@ -126,18 +129,20 @@ async function clearExpiredFromCache(cache) {const deleted = await cache.opts.store.delete(key);if (!deleted) {cacheConfig.DEBUG_MEMORY_CACHE &&- console.warn(`[Cache] Error deleting entry: ${key} from ${cache.opts.namespace}`);+ logger.warn(`[Cache] Error deleting entry from ${cache.opts.namespace}`);continue;}cleared++;}} catch (error) {cacheConfig.DEBUG_MEMORY_CACHE &&- console.log(`[Cache] Error processing entry from ${cache.opts.namespace}:`, error);+ logger.info(`[Cache] Error processing entry from ${cache.opts.namespace}`, {+ error: error?.name,+ });const deleted = await cache.opts.store.delete(key);if (!deleted) {cacheConfig.DEBUG_MEMORY_CACHE &&- console.warn(`[Cache] Error deleting entry: ${key} from ${cache.opts.namespace}`);+ logger.warn(`[Cache] Error deleting entry from ${cache.opts.namespace}`);continue;}cleared++;@@ -146,32 +151,13 @@ async function clearExpiredFromCache(cache) {if (cleared > 0) {cacheConfig.DEBUG_MEMORY_CACHE &&- console.log(+ logger.info(`[Cache] Cleared ${cleared} entries older than ${ttl}ms from ${cache.opts.namespace}`,);}}-const auditCache = () => {- const ttlStores = getTTLStores();- console.log('[Cache] Starting audit');-- ttlStores.forEach((store) => {- if (!store?.opts?.store?.entries) {- return;- }-- console.log(`[Cache] ${store.opts.namespace} entries:`, {- count: store.opts.store.size,- ttl: store.opts.ttl,- keys: Array.from(store.opts.store.keys()),- entriesWithTimestamps: Array.from(store.opts.store.entries()).map(([key, value]) => ({- key,- value,- })),- });- });-};+const auditCache = () => auditCacheStores(getTTLStores(), logger);/*** Clears expired entries from all TTL-enabled stores@@ -203,13 +189,7 @@ if (!cacheConfig.USE_REDIS && !cacheConfig.CI) {const memory = process.memoryUsage();const totalSize = ttlStores.reduce((sum, store) => sum + (store.opts?.store?.size ?? 0), 0);- console.log('[Cache] Memory usage:', {- heapUsed: `${(memory.heapUsed / 1024 / 1024).toFixed(2)} MB`,- heapTotal: `${(memory.heapTotal / 1024 / 1024).toFixed(2)} MB`,- rss: `${(memory.rss / 1024 / 1024).toFixed(2)} MB`,- external: `${(memory.external / 1024 / 1024).toFixed(2)} MB`,- totalCacheEntries: totalSize,- });+ logger.info(formatMemoryUsage(memory, totalSize));auditCache();}, Time.ONE_MINUTE);@@ -221,11 +201,11 @@ if (!cacheConfig.USE_REDIS && !cacheConfig.CI) {// (see packages/api/src/app/shutdown.ts) rather than attaching a direct// signal handler — multiple competing handlers race the HTTP drain.registerShutdownTask('cache cleanup', async () => {- cacheConfig.DEBUG_MEMORY_CACHE && console.log('[Cache] Cleaning up and shutting down...');+ cacheConfig.DEBUG_MEMORY_CACHE && logger.info('[Cache] Cleaning up and shutting down...');cleanupIntervals.forEach((interval) => clearInterval(interval));cleanupIntervals.clear();await clearAllExpiredFromCache();- cacheConfig.DEBUG_MEMORY_CACHE && console.log('[Cache] Final cleanup completed');+ cacheConfig.DEBUG_MEMORY_CACHE && logger.info('[Cache] Final cleanup completed');});}@@ -245,3 +225,4 @@ const getLogStores = (key) => {};module.exports = getLogStores;+module.exports.auditCache = auditCache;diff --git a/api/cache/getLogStores.spec.js b/api/cache/getLogStores.spec.jsnew file mode 100644index 0000000000..a6bd4f63a8--- /dev/null+++ b/api/cache/getLogStores.spec.js@@ -0,0 +1,27 @@+const { logger } = require('@librechat/data-schemas');+const { CacheKeys } = require('librechat-data-provider');+const getLogStores = require('./getLogStores');++describe('auditCache', () => {+ it('logs counts and TTL info without cache keys or values', async () => {+ const secretKey = 'user-id-1234567890';+ const secretValue = 'super-secret-access-token';+ const store = getLogStores(CacheKeys.ADMIN_OAUTH_EXCHANGE);+ await store.set(secretKey, { access_token: secretValue });++ const info = jest.spyOn(logger, 'info').mockImplementation(() => {});+ const log = jest.spyOn(console, 'log').mockImplementation(() => {});+ getLogStores.auditCache();++ const output = JSON.stringify(info.mock.calls);+ expect(output).toContain(CacheKeys.ADMIN_OAUTH_EXCHANGE);+ expect(output).toContain('count=1');+ expect(output).not.toContain(secretValue);+ expect(output).not.toContain(secretKey);+ expect(log).not.toHaveBeenCalled();++ info.mockRestore();+ log.mockRestore();+ await store.clear();+ });+});diff --git a/e2e/specs/mock/scenarios/cache-audit-redaction.spec.ts b/e2e/specs/mock/scenarios/cache-audit-redaction.spec.tsnew file mode 100644index 0000000000..fa539f16b7--- /dev/null+++ b/e2e/specs/mock/scenarios/cache-audit-redaction.spec.ts@@ -0,0 +1,49 @@+import { resolve } from 'node:path';+import { expect, test } from '@playwright/test';+import { run, repoRoot, inOneProject } from './lint.helpers';++/**+ * The in-memory cache audit is an operator diagnostic behind+ * DEBUG_MEMORY_CACHE. It reports how many entries each store holds and their+ * TTL, which is what an operator needs to size a cache. The keys and values are+ * what it must never print: they carry user identifiers, OAuth exchange+ * tokens, and message content, and the log sink retains them.+ */++const PROBE = `+process.env.USE_REDIS = 'false';+const { logger } = require('@librechat/data-schemas');+const { CacheKeys } = require('librechat-data-provider');+const getLogStores = require('./cache/getLogStores');+(async () => {+ const lines = [];+ const capture = (...args) => lines.push(JSON.stringify(args));+ logger.info = capture;+ logger.debug = capture;+ console.log = capture;+ const store = getLogStores(CacheKeys.ADMIN_OAUTH_EXCHANGE);+ await store.set('probe-user-key-1234', { access_token: 'probe-secret-token' });+ getLogStores.auditCache();+ process.stdout.write('RESULT' + JSON.stringify(lines));+ process.exit(0);+})();+`;++test.describe('the cache audit log', () => {+ test.beforeEach(() => inOneProject());++ test('the cache audit logs entry counts without keys or values @scenario:the-cache-audit-logs-counts-without-keys-or-values', () => {+ test.setTimeout(120_000);+ const result = run('node', ['-e', PROBE], {+ cwd: resolve(repoRoot, 'api'),+ });+ expect(result.status, result.output).toBe(0);+ const lines: string[] = JSON.parse(+ result.stdout.slice(result.stdout.indexOf('RESULT') + 'RESULT'.length),+ );+ const output = lines.join('\n');+ expect(output).toContain('count=1');+ expect(output).not.toContain('probe-secret-token');+ expect(output).not.toContain('probe-user-key-1234');+ });+});diff --git a/packages/api/src/cache/audit.spec.ts b/packages/api/src/cache/audit.spec.tsnew file mode 100644index 0000000000..05828cc829--- /dev/null+++ b/packages/api/src/cache/audit.spec.ts@@ -0,0 +1,36 @@+import { auditCacheStores, formatMemoryUsage } from './audit';++describe('auditCacheStores', () => {+ it('puts count and ttl in the message and never reads keys or values', () => {+ const messages: string[] = [];+ const store = new Map([['user-key-1', { access_token: 'secret-token' }]]);+ const keys = jest.spyOn(store, 'keys');+ const values = jest.spyOn(store, 'values');+ const entries = jest.spyOn(store, 'entries');++ auditCacheStores([{ opts: { namespace: 'TEST', ttl: 1000, store } }, { opts: {} }], {+ info: (message) => messages.push(message),+ });++ expect(messages).toEqual([+ '[Cache] Starting audit',+ '[Cache] TEST entries: count=1, ttl=1000ms',+ ]);+ expect(keys).not.toHaveBeenCalled();+ expect(values).not.toHaveBeenCalled();+ expect(entries).not.toHaveBeenCalled();+ });+});++describe('formatMemoryUsage', () => {+ it('renders every figure in the message', () => {+ const mb = 1024 * 1024;+ const message = formatMemoryUsage(+ { heapUsed: mb, heapTotal: 2 * mb, rss: 3 * mb, external: 4 * mb } as NodeJS.MemoryUsage,+ 7,+ );+ expect(message).toContain('heapUsed=1.00 MB');+ expect(message).toContain('rss=3.00 MB');+ expect(message).toContain('totalCacheEntries=7');+ });+});diff --git a/packages/api/src/cache/audit.ts b/packages/api/src/cache/audit.tsnew file mode 100644index 0000000000..1ee847b53b--- /dev/null+++ b/packages/api/src/cache/audit.ts@@ -0,0 +1,32 @@+interface AuditableStore {+ opts?: { namespace?: string; ttl?: number; store?: { size?: number; entries?: unknown } };+}++interface AuditLogger {+ info: (message: string) => void;+}++const toMB = (bytes: number) => `${(bytes / 1024 / 1024).toFixed(2)} MB`;++/**+ * Logs per-namespace entry counts and TTL only. Cache values and keys can hold+ * tokens, challenges, message content, or user identifiers, so neither is ever output.+ * Statistics are part of the message because the console formatter drops log metadata.+ */+export function auditCacheStores(stores: AuditableStore[], logger: AuditLogger): void {+ logger.info('[Cache] Starting audit');+ for (const store of stores) {+ if (!store?.opts?.store?.entries) {+ continue;+ }+ const { namespace, ttl, store: entries } = store.opts;+ logger.info(`[Cache] ${namespace} entries: count=${entries.size}, ttl=${ttl}ms`);+ }+}++export function formatMemoryUsage(memory: NodeJS.MemoryUsage, totalCacheEntries: number): string {+ return (+ `[Cache] Memory usage: heapUsed=${toMB(memory.heapUsed)}, heapTotal=${toMB(memory.heapTotal)}, ` ++ `rss=${toMB(memory.rss)}, external=${toMB(memory.external)}, totalCacheEntries=${totalCacheEntries}`+ );+}diff --git a/packages/api/src/cache/index.ts b/packages/api/src/cache/index.tsindex 473be450b1..89a1156f0f 100644--- a/packages/api/src/cache/index.ts+++ b/packages/api/src/cache/index.ts@@ -7,3 +7,4 @@ export * from './principals';export * from './pullRequests';export * from './redisUtils';export * from './redisTelemetry';+export * from './audit';