diff --git a/InfoLogger/public/Model.js b/InfoLogger/public/Model.js index 7dcd8d802..308633a6a 100644 --- a/InfoLogger/public/Model.js +++ b/InfoLogger/public/Model.js @@ -333,7 +333,13 @@ export default class Model extends Observable { return; } else if (params.q) { this.getUserProfile(); - this.log.filter.fromObject(JSON.parse(params.q.replaceAll('\n', '\\n'))); + try { + this.log.filter.fromObject(JSON.parse(params.q.replaceAll('\n', '\\n'))); + } catch (error) { + this.log.filter.resetCriteria(); + this.updateRouteOnModelChange(); + this.notification.show(`Invalid URL filter format: ${error.message}`, 'danger'); + } } else { this.getUserProfile(); } diff --git a/InfoLogger/test/public/log-filter-actions-mocha.js b/InfoLogger/test/public/log-filter-actions-mocha.js index 4b67ebe56..1cba99c65 100644 --- a/InfoLogger/test/public/log-filter-actions-mocha.js +++ b/InfoLogger/test/public/log-filter-actions-mocha.js @@ -93,6 +93,26 @@ describe('Filter actions test-suite', async () => { assert.strictEqual(searchParams, expectedParams); }); + it('should redirect to default filters and show JSON parse error on malformed q in URI', async () => { + const expectedDefaultParams = '?q={"severity":{"in":"I W E F"}}'; + + const locationAndNotification = await page.evaluate(() => { + const params = { q: '{"severity":{"in":"W I E F"' }; + window.model.parseLocation(params); + return { + search: window.location.search, + notification: window.model.notification, + }; + }); + + assert.strictEqual(decodeURI(locationAndNotification.search), expectedDefaultParams); + assert.strictEqual(locationAndNotification.notification.type, 'danger'); + // CI/CD runs on Chromium so this assertion is based on Chromium's JSON engine's error message + assert.strictEqual( + locationAndNotification.notification.message, + 'Invalid URL filter format: Expected \',\' or \'}\' after property value in JSON at position 27 (line 1 column 28)'); + }); + it('should update URI with new encoded "match" criteria', async () => { /* eslint-disable max-len */ const decodedParams = '?q={"hostname":{"match":"\\"%ald_qdip01%"},"severity":{"in":"I W E F"}}';