Skip to content

Commit a6ccc74

Browse files
committed
fix(web): handle arbitrary model key strings
1 parent 7a9c849 commit a6ccc74

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

‎apps/web/src/components/chat/modelPickerKeys.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@ describe("model picker item keys", () => {
1919
slug: "codex",
2020
});
2121
});
22+
23+
it("round-trips arbitrary strings without throwing", () => {
24+
const instanceId = ProviderInstanceId.make("custom");
25+
const slug = "model:\udfff";
26+
27+
const key = modelPickerModelKey(instanceId, slug);
28+
29+
expect(parseModelPickerModelKey(key)).toEqual({ instanceId, slug });
30+
});
2231
});

‎apps/web/src/components/chat/modelPickerKeys.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const MODEL_KEY_PREFIX = "model:";
44
const LEGACY_SECTION_KEY_PREFIX = "legacy-models:";
55

66
export function modelPickerModelKey(instanceId: ProviderInstanceId, slug: string): string {
7-
return `${MODEL_KEY_PREFIX}${encodeURIComponent(instanceId)}:${encodeURIComponent(slug)}`;
7+
return `${MODEL_KEY_PREFIX}${instanceId.length}:${instanceId}${slug}`;
88
}
99

1010
export function parseModelPickerModelKey(
@@ -18,14 +18,22 @@ export function parseModelPickerModelKey(
1818
if (separatorIndex === -1) {
1919
return null;
2020
}
21-
try {
22-
return {
23-
instanceId: decodeURIComponent(encoded.slice(0, separatorIndex)) as ProviderInstanceId,
24-
slug: decodeURIComponent(encoded.slice(separatorIndex + 1)),
25-
};
26-
} catch {
21+
22+
const instanceIdLengthText = encoded.slice(0, separatorIndex);
23+
if (!/^\d+$/.test(instanceIdLengthText)) {
2724
return null;
2825
}
26+
27+
const instanceIdLength = Number(instanceIdLengthText);
28+
const value = encoded.slice(separatorIndex + 1);
29+
if (!Number.isSafeInteger(instanceIdLength) || instanceIdLength > value.length) {
30+
return null;
31+
}
32+
33+
return {
34+
instanceId: value.slice(0, instanceIdLength) as ProviderInstanceId,
35+
slug: value.slice(instanceIdLength),
36+
};
2937
}
3038

3139
export function modelPickerLegacySectionKey(instanceId: ProviderInstanceId): string {

0 commit comments

Comments
 (0)