Skip to content

Commit 0e93ae3

Browse files
authored
UI: Add validator for CIDR being passed (#11465)
1 parent a5a934d commit 0e93ae3

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

ui/src/utils/util.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,24 @@ export function toCsv ({ keys = null, data = null, columnDelimiter = ',', lineDe
102102

103103
return result
104104
}
105+
106+
export function isValidIPv4Cidr (rule, value) {
107+
return new Promise((resolve, reject) => {
108+
if (!value) {
109+
reject(new Error())
110+
return
111+
}
112+
const cidrRegex = /^(\d{1,3}\.){3}\d{1,3}\/([0-9]|[1-2][0-9]|3[0-2])$/
113+
if (!cidrRegex.test(value)) {
114+
reject(new Error('Invalid CIDR format'))
115+
return
116+
}
117+
const ip = value.split('/')[0]
118+
const octets = ip.split('.').map(Number)
119+
if (octets.some(octet => octet < 0 || octet > 255)) {
120+
reject(new Error('Invalid CIDR format'))
121+
return
122+
}
123+
resolve()
124+
})
125+
}

ui/src/views/network/CreateVpc.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
import { ref, reactive, toRaw } from 'vue'
221221
import { getAPI, postAPI } from '@/api'
222222
import { isAdmin, isAdminOrDomainAdmin } from '@/role'
223+
import { isValidIPv4Cidr } from '@/utils/util.js'
223224
import ResourceIcon from '@/components/view/ResourceIcon'
224225
import TooltipLabel from '@/components/widgets/TooltipLabel'
225226
import OwnershipSelection from '@/views/compute/wizard/OwnershipSelection.vue'
@@ -291,6 +292,7 @@ export default {
291292
this.rules = reactive({
292293
name: [{ required: true, message: this.$t('message.error.required.input') }],
293294
zoneid: [{ required: true, message: this.$t('label.required') }],
295+
cidr: [{ validator: isValidIPv4Cidr }],
294296
vpcofferingid: [{ required: true, message: this.$t('label.required') }]
295297
})
296298
},
@@ -417,7 +419,7 @@ export default {
417419
},
418420
updateCidrRule () {
419421
if (!this.selectedVpcOfferingHavingRoutedNetworkMode) {
420-
this.rules.cidr = [{ required: true, message: this.$t('message.error.required.input') }]
422+
this.rules.cidr = [{ required: true, message: this.$t('message.error.required.input') }, { validator: isValidIPv4Cidr }]
421423
} else {
422424
delete this.rules.cidr
423425
}

0 commit comments

Comments
 (0)