From 96f4ebae5a8179a13bd09aff1388efe46fb8455f Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Thu, 9 May 2024 16:47:47 -0400 Subject: [PATCH] fix(core): malformed URIs will not throw exception --- core/src/global/config.ts | 8 +++++++- core/src/global/test/config-controller.spec.ts | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/src/global/config.ts b/core/src/global/config.ts index e79de898651..47fbf3b1919 100644 --- a/core/src/global/config.ts +++ b/core/src/global/config.ts @@ -60,7 +60,13 @@ export const configFromURL = (win: Window) => { .slice(1) .split('&') .map((entry) => entry.split('=')) - .map(([key, value]) => [decodeURIComponent(key), decodeURIComponent(value)]) + .map(([key, value]) => { + try { + return [decodeURIComponent(key), decodeURIComponent(value)]; + } catch (e) { + return ['', '']; + } + }) .filter(([key]) => startsWith(key, IONIC_PREFIX)) .map(([key, value]) => [key.slice(IONIC_PREFIX.length), value]) .forEach(([key, value]) => { diff --git a/core/src/global/test/config-controller.spec.ts b/core/src/global/test/config-controller.spec.ts index 7550c5fc0ac..0be7fec0aea 100644 --- a/core/src/global/test/config-controller.spec.ts +++ b/core/src/global/test/config-controller.spec.ts @@ -1,5 +1,5 @@ import type { IonicConfig } from '../../interface'; -import { Config } from '../config'; +import { Config, configFromURL } from '../config'; describe('Config', () => { it('should get a value from the config', () => { @@ -82,4 +82,16 @@ describe('Config', () => { config.set('text0' as any, 'hola'); expect(config.get('text0' as any, 'HEY')).toEqual('hola'); }); + + it('should not throw an exception with a malformed URI', () => { + // https://github.com/ionic-team/ionic-framework/issues/29479 + + expect( + configFromURL({ + location: { + search: '?test=%', + }, + } as unknown as Window) + ).toEqual({}); + }); });