Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions electron/src/electron-utils/utilsSQLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions electron/src/electron-utils/utilsSqlstatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions ios/Plugin/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))'"
}

}
8 changes: 4 additions & 4 deletions ios/Plugin/Utils/UtilsEncryption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) ==
Expand Down Expand Up @@ -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;")
Expand Down
4 changes: 2 additions & 2 deletions ios/Plugin/Utils/UtilsSQLCipher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Loading