From fe5e71e08efd370c1f2e1ee27c9e0043b1551206 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:17:54 +1000 Subject: [PATCH] [8.x] [Security Solution] [Security Assistant] Fixes an unable to load page error in the Security assistant (#194488) (#194507) # Backport This will backport the following commits from `main` to `8.x`: - [[Security Solution] [Security Assistant] Fixes an unable to load page error in the Security assistant (#194488)](https://github.com/elastic/kibana/pull/194488) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Andrew Macri --- .../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,