[chrome] strip # from hash before updating $location (#32732)

* add functional test to prove regression

* [chrome] strip # from hash before updating $location

* add opt-out level for disabling auto fix, disable in APM
This commit is contained in:
Spencer 2019-03-10 13:34:56 -07:00 committed by GitHub
parent 97f8145fd5
commit 319921ebfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 14 deletions

View file

@ -34,6 +34,12 @@ const URL_LIMIT_WARN_WITHIN = 1000;
export function initAngularApi(chrome, internals) {
chrome.getFirstPathSegment = _.noop;
internals.disableAutoAngularUrlEncodingFix = false;
chrome.disableAutoAngularUrlEncodingFix = () => {
internals.disableAutoAngularUrlEncodingFix = true;
return chrome;
};
chrome.setupAngular = function () {
const kibana = uiModules.get('kibana');

View file

@ -74,19 +74,21 @@ export function kbnChromeProvider(chrome, internals) {
}
}
$rootScope.$on('$locationChangeStart', (e, newUrl) => {
// This handler fixes issue #31238 where browser back navigation
// fails due to angular 1.6 parsing url encoded params wrong.
const absUrlHash = url.parse($location.absUrl()).hash;
const decodedAbsUrlHash = decodeURIComponent(absUrlHash);
const hash = url.parse(newUrl).hash;
const decodedHash = decodeURIComponent(hash);
if (absUrlHash !== hash && decodedHash === decodedAbsUrlHash) {
// replace the urlencoded hash with the version that angular sees.
$location.url(absUrlHash).replace();
}
if (!internals.disableAutoAngularUrlEncodingFix) {
$rootScope.$on('$locationChangeStart', (e, newUrl) => {
// This handler fixes issue #31238 where browser back navigation
// fails due to angular 1.6 parsing url encoded params wrong.
const absUrlHash = url.parse($location.absUrl()).hash.slice(1);
const decodedAbsUrlHash = decodeURIComponent(absUrlHash);
const hash = url.parse(newUrl).hash.slice(1);
const decodedHash = decodeURIComponent(hash);
if (absUrlHash !== hash && decodedHash === decodedAbsUrlHash) {
// replace the urlencoded hash with the version that angular sees.
$location.url(absUrlHash).replace();
}
});
}
});
$rootScope.$on('$routeChangeSuccess', onRouteChange);
$rootScope.$on('$routeUpdate', onRouteChange);
updateSubUrls(); // initialize sub urls

View file

@ -45,6 +45,7 @@ declare interface Chrome extends ChromeNavLinks {
addApplicationClass(classNames: string | string[]): this;
removeApplicationClass(classNames: string | string[]): this;
getApplicationClasses(): string;
disableAutoAngularUrlEncodingFix(): this;
}
declare const chrome: Chrome;

View file

@ -22,16 +22,18 @@ import expect from 'expect.js';
export default function ({ getService, getPageObjects }) {
const browser = getService('browser');
const PageObjects = getPageObjects(['dashboard', 'common', 'home', 'timePicker']);
const PageObjects = getPageObjects(['common', 'home', 'timePicker']);
const appsMenu = getService('appsMenu');
const kibanaServer = getService('kibanaServer');
const esArchiver = getService('esArchiver');
const retry = getService('retry');
const fromTime = '2015-09-19 06:31:44.000';
const toTime = '2015-09-23 18:31:44.000';
describe('Kibana browser back navigation should work', function describeIndexTests() {
before(async () => {
await PageObjects.dashboard.initTests();
await esArchiver.loadIfNeeded('makelogs');
await kibanaServer.uiSettings.disableToastAutohide();
await browser.refresh();
});
@ -69,6 +71,18 @@ export default function ({ getService, getPageObjects }) {
currUrl = await browser.getCurrentUrl();
expect(currUrl).to.be(homeUrl);
});
it('encodes portions of the URL as necessary', async () => {
await browser.get('http://localhost:5620/app/kibana#/home', false);
await retry.waitFor('navigation to home app', async () => (
(await browser.getCurrentUrl()) === 'http://localhost:5620/app/kibana#/home?_g=()'
));
await browser.get('http://localhost:5620/app/kibana#/home?_g=()&a=b/c', false);
await retry.waitFor('hash to be properly encoded', async () => (
(await browser.getCurrentUrl()) === 'http://localhost:5620/app/kibana#/home?_g=()&a=b%2Fc'
));
});
});
}

View file

@ -38,6 +38,7 @@ type PromiseResolver = (value?: {} | PromiseLike<{}> | undefined) => void;
// @ts-ignore
chrome.setRootTemplate(template);
chrome.disableAutoAngularUrlEncodingFix();
const store = configureStore();
const checkForRoot = (resolve: PromiseResolver) => {
const ready = !!document.getElementById(REACT_APP_ROOT_ID);