diff --git a/electron/src/electron-utils/utilsSQLite.ts b/electron/src/electron-utils/utilsSQLite.ts index 8dc47754..4ab99eab 100644 --- a/electron/src/electron-utils/utilsSQLite.ts +++ b/electron/src/electron-utils/utilsSQLite.ts @@ -88,7 +88,7 @@ export class UtilsSQLite { try { mDB.pragma(`cipher='sqlcipher'`); mDB.pragma(`legacy=4`); - mDB.pragma(`key='${passphrase}'`); + mDB.pragma(`key=${this.statUtil.quoteAsSQLStringLiteral(passphrase)}`); return; } catch (err: any) { const errmsg = err.message ? err.message : err; @@ -189,8 +189,8 @@ export class UtilsSQLite { try { mDB.pragma(`cipher='sqlcipher'`); mDB.pragma(`legacy=4`); - mDB.pragma(`key='${passphrase}'`); - mDB.pragma(`rekey='${newpassphrase}'`); + mDB.pragma(`key=${this.statUtil.quoteAsSQLStringLiteral(passphrase)}`); + mDB.pragma(`rekey=${this.statUtil.quoteAsSQLStringLiteral(newpassphrase)}`); return; } catch (err: any) { const errmsg = err.message ? err.message : err; diff --git a/electron/src/electron-utils/utilsSqlstatement.ts b/electron/src/electron-utils/utilsSqlstatement.ts index cbce725e..a001816b 100644 --- a/electron/src/electron-utils/utilsSqlstatement.ts +++ b/electron/src/electron-utils/utilsSqlstatement.ts @@ -168,6 +168,11 @@ export class UtilsSQLStatement { return indices; } + /** Returns the value as an SQL string literal: in single quotes, with each single quote doubled. */ + public quoteAsSQLStringLiteral(value: string): string { + return `'${value.replaceAll("'", "''")}'`; + } + public getWhereStmtForNonCombinedPK(whStmt: string, withRefs: string[], colNames: string[]): string { let whereStmt = ''; let stmt: string = whStmt.substring(6); diff --git a/ios/Plugin/Extensions/String.swift b/ios/Plugin/Extensions/String.swift index 23f1b1c3..33b7520c 100644 --- a/ios/Plugin/Extensions/String.swift +++ b/ios/Plugin/Extensions/String.swift @@ -39,5 +39,10 @@ extension String { public func trimmingLeadingAndTrailingSpaces(using characterSet: CharacterSet = .whitespacesAndNewlines) -> String { return trimmingCharacters(in: characterSet) } + /// Returns the string as an SQL string literal: in single quotes, with each single quote doubled. + public func quotedAsSQLStringLiteral() -> String { + // A literal search also finds a quote that is followed by a combining character. + return "'\(replacingOccurrences(of: "'", with: "''", options: .literal))'" + } } diff --git a/ios/Plugin/Utils/UtilsEncryption.swift b/ios/Plugin/Utils/UtilsEncryption.swift index 0b74598a..5f233920 100644 --- a/ios/Plugin/Utils/UtilsEncryption.swift +++ b/ios/Plugin/Utils/UtilsEncryption.swift @@ -41,8 +41,8 @@ class UtilsEncryption { password: password, readonly: false) - var stmt: String = "ATTACH DATABASE '\(filePath)' " - stmt.append("AS encrypted KEY '\(password)';") + var stmt: String = "ATTACH DATABASE \(filePath.quotedAsSQLStringLiteral()) " + stmt.append("AS encrypted KEY \(password.quotedAsSQLStringLiteral());") stmt.append("SELECT sqlcipher_export('encrypted');") stmt.append("DETACH DATABASE encrypted;") if sqlite3_exec(oDB, stmt, nil, nil, nil) == @@ -114,8 +114,8 @@ class UtilsEncryption { password: "", readonly: false) - var stmt: String = "PRAGMA key = '\(password)';" - stmt.append("ATTACH DATABASE '\(filePath)' ") + var stmt: String = "PRAGMA key = \(password.quotedAsSQLStringLiteral());" + stmt.append("ATTACH DATABASE \(filePath.quotedAsSQLStringLiteral()) ") stmt.append("AS plaintext KEY '';") stmt.append("SELECT sqlcipher_export('plaintext');") stmt.append("DETACH DATABASE plaintext;") diff --git a/ios/Plugin/Utils/UtilsSQLCipher.swift b/ios/Plugin/Utils/UtilsSQLCipher.swift index 772bf438..2b85d096 100644 --- a/ios/Plugin/Utils/UtilsSQLCipher.swift +++ b/ios/Plugin/Utils/UtilsSQLCipher.swift @@ -172,7 +172,7 @@ class UtilsSQLCipher { if password.count > 0 { let keyStatementString = """ - PRAGMA key = '\(password)'; + PRAGMA key = \(password.quotedAsSQLStringLiteral()); """ if sqlite3_exec(mDB, keyStatementString, nil, nil, nil) != SQLITE_OK { @@ -219,7 +219,7 @@ class UtilsSQLCipher { readonly: false) // change password let keyStatementString = """ - PRAGMA rekey = '\(newpassword)'; + PRAGMA rekey = \(newpassword.quotedAsSQLStringLiteral()); """ let returnCode: Int32 = sqlite3_exec( oDB, keyStatementString, nil, nil, nil)