🔤 fix: Validate MIME Patterns With the Active Regex Engine (#16177)

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-09-21 21:14:56 -04:00 • committed by GitHub
parent 9efb2cb7b0
commit e8b88a2fdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 1 deletions

View file

@ -567,7 +567,20 @@ export const fileConfig = {
},
};
const supportedMimeTypesSchema = z.array(z.string()).optional();
const supportedMimeTypesSchema = z
.array(
z.string().superRefine((pattern, context) => {
try {
compileMimeRegex(pattern);
} catch {
context.addIssue({
code: z.ZodIssueCode.custom,
message: 'Invalid MIME type regex: not supported by the configured regex engine',
});
}
}),
)
.optional();
export const DefaultLLMDeliveryPath = z.enum(['provider', 'text', 'none']);
export type TDefaultLLMDeliveryPath = z.infer<typeof DefaultLLMDeliveryPath>;

View file

@ -0,0 +1,79 @@
import { RE2JS } from 're2js';
import {
fileConfigSchema,
mergeFileConfig,
convertStringsToRegex,
setFileConfigRegexCompiler,
} from './file-config';
import { configSchema } from './config';
describe('MIME pattern config validation', () => {
afterEach(() => {
setFileConfigRegexCompiler((pattern) => new RegExp(pattern));
jest.restoreAllMocks();
});
it('uses the active compiler at parse time, including schemas created before startup', () => {
const config = { endpoints: { default: { supportedMimeTypes: ['^text/(?=plain$)plain$'] } } };
expect(fileConfigSchema.safeParse(config).success).toBe(true);
setFileConfigRegexCompiler((pattern) => RE2JS.compile(pattern));
expect(fileConfigSchema.safeParse(config).success).toBe(false);
});
it.each(['(?=text)', '(?!image)', '(?<=text)/plain', '(text)/\\1', '['])(
'reports the full config path for an unsupported pattern: %s',
(pattern) => {
setFileConfigRegexCompiler((value) => RE2JS.compile(value));
const result = configSchema.safeParse({
version: '1.3.16',
fileConfig: {
endpoints: { default: { supportedMimeTypes: ['application/pdf', pattern] } },
},
});
expect(result.success).toBe(false);
if (result.success) {
throw new Error('Expected an invalid MIME pattern to fail config validation');
}
expect(result.error.issues).toEqual(
expect.arrayContaining([
expect.objectContaining({
path: ['fileConfig', 'endpoints', 'default', 'supportedMimeTypes', 1],
message: 'Invalid MIME type regex: not supported by the configured regex engine',
}),
]),
);
},
);
it.each(['ocr', 'text'])('validates %s MIME lists too', (section) => {
setFileConfigRegexCompiler((pattern) => RE2JS.compile(pattern));
const result = fileConfigSchema.safeParse({ [section]: { supportedMimeTypes: ['(?=text)'] } });
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0].path).toEqual([section, 'supportedMimeTypes', 0]);
}
});
it('preserves supported patterns, empty lists, and omitted configuration', () => {
setFileConfigRegexCompiler((pattern) => RE2JS.compile(pattern));
expect(fileConfigSchema.safeParse({}).success).toBe(true);
expect(
fileConfigSchema.safeParse({ endpoints: { default: { supportedMimeTypes: [] } } }).success,
).toBe(true);
const config = {
endpoints: { default: { supportedMimeTypes: ['^text/plain$', 'application/pdf'] } },
};
expect(fileConfigSchema.parse(config)).toEqual(config);
const matchers = mergeFileConfig(config).endpoints.default.supportedMimeTypes;
expect(matchers?.some((matcher) => matcher.test('text/plain'))).toBe(true);
expect(matchers?.some((matcher) => matcher.test('image/png'))).toBe(false);
});
it('still fails closed if a caller bypasses schema validation', () => {
setFileConfigRegexCompiler((pattern) => RE2JS.compile(pattern));
jest.spyOn(console, 'error').mockImplementation(() => {});
const matchers = convertStringsToRegex(['(?=text)']);
expect(matchers).toHaveLength(1);
expect(matchers[0].test('text/plain')).toBe(false);
});
});