InfluxDB: Fix escaping template variable when it was used in parentheses (#83400)

escape properly regardless of the parentheses
This commit is contained in:
ismail simsek 2024-02-27 15:40:32 +01:00 committed by GitHub
parent e564024883
commit 6fab62739d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View File

@ -443,6 +443,19 @@ describe('InfluxDataSource Frontend Mode', () => {
expect(result).toBe(expectation);
});
it('should return the escaped value if the value wrapped in regex 3', () => {
const value = ['env', 'env2', 'env3'];
const variableMock = queryBuilder()
.withId('tempVar')
.withName('tempVar')
.withMulti(false)
.withIncludeAll(true)
.build();
const result = ds.interpolateQueryExpr(value, variableMock, 'select from /^($tempVar)$/');
const expectation = `env|env2|env3`;
expect(result).toBe(expectation);
});
it('should **not** return the escaped value if the value **is not** wrapped in regex', () => {
const value = '/special/path';
const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build();

View File

@ -316,8 +316,8 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
// we want to see how it's been used. If it is used in a regex expression
// we escape it. Otherwise, we return it directly.
// regex below checks if the variable inside /^...$/ (^ and $ is optional)
// i.e. /^$myVar$/ or /$myVar/
const regex = new RegExp(`\\/(?:\\^)?\\$${variable.name}(?:\\$)?\\/`, 'gm');
// i.e. /^$myVar$/ or /$myVar/ or /^($myVar)$/
const regex = new RegExp(`\\/(?:\\^)?(.*)(\\$${variable.name})(.*)(?:\\$)?\\/`, 'gm');
if (regex.test(query)) {
if (typeof value === 'string') {
return escapeRegex(value);