Skip to content

Commit fd4691f

Browse files
authored
fix: use keyEncoder instead of insecure cache key getter (langchain-ai#8978)
1 parent f580633 commit fd4691f

File tree

11 files changed

+35
-36
lines changed

11 files changed

+35
-36
lines changed

.changeset/angry-ads-retire.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@langchain/azure-cosmosdb": patch
3+
"@langchain/cloudflare": patch
4+
"@langchain/community": patch
5+
"@langchain/redis": patch
6+
"langchain": patch
7+
---
8+
9+
use `keyEncoder` instead of insecure cache key getter

langchain/src/cache/file_system.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import fs from "node:fs/promises";
33

44
import {
55
BaseCache,
6-
getCacheKey,
76
serializeGeneration,
87
deserializeStoredGeneration,
98
} from "@langchain/core/caches";
@@ -46,7 +45,7 @@ export class LocalFileCache extends BaseCache {
4645
* @returns An array of Generations if found, null otherwise.
4746
*/
4847
public async lookup(prompt: string, llmKey: string) {
49-
const key = `${getCacheKey(prompt, llmKey)}.json`;
48+
const key = `${this.keyEncoder(prompt, llmKey)}.json`;
5049
try {
5150
const content = await fs.readFile(path.join(this.cacheDir, key));
5251
return JSON.parse(content.toString()).map(deserializeStoredGeneration);
@@ -68,7 +67,7 @@ export class LocalFileCache extends BaseCache {
6867
llmKey: string,
6968
generations: Generation[]
7069
) {
71-
const key = `${getCacheKey(prompt, llmKey)}.json`;
70+
const key = `${this.keyEncoder(prompt, llmKey)}.json`;
7271
await fs.writeFile(
7372
path.join(this.cacheDir, key),
7473
JSON.stringify(generations.map(serializeGeneration))

libs/langchain-azure-cosmosdb/src/caches/caches_mongodb.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
BaseCache,
33
deserializeStoredGeneration,
4-
getCacheKey,
54
serializeGeneration,
65
} from "@langchain/core/caches";
76
import { Generation } from "@langchain/core/outputs";
@@ -84,7 +83,7 @@ export class AzureCosmosDBMongoDBSemanticCache extends BaseCache {
8483
}
8584

8685
private getLlmCache(llmKey: string) {
87-
const key = getCacheKey(llmKey);
86+
const key = this.keyEncoder(llmKey);
8887
if (!this.cacheDict[key]) {
8988
this.cacheDict[key] = new AzureCosmosDBMongoDBVectorStore(
9089
this.embeddings,
@@ -170,7 +169,7 @@ export class AzureCosmosDBMongoDBSemanticCache extends BaseCache {
170169
* @param llmKey
171170
*/
172171
public async clear(llmKey: string) {
173-
const key = getCacheKey(llmKey);
172+
const key = this.keyEncoder(llmKey);
174173
if (this.cacheDict[key]) {
175174
await this.cacheDict[key].delete();
176175
}

libs/langchain-azure-cosmosdb/src/caches/caches_nosql.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
BaseCache,
33
deserializeStoredGeneration,
4-
getCacheKey,
54
serializeGeneration,
65
} from "@langchain/core/caches";
76
import { Generation } from "@langchain/core/outputs";
@@ -108,7 +107,7 @@ export class AzureCosmosDBNoSQLSemanticCache extends BaseCache {
108107
}
109108

110109
private getLlmCache(llmKey: string) {
111-
const key = getCacheKey(llmKey);
110+
const key = this.keyEncoder(llmKey);
112111
if (!this.cacheDict[key]) {
113112
this.cacheDict[key] = new AzureCosmosDBNoSQLVectorStore(
114113
this.embeddings,
@@ -183,7 +182,7 @@ export class AzureCosmosDBNoSQLSemanticCache extends BaseCache {
183182
* @param llmKey
184183
*/
185184
public async clear(llmKey: string) {
186-
const key = getCacheKey(llmKey);
185+
const key = this.keyEncoder(llmKey);
187186
if (this.cacheDict[key]) {
188187
await this.cacheDict[key].delete();
189188
}

libs/langchain-cloudflare/src/caches.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { KVNamespace } from "@cloudflare/workers-types";
22

33
import {
44
BaseCache,
5-
getCacheKey,
65
serializeGeneration,
76
deserializeStoredGeneration,
87
} from "@langchain/core/caches";
@@ -44,14 +43,14 @@ export class CloudflareKVCache extends BaseCache {
4443
*/
4544
public async lookup(prompt: string, llmKey: string) {
4645
let idx = 0;
47-
let key = getCacheKey(prompt, llmKey, String(idx));
46+
let key = this.keyEncoder(prompt, llmKey, String(idx));
4847
let value = await this.binding.get(key);
4948
const generations: Generation[] = [];
5049

5150
while (value) {
5251
generations.push(deserializeStoredGeneration(JSON.parse(value)));
5352
idx += 1;
54-
key = getCacheKey(prompt, llmKey, String(idx));
53+
key = this.keyEncoder(prompt, llmKey, String(idx));
5554
value = await this.binding.get(key);
5655
}
5756

@@ -68,7 +67,7 @@ export class CloudflareKVCache extends BaseCache {
6867
*/
6968
public async update(prompt: string, llmKey: string, value: Generation[]) {
7069
for (let i = 0; i < value.length; i += 1) {
71-
const key = getCacheKey(prompt, llmKey, String(i));
70+
const key = this.keyEncoder(prompt, llmKey, String(i));
7271
await this.binding.put(
7372
key,
7473
JSON.stringify(serializeGeneration(value[i]))

libs/langchain-community/src/caches/cloudflare_kv.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { KVNamespace } from "@cloudflare/workers-types";
22

33
import {
44
BaseCache,
5-
getCacheKey,
65
serializeGeneration,
76
deserializeStoredGeneration,
87
} from "@langchain/core/caches";
@@ -46,14 +45,14 @@ export class CloudflareKVCache extends BaseCache {
4645
*/
4746
public async lookup(prompt: string, llmKey: string) {
4847
let idx = 0;
49-
let key = getCacheKey(prompt, llmKey, String(idx));
48+
let key = this.keyEncoder(prompt, llmKey, String(idx));
5049
let value = await this.binding.get(key);
5150
const generations: Generation[] = [];
5251

5352
while (value) {
5453
generations.push(deserializeStoredGeneration(JSON.parse(value)));
5554
idx += 1;
56-
key = getCacheKey(prompt, llmKey, String(idx));
55+
key = this.keyEncoder(prompt, llmKey, String(idx));
5756
value = await this.binding.get(key);
5857
}
5958

@@ -70,7 +69,7 @@ export class CloudflareKVCache extends BaseCache {
7069
*/
7170
public async update(prompt: string, llmKey: string, value: Generation[]) {
7271
for (let i = 0; i < value.length; i += 1) {
73-
const key = getCacheKey(prompt, llmKey, String(i));
72+
const key = this.keyEncoder(prompt, llmKey, String(i));
7473
await this.binding.put(
7574
key,
7675
JSON.stringify(serializeGeneration(value[i]))

libs/langchain-community/src/caches/ioredis.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Redis } from "ioredis";
22
import {
33
BaseCache,
4-
getCacheKey,
54
serializeGeneration,
65
deserializeStoredGeneration,
76
} from "@langchain/core/caches";
@@ -49,15 +48,15 @@ export class RedisCache extends BaseCache {
4948
*/
5049
public async lookup(prompt: string, llmKey: string) {
5150
let idx = 0;
52-
let key = getCacheKey(prompt, llmKey, String(idx));
51+
let key = this.keyEncoder(prompt, llmKey, String(idx));
5352
let value = await this.redisClient.get(key);
5453
const generations: Generation[] = [];
5554

5655
while (value) {
5756
const storedGeneration = JSON.parse(value);
5857
generations.push(deserializeStoredGeneration(storedGeneration));
5958
idx += 1;
60-
key = getCacheKey(prompt, llmKey, String(idx));
59+
key = this.keyEncoder(prompt, llmKey, String(idx));
6160
value = await this.redisClient.get(key);
6261
}
6362

@@ -72,7 +71,7 @@ export class RedisCache extends BaseCache {
7271
*/
7372
public async update(prompt: string, llmKey: string, value: Generation[]) {
7473
for (let i = 0; i < value.length; i += 1) {
75-
const key = getCacheKey(prompt, llmKey, String(i));
74+
const key = this.keyEncoder(prompt, llmKey, String(i));
7675
if (this.ttl !== undefined) {
7776
await this.redisClient.set(
7877
key,

libs/langchain-community/src/caches/momento.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import {
1010
BaseCache,
1111
deserializeStoredGeneration,
12-
getCacheKey,
1312
serializeGeneration,
1413
} from "@langchain/core/caches";
1514
import { Generation } from "@langchain/core/outputs";
@@ -124,7 +123,7 @@ export class MomentoCache extends BaseCache {
124123
prompt: string,
125124
llmKey: string
126125
): Promise<Generation[] | null> {
127-
const key = getCacheKey(prompt, llmKey);
126+
const key = this.keyEncoder(prompt, llmKey);
128127
const getResponse = await this.client.get(this.cacheName, key);
129128

130129
if (getResponse instanceof CacheGet.Hit) {
@@ -157,7 +156,7 @@ export class MomentoCache extends BaseCache {
157156
llmKey: string,
158157
value: Generation[]
159158
): Promise<void> {
160-
const key = getCacheKey(prompt, llmKey);
159+
const key = this.keyEncoder(prompt, llmKey);
161160
const setResponse = await this.client.set(
162161
this.cacheName,
163162
key,

libs/langchain-community/src/caches/upstash_redis.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Generation } from "@langchain/core/outputs";
44
import {
55
BaseCache,
66
deserializeStoredGeneration,
7-
getCacheKey,
87
serializeGeneration,
98
} from "@langchain/core/caches";
109
import { StoredGeneration } from "@langchain/core/messages";
@@ -71,14 +70,14 @@ export class UpstashRedisCache extends BaseCache {
7170
*/
7271
public async lookup(prompt: string, llmKey: string) {
7372
let idx = 0;
74-
let key = getCacheKey(prompt, llmKey, String(idx));
73+
let key = this.keyEncoder(prompt, llmKey, String(idx));
7574
let value = await this.redisClient.get<StoredGeneration | null>(key);
7675
const generations: Generation[] = [];
7776

7877
while (value) {
7978
generations.push(deserializeStoredGeneration(value));
8079
idx += 1;
81-
key = getCacheKey(prompt, llmKey, String(idx));
80+
key = this.keyEncoder(prompt, llmKey, String(idx));
8281
value = await this.redisClient.get<StoredGeneration | null>(key);
8382
}
8483

@@ -92,7 +91,7 @@ export class UpstashRedisCache extends BaseCache {
9291
*/
9392
public async update(prompt: string, llmKey: string, value: Generation[]) {
9493
for (let i = 0; i < value.length; i += 1) {
95-
const key = getCacheKey(prompt, llmKey, String(i));
94+
const key = this.keyEncoder(prompt, llmKey, String(i));
9695
const serializedValue = JSON.stringify(serializeGeneration(value[i]));
9796

9897
if (this.ttl) {

libs/langchain-community/src/caches/vercel_kv.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Generation } from "@langchain/core/outputs";
44
import {
55
BaseCache,
66
deserializeStoredGeneration,
7-
getCacheKey,
87
serializeGeneration,
98
} from "@langchain/core/caches";
109
import { StoredGeneration } from "@langchain/core/messages";
@@ -54,14 +53,14 @@ export class VercelKVCache extends BaseCache {
5453
*/
5554
public async lookup(prompt: string, llmKey: string) {
5655
let idx = 0;
57-
let key = getCacheKey(prompt, llmKey, String(idx));
56+
let key = this.keyEncoder(prompt, llmKey, String(idx));
5857
let value = await this.client.get<StoredGeneration | null>(key);
5958
const generations: Generation[] = [];
6059

6160
while (value) {
6261
generations.push(deserializeStoredGeneration(value));
6362
idx += 1;
64-
key = getCacheKey(prompt, llmKey, String(idx));
63+
key = this.keyEncoder(prompt, llmKey, String(idx));
6564
value = await this.client.get<StoredGeneration | null>(key);
6665
}
6766

@@ -75,7 +74,7 @@ export class VercelKVCache extends BaseCache {
7574
*/
7675
public async update(prompt: string, llmKey: string, value: Generation[]) {
7776
for (let i = 0; i < value.length; i += 1) {
78-
const key = getCacheKey(prompt, llmKey, String(i));
77+
const key = this.keyEncoder(prompt, llmKey, String(i));
7978
const serializedValue = JSON.stringify(serializeGeneration(value[i]));
8079

8180
if (this.ttl) {

0 commit comments

Comments
 (0)