Skip to content

Commit

Permalink
Validate textarea against FQDN (#41)
Browse files Browse the repository at this point in the history
- Fix FQDN validator
- Implement backend validation with JSON schema
- Remove frontend validation logic

Refs NethServer/dev#6900

---------
Co-authored-by: Andrea Leardini <andre8244@gmail.com>
  • Loading branch information
stephdl authored Apr 9, 2024
1 parent a314510 commit c403740
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 75 deletions.
28 changes: 26 additions & 2 deletions imageroot/actions/configure-module/validate-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion ui/public/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
76 changes: 4 additions & 72 deletions ui/src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -381,88 +381,20 @@ 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;

for (const validationError of validationErrors) {
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();
Expand Down

0 comments on commit c403740

Please sign in to comment.