From 3d9eb0a8f6ffacd63a622ee665ce32b31ef00fcb Mon Sep 17 00:00:00 2001 From: Andrew Macri Date: Mon, 30 Sep 2024 20:49:52 -0400 Subject: [PATCH] [Security Solution] [Security Assistant] Fixes an unable to load page error in the Security assistant (#194488) ### [Security Solution] [Security Assistant] Fixes an unable to load page error in the Security assistant This PR fixes an `Unable to load page error` in the Security Assistant, introduced in , where a check that appeared to be optional was removed. - The issue occurs when the assistant is asked a question that is likely to require anonymization, for example: `which alerts should I look at first?` - After the error occurs, re-opening the assistant for the same conversation may re-trigger the error - As a workaround, clear Kibana local storage for the current user, or via the browser's dev tools, specifically delete the `elasticAssistantDefault.lastConversationId` setting illustrated by the screenshot below: ![local_storage_key](https://github.com/user-attachments/assets/882f68e2-a253-49d7-84ad-fe2326bbfe20) ### Steps to reproduce To reproduce: 1) Ask the assistant a question that is likely to cause anonymized alerts to be returned, for example: `which alerts should I look at first?` **Expected result** - Anonymized alerts are displayed **Actual result** - An `Unable to load page` error is displayed, as illustrated by the screenshot below: ![unable_to_load_page](https://github.com/user-attachments/assets/54006a07-5a79-45d6-97cb-f36e0a71361f) - The error details include text similar to the output below: ``` TypeError: Cannot convert undefined or null to object at Function.keys () at replaceAnonymizedValuesWithOriginalValues (http://localhost:5601/XXXXXXXXXXXX/bundles/plugin/securitySolution/1.0.0/securitySolution.chunk.lazy_application_dependencies.js:60965:14) at transformMessageWithReplacements (http://localhost:5601/XXXXXXXXXXXX/bundles/plugin/securitySolution/1.0.0/securitySolution.chunk.lazy_application_dependencies.js:100511:158) at transformMessage (http://localhost:5601/XXXXXXXXXXXX/bundles/plugin/securitySolution/1.0.0/securitySolution.chunk.lazy_application_dependencies.js:100612:41) at http://localhost:5601/XXXXXXXXXXXX/bundles/plugin/securitySolution/1.0.0/securitySolution.chunk.lazy_application_dependencies.js:100638:32 at Array.map () at getComments (http://localhost:5601/XXXXXXXXXXXX/bundles/plugin/securitySolution/1.0.0/securitySolution.chunk.lazy_application_dependencies.js:100595:45) at http://localhost:5601/XXXXXXXXXXXX/bundles/plugin/securitySolution/1.0.0/securitySolution.chunk.lazy_application_dependencies.js:68997:15 at updateMemo (http://localhost:5601/XXXXXXXXXXXX/bundles/kbn-ui-shared-deps-npm/kbn-ui-shared-deps-npm.dll.js:369829:19) at Object.useMemo (http://localhost:5601/XXXXXXXXXXXX/bundles/kbn-ui-shared-deps-npm/kbn-ui-shared-deps-npm.dll.js:370375:16) The above error occurred in AssistantComponent: at AssistantComponent (http://localhost:5601/XXXXXXXXXXXX/bundles/plugin/securitySolution/1.0.0/securitySolution.chunk.lazy_application_dependencies.js:68776:3) at div at http://localhost:5601/XXXXXXXXXXXX/bundles/kbn-ui-shared-deps-npm/kbn-ui-shared-deps-npm.dll.js:160008:73 at div at http://loc ``` - The following video illustrates the error: (cherry picked from commit 35233ba890558a8251b71a0fba33d8f42fae662d) --- .../data_anonymization/helpers/index.test.ts | 24 +++++++++++++++++++ .../impl/data_anonymization/helpers/index.ts | 12 ++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.test.ts b/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.test.ts index 02294bdd870f5..7048ab442cafa 100644 --- a/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.test.ts +++ b/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.test.ts @@ -123,6 +123,30 @@ describe('helpers', () => { expect(result).toEqual(messageContent); }); + it('returns the original messageContent if replacements is null', () => { + const messageContent = + 'User {{ user.name 3541b730-1dce-4937-b63f-0d618ea1cc5f }} added a member to the Administrators group on host {{ host.name b222e892-431e-4e4f-9295-2ba92ef9d12d }}'; + + const result = replaceAnonymizedValuesWithOriginalValues({ + messageContent, + replacements: null, // <-- null + }); + + expect(result).toEqual(messageContent); + }); + + it('returns the original messageContent if replacements is undefined', () => { + const messageContent = + 'User {{ user.name 3541b730-1dce-4937-b63f-0d618ea1cc5f }} added a member to the Administrators group on host {{ host.name b222e892-431e-4e4f-9295-2ba92ef9d12d }}'; + + const result = replaceAnonymizedValuesWithOriginalValues({ + messageContent, + replacements: undefined, // <-- undefined + }); + + expect(result).toEqual(messageContent); + }); + it('replaces multiple occurrences of the same replacement key', () => { const messageContent = 'User {{ user.name 3541b730-1dce-4937-b63f-0d618ea1cc5f }} added a member to the Administrators group on host {{ host.name b222e892-431e-4e4f-9295-2ba92ef9d12d }}, which is unusual because {{ user.name 3541b730-1dce-4937-b63f-0d618ea1cc5f }} is not a member of the Administrators group.'; diff --git a/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.ts b/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.ts index 62f190f3d37e4..6b17b2891635a 100644 --- a/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.ts +++ b/x-pack/packages/kbn-elastic-assistant-common/impl/data_anonymization/helpers/index.ts @@ -40,13 +40,15 @@ export const replaceAnonymizedValuesWithOriginalValues = ({ replacements, }: { messageContent: string; - replacements: Replacements; + replacements: Replacements | null | undefined; }): string => - Object.keys(replacements).reduce((acc, key) => { - const value = replacements[key]; + replacements != null + ? Object.keys(replacements).reduce((acc, key) => { + const value = replacements[key]; - return acc.replaceAll(key, value); - }, messageContent); + return acc.replaceAll(key, value); + }, messageContent) + : messageContent; export const replaceOriginalValuesWithUuidValues = ({ messageContent,