[Security assistant] Update custom tool name regex (#206487)

This commit is contained in:
Steph Milovic 2025-01-13 13:57:34 -07:00 committed by GitHub
parent 95b76dc12b
commit ef5977e653
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View file

@ -221,4 +221,31 @@ describe('getStructuredToolForIndexEntry', () => {
);
expect(result).toContain(`I'm sorry, but I was unable to find any information`);
});
it('should match the name regex correctly', () => {
const tool = getStructuredToolForIndexEntry({
indexEntry: getCreateKnowledgeBaseEntrySchemaMock({
type: 'index',
name: `1bad-name?`,
}) as IndexEntry,
esClient: mockEsClient,
logger: mockLogger,
});
const nameRegex = /^[a-zA-Z0-9_-]+$/;
expect(tool.lc_kwargs.name).toMatch(nameRegex);
});
it('dashes get removed before `a` is prepended', () => {
const tool = getStructuredToolForIndexEntry({
indexEntry: getCreateKnowledgeBaseEntrySchemaMock({
type: 'index',
name: `-testing`,
}) as IndexEntry,
esClient: mockEsClient,
logger: mockLogger,
});
expect(tool.lc_kwargs.name).toMatch('testing');
});
});

View file

@ -157,7 +157,11 @@ export const getStructuredToolForIndexEntry = ({
}, {});
return new DynamicStructuredTool({
name: indexEntry.name.replace(/[^a-zA-Z0-9-]/g, ''), // // Tool names expects a string that matches the pattern '^[a-zA-Z0-9-]+$'
name: indexEntry.name
// Replace invalid characters with an empty string
.replace(/[^a-zA-Z0-9_]/g, '')
// Ensure it starts with a letter. If not, prepend 'a'
.replace(/^[^a-zA-Z]/, 'a'),
description: indexEntry.description,
schema: z.object({
query: z.string().describe(indexEntry.queryDescription),