🚈 fix: Bound Git Identity Email Validation (#17389)

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-10-23 15:57:19 +02:00 • committed by GitHub
parent 8d921e6f7e
commit a7e4de8c82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 1 deletions

View file

@ -48,6 +48,31 @@ describe('agent Git identity validation', () => {
expect(agentCreateSchema.safeParse({ ...base, git_identity }).success).toBe(false);
});
it.each(['create', 'update'] as const)(
'rejects an oversized git email on %s without passing it to a regex',
(operation) => {
const schema = operation === 'create' ? agentCreateSchema : agentUpdateSchema;
const email = `1+${'a'.repeat(128_000)}!`;
const regexCheck = jest.spyOn(RegExp.prototype, 'test');
const result = schema.safeParse({ ...base, git_identity: { name: 'Lia', email } });
const regexCheckedEmail = regexCheck.mock.calls.some(([value]) => value === email);
regexCheck.mockRestore();
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues).toContainEqual(
expect.objectContaining({
code: 'too_big',
maximum: 254,
path: ['git_identity', 'email'],
}),
);
}
expect(regexCheckedEmail).toBe(false);
},
);
it('accepts null only when updating to clear a configured identity', () => {
expect(agentCreateSchema.safeParse({ ...base, git_identity: null }).success).toBe(false);
expect(agentUpdateSchema.parse({ git_identity: null })).toEqual({ git_identity: null });

View file

@ -13,7 +13,54 @@ describe('agentGitIdentitySchema', () => {
).toBe(true);
});
it.each(['a', 'ab', 'a-b', 'a--b', '0', 'Lia-By-LibreChat'])(
'accepts a bot name with valid boundaries: %s',
(name) => {
expect(
agentGitIdentitySchema.safeParse({
name: 'Lia',
email: `1+${name}[bot]@users.noreply.github.com`,
}).success,
).toBe(true);
},
);
it('accepts and trims a bot address at the 254-character limit', () => {
const suffix = '[bot]@users.noreply.github.com';
const email = `1+${'a'.repeat(254 - 2 - suffix.length)}${suffix}`;
expect(agentGitIdentitySchema.parse({ name: ' Lia ', email: ` ${email} ` })).toEqual({
name: 'Lia',
email,
});
});
it.each([255, 128_003])(
'rejects a %i-character email before either email validator runs',
(length) => {
const email = `1+${'a'.repeat(length - 3)}!`;
const regexCheck = jest.spyOn(RegExp.prototype, 'test');
const result = agentGitIdentitySchema.safeParse({ name: 'Lia', email });
const regexCheckedEmail = regexCheck.mock.calls.some(([value]) => value === email);
regexCheck.mockRestore();
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues).toContainEqual(
expect.objectContaining({ code: 'too_big', maximum: 254, path: ['email'] }),
);
}
expect(regexCheckedEmail).toBe(false);
},
);
it.each([
'1+[bot]@users.noreply.github.com',
'1+-a[bot]@users.noreply.github.com',
'1+a-[bot]@users.noreply.github.com',
'1+a_b[bot]@users.noreply.github.com',
'1+a\0[bot]@users.noreply.github.com',
'1+a\r[bot]@users.noreply.github.com',
'lia-by-librechat[bot]@users.noreply.github.com',
'328778573+lia-by-librechat[bot]@example.com',
'328778573+lia-by-librechat[bot]@users.noreply.github.com\nInjected',

View file

@ -1038,7 +1038,7 @@ export type AgentGitIdentity = {
// GitHub App bot noreply addresses contain `[bot]`, which Zod's email validator rejects.
const githubAppBotEmail =
/^\d+\+[a-z0-9]+(?:[a-z0-9-]*[a-z0-9])?\[bot\]@users\.noreply\.github\.com$/i;
/^\d+\+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\[bot\]@users\.noreply\.github\.com$/i;
const standardGitEmail = z.string().email();
export const agentGitIdentitySchema: z.ZodType<AgentGitIdentity | undefined> = z
@ -1055,6 +1055,8 @@ export const agentGitIdentitySchema: z.ZodType<AgentGitIdentity | undefined> = z
.max(254)
.refine(
(value) =>
// Zod still runs refinements after .max() fails.
value.length <= 254 &&
!/[\0\r\n]/.test(value) &&
(standardGitEmail.safeParse(value).success || githubAppBotEmail.test(value)),
),