Skip to content

Commit 4a41983

Browse files
authored
fix(core): malformed URIs will not throw exception (#29486)
Issue number: resolves #29479 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> 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? <!-- Please describe the behavior or changes that are being added by this PR. --> - 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 <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. -->
1 parent 31a5252 commit 4a41983

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

core/src/global/config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ export const configFromURL = (win: Window) => {
6060
.slice(1)
6161
.split('&')
6262
.map((entry) => entry.split('='))
63-
.map(([key, value]) => [decodeURIComponent(key), decodeURIComponent(value)])
63+
.map(([key, value]) => {
64+
try {
65+
return [decodeURIComponent(key), decodeURIComponent(value)];
66+
} catch (e) {
67+
return ['', ''];
68+
}
69+
})
6470
.filter(([key]) => startsWith(key, IONIC_PREFIX))
6571
.map(([key, value]) => [key.slice(IONIC_PREFIX.length), value])
6672
.forEach(([key, value]) => {

core/src/global/test/config-controller.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IonicConfig } from '../../interface';
2-
import { Config } from '../config';
2+
import { Config, configFromURL } from '../config';
33

44
describe('Config', () => {
55
it('should get a value from the config', () => {
@@ -82,4 +82,16 @@ describe('Config', () => {
8282
config.set('text0' as any, 'hola');
8383
expect(config.get('text0' as any, 'HEY')).toEqual('hola');
8484
});
85+
86+
it('should not throw an exception with a malformed URI', () => {
87+
// https://github.com/ionic-team/ionic-framework/issues/29479
88+
89+
expect(
90+
configFromURL({
91+
location: {
92+
search: '?test=%',
93+
},
94+
} as unknown as Window)
95+
).toEqual({});
96+
});
8597
});

0 commit comments

Comments
 (0)