Skip to content

Commit f057f4b

Browse files
authored
ui: fix userdata base64 encoding (#7749)
* ui: fix userdata abse64 encoding Fixes #7748 Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 80ca3ac commit f057f4b

File tree

7 files changed

+14
-40
lines changed

7 files changed

+14
-40
lines changed

ui/src/utils/plugins.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ export const genericUtilPlugin = {
484484
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
485485
return regexExp.test(uuid)
486486
}
487+
488+
app.config.globalProperties.$toBase64AndURIEncoded = function (text) {
489+
const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/
490+
if (base64regex.test(text)) {
491+
return text
492+
}
493+
return encodeURIComponent(btoa(unescape(encodeURIComponent(text))))
494+
}
487495
}
488496
}
489497

ui/src/views/compute/AutoScaleVmProfile.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ export default {
519519
params.autoscaleuserid = this.autoscaleuserid
520520
}
521521
if (this.userdata && this.userdata.length > 0) {
522-
params.userdata = encodeURIComponent(btoa(this.sanitizeReverse(this.userdata)))
522+
params.userdata = this.$toBase64AndURIEncoded(this.userdata)
523523
}
524524
525525
const httpMethod = params.userdata ? 'POST' : 'GET'
@@ -539,14 +539,6 @@ export default {
539539
this.loading = false
540540
})
541541
},
542-
sanitizeReverse (value) {
543-
const reversedValue = value
544-
.replace(/&amp;/g, '&')
545-
.replace(/&lt;/g, '<')
546-
.replace(/&gt;/g, '>')
547-
548-
return reversedValue
549-
},
550542
decodeUserData (userdata) {
551543
const decodedData = Buffer.from(userdata, 'base64')
552544
return decodedData.toString('utf-8')

ui/src/views/compute/CreateAutoScaleVmGroup.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ export default {
24252425
createVmGroupData.keypairs = this.sshKeyPairs.join(',')
24262426
createVmGroupData.affinitygroupids = (values.affinitygroupids || []).join(',')
24272427
if (values.userdata && values.userdata.length > 0) {
2428-
createVmGroupData.userdata = encodeURIComponent(btoa(this.sanitizeReverse(values.userdata)))
2428+
createVmGroupData.userdata = this.$toBase64AndURIEncoded(values.userdata)
24292429
}
24302430
24312431
// vm profile details
@@ -2702,14 +2702,6 @@ export default {
27022702
this.params[name].options = { ...this.params[name].options, ...options }
27032703
this.fetchOptions(this.params[name], name)
27042704
},
2705-
sanitizeReverse (value) {
2706-
const reversedValue = value
2707-
.replace(/&amp;/g, '&')
2708-
.replace(/&lt;/g, '<')
2709-
.replace(/&gt;/g, '>')
2710-
2711-
return reversedValue
2712-
},
27132705
fetchTemplateNics (template) {
27142706
var nics = []
27152707
this.nicToNetworkSelection = []

ui/src/views/compute/DeployVM.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,6 @@ import UserDataSelection from '@views/compute/wizard/UserDataSelection'
836836
import SecurityGroupSelection from '@views/compute/wizard/SecurityGroupSelection'
837837
import TooltipLabel from '@/components/widgets/TooltipLabel'
838838
import InstanceNicsNetworkSelectListView from '@/components/view/InstanceNicsNetworkSelectListView.vue'
839-
import { sanitizeReverse } from '@/utils/util'
840839
841840
export default {
842841
name: 'Wizard',
@@ -1971,7 +1970,7 @@ export default {
19711970
deployVmData.iothreadsenabled = values.iothreadsenabled
19721971
deployVmData.iodriverpolicy = values.iodriverpolicy
19731972
if (values.userdata && values.userdata.length > 0) {
1974-
deployVmData.userdata = encodeURIComponent(btoa(sanitizeReverse(values.userdata)))
1973+
deployVmData.userdata = this.$toBase64AndURIEncoded(values.userdata)
19751974
}
19761975
// step 2: select template/iso
19771976
if (this.tabKey === 'templateid') {

ui/src/views/compute/EditVM.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
import { ref, reactive, toRaw } from 'vue'
124124
import { api } from '@/api'
125125
import TooltipLabel from '@/components/widgets/TooltipLabel'
126-
import { sanitizeReverse } from '@/utils/util'
127126
128127
export default {
129128
name: 'EditVM',
@@ -317,7 +316,7 @@ export default {
317316
params.group = values.group
318317
}
319318
if (values.userdata && values.userdata.length > 0) {
320-
params.userdata = encodeURIComponent(btoa(sanitizeReverse(values.userdata)))
319+
params.userdata = this.$toBase64AndURIEncoded(values.userdata)
321320
}
322321
this.loading = true
323322

ui/src/views/compute/RegisterUserData.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,6 @@ export default {
184184
handleDomainChanged (domain) {
185185
this.selectedDomain = domain
186186
},
187-
sanitizeReverse (value) {
188-
const reversedValue = value
189-
.replace(/&amp;/g, '&')
190-
.replace(/&lt;/g, '<')
191-
.replace(/&gt;/g, '>')
192-
193-
return reversedValue
194-
},
195187
handleSubmit (e) {
196188
e.preventDefault()
197189
if (this.loading) return
@@ -209,7 +201,7 @@ export default {
209201
if (this.isValidValueForKey(values, 'account') && values.account.length > 0) {
210202
params.account = values.account
211203
}
212-
params.userdata = encodeURIComponent(btoa(this.sanitizeReverse(values.userdata)))
204+
params.userdata = this.$toBase64AndURIEncoded(values.userdata)
213205
214206
if (values.params != null && values.params.length > 0) {
215207
var userdataparams = values.params.join(',')

ui/src/views/compute/ResetUserData.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,6 @@ export default {
289289
this[type] = key
290290
this.userDataParams = []
291291
},
292-
sanitizeReverse (value) {
293-
const reversedValue = value
294-
.replace(/&amp;/g, '&')
295-
.replace(/&lt;/g, '<')
296-
.replace(/&gt;/g, '>')
297-
298-
return reversedValue
299-
},
300292
isUserAllowedToListUserDatas () {
301293
return Boolean('listUserData' in this.$store.getters.apis)
302294
},
@@ -346,7 +338,7 @@ export default {
346338
id: this.resource.id
347339
}
348340
if (values.userdata && values.userdata.length > 0) {
349-
params.userdata = encodeURIComponent(btoa(this.sanitizeReverse(values.userdata)))
341+
params.userdata = this.$toBase64AndURIEncoded(values.userdata)
350342
}
351343
if (values.userdataid) {
352344
params.userdataid = values.userdataid

0 commit comments

Comments
 (0)