From 907ed7cd6272a25cfc4c745bb591b50abb726ccf Mon Sep 17 00:00:00 2001 From: Nico Guevara <42900763+Tostti@users.noreply.github.com> Date: Thu, 22 Dec 2022 11:17:04 -0300 Subject: [PATCH] [Backport 4.4-7.16] Add length validation to agent name (#5029) * Add validation to agent name * Update changelog * Add regex to constant * Reduce number of setState * Add length validation * Update changelog --- CHANGELOG.md | 3 ++- .../agent/components/register-agent.js | 24 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 328fd1bf18..2fa844be8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,8 @@ All notable changes to the Wazuh app project will be documented in this file. - Added a centralized service to handle the requests [#4831](https://github.com/wazuh/wazuh-kibana-app/pull/4831) - Added data-test-subj create policy [#4873](https://github.com/wazuh/wazuh-kibana-app/pull/4873) - Added file saving conditions in File Editor [#4970](https://github.com/wazuh/wazuh-kibana-app/pull/4970) -- Added character validation to avoid invalid agent names in the section 'Deploy new agent'. [#5021](https://github.com/wazuh/wazuh-kibana-app/pull/5021) +- Added character validation to avoid invalid agent names in the section 'Deploy new agent'. [#5021](https://github.com/wazuh/wazuh-kibana-app/pull/5021)[#5028](https://github.com/wazuh/wazuh-kibana-app/pull/5028) + ### Changed diff --git a/public/controllers/agent/components/register-agent.js b/public/controllers/agent/components/register-agent.js index 363707ec16..d8fe7c3b2c 100644 --- a/public/controllers/agent/components/register-agent.js +++ b/public/controllers/agent/components/register-agent.js @@ -302,18 +302,26 @@ export const RegisterAgent = withErrorBoundary( setAgentName(event) { const validation = /^[a-z0-9-_.]+$/i; - this.setState({ agentName: event.target.value }); - if (validation.test(event.target.value) || event.target.value.length <= 0) { - this.setState({ agentNameError: false }); - this.setState({ badCharacters: [] }); + if ((validation.test(event.target.value) && event.target.value.length >= 2) + || event.target.value.length <= 0) { + this.setState({ + agentName: event.target.value, + agentNameError: false, + badCharacters: [] + }); + } else { let badCharacters = event.target.value.split('').map(char => char.replace(validation, '')).join(''); badCharacters = badCharacters.split('').map(char => char.replace(/\s/, 'whitespace')); const characters = [...new Set(badCharacters)]; - this.setState({ badCharacters: characters }); - this.setState({ agentNameError: true }); + this.setState({ + agentName: event.target.value, + badCharacters: characters, + agentNameError: true + }); + } } @@ -880,7 +888,9 @@ export const RegisterAgent = withErrorBoundary( ` "${char}"`)} ${this.state.badCharacters.length <= 1 ? ('is') : ('are')} not valid. Allowed characters are A-Z, a-z, ".", "-", "_"`]}>