Skip to content

Commit

Permalink
fix: improve validation of host/domain set name (#416)
Browse files Browse the repository at this point in the history
* fix: improve validation of host/domain set name

* fix: improve validation of domains in domain sets
  • Loading branch information
andre8244 authored Oct 31, 2024
1 parent be2e7cb commit 4dbfde5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import {
} from '@nethesis/vue-components'
import { ref, type PropType, watch, type Ref, computed } from 'vue'
import { ubusCall, ValidationError } from '@/lib/standalone/ubus'
import { MessageBag, validateRequired, type validationOutput } from '@/lib/validation'
import {
MessageBag,
validateAlphanumeric,
validateDomainName,
validateRequired,
type validationOutput
} from '@/lib/validation'
import type { IpVersion } from '@/views/standalone/users_objects/ObjectsView.vue'
import NeMultiTextInput from '../NeMultiTextInput.vue'
import type { DomainSet } from '@/composables/useDomainSets'
Expand Down Expand Up @@ -120,9 +126,24 @@ function runFieldValidators(
}

function validateRecordsRequired() {
let totalChars = 0

for (const record of records.value) {
if (!record) {
return { valid: false, errMessage: 'error.invalid_ipaddr' }
return { valid: false, errMessage: 'error.empty_domains' }
}

const { valid } = validateDomainName(record)
if (!valid) {
return { valid: false, errMessage: 'error.invalid_domains' }
}
totalChars += record.length
}

if (totalChars >= 1024) {
return {
valid: false,
errMessage: 'error.domains_total_chars_exceeded'
}
}
return { valid: true }
Expand All @@ -143,7 +164,15 @@ function validateDomainSetNotExists(value: string) {
function validate() {
const allValidators: [validationOutput[], string, Ref<any>][] = [
// name
[[validateRequired(name.value), validateDomainSetNotExists(name.value)], 'name', nameRef],
[
[
validateRequired(name.value),
validateAlphanumeric(name.value, true),
validateDomainSetNotExists(name.value)
],
'name',
nameRef
],
// records
[[validateRecordsRequired()], 'ipaddr', recordRef]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
} from '@nethesis/vue-components'
import { ref, type PropType, watch, type Ref, computed } from 'vue'
import { ubusCall, ValidationError } from '@/lib/standalone/ubus'
import { MessageBag, validateRequired, type validationOutput } from '@/lib/validation'
import {
MessageBag,
validateAlphanumeric,
validateRequired,
type validationOutput
} from '@/lib/validation'
import type { IpVersion } from '@/views/standalone/users_objects/ObjectsView.vue'
import type { HostSet } from '@/composables/useHostSets'

Expand Down Expand Up @@ -143,7 +148,15 @@ function validateHostSetNotExists(value: string) {
function validate() {
const allValidators: [validationOutput[], string, Ref<any>][] = [
// name
[[validateRequired(name.value), validateHostSetNotExists(name.value)], 'name', nameRef],
[
[
validateRequired(name.value),
validateAlphanumeric(name.value, true),
validateHostSetNotExists(name.value)
],
'name',
nameRef
],
// records
[[validateRequired(records.value[0])], 'ipaddr', recordRef]
]
Expand Down
5 changes: 5 additions & 0 deletions src/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
"invalid_port": "Invalid port number",
"invalid_port_or_port_range": "Enter a valid port or port range",
"invalid_uci_name": "Only letters, numbers and underscore (_) are allowed",
"invalid_alphanumeric_starting_with_letter": "Only letters and numbers are allowed, starting with a letter",
"invalid_alphanumeric": "Only letters and numbers are allowed",
"invalid_lease_time": "Invalid lease time",
"invalid_lease_time_duration": "Lease time must be at least 2 minutes",
"invalid_future_date": "Date cannot be set in the past",
Expand Down Expand Up @@ -355,6 +357,9 @@
"cannot_save_host_set": "Cannot save host set",
"cannot_save_domain_set": "Cannot save domain set",
"invalid_ipaddr": "One or more records are invalid",
"empty_domains": "One or more domains are empty",
"invalid_domains": "One or more domains are invalid",
"domains_total_chars_exceeded": "Total characters of all domains must not exceed 1024. Please remove some domains or create a new domain set.",
"cannot_delete_host_set": "Cannot delete host set",
"cannot_delete_domain_set": "Cannot delete domain set",
"cannot_retrieve_host_sets": "Cannot retrieve host sets",
Expand Down
27 changes: 27 additions & 0 deletions src/lib/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ export const validateHostname = (hostname: String): validationOutput => {
return { valid: false, errMessage: 'error.hostname_is_too_long' }
}

export const validateDomainName = (value: String): validationOutput => {
const isValid =
value.match(/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}$/) != null

if (isValid) {
return { valid: true }
} else {
return { valid: false, errMessage: 'error.invalid_domain_name' }
}
}

export const validateMacAddress = (macAddress: String): validationOutput => {
const isValid = macAddress.match(/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/) != null

Expand Down Expand Up @@ -359,6 +370,22 @@ export const validateUciName = (value: String, maxLength = 0): validationOutput
return { valid: true }
}

export const validateAlphanumeric = (
value: String,
mustStartWithLetter = false
): validationOutput => {
const re = mustStartWithLetter ? /^[a-zA-Z][a-zA-Z0-9]*$/ : /^[a-zA-Z0-9]+$/
const match = value.match(re)

if (!match) {
const message = mustStartWithLetter
? 'error.invalid_alphanumeric_starting_with_letter'
: 'error.invalid_alphanumeric'
return { valid: false, errMessage: message }
}
return { valid: true }
}

export const validateVlanId = (value: String): validationOutput => {
const vlanId = Number(value)

Expand Down

0 comments on commit 4dbfde5

Please sign in to comment.