Skip to content

Commit d6069ed

Browse files
authored
Merge pull request #10380 from uinstinct/validate-unicode
fix: validate unicode in model apikey or headers
2 parents 1e9deb7 + 8160711 commit d6069ed

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

packages/config-yaml/src/validation.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export interface ConfigResult<T> {
1212
configLoadInterrupted: boolean;
1313
}
1414

15+
function containsUnicode(str: string): boolean {
16+
return /[^\x00-\x7F]/.test(str);
17+
}
18+
1519
export function validateConfigYaml(
1620
config: ConfigYaml,
1721
): ConfigValidationError[] {
@@ -32,6 +36,25 @@ export function validateConfigYaml(
3236
if ("uses" in model) {
3337
return;
3438
}
39+
40+
// request headers to the llm api should not contain unicode characters
41+
if (model.apiKey && containsUnicode(model.apiKey)) {
42+
errors.push({
43+
fatal: true,
44+
message: `Model "${model.name}" has an API key containing unicode characters. API keys should only contain ASCII characters.`,
45+
});
46+
}
47+
48+
if (model.requestOptions?.headers) {
49+
for (const [key, value] of Object.entries(model.requestOptions.headers)) {
50+
if (containsUnicode(key) || containsUnicode(value)) {
51+
errors.push({
52+
fatal: true,
53+
message: `Model "${model.name}" has a request header "${key}" containing unicode characters. Request headers should only contain ASCII characters.`,
54+
});
55+
}
56+
}
57+
}
3558
// Max tokens not too close to context length
3659
if (
3760
model.defaultCompletionOptions?.contextLength &&

0 commit comments

Comments
 (0)