From 090db5490b0266696b34a8620a17044bf876f84a Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 23 Mar 2016 15:58:00 +0000 Subject: [PATCH] URI-decode the hash-fragment It looks like % characters in the hash-fragment are meant to be interpreted as a URI-encoding, so we should decode them. --- src/vector/index.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/vector/index.js b/src/vector/index.js index ed5394123..b7ff42cf0 100644 --- a/src/vector/index.js +++ b/src/vector/index.js @@ -74,12 +74,27 @@ var validBrowser = checkBrowserFeatures([ // We want to support some name / value pairs in the fragment // so we're re-using query string like format +// +// returns {location, params} function parseQsFromFragment(location) { - var hashparts = location.hash.split('?'); + // if we have a fragment, it will start with '#', which we need to drop. + // (if we don't, this will return ''). + var fragment = location.hash.substring(1); + + // our fragment may contain a query-param-like section. we need to fish + // this out *before* URI-decoding because the params may contain ? and & + // characters which are only URI-encoded once. + var hashparts = fragment.split('?'); + + var result = { + location: decodeURIComponent(hashparts[0]), + params: {} + }; + if (hashparts.length > 1) { - return qs.parse(hashparts[1]); + result.params = qs.parse(hashparts[1]); } - return {}; + return result; } function parseQs(location) { @@ -92,14 +107,13 @@ function routeUrl(location) { var params = parseQs(location); var loginToken = params.loginToken; if (loginToken) { - window.matrixChat.showScreen('token_login', parseQs(location)); - } - else if (location.hash.indexOf('#/register') == 0) { - window.matrixChat.showScreen('register', parseQsFromFragment(location)); - } else { - var hashparts = location.hash.split('?'); - window.matrixChat.showScreen(hashparts[0].substring(2), parseQsFromFragment(location)); + window.matrixChat.showScreen('token_login', params); + return; } + + var fragparts = parseQsFromFragment(location); + window.matrixChat.showScreen(fragparts.location.substring(1), + fragparts.params); } function onHashChange(ev) { @@ -160,13 +174,14 @@ window.onload = function() { function loadApp() { if (validBrowser) { var MatrixChat = sdk.getComponent('structures.MatrixChat'); + var fragParts = parseQsFromFragment(window.location); window.matrixChat = ReactDOM.render( , document.getElementById('matrixchat') );