From 799ff556d5f5e9f7e938fe8fd321421a59394342 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 18 Jul 2023 15:25:04 +0530 Subject: [PATCH 1/3] ui: fix userdata abse64 encoding Fixes #7748 Signed-off-by: Abhishek Kumar --- ui/src/utils/plugins.js | 8 ++++++++ ui/src/views/compute/AutoScaleVmProfile.vue | 10 +--------- ui/src/views/compute/CreateAutoScaleVmGroup.vue | 10 +--------- ui/src/views/compute/DeployVM.vue | 3 +-- ui/src/views/compute/EditVM.vue | 3 +-- ui/src/views/compute/RegisterUserData.vue | 10 +--------- ui/src/views/compute/ResetUserData.vue | 10 +--------- 7 files changed, 14 insertions(+), 40 deletions(-) diff --git a/ui/src/utils/plugins.js b/ui/src/utils/plugins.js index 2e2c787366e5..15627c59fe83 100644 --- a/ui/src/utils/plugins.js +++ b/ui/src/utils/plugins.js @@ -484,6 +484,14 @@ export const genericUtilPlugin = { const regexExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi return regexExp.test(uuid) } + + app.config.globalProperties.$toBase64Encoded = function (text) { + const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/ + if (base64regex.test(text)) { + return text + } + return btoa(unescape(encodeURIComponent(text))) + } } } diff --git a/ui/src/views/compute/AutoScaleVmProfile.vue b/ui/src/views/compute/AutoScaleVmProfile.vue index b00a35f04dd0..3b799d1c791d 100644 --- a/ui/src/views/compute/AutoScaleVmProfile.vue +++ b/ui/src/views/compute/AutoScaleVmProfile.vue @@ -519,7 +519,7 @@ export default { params.autoscaleuserid = this.autoscaleuserid } if (this.userdata && this.userdata.length > 0) { - params.userdata = encodeURIComponent(btoa(this.sanitizeReverse(this.userdata))) + params.userdata = encodeURIComponent(this.$toBase64Encoded(this.userdata)) } const httpMethod = params.userdata ? 'POST' : 'GET' @@ -539,14 +539,6 @@ export default { this.loading = false }) }, - sanitizeReverse (value) { - const reversedValue = value - .replace(/&/g, '&') - .replace(/</g, '<') - .replace(/>/g, '>') - - return reversedValue - }, decodeUserData (userdata) { const decodedData = Buffer.from(userdata, 'base64') return decodedData.toString('utf-8') diff --git a/ui/src/views/compute/CreateAutoScaleVmGroup.vue b/ui/src/views/compute/CreateAutoScaleVmGroup.vue index c807228f7037..e0302dfbe91c 100644 --- a/ui/src/views/compute/CreateAutoScaleVmGroup.vue +++ b/ui/src/views/compute/CreateAutoScaleVmGroup.vue @@ -2425,7 +2425,7 @@ export default { createVmGroupData.keypairs = this.sshKeyPairs.join(',') createVmGroupData.affinitygroupids = (values.affinitygroupids || []).join(',') if (values.userdata && values.userdata.length > 0) { - createVmGroupData.userdata = encodeURIComponent(btoa(this.sanitizeReverse(values.userdata))) + createVmGroupData.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) } // vm profile details @@ -2702,14 +2702,6 @@ export default { this.params[name].options = { ...this.params[name].options, ...options } this.fetchOptions(this.params[name], name) }, - sanitizeReverse (value) { - const reversedValue = value - .replace(/&/g, '&') - .replace(/</g, '<') - .replace(/>/g, '>') - - return reversedValue - }, fetchTemplateNics (template) { var nics = [] this.nicToNetworkSelection = [] diff --git a/ui/src/views/compute/DeployVM.vue b/ui/src/views/compute/DeployVM.vue index b15fa8bcccf7..903658455719 100644 --- a/ui/src/views/compute/DeployVM.vue +++ b/ui/src/views/compute/DeployVM.vue @@ -836,7 +836,6 @@ import UserDataSelection from '@views/compute/wizard/UserDataSelection' import SecurityGroupSelection from '@views/compute/wizard/SecurityGroupSelection' import TooltipLabel from '@/components/widgets/TooltipLabel' import InstanceNicsNetworkSelectListView from '@/components/view/InstanceNicsNetworkSelectListView.vue' -import { sanitizeReverse } from '@/utils/util' export default { name: 'Wizard', @@ -1971,7 +1970,7 @@ export default { deployVmData.iothreadsenabled = values.iothreadsenabled deployVmData.iodriverpolicy = values.iodriverpolicy if (values.userdata && values.userdata.length > 0) { - deployVmData.userdata = encodeURIComponent(btoa(sanitizeReverse(values.userdata))) + deployVmData.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) } // step 2: select template/iso if (this.tabKey === 'templateid') { diff --git a/ui/src/views/compute/EditVM.vue b/ui/src/views/compute/EditVM.vue index b3861d89f46b..a92068d37fd8 100644 --- a/ui/src/views/compute/EditVM.vue +++ b/ui/src/views/compute/EditVM.vue @@ -123,7 +123,6 @@ import { ref, reactive, toRaw } from 'vue' import { api } from '@/api' import TooltipLabel from '@/components/widgets/TooltipLabel' -import { sanitizeReverse } from '@/utils/util' export default { name: 'EditVM', @@ -317,7 +316,7 @@ export default { params.group = values.group } if (values.userdata && values.userdata.length > 0) { - params.userdata = encodeURIComponent(btoa(sanitizeReverse(values.userdata))) + params.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) } this.loading = true diff --git a/ui/src/views/compute/RegisterUserData.vue b/ui/src/views/compute/RegisterUserData.vue index 36e469f676c9..31c5c2b17d19 100644 --- a/ui/src/views/compute/RegisterUserData.vue +++ b/ui/src/views/compute/RegisterUserData.vue @@ -184,14 +184,6 @@ export default { handleDomainChanged (domain) { this.selectedDomain = domain }, - sanitizeReverse (value) { - const reversedValue = value - .replace(/&/g, '&') - .replace(/</g, '<') - .replace(/>/g, '>') - - return reversedValue - }, handleSubmit (e) { e.preventDefault() if (this.loading) return @@ -209,7 +201,7 @@ export default { if (this.isValidValueForKey(values, 'account') && values.account.length > 0) { params.account = values.account } - params.userdata = encodeURIComponent(btoa(this.sanitizeReverse(values.userdata))) + params.userdata = encodeURIComponent(this.toBase64Encoded(values.userdata)) if (values.params != null && values.params.length > 0) { var userdataparams = values.params.join(',') diff --git a/ui/src/views/compute/ResetUserData.vue b/ui/src/views/compute/ResetUserData.vue index b72877510321..4ace837bdb5d 100644 --- a/ui/src/views/compute/ResetUserData.vue +++ b/ui/src/views/compute/ResetUserData.vue @@ -289,14 +289,6 @@ export default { this[type] = key this.userDataParams = [] }, - sanitizeReverse (value) { - const reversedValue = value - .replace(/&/g, '&') - .replace(/</g, '<') - .replace(/>/g, '>') - - return reversedValue - }, isUserAllowedToListUserDatas () { return Boolean('listUserData' in this.$store.getters.apis) }, @@ -346,7 +338,7 @@ export default { id: this.resource.id } if (values.userdata && values.userdata.length > 0) { - params.userdata = encodeURIComponent(btoa(this.sanitizeReverse(values.userdata))) + params.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) } if (values.userdataid) { params.userdataid = values.userdataid From 8105429555eb6b0b9ebb631883a9cde80756400b Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 20 Jul 2023 13:51:25 +0530 Subject: [PATCH 2/3] refactor Signed-off-by: Abhishek Kumar --- ui/src/utils/plugins.js | 8 ++++++++ ui/src/views/compute/AutoScaleVmProfile.vue | 2 +- ui/src/views/compute/CreateAutoScaleVmGroup.vue | 2 +- ui/src/views/compute/DeployVM.vue | 2 +- ui/src/views/compute/EditVM.vue | 2 +- ui/src/views/compute/RegisterUserData.vue | 2 +- ui/src/views/compute/ResetUserData.vue | 2 +- 7 files changed, 14 insertions(+), 6 deletions(-) diff --git a/ui/src/utils/plugins.js b/ui/src/utils/plugins.js index 15627c59fe83..86f71ce1e35e 100644 --- a/ui/src/utils/plugins.js +++ b/ui/src/utils/plugins.js @@ -492,6 +492,14 @@ export const genericUtilPlugin = { } return btoa(unescape(encodeURIComponent(text))) } + + app.config.globalProperties.$toBase64AndURIEncoded = function (text) { + const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/ + if (base64regex.test(text)) { + return text + } + return btoa(unescape(encodeURIComponent(text))) + } } } diff --git a/ui/src/views/compute/AutoScaleVmProfile.vue b/ui/src/views/compute/AutoScaleVmProfile.vue index 3b799d1c791d..45947753b06e 100644 --- a/ui/src/views/compute/AutoScaleVmProfile.vue +++ b/ui/src/views/compute/AutoScaleVmProfile.vue @@ -519,7 +519,7 @@ export default { params.autoscaleuserid = this.autoscaleuserid } if (this.userdata && this.userdata.length > 0) { - params.userdata = encodeURIComponent(this.$toBase64Encoded(this.userdata)) + params.userdata = this.$toBase64AndURIEncoded(this.userdata) } const httpMethod = params.userdata ? 'POST' : 'GET' diff --git a/ui/src/views/compute/CreateAutoScaleVmGroup.vue b/ui/src/views/compute/CreateAutoScaleVmGroup.vue index e0302dfbe91c..4dcb2955274b 100644 --- a/ui/src/views/compute/CreateAutoScaleVmGroup.vue +++ b/ui/src/views/compute/CreateAutoScaleVmGroup.vue @@ -2425,7 +2425,7 @@ export default { createVmGroupData.keypairs = this.sshKeyPairs.join(',') createVmGroupData.affinitygroupids = (values.affinitygroupids || []).join(',') if (values.userdata && values.userdata.length > 0) { - createVmGroupData.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) + createVmGroupData.userdata = this.$toBase64AndURIEncoded(values.userdata) } // vm profile details diff --git a/ui/src/views/compute/DeployVM.vue b/ui/src/views/compute/DeployVM.vue index 903658455719..de74ab8f268d 100644 --- a/ui/src/views/compute/DeployVM.vue +++ b/ui/src/views/compute/DeployVM.vue @@ -1970,7 +1970,7 @@ export default { deployVmData.iothreadsenabled = values.iothreadsenabled deployVmData.iodriverpolicy = values.iodriverpolicy if (values.userdata && values.userdata.length > 0) { - deployVmData.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) + deployVmData.userdata = this.$toBase64AndURIEncoded(values.userdata) } // step 2: select template/iso if (this.tabKey === 'templateid') { diff --git a/ui/src/views/compute/EditVM.vue b/ui/src/views/compute/EditVM.vue index a92068d37fd8..9aae2f387557 100644 --- a/ui/src/views/compute/EditVM.vue +++ b/ui/src/views/compute/EditVM.vue @@ -316,7 +316,7 @@ export default { params.group = values.group } if (values.userdata && values.userdata.length > 0) { - params.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) + params.userdata = this.$toBase64AndURIEncoded(values.userdata) } this.loading = true diff --git a/ui/src/views/compute/RegisterUserData.vue b/ui/src/views/compute/RegisterUserData.vue index 31c5c2b17d19..a8e5577d0055 100644 --- a/ui/src/views/compute/RegisterUserData.vue +++ b/ui/src/views/compute/RegisterUserData.vue @@ -201,7 +201,7 @@ export default { if (this.isValidValueForKey(values, 'account') && values.account.length > 0) { params.account = values.account } - params.userdata = encodeURIComponent(this.toBase64Encoded(values.userdata)) + params.userdata = this.$toBase64AndURIEncoded(values.userdata) if (values.params != null && values.params.length > 0) { var userdataparams = values.params.join(',') diff --git a/ui/src/views/compute/ResetUserData.vue b/ui/src/views/compute/ResetUserData.vue index 4ace837bdb5d..46561f15c1a5 100644 --- a/ui/src/views/compute/ResetUserData.vue +++ b/ui/src/views/compute/ResetUserData.vue @@ -338,7 +338,7 @@ export default { id: this.resource.id } if (values.userdata && values.userdata.length > 0) { - params.userdata = encodeURIComponent(this.$toBase64Encoded(values.userdata)) + params.userdata = this.$toBase64AndURIEncoded(values.userdata) } if (values.userdataid) { params.userdataid = values.userdataid From f7fc22d04123b4c666204a8a6f2f43a50427c3b0 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 21 Jul 2023 11:54:51 +0530 Subject: [PATCH 3/3] refactor Signed-off-by: Abhishek Kumar --- ui/src/utils/plugins.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ui/src/utils/plugins.js b/ui/src/utils/plugins.js index 86f71ce1e35e..5cc4bc6269a1 100644 --- a/ui/src/utils/plugins.js +++ b/ui/src/utils/plugins.js @@ -485,20 +485,12 @@ export const genericUtilPlugin = { return regexExp.test(uuid) } - app.config.globalProperties.$toBase64Encoded = function (text) { - const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/ - if (base64regex.test(text)) { - return text - } - return btoa(unescape(encodeURIComponent(text))) - } - app.config.globalProperties.$toBase64AndURIEncoded = function (text) { const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/ if (base64regex.test(text)) { return text } - return btoa(unescape(encodeURIComponent(text))) + return encodeURIComponent(btoa(unescape(encodeURIComponent(text)))) } } }