diff --git a/imageroot/actions/configure-module/validate-input.json b/imageroot/actions/configure-module/validate-input.json index 5a36e58..2bf4456 100644 --- a/imageroot/actions/configure-module/validate-input.json +++ b/imageroot/actions/configure-module/validate-input.json @@ -78,8 +78,32 @@ }, "whitelists": { "type": "array", - "title": "whitelists", - "description": "Whitelists array of IP, network or hostname" + "items": { + "type": "string", + "anyOf": [ + { + "description": "textarea empty", + "type": "string", + "maxLength": 0 + }, + { + "description": "ipv4", + "format": "ipv4" + }, + { + "description": "cidr ipv4", + "pattern": "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\/(3[0-2]|[12]?[0-9])$" + }, + { + "description": "ipv6 and cidr ipv6", + "pattern": "^[0-9a-fA-F:]+(/[0-9]+)?$" + }, + { + "description": "hostname", + "pattern": "^[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" + } + ] + } }, "ban_local_network": { "type": "boolean", diff --git a/ui/public/i18n/en/translation.json b/ui/public/i18n/en/translation.json index ed27f98..3f81d06 100644 --- a/ui/public/i18n/en/translation.json +++ b/ui/public/i18n/en/translation.json @@ -54,7 +54,12 @@ "token_not_valid":"The token provided is not valid", "whitelists_tips": "No ban will occur for members of this list", "smarthost_is_disabled": "Email notifications are disabled", - "smarthosts_is_needed_to_send_notifications": "To receive mail notifications from Crowdsec, change the cluster's email settings" + "smarthosts_is_needed_to_send_notifications": "To receive mail notifications from Crowdsec, change the cluster's email settings", + "whitelists_pattern": "Invalid CIDR, IP or FQDN: {value}", + "whitelists_format": "Invalid CIDR, IP or FQDN: {value}", + "whitelists_string_lte": "Invalid CIDR, IP or FQDN: {value}", + "receiver_emails_string_lte": "Invalid email address: {value}", + "receiver_emails_format": "Invalid email address: {value}" }, "unban":{ "title": "Banned IP", diff --git a/ui/src/views/Settings.vue b/ui/src/views/Settings.vue index 58d6b68..face03b 100644 --- a/ui/src/views/Settings.vue +++ b/ui/src/views/Settings.vue @@ -381,72 +381,6 @@ export default { this.enroll_instance = config.enroll_instance; this.mail_configured = config.mail_configured; }, - validateConfigureModule() { - this.clearErrors(this); - let isValidationOk = true; - - function validateEmail(email) { - var re = /\S+@\S+\.\S+/; - return re.test(email); - } - function validateIpv4(test) { - //https://regex101.com/r/dT0vT3/1 - var re = - /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; - return re.test(test); - } - function validateNetworkIpv4(test) { - var re = - /^(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]*\d),)*(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]*\d)$/; - return re.test(test); - } - function validateNetworkIpv6(test) { - //https://regex101.com/r/o6qEkY/1 - var re = - /(?:(?:(?:[A-F0-9]{1,4}:){6}|(?=(?:[A-F0-9]{0,4}:){0,6}(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?![:.\w]))(([0-9A-F]{1,4}:){0,5}|:)((:[0-9A-F]{1,4}){1,5}:|:)|::(?:[A-F0-9]{1,4}:){5})(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}|(?=(?:[A-F0-9]{0,4}:){0,7}[A-F0-9]{0,4}(?![:.\w]))(([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:)|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7})(?![:.\w])\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])/; - return re.test(test); - } - function validateIpv6(test) { - //https://regex101.com/r/iP2mG8/1 - var re = - /^([0-9A-Fa-f]{0,4}:){2,7}([0-9A-Fa-f]{1,4}$|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})$/; - return re.test(test); - } - function validateHostname(test) { - var re = /^(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61})\.([a-z0-9]{2,7})$/; - return re.test(test); - } - - if (this.receiver_emails) { - const array = this.receiver_emails.split("\n"); - array.forEach((element) => { - var email = validateEmail(element.trim()); - if (!email) { - this.error.receiver_emails = - this.$t("settings.bad_email_address") + " ' " + element + " '"; - this.focusElement("receiver_emails"); - isValidationOk = false; - } - }); - } - if (this.whitelists) { - const array = this.whitelists.split("\n"); - array.forEach((element) => { - var hostname = validateHostname(element.trim().toLowerCase()); - var ipv4 = validateIpv4(element.trim()); - var ipv6 = validateIpv6(element.trim()); - var NetworkIPV4 = validateNetworkIpv4(element.trim()); - var NetworkIPV6 = validateNetworkIpv6(element.trim()); - if (!hostname && !ipv4 && !ipv6 && !NetworkIPV4 && !NetworkIPV6) { - this.error.whitelists = - this.$t("settings.bad_IP_or_hostname") + " ' " + element + " '"; - this.focusElement("whitelists"); - isValidationOk = false; - } - }); - } - return isValidationOk; - }, configureModuleValidationFailed(validationErrors) { this.loading.configureModule = false; @@ -454,15 +388,13 @@ export default { const param = validationError.parameter; // set i18n error message - this.error[param] = this.$t("settings." + validationError.error); + this.error[param] = this.$t("settings." + validationError.error, { + value: validationError.value, + }); } }, async configureModule() { - const isValidationOk = this.validateConfigureModule(); - if (!isValidationOk) { - return; - } - + this.clearErrors(this); this.loading.configureModule = true; const taskAction = "configure-module"; const eventId = this.getUuid();