Skip to content
Open
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
20 changes: 12 additions & 8 deletions DatabaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public class DatabaseManager {
public func buildDatabaseConfig(_ config: [AnyHashable: Any]?) -> DatabaseConfiguration {
var databaseConfiguration = DatabaseConfiguration()
if let encKey = config?["encryptionKey"] as? String {
let key = EncryptionKey.password(encKey)
databaseConfiguration.encryptionKey = key
#if COUCHBASE_ENTERPRISE
let key = EncryptionKey.password(encKey)
databaseConfiguration.encryptionKey = key
#endif
Comment on lines +59 to +62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When building without COUCHBASE_ENTERPRISE, the encryptionKey from the configuration is silently ignored. This can be misleading, as a developer might assume the database is being encrypted when it is not. This could lead to sensitive data being stored unencrypted without any warning.

To prevent this, it's better to provide explicit feedback. Since this function doesn't throw, consider adding an #else block to print a warning to the console.

Suggested change
#if COUCHBASE_ENTERPRISE
let key = EncryptionKey.password(encKey)
databaseConfiguration.encryptionKey = key
#endif
#if COUCHBASE_ENTERPRISE
let key = EncryptionKey.password(encKey)
databaseConfiguration.encryptionKey = key
#else
// Using print as a fallback, but a dedicated logger would be better if available.
print("Warning: Encryption is an enterprise-only feature. The provided encryptionKey will be ignored.")
#endif

}
if let directory = config?["directory"] as? String {
// Used to auto set the database to be in the documents folder,
Expand Down Expand Up @@ -149,12 +151,14 @@ public class DatabaseManager {
guard let database = self.getDatabase(databaseName) else {
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
}
if let newKeyString = newKey {
let encryptionKey = EncryptionKey.password(newKeyString)
try database.changeEncryptionKey(encryptionKey)
return
}
try database.changeEncryptionKey(nil)
#if COUCHBASE_ENTERPRISE
if let newKeyString = newKey {
let encryptionKey = EncryptionKey.password(newKeyString)
try database.changeEncryptionKey(encryptionKey)
return
}
try database.changeEncryptionKey(nil)
#endif
Comment on lines +154 to +161

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In a non-enterprise build, calling changeEncryptionKey currently does nothing and returns successfully. This is misleading and potentially dangerous, as the caller would assume the encryption key has been changed or removed when, in fact, no operation was performed.

Since this function can throw, it's better to throw an error in the non-enterprise case to clearly signal that the feature is not available. This prevents any misunderstanding about the database's encryption state.

        #if COUCHBASE_ENTERPRISE
            if let newKeyString =  newKey {
                let encryptionKey = EncryptionKey.password(newKeyString)
                try database.changeEncryptionKey(encryptionKey)
                return
            }
            try database.changeEncryptionKey(nil)
        #else
            throw DatabaseError.unknownError(message: "Changing the database encryption key is an enterprise-only feature.")
        #endif

return
}

Expand Down