This is an automated email from the ASF dual-hosted git repository.

weizhou pushed a commit to branch 4.18
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.18 by this push:
     new f057f4b412e ui: fix userdata base64 encoding (#7749)
f057f4b412e is described below

commit f057f4b412e0e913448c9ce9f34ee5c7a746c6c4
Author: Abhishek Kumar <[email protected]>
AuthorDate: Fri Jul 21 16:08:08 2023 +0530

    ui: fix userdata base64 encoding (#7749)
    
    * ui: fix userdata abse64 encoding
    
    Fixes #7748
    
    Signed-off-by: Abhishek Kumar <[email protected]>
---
 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 2e2c787366e..5cc4bc6269a 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.$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 encodeURIComponent(btoa(unescape(encodeURIComponent(text))))
+    }
   }
 }
 
diff --git a/ui/src/views/compute/AutoScaleVmProfile.vue 
b/ui/src/views/compute/AutoScaleVmProfile.vue
index b00a35f04dd..45947753b06 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 = this.$toBase64AndURIEncoded(this.userdata)
       }
 
       const httpMethod = params.userdata ? 'POST' : 'GET'
@@ -539,14 +539,6 @@ export default {
         this.loading = false
       })
     },
-    sanitizeReverse (value) {
-      const reversedValue = value
-        .replace(/&amp;/g, '&')
-        .replace(/&lt;/g, '<')
-        .replace(/&gt;/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 c807228f703..4dcb2955274 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 = 
this.$toBase64AndURIEncoded(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(/&amp;/g, '&')
-        .replace(/&lt;/g, '<')
-        .replace(/&gt;/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 b15fa8bcccf..de74ab8f268 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 = 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 b3861d89f46..9aae2f38755 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 = this.$toBase64AndURIEncoded(values.userdata)
         }
         this.loading = true
 
diff --git a/ui/src/views/compute/RegisterUserData.vue 
b/ui/src/views/compute/RegisterUserData.vue
index 36e469f676c..a8e5577d005 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(/&amp;/g, '&')
-        .replace(/&lt;/g, '<')
-        .replace(/&gt;/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 = 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 b7287751032..46561f15c1a 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(/&amp;/g, '&')
-        .replace(/&lt;/g, '<')
-        .replace(/&gt;/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 = this.$toBase64AndURIEncoded(values.userdata)
       }
       if (values.userdataid) {
         params.userdataid = values.userdataid

Reply via email to