From 6bfbdbe20bc53eaaa305fde4b9e547fa2c538e2b Mon Sep 17 00:00:00 2001 From: SamuelToh Date: Wed, 9 Jan 2019 11:56:45 +1000 Subject: [PATCH 1/2] 11780: invalid reg value can cause unexpected behaviour --- public/app/core/specs/kbn.test.ts | 15 +++++++++++++++ public/app/core/time_series2.ts | 9 +++++++-- public/app/core/utils/kbn.ts | 5 +++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 public/app/core/specs/kbn.test.ts diff --git a/public/app/core/specs/kbn.test.ts b/public/app/core/specs/kbn.test.ts new file mode 100644 index 00000000000..25f82a5f850 --- /dev/null +++ b/public/app/core/specs/kbn.test.ts @@ -0,0 +1,15 @@ +import kbn from '../utils/kbn'; + +describe('stringToJsRegex', () => { + it('should parse the valid regex value', () => { + const output = kbn.stringToJsRegex("/validRegexp/"); + expect(output).toBeInstanceOf(RegExp); + }); + + it('should throw error on invalid regex value', () => { + const input = "/etc/hostname"; + expect(() => { + kbn.stringToJsRegex(input); + }).toThrow(); + }); +}); diff --git a/public/app/core/time_series2.ts b/public/app/core/time_series2.ts index 23a0a0c19ea..c24e1f64c43 100644 --- a/public/app/core/time_series2.ts +++ b/public/app/core/time_series2.ts @@ -1,4 +1,5 @@ import kbn from 'app/core/utils/kbn'; +import { appEvents } from 'app/core/core'; import { getFlotTickDecimals } from 'app/core/utils/ticks'; import _ from 'lodash'; import { getValueFormat } from '@grafana/ui'; @@ -9,8 +10,12 @@ function matchSeriesOverride(aliasOrRegex, seriesAlias) { } if (aliasOrRegex[0] === '/') { - const regex = kbn.stringToJsRegex(aliasOrRegex); - return seriesAlias.match(regex) != null; + try { + const regex = kbn.stringToJsRegex(aliasOrRegex); + return seriesAlias.match(regex) != null; + } catch (e) { + return appEvents.emit('alert-error', ['Invalid aliasOrRegex value.', e.message]); + } } return aliasOrRegex === seriesAlias; diff --git a/public/app/core/utils/kbn.ts b/public/app/core/utils/kbn.ts index 887c30229d3..43886fafd07 100644 --- a/public/app/core/utils/kbn.ts +++ b/public/app/core/utils/kbn.ts @@ -234,6 +234,11 @@ kbn.stringToJsRegex = str => { } const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$')); + + if (!match) { + throw new Error(`'${str}' is not a valid regular expression.`); + } + return new RegExp(match[1], match[2]); }; From d2c161c2e62c2c803064dda83d9354d1489a56d5 Mon Sep 17 00:00:00 2001 From: SamuelToh Date: Sat, 2 Mar 2019 20:50:25 +1000 Subject: [PATCH 2/2] Catch bad regex exception at controller level --- public/app/core/time_series2.ts | 9 ++------- public/app/plugins/panel/graph/module.ts | 6 +++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/public/app/core/time_series2.ts b/public/app/core/time_series2.ts index c24e1f64c43..23a0a0c19ea 100644 --- a/public/app/core/time_series2.ts +++ b/public/app/core/time_series2.ts @@ -1,5 +1,4 @@ import kbn from 'app/core/utils/kbn'; -import { appEvents } from 'app/core/core'; import { getFlotTickDecimals } from 'app/core/utils/ticks'; import _ from 'lodash'; import { getValueFormat } from '@grafana/ui'; @@ -10,12 +9,8 @@ function matchSeriesOverride(aliasOrRegex, seriesAlias) { } if (aliasOrRegex[0] === '/') { - try { - const regex = kbn.stringToJsRegex(aliasOrRegex); - return seriesAlias.match(regex) != null; - } catch (e) { - return appEvents.emit('alert-error', ['Invalid aliasOrRegex value.', e.message]); - } + const regex = kbn.stringToJsRegex(aliasOrRegex); + return seriesAlias.match(regex) != null; } return aliasOrRegex === seriesAlias; diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 3919c4f69a9..e3e3c4eb588 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -235,7 +235,11 @@ class GraphCtrl extends MetricsPanelCtrl { } for (const series of this.seriesList) { - series.applySeriesOverrides(this.panel.seriesOverrides); + try { + series.applySeriesOverrides(this.panel.seriesOverrides); + } catch (e) { + this.publishAppEvent('alert-error', [e.message]); + } if (series.unit) { this.panel.yaxes[series.yaxis - 1].format = series.unit;