-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] [AI Assistant] Replace polynomial regular express…
…ion with constant time string manipulation (#209314) ## Summary Fixes elastic/kibana-team#1442 This PR replaces a poorly performing regular expression with a constant time string manipulation approach. Context: The regex is used to remove all references from a string when a user copies a message from the assistant and when conversation history is passed to the LLM e.g. ``` "The sky is blue{reference(1234)} and the grass is green{reference(4321)}" -> "The sky is blue and the grass is green" ``` Changes: - Replace the regular expression inside of `removeContentReferences()` - Add tests to verify new logic is correct. - Fix a bug in the contentReference markdown parser that was found by @andrew-goldstein [here](https://github.com/elastic/kibana/pull/209314/files#r1943198510) - For alerts page citations, add a filter for open and acknowledge alerts within the last 24 hours [here](https://github.com/elastic/kibana/pull/209314/files#diff-f17fbe7edfe72943fecbe5ddd8dca6c024a48fe4f90bf4f66650cef16091b769R36) ### How to test new regex: One of the changes in this PR improves the performance of a regex. In real life, no one has ever reached any performance issues with this regex's and I don't think it is realistically possible to reach that limit without other things breaking (i.e. the message sent to/returned by the assistant would need to be so large that it would exceed the context window). Therefore, all we can test is that the functionality still works as expected after this change. - Enable the feature flag ```yaml # kibana.dev.yml xpack.securitySolution.enableExperimental: ['contentReferencesEnabled'] ``` - Open the security assistant - Ask the assistant a question about your alerts or a document in your KB. The assistant response should contain citations. - Copy the response to the clipboard using the copy button. <img width="785" alt="image" src="https://github.com/user-attachments/assets/edded3a3-8cb9-40a8-918e-a9718e7afc22" /> - Your clipboard should contain the response without any citations ### How to test the alerts page filter - Ask a question about your open alerts and make sure a citation is returned. - Click on the citation - Verify a new tab is opened and the alerts page is visible with a filter for open and acknowledge alerts and there is a now-24h time window filter. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [X] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [X] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [X] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [X] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [X] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [X] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [X] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
8831e5b
commit 2bf8a24
Showing
9 changed files
with
212 additions
and
29 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...ages/shared/kbn-elastic-assistant-common/impl/content_references/references/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { removeContentReferences } from './utils'; | ||
|
||
describe('utils', () => { | ||
it.each([ | ||
['this has no content references', 'this has no content references'], | ||
[ | ||
'The sky is blue{reference(1234)} and the grass is green{reference(4321)}', | ||
'The sky is blue and the grass is green', | ||
], | ||
['', ''], | ||
['{reference(1234)}', ''], | ||
[' {reference(1234)} ', ' '], | ||
['{reference(1234', '{reference(1234'], | ||
['{reference(1234)', '{reference(1234)'], | ||
['{reference(1234)}{reference(1234)}{reference(1234)}', ''], | ||
['{reference(1234)}reference(1234)}{reference(1234)}', 'reference(1234)}'], | ||
])('removesContentReferences from "%s"', (input: string, expected: string) => { | ||
const result = removeContentReferences(input); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
// https://github.com/elastic/kibana/security/code-scanning/539 | ||
it('removesContentReferences does not run in polynomial time', () => { | ||
const input = `${'{reference('.repeat(100000)}x${')'.repeat(100000)}`; | ||
const startTime = performance.now(); // Start timing | ||
|
||
removeContentReferences(input); | ||
|
||
const endTime = performance.now(); // End timing | ||
const executionTime = endTime - startTime; // Time in milliseconds | ||
|
||
expect(executionTime).toBeLessThan(1000); // Assert under 1 second | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
x-pack/solutions/security/plugins/elastic_assistant/server/lib/prompt/prompts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
BEDROCK_SYSTEM_PROMPT, | ||
DEFAULT_SYSTEM_PROMPT, | ||
GEMINI_SYSTEM_PROMPT, | ||
STRUCTURED_SYSTEM_PROMPT, | ||
} from './prompts'; | ||
|
||
describe('prompts', () => { | ||
it.each([ | ||
[DEFAULT_SYSTEM_PROMPT, '{include_citations_prompt_placeholder}', 1], | ||
[GEMINI_SYSTEM_PROMPT, '{include_citations_prompt_placeholder}', 1], | ||
[BEDROCK_SYSTEM_PROMPT, '{include_citations_prompt_placeholder}', 1], | ||
[STRUCTURED_SYSTEM_PROMPT, '{include_citations_prompt_placeholder}', 1], | ||
[DEFAULT_SYSTEM_PROMPT, 'You are a security analyst', 1], | ||
[GEMINI_SYSTEM_PROMPT, 'You are an assistant', 1], | ||
[BEDROCK_SYSTEM_PROMPT, 'You are a security analyst', 1], | ||
])( | ||
'"%s" contains "%s" %s times', | ||
(prompt: string, containedString: string, expectedCount: number) => { | ||
const regex = new RegExp(containedString, 'g'); | ||
expect((prompt.match(regex) || []).length).toBe(expectedCount); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters