Skip to content

Commit 211a6ec

Browse files
authored
Use temporary memory database connection to quote the password. (#36962)
1 parent b113a59 commit 211a6ec

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ public SqliteConnectionInternal(SqliteConnectionStringBuilder connectionOptions,
109109

110110
// NB: SQLite doesn't support parameters in PRAGMA statements, so we escape the value using the
111111
// quote function before concatenating.
112-
var quotedPassword = ExecuteScalar(
113-
"SELECT quote($password);",
114-
connectionOptions.Password,
115-
connectionOptions.DefaultTimeout);
112+
var quotedPassword = QuotePassword(connectionOptions.Password);
116113
ExecuteNonQuery(
117114
"PRAGMA key = " + quotedPassword + ";",
118115
connectionOptions.DefaultTimeout);
@@ -196,6 +193,29 @@ public void Dispose()
196193
_pool = null;
197194
}
198195

196+
private string QuotePassword(string password)
197+
{
198+
SqliteException.ThrowExceptionForRC(sqlite3_open(":memory:", out var db), db);
199+
try
200+
{
201+
SqliteException.ThrowExceptionForRC(sqlite3_prepare_v2(db, "SELECT quote($password);", out var stmt), db);
202+
try
203+
{
204+
sqlite3_bind_text(stmt, 1, password);
205+
SqliteException.ThrowExceptionForRC(sqlite3_step(stmt), db);
206+
return sqlite3_column_text(stmt, 0).utf8_to_string();
207+
}
208+
finally
209+
{
210+
stmt.Dispose();
211+
}
212+
}
213+
finally
214+
{
215+
db.Dispose();
216+
}
217+
}
218+
199219
private void ExecuteNonQuery(string sql, int timeout)
200220
=> RetryWhileBusy(() => sqlite3_exec(_db, sql), timeout);
201221

0 commit comments

Comments
 (0)