mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 17:15:40 -06:00
11780: invalid reg value can cause unexpected behaviour
This commit is contained in:
parent
1478f38dd9
commit
6bfbdbe20b
15
public/app/core/specs/kbn.test.ts
Normal file
15
public/app/core/specs/kbn.test.ts
Normal file
@ -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();
|
||||
});
|
||||
});
|
@ -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;
|
||||
|
@ -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]);
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user