[PATCH 0/3] Bug fix: Access guest console when guest name has non-ASCII characters

*** BLURB HERE *** Aline Manera (3): Import Jquery Base64 code Update server configuration to expose jquery.base64.js Bug fix: Access guest console when guest name has non-ASCII characters COPYING | 9 ++-- src/kimchi/config.py.in | 6 +++ tests/test_config.py.in | 6 +++ ui/base64/README.md | 27 ++++++++++ ui/base64/jquery.base64.js | 122 +++++++++++++++++++++++++++++++++++++++++++ ui/js/src/kimchi.utils.js | 6 +-- ui/pages/kimchi-ui.html.tmpl | 1 + 7 files changed, 170 insertions(+), 7 deletions(-) create mode 100644 ui/base64/README.md create mode 100644 ui/base64/jquery.base64.js -- 2.1.0

The Jquery Base64 code was imported from https://plugins.jquery.com/base64/ This patch also updates the COPYING file to add the reference for Jquery Base64 code. Signed-off-by: Aline Manera <alinefm@linux.vnet.ibm.com> --- COPYING | 9 ++-- ui/base64/README.md | 27 ++++++++++ ui/base64/jquery.base64.js | 122 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 ui/base64/README.md create mode 100644 ui/base64/jquery.base64.js diff --git a/COPYING b/COPYING index b34c747..288f52c 100644 --- a/COPYING +++ b/COPYING @@ -2,10 +2,11 @@ Kimchi is distributed pursuant to the terms of two different licenses. The user interface (located in ui/ in this distribution) is governed by the Apache License version 2.0. -imported from JQuery UI (http://jqueryui.com) -The code under ui/spice-html5 is imported from spice-html5 project -(http://cgit.freedesktop.org/spice/spice-html5) and the code under ui/libs -which is imported from JQuery UI (http://jqueryui.com) +Kimchi makes use of different projects and they are placed as below: +- the code under ui/spice-html5 is imported from spice-html5 project +(http://cgit.freedesktop.org/spice/spice-html5); +- the code under ui/libs which is imported from JQuery UI (http://jqueryui.com); +- the code under ui/base64 is imported from Jquery Base64 (https://plugins.jquery.com/base64/). The rest of this distribution is governed by the GNU Lesser General Public License version 3. diff --git a/ui/base64/README.md b/ui/base64/README.md new file mode 100644 index 0000000..bc8a1a9 --- /dev/null +++ b/ui/base64/README.md @@ -0,0 +1,27 @@ +#No documentation yet! +(feel free to write a bit about it) + +You can check for `btoa` and `atob` support and refer to `jQuery.base64` + + if (!window.btoa) window.btoa = $.base64.btoa + if (!window.atob) window.atob = $.base64.atob + +## Download + Get the [raw](https://raw.github.com/yckart/jquery.base64.js/master/jquery.base64.js) script, download the complete [package](https://github.com/yckart/jquery.base64.js/zipball/master) or fork it on [GitHub](https://github.com/yckart/jquery.base64.js/). + +## Support + + [@yckart](http://twitter.com/yckart) #jquery #base64 + [http://yckart.com](http://yckart.com/) + +### Thanks +- https://github.com/carlo/jquery-base64 + +###License +Copyright (c) 2013 Yannick Albert ([http://yckart.com/](http://yckart.com/)) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/ui/base64/jquery.base64.js b/ui/base64/jquery.base64.js new file mode 100644 index 0000000..bbce5f0 --- /dev/null +++ b/ui/base64/jquery.base64.js @@ -0,0 +1,122 @@ +/*! + * jquery.base64.js 0.1 - https://github.com/yckart/jquery.base64.js + * Makes Base64 en & -decoding simpler as it is. + * + * Based upon: https://gist.github.com/Yaffle/1284012 + * + * Copyright (c) 2012 Yannick Albert (http://yckart.com) + * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php). + * 2013/02/10 + **/ +;(function($) { + + var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", + a256 = '', + r64 = [256], + r256 = [256], + i = 0; + + var UTF8 = { + + /** + * Encode multi-byte Unicode string into utf-8 multiple single-byte characters + * (BMP / basic multilingual plane only) + * + * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars + * + * @param {String} strUni Unicode string to be encoded as UTF-8 + * @returns {String} encoded string + */ + encode: function(strUni) { + // use regular expressions & String.replace callback function for better efficiency + // than procedural approaches + var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz + function(c) { + var cc = c.charCodeAt(0); + return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f); + }) + .replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz + function(c) { + var cc = c.charCodeAt(0); + return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f); + }); + return strUtf; + }, + + /** + * Decode utf-8 encoded string back into multi-byte Unicode characters + * + * @param {String} strUtf UTF-8 string to be decoded back to Unicode + * @returns {String} decoded string + */ + decode: function(strUtf) { + // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char! + var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars + function(c) { // (note parentheses for precence) + var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f); + return String.fromCharCode(cc); + }) + .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars + function(c) { // (note parentheses for precence) + var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f; + return String.fromCharCode(cc); + }); + return strUni; + } + }; + + while(i < 256) { + var c = String.fromCharCode(i); + a256 += c; + r256[i] = i; + r64[i] = b64.indexOf(c); + ++i; + } + + function code(s, discard, alpha, beta, w1, w2) { + s = String(s); + var buffer = 0, + i = 0, + length = s.length, + result = '', + bitsInBuffer = 0; + + while(i < length) { + var c = s.charCodeAt(i); + c = c < 256 ? alpha[c] : -1; + + buffer = (buffer << w1) + c; + bitsInBuffer += w1; + + while(bitsInBuffer >= w2) { + bitsInBuffer -= w2; + var tmp = buffer >> bitsInBuffer; + result += beta.charAt(tmp); + buffer ^= tmp << bitsInBuffer; + } + ++i; + } + if(!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer)); + return result; + } + + var Plugin = $.base64 = function(dir, input, encode) { + return input ? Plugin[dir](input, encode) : dir ? null : this; + }; + + Plugin.btoa = Plugin.encode = function(plain, utf8encode) { + plain = Plugin.raw === false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain; + plain = code(plain, false, r256, b64, 8, 6); + return plain + '===='.slice((plain.length % 4) || 4); + }; + + Plugin.atob = Plugin.decode = function(coded, utf8decode) { + coded = String(coded).split('='); + var i = coded.length; + do {--i; + coded[i] = code(coded[i], true, r64, a256, 6, 8); + } while (i > 0); + coded = coded.join(''); + return Plugin.raw === false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded; + }; +}(jQuery)); \ No newline at end of file -- 2.1.0

Also update the tests accordingly. Signed-off-by: Aline Manera <alinefm@linux.vnet.ibm.com> --- src/kimchi/config.py.in | 6 ++++++ tests/test_config.py.in | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/kimchi/config.py.in b/src/kimchi/config.py.in index 8cc63e7..7ca1a1f 100644 --- a/src/kimchi/config.py.in +++ b/src/kimchi/config.py.in @@ -229,6 +229,12 @@ class KimchiConfig(dict): 'tools.staticfile.filename': paths.spice_css_file, 'tools.nocache.on': True, }, + '/base64/jquery.base64.js': { + 'tools.staticfile.on': True, + 'tools.staticfile.filename': '%s/base64/jquery.base64.js' % + paths.ui_dir, + 'tools.nocache.on': True, + }, '/kimchi-ui.html': { 'tools.kimchiauth.on': True }, diff --git a/tests/test_config.py.in b/tests/test_config.py.in index 6123f66..96a339d 100644 --- a/tests/test_config.py.in +++ b/tests/test_config.py.in @@ -128,6 +128,12 @@ class ConfigTests(unittest.TestCase): 'tools.staticfile.filename': paths.spice_css_file, 'tools.nocache.on': True, }, + '/base64/jquery.base64.js': { + 'tools.staticfile.on': True, + 'tools.staticfile.filename': '%s/base64/jquery.base64.js' % + paths.ui_dir, + 'tools.nocache.on': True, + }, '/kimchi-ui.html': { 'tools.kimchiauth.on': True }, -- 2.1.0

atob() and btoa() do not work well with non-ASCII characters which turns into a wrong base64 result. For example, "café" would be "Y2Fmw6k" but btod() turns it into "Y2Fm6Q" This was preventing Kimchi to launch the guest console when guest name had non-ASCII characaters. Jquery Base64 has UTF-8 support so it can properly encode/decode strings with non-ASCII characters. Signed-off-by: Aline Manera <alinefm@linux.vnet.ibm.com> --- ui/js/src/kimchi.utils.js | 6 +++--- ui/pages/kimchi-ui.html.tmpl | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ui/js/src/kimchi.utils.js b/ui/js/src/kimchi.utils.js index f26a560..2fc7dfa 100644 --- a/ui/js/src/kimchi.utils.js +++ b/ui/js/src/kimchi.utils.js @@ -1,7 +1,7 @@ /* * Project Kimchi * - * Copyright IBM, Corp. 2013-2014 + * Copyright IBM, Corp. 2013-2015 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -185,9 +185,9 @@ kimchi.escapeStr = function(str) { }; kimchi.urlSafeB64Decode = function(str) { - return atob(str.replace(/-/g, '+').replace(/_/g, '/')); + return $.base64.atob(str.replace(/-/g, '+').replace(/_/g, '/'), true); } kimchi.urlSafeB64Encode = function(str) { - return btoa(str).replace(/\+/g, '-').replace(/\//g, '_'); + return $.base64.btoa(str, true).replace(/\+/g, '-').replace(/\//g, '_'); } diff --git a/ui/pages/kimchi-ui.html.tmpl b/ui/pages/kimchi-ui.html.tmpl index 8da8acc..548f5c8 100644 --- a/ui/pages/kimchi-ui.html.tmpl +++ b/ui/pages/kimchi-ui.html.tmpl @@ -36,6 +36,7 @@ <script src="$href('libs/jquery-1.10.0.min.js')"></script> <script src="$href('libs/jquery-ui.min.js')"></script> <script src="$href('libs/jquery-ui-i18n.min.js')"></script> +<script src="$href('base64/jquery.base64.js')"></script> <script src="$href('js/kimchi.min.js')"></script> <!-- This is used for detecting if the UI needs to be built --> -- 2.1.0

Reviewed-By: Ramon Medeiros <ramonn@br.ibm.com> Tested-By: Ramon Medeiros <ramonn@br.ibm.com> On 09/03/2015 09:52 AM, Aline Manera wrote:
atob() and btoa() do not work well with non-ASCII characters which turns into a wrong base64 result. For example, "café" would be "Y2Fmw6k" but btod() turns it into "Y2Fm6Q" This was preventing Kimchi to launch the guest console when guest name had non-ASCII characaters.
Jquery Base64 has UTF-8 support so it can properly encode/decode strings with non-ASCII characters.
Signed-off-by: Aline Manera <alinefm@linux.vnet.ibm.com> --- ui/js/src/kimchi.utils.js | 6 +++--- ui/pages/kimchi-ui.html.tmpl | 1 + 2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/ui/js/src/kimchi.utils.js b/ui/js/src/kimchi.utils.js index f26a560..2fc7dfa 100644 --- a/ui/js/src/kimchi.utils.js +++ b/ui/js/src/kimchi.utils.js @@ -1,7 +1,7 @@ /* * Project Kimchi * - * Copyright IBM, Corp. 2013-2014 + * Copyright IBM, Corp. 2013-2015 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -185,9 +185,9 @@ kimchi.escapeStr = function(str) { };
kimchi.urlSafeB64Decode = function(str) { - return atob(str.replace(/-/g, '+').replace(/_/g, '/')); + return $.base64.atob(str.replace(/-/g, '+').replace(/_/g, '/'), true); }
kimchi.urlSafeB64Encode = function(str) { - return btoa(str).replace(/\+/g, '-').replace(/\//g, '_'); + return $.base64.btoa(str, true).replace(/\+/g, '-').replace(/\//g, '_'); } diff --git a/ui/pages/kimchi-ui.html.tmpl b/ui/pages/kimchi-ui.html.tmpl index 8da8acc..548f5c8 100644 --- a/ui/pages/kimchi-ui.html.tmpl +++ b/ui/pages/kimchi-ui.html.tmpl @@ -36,6 +36,7 @@ <script src="$href('libs/jquery-1.10.0.min.js')"></script> <script src="$href('libs/jquery-ui.min.js')"></script> <script src="$href('libs/jquery-ui-i18n.min.js')"></script> +<script src="$href('base64/jquery.base64.js')"></script> <script src="$href('js/kimchi.min.js')"></script>
<!-- This is used for detecting if the UI needs to be built -->
-- Ramon Nunes Medeiros Kimchi Developer Linux Technology Center Brazil IBM Systems & Technology Group Phone : +55 19 2132 7878 ramonn@br.ibm.com

An error was added with this patch: i added the other patchset, that changed to this errors: ====================================================================== ERROR: test_debug_reports (test_model.ModelTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model.py", line 1074, in test_debug_reports inst.task_wait(taskid, timeout) File "/home/ramonn/GitTrees/KIMCHI/src/kimchi/model/tasks.py", line 64, in wait 'task': task['target_uri']}) TimeoutExpired: KCHASYNC0003E: Timeout of 120 seconds expired while running task '/debugreports/unitTestReport1cb17db2ccb241c28882eb8edc833504. ====================================================================== FAIL: test_storagevolume_action (test_model_storagevolume.StorageVolumeTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model_storagevolume.py", line 269, in test_storagevolume_action _do_volume_test(self, model, host, ssl_port, 'default') File "test_model_storagevolume.py", line 120, in _do_volume_test self.assertEquals(0, storagevolume['allocation']) AssertionError: 0 != 1024 ====================================================================== FAIL: test_async_tasks (test_model.ModelTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model.py", line 902, in test_async_tasks self.assertEquals(1, taskid) AssertionError: 1 != 2 ====================================================================== FAIL: test_vlan_tag_bridge (test_mock_network.MockNetworkTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_mock_network.py", line 71, in test_vlan_tag_bridge 'interface': iface, 'vlan_id': 987}) File "test_model_network.py", line 69, in _do_network_test self.assertEquals(201, resp.status) AssertionError: 201 != 500 ---------------------------------------------------------------------- Ran 147 tests in 355.646s FAILED (failures=3, errors=1, skipped=2) And then added this error: ====================================================================== FAIL: test_storagevolume_action (test_model_storagevolume.StorageVolumeTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model_storagevolume.py", line 269, in test_storagevolume_action _do_volume_test(self, model, host, ssl_port, 'default') File "test_model_storagevolume.py", line 120, in _do_volume_test self.assertEquals(0, storagevolume['allocation']) AssertionError: 0 != 1024 -- Ramon Nunes Medeiros Kimchi Developer Linux Technology Center Brazil IBM Systems & Technology Group Phone : +55 19 2132 7878 ramonn@br.ibm.com

It's strange as this patch only changed UI code. I tested on Ubuntu 15.04 and Fedora 22 and didn't get any new error. Could you, please, verify it again? Regards, Aline Manera On 03/09/2015 10:53, Ramon Medeiros wrote:
An error was added with this patch:
i added the other patchset, that changed to this errors:
====================================================================== ERROR: test_debug_reports (test_model.ModelTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model.py", line 1074, in test_debug_reports inst.task_wait(taskid, timeout) File "/home/ramonn/GitTrees/KIMCHI/src/kimchi/model/tasks.py", line 64, in wait 'task': task['target_uri']}) TimeoutExpired: KCHASYNC0003E: Timeout of 120 seconds expired while running task '/debugreports/unitTestReport1cb17db2ccb241c28882eb8edc833504.
====================================================================== FAIL: test_storagevolume_action (test_model_storagevolume.StorageVolumeTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model_storagevolume.py", line 269, in test_storagevolume_action _do_volume_test(self, model, host, ssl_port, 'default') File "test_model_storagevolume.py", line 120, in _do_volume_test self.assertEquals(0, storagevolume['allocation']) AssertionError: 0 != 1024
====================================================================== FAIL: test_async_tasks (test_model.ModelTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model.py", line 902, in test_async_tasks self.assertEquals(1, taskid) AssertionError: 1 != 2
====================================================================== FAIL: test_vlan_tag_bridge (test_mock_network.MockNetworkTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_mock_network.py", line 71, in test_vlan_tag_bridge 'interface': iface, 'vlan_id': 987}) File "test_model_network.py", line 69, in _do_network_test self.assertEquals(201, resp.status) AssertionError: 201 != 500
---------------------------------------------------------------------- Ran 147 tests in 355.646s
FAILED (failures=3, errors=1, skipped=2)
And then added this error:
====================================================================== FAIL: test_storagevolume_action (test_model_storagevolume.StorageVolumeTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model_storagevolume.py", line 269, in test_storagevolume_action _do_volume_test(self, model, host, ssl_port, 'default') File "test_model_storagevolume.py", line 120, in _do_volume_test self.assertEquals(0, storagevolume['allocation']) AssertionError: 0 != 1024

Just verified again, the error was reported before. Please, ignore the last message On 09/03/2015 03:43 PM, Aline Manera wrote:
It's strange as this patch only changed UI code.
I tested on Ubuntu 15.04 and Fedora 22 and didn't get any new error.
Could you, please, verify it again?
Regards, Aline Manera
On 03/09/2015 10:53, Ramon Medeiros wrote:
An error was added with this patch:
i added the other patchset, that changed to this errors:
====================================================================== ERROR: test_debug_reports (test_model.ModelTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model.py", line 1074, in test_debug_reports inst.task_wait(taskid, timeout) File "/home/ramonn/GitTrees/KIMCHI/src/kimchi/model/tasks.py", line 64, in wait 'task': task['target_uri']}) TimeoutExpired: KCHASYNC0003E: Timeout of 120 seconds expired while running task '/debugreports/unitTestReport1cb17db2ccb241c28882eb8edc833504.
====================================================================== FAIL: test_storagevolume_action (test_model_storagevolume.StorageVolumeTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model_storagevolume.py", line 269, in test_storagevolume_action _do_volume_test(self, model, host, ssl_port, 'default') File "test_model_storagevolume.py", line 120, in _do_volume_test self.assertEquals(0, storagevolume['allocation']) AssertionError: 0 != 1024
====================================================================== FAIL: test_async_tasks (test_model.ModelTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model.py", line 902, in test_async_tasks self.assertEquals(1, taskid) AssertionError: 1 != 2
====================================================================== FAIL: test_vlan_tag_bridge (test_mock_network.MockNetworkTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_mock_network.py", line 71, in test_vlan_tag_bridge 'interface': iface, 'vlan_id': 987}) File "test_model_network.py", line 69, in _do_network_test self.assertEquals(201, resp.status) AssertionError: 201 != 500
---------------------------------------------------------------------- Ran 147 tests in 355.646s
FAILED (failures=3, errors=1, skipped=2)
And then added this error:
====================================================================== FAIL: test_storagevolume_action (test_model_storagevolume.StorageVolumeTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_model_storagevolume.py", line 269, in test_storagevolume_action _do_volume_test(self, model, host, ssl_port, 'default') File "test_model_storagevolume.py", line 120, in _do_volume_test self.assertEquals(0, storagevolume['allocation']) AssertionError: 0 != 1024
-- Ramon Nunes Medeiros Kimchi Developer Linux Technology Center Brazil IBM Systems & Technology Group Phone : +55 19 2132 7878 ramonn@br.ibm.com
participants (2)
-
Aline Manera
-
Ramon Medeiros