Skip to content

Commit 099454d

Browse files
committed
sqlcmd quote around DB name
1 parent 0e9a1d7 commit 099454d

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

__tests__/AzureSqlAction.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ describe('AzureSqlAction tests', () => {
5555
describe('sql script action tests for different auth types', () => {
5656
// Format: [test case description, connection string, expected sqlcmd arguments]
5757
const testCases = [
58-
['SQL login', 'Server=testServer.database.windows.net;Database=testDB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d testDB -U "testUser" -i "./TestFile.sql" -t 20'],
59-
['AAD password', 'Server=testServer.database.windows.net;Database=testDB;Authentication=Active Directory Password;User Id=testAADUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d testDB --authentication-method=ActiveDirectoryPassword -U "testAADUser" -i "./TestFile.sql" -t 20'],
60-
['AAD service principal', 'Server=testServer.database.windows.net;Database=testDB;Authentication=Active Directory Service Principal;User Id=appId;Password=placeholder', '-S testServer.database.windows.net,1433 -d testDB --authentication-method=ActiveDirectoryServicePrincipal -U "appId" -i "./TestFile.sql" -t 20'],
61-
['AAD default', 'Server=testServer.database.windows.net;Database=testDB;Authentication=Active Directory Default;', '-S testServer.database.windows.net,1433 -d testDB --authentication-method=ActiveDirectoryDefault -i "./TestFile.sql" -t 20']
58+
['SQL login', 'Server=testServer.database.windows.net;Database=testDB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d "testDB" -U "testUser" -i "./TestFile.sql" -t 20'],
59+
['AAD password', 'Server=testServer.database.windows.net;Database=testDB;Authentication=Active Directory Password;User Id=testAADUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d "testDB" --authentication-method=ActiveDirectoryPassword -U "testAADUser" -i "./TestFile.sql" -t 20'],
60+
['AAD service principal', 'Server=testServer.database.windows.net;Database=testDB;Authentication=Active Directory Service Principal;User Id=appId;Password=placeholder', '-S testServer.database.windows.net,1433 -d "testDB" --authentication-method=ActiveDirectoryServicePrincipal -U "appId" -i "./TestFile.sql" -t 20'],
61+
['AAD default', 'Server=testServer.database.windows.net;Database=testDB;Authentication=Active Directory Default;', '-S testServer.database.windows.net,1433 -d "testDB" --authentication-method=ActiveDirectoryDefault -i "./TestFile.sql" -t 20'],
62+
['SQL login (DB has space)', 'Server=testServer.database.windows.net;Database=test DB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d "test DB" -U "testUser" -i "./TestFile.sql" -t 20'],
63+
['SQL login (DB has quote)', 'Server=testServer.database.windows.net;Database=test"DB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d "test\\"DB" -U "testUser" -i "./TestFile.sql" -t 20']
6264
];
6365

6466
it.each(testCases)('%s', async (testCase, connectionString, expectedSqlCmdCall) => {
@@ -88,8 +90,8 @@ describe('AzureSqlAction tests', () => {
8890
describe('sql script action tests for different port numbers', () => {
8991
// Format: [test case description, connection string, expected sqlcmd arguments]
9092
const testCases = [
91-
['Default port', 'Server=testServer.database.windows.net;Database=testDB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d testDB -U "testUser" -i "./TestFile.sql" -t 20'],
92-
['Custom port', 'Server=testServer.database.windows.net,1234;Database=testDB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1234 -d testDB -U "testUser" -i "./TestFile.sql" -t 20']
93+
['Default port', 'Server=testServer.database.windows.net;Database=testDB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1433 -d "testDB" -U "testUser" -i "./TestFile.sql" -t 20'],
94+
['Custom port', 'Server=testServer.database.windows.net,1234;Database=testDB;User Id=testUser;Password=placeholder', '-S testServer.database.windows.net,1234 -d "testDB" -U "testUser" -i "./TestFile.sql" -t 20']
9395
];
9496

9597
it.each(testCases)('%s', async (testCase, connectionString, expectedSqlCmdCall) => {

lib/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SqlConnectionConfig.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ export default class SqlConnectionConfig {
8383
return result;
8484
}
8585

86+
/**
87+
* Returns the database name enclosed by quotes for use in sqlcmd commands.
88+
* Escapes any quotes in the database name with \"
89+
*/
90+
public get QuotedDatabaseName(): string {
91+
let dbName = this.Database.replace(/"/g, '\\"');
92+
return `"${dbName}"`;
93+
}
94+
8695
/**
8796
* The basic format of a connection string includes a series of keyword/value pairs separated by semicolons.
8897
* The equal sign (=) connects each keyword and its value. (Ex: Key1=Val1;Key2=Val2)

src/SqlUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class SqlUtils {
5252
* @returns A ConnectionResult object indicating success/failure, the connection on success, or the error on failure.
5353
*/
5454
private static async tryConnection(config: SqlConnectionConfig, useMaster?: boolean): Promise<ConnectionResult> {
55-
const database = useMaster ? "master" : config.Database;
55+
const database = useMaster ? "master" : config.QuotedDatabaseName;
5656

5757
let sqlCmdError = '';
5858
try {
@@ -120,7 +120,7 @@ export default class SqlUtils {
120120
}
121121

122122
if (!database) {
123-
database = connectionConfig.Database;
123+
database = connectionConfig.QuotedDatabaseName;
124124
}
125125

126126
let sqlcmdCall = `"${sqlCmdPath}" -S ${connectionConfig.Server},${connectionConfig.Port ?? 1433} -d ${database}`;

0 commit comments

Comments
 (0)