From 29c1c5481c3f09f8b617652b7b0b60e0c74eb52c Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 4 Apr 2019 15:55:53 -0600 Subject: [PATCH] inline setupConnection, re-order method calls to call validateFormat later, convert static->instance methods where possible --- GRDB/Core/Database.swift | 153 +++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 80 deletions(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index b4a2738b6..51e9ed23b 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -203,101 +203,33 @@ extension Database { } throw DatabaseError(resultCode: .SQLITE_INTERNAL) // WTF SQLite? } - - private static func activateExtendedCodes(_ sqliteConnection: SQLiteConnection) throws { - let code = sqlite3_extended_result_codes(sqliteConnection, 1) - guard code == SQLITE_OK else { - throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) - } - } - - #if SQLITE_HAS_CODEC - private static func validateSQLCipher(_ sqliteConnection: SQLiteConnection) throws { - // https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 - // - // > In order to avoid situations where SQLite might be used - // > improperly at runtime, we strongly recommend that - // > applications institute a runtime test to ensure that the - // > application is actually using SQLCipher on the active - // > connection. - var sqliteStatement: SQLiteStatement? = nil - let code = sqlite3_prepare_v2(sqliteConnection, "PRAGMA cipher_version", -1, &sqliteStatement, nil) - guard code == SQLITE_OK else { - throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) - } - defer { - sqlite3_finalize(sqliteStatement) - } - if sqlite3_step(sqliteStatement) != SQLITE_ROW || (sqlite3_column_text(sqliteStatement, 0) == nil) { - throw DatabaseError(resultCode: .SQLITE_MISUSE, message: """ - GRDB is not linked against SQLCipher. \ - Check https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 - """) - } - } - - private static func set(passphrase: String, forConnection sqliteConnection: SQLiteConnection) throws { - let data = passphrase.data(using: .utf8)! - #if swift(>=5.0) - let code = data.withUnsafeBytes { - sqlite3_key(sqliteConnection, $0.baseAddress, Int32($0.count)) - } - #else - let code = data.withUnsafeBytes { - sqlite3_key(sqliteConnection, $0, Int32(data.count)) - } - #endif - guard code == SQLITE_OK else { - throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) - } - } - #endif - - private static func validateDatabaseFormat(_ sqliteConnection: SQLiteConnection) throws { - // Users are surprised when they open a picture as a database and - // see no error (https://github.com/groue/GRDB.swift/issues/54). - // - // So let's fail early if file is not a database, or encrypted with - // another passphrase. - let code = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil) - guard code == SQLITE_OK else { - throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) - } - } } extension Database { // MARK: - Database Setup - func setupConnection() throws { - do { - try Database.activateExtendedCodes(sqliteConnection) - #if SQLITE_HAS_CODEC - try Database.validateSQLCipher(sqliteConnection) - if let passphrase = configuration.passphrase { - try Database.set(passphrase: passphrase, forConnection: sqliteConnection) - } - #endif - try configuration.prepareDatabase?(self) - - try Database.validateDatabaseFormat(sqliteConnection) - } catch { - throw error - } - configuration.SQLiteConnectionDidOpen?() - } /// This method must be called after database initialization func setup() throws { // Setup trace first, so that setup queries are traced. setupTrace() - - try setupConnection() try setupForeignKeys() setupBusyMode() setupDefaultFunctions() setupDefaultCollations() observationBroker.installCommitAndRollbackHooks() + try activateExtendedCodes() + + #if SQLITE_HAS_CODEC + try validateSQLCipher() + if let passphrase = configuration.passphrase { + try setCipherPassphrase(passphrase) + } + #endif + + try configuration.prepareDatabase?(self) + try validateFormat() + configuration.SQLiteConnectionDidOpen?() } private func setupTrace() { @@ -402,6 +334,67 @@ extension Database { add(collation: .localizedCompare) add(collation: .localizedStandardCompare) } + + private func activateExtendedCodes() throws { + let code = sqlite3_extended_result_codes(sqliteConnection, 1) + guard code == SQLITE_OK else { + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) + } + } + + #if SQLITE_HAS_CODEC + private func validateSQLCipher() throws { + // https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 + // + // > In order to avoid situations where SQLite might be used + // > improperly at runtime, we strongly recommend that + // > applications institute a runtime test to ensure that the + // > application is actually using SQLCipher on the active + // > connection. + var sqliteStatement: SQLiteStatement? = nil + let code = sqlite3_prepare_v2(sqliteConnection, "PRAGMA cipher_version", -1, &sqliteStatement, nil) + guard code == SQLITE_OK else { + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) + } + defer { + sqlite3_finalize(sqliteStatement) + } + if sqlite3_step(sqliteStatement) != SQLITE_ROW || (sqlite3_column_text(sqliteStatement, 0) == nil) { + throw DatabaseError(resultCode: .SQLITE_MISUSE, message: """ + GRDB is not linked against SQLCipher. \ + Check https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 + """) + } + } + + private func setCipherPassphrase(_ passphrase: String) throws { + let data = passphrase.data(using: .utf8)! + #if swift(>=5.0) + let code = data.withUnsafeBytes { + sqlite3_key(sqliteConnection, $0.baseAddress, Int32($0.count)) + } + #else + let code = data.withUnsafeBytes { + sqlite3_key(sqliteConnection, $0, Int32(data.count)) + } + #endif + guard code == SQLITE_OK else { + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) + } + } + #endif + + private func validateFormat() throws { + // Users are surprised when they open a picture as a database and + // see no error (https://github.com/groue/GRDB.swift/issues/54). + // + // So let's fail early if file is not a database, or encrypted with + // another passphrase. + let code = sqlite3_exec(sqliteConnection, "SELECT * FROM sqlite_master LIMIT 1", nil, nil, nil) + guard code == SQLITE_OK else { + throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection))) + } + } } extension Database {