From 4a41983098fe9bf83fdf05ce7ab28c79f414e11b Mon Sep 17 00:00:00 2001 From: Sean Perkins <13732623+sean-perkins@users.noreply.github.com> Date: Wed, 15 May 2024 15:30:21 -0400 Subject: [PATCH] fix(core): malformed URIs will not throw exception (#29486) Issue number: resolves #29479 --------- ## What is the current behavior? If an application includes a malformed URI, an Ionic Framework can "crash" due to an uncaught exception in parsing the URI for the Ionic config. ## What is the new behavior? - Handle the malformed URI fallback if the config cannot be determined - Added unit tests for this case ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information --- 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({}); + }); });