From bb7e5838635fa044e75507c03827e4ba97cb7f53 Mon Sep 17 00:00:00 2001 From: Brice Maron Date: Wed, 1 Aug 2018 19:38:13 +0200 Subject: [PATCH] fix custom variable quoting in sql* query interpolations --- public/app/plugins/datasource/mssql/datasource.ts | 4 ++-- .../app/plugins/datasource/mssql/specs/datasource.jest.ts | 7 +++++++ public/app/plugins/datasource/mysql/datasource.ts | 4 ++-- .../app/plugins/datasource/mysql/specs/datasource.jest.ts | 7 +++++++ public/app/plugins/datasource/postgres/datasource.ts | 4 ++-- .../plugins/datasource/postgres/specs/datasource.jest.ts | 7 +++++++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/datasource/mssql/datasource.ts b/public/app/plugins/datasource/mssql/datasource.ts index 6656d4f96f7..dab7335ec97 100644 --- a/public/app/plugins/datasource/mssql/datasource.ts +++ b/public/app/plugins/datasource/mssql/datasource.ts @@ -16,7 +16,7 @@ export class MssqlDatasource { interpolateVariable(value, variable) { if (typeof value === 'string') { if (variable.multi || variable.includeAll) { - return "'" + value + "'"; + return "'" + value.replace(/'/g, `''`) + "'"; } else { return value; } @@ -31,7 +31,7 @@ export class MssqlDatasource { return value; } - return "'" + val + "'"; + return "'" + val.replace(/'/g, `''`) + "'"; }); return quotedValues.join(','); } diff --git a/public/app/plugins/datasource/mssql/specs/datasource.jest.ts b/public/app/plugins/datasource/mssql/specs/datasource.jest.ts index dd2d4a60cec..0308717775b 100644 --- a/public/app/plugins/datasource/mssql/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/mssql/specs/datasource.jest.ts @@ -218,6 +218,13 @@ describe('MSSQLDatasource', function() { }); }); + describe('and variable contains single quote', () => { + it('should return a quoted value', () => { + ctx.variable.multi = true; + expect(ctx.ds.interpolateVariable("a'bc", ctx.variable)).toEqual("'a''bc'"); + }); + }); + describe('and variable allows all and value is a string', () => { it('should return a quoted value', () => { ctx.variable.includeAll = true; diff --git a/public/app/plugins/datasource/mysql/datasource.ts b/public/app/plugins/datasource/mysql/datasource.ts index 42fcf7b4564..67bb9d0a817 100644 --- a/public/app/plugins/datasource/mysql/datasource.ts +++ b/public/app/plugins/datasource/mysql/datasource.ts @@ -16,7 +16,7 @@ export class MysqlDatasource { interpolateVariable(value, variable) { if (typeof value === 'string') { if (variable.multi || variable.includeAll) { - return "'" + value + "'"; + return "'" + value.replace(/'/g, `''`) + "'"; } else { return value; } @@ -31,7 +31,7 @@ export class MysqlDatasource { return value; } - return "'" + val + "'"; + return "'" + val.replace(/'/g, `''`) + "'"; }); return quotedValues.join(','); } diff --git a/public/app/plugins/datasource/mysql/specs/datasource.jest.ts b/public/app/plugins/datasource/mysql/specs/datasource.jest.ts index be33f5f8858..85fa2b8cc4e 100644 --- a/public/app/plugins/datasource/mysql/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/mysql/specs/datasource.jest.ts @@ -214,6 +214,13 @@ describe('MySQLDatasource', function() { }); }); + describe('and variable contains single quote', () => { + it('should return a quoted value', () => { + ctx.variable.multi = true; + expect(ctx.ds.interpolateVariable("a'bc", ctx.variable)).toEqual("'a''bc'"); + }); + }); + describe('and variable allows all and value is a string', () => { it('should return a quoted value', () => { ctx.variable.includeAll = true; diff --git a/public/app/plugins/datasource/postgres/datasource.ts b/public/app/plugins/datasource/postgres/datasource.ts index 8eee389d1a5..644c9e48b9b 100644 --- a/public/app/plugins/datasource/postgres/datasource.ts +++ b/public/app/plugins/datasource/postgres/datasource.ts @@ -16,7 +16,7 @@ export class PostgresDatasource { interpolateVariable(value, variable) { if (typeof value === 'string') { if (variable.multi || variable.includeAll) { - return "'" + value + "'"; + return "'" + value.replace(/'/g, `''`) + "'"; } else { return value; } @@ -27,7 +27,7 @@ export class PostgresDatasource { } var quotedValues = _.map(value, function(val) { - return "'" + val + "'"; + return "'" + val.replace(/'/g, `''`) + "'"; }); return quotedValues.join(','); } diff --git a/public/app/plugins/datasource/postgres/specs/datasource.jest.ts b/public/app/plugins/datasource/postgres/specs/datasource.jest.ts index 107cd76e6c5..cd6f57ee3fc 100644 --- a/public/app/plugins/datasource/postgres/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/postgres/specs/datasource.jest.ts @@ -215,6 +215,13 @@ describe('PostgreSQLDatasource', function() { }); }); + describe('and variable contains single quote', () => { + it('should return a quoted value', () => { + ctx.variable.multi = true; + expect(ctx.ds.interpolateVariable("a'bc", ctx.variable)).toEqual("'a''bc'"); + }); + }); + describe('and variable allows all and is a string', () => { it('should return a quoted value', () => { ctx.variable.includeAll = true;