cloudstack icon indicating copy to clipboard operation
cloudstack copied to clipboard

Userdata incorrectly encoded

Open DagSonsteboSB opened this issue 7 years ago • 3 comments

ISSUE TYPE
  • Bug Report
COMPONENT NAME
  • Userdata input in CloudStack 4.11
CLOUDSTACK VERSION
  • ACS 4.11.2
CONFIGURATION
  • N/A
OS / ENVIRONMENT
  • Tested on CentOS7 but is version agnostic.
SUMMARY
  • Userdata is incorrectly URLencoded before base64 encoding
STEPS TO REPRODUCE
  • Create new VM and pass the following userdata string: cat ${hostname}-local >>/etc/hostname
  • Check the string passed through API call and landing in database:

SELECT * FROM cloud.user_vm where id=8 | id | iso_id | display_name | user_data | update_parameters | | 8 | | d1ct | Y2F0ICR7aG9zdG5hbWV9LWxvY2FsICZndDsmZ3Q7L2V0Yy9ob3N0bmFtZQ== | 1 |

  • Decode this string and note greater than symbols converted to & gt ; - note Github will not parse this correctly hence spaces inserted to prove the point:

echo Y2F0ICR7aG9zdG5hbWV9LWxvY2FsICZndDsmZ3Q7L2V0Yy9ob3N0bmFtZQ== | base64 -d cat ${hostname}-local & gt ; & gt ;/etc/hostname

  • For completeness sake also do a curl from the guest VM and confirm the same wrongly converted string is passed back from the VR
EXPECTED RESULTS
  • Expected output to be same as input: cat ${hostname}-local >>/etc/hostname
ACTUAL RESULTS
  • Actual output was URLencoded (spaces inserted to work around Github parsing): cat ${hostname}-local & gt ;& gt ;/etc/hostname

DagSonsteboSB avatar Mar 07 '19 15:03 DagSonsteboSB

@DagSonsteboSB It seems to me this is due to the textarea for userdata being of class disallowSpecialCharacters. This class is tied to a validation method that specically filters for '<' and '>' This would be redundant as the field is translated before passing to the API: encodeURIComponent(btoa(userdata)). I am not sure (yet) where the translation from '>>' to '&gt;&gt;' is done. It is clearly happening before btoa() is called.

PS '&gt;' you write as '\&gt;' in github

DaanHoogland avatar Mar 08 '19 09:03 DaanHoogland

Sounds reasonable to me @DaanHoogland, let me know how you get on and we can test.

DagSonsteboSB avatar Mar 11 '19 09:03 DagSonsteboSB

The removal of the class was not enough, but I found the code that let's jquery do the automatic en-/decoding. It does this using the methods sanatize and sanatizeReverse from ui/utils.js so I wrote a piece of code that undoes it before passing it on to the API:

commit 0cbc25d83f62f7e37774a86fa09330818e603cc9 (HEAD -> base64-userdata, sb/base64-userdata)
Author: Daan Hoogland <[email protected]>
Date:   Mon Mar 18 12:30:12 2019 +0000

    reverse character replacements that are generic for all fields and hurt userdata

diff --git a/ui/scripts/instanceWizard.js b/ui/scripts/instanceWizard.js
index 351ca7b302..3a85e66269 100644
--- a/ui/scripts/instanceWizard.js
+++ b/ui/scripts/instanceWizard.js
@@ -1094,8 +1094,15 @@
 
             var userdata = args.data.userdata;
             if (userdata != null && userdata.length > 0) {
+
+                // NOTE code duplicated from sanatizeReverse in ui/utils.js because of call propagation issues
+                userdata = userdata
+                            .replace(/&amp;/g, "&")
+                            .replace(/&lt;/g, "<")
+                            .replace(/&gt;/g, ">");
+
                 $.extend(deployVmData, {
-                    userdata : encodeURIComponent(btoa(userdata))
+                    userdata : btoa(userdata)
                 });
             }

No other characters are encoded as far as I can tell but we might define a reassuring large set to test agains. (@borisstoyanov ?)

DaanHoogland avatar Mar 18 '19 12:03 DaanHoogland