isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.11k stars 409 forks source link

Handling null in encryptionCipher parameter of Hive.openBox causes runtime error #1304

Open manishgautammg3994 opened 3 months ago

manishgautammg3994 commented 3 months ago

When using the hive_flutter package (version: 2.0.0-dev), a runtime error occurs if the encryptionCipher parameter is set to null. The error trace points to internal Dart SDK files and Hive's binary reader implementation, which indicates that the current logic does not properly handle a null value for encryptionCipher.

Here is the relevant code snippet causing the issue:

static Future<CacheManagerImpl> setup({
  bool encrypt = false,
  String encryptKey = HiveKeys.encryptKey,
}) async {
  Hive.initFlutter();

  var encryptionKey = encryptKey.codeUnits;
  _box = await Hive.openBox(
    HiveKeys.globalkey,
    encryptionCipher: encrypt ? HiveAesCipher(encryptionKey) : null, // Bug: passing null here causes an error , say simply encryptionCipher: null
  );
}
Error Trace:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3       throw_
packages/hive/src/binary/binary_reader_impl.dart 325:11                           read
packages/hive/src/backend/js/native/storage_backend_js.dart 86:24                 <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 603:19               <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 627:23               <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 525:3                _asyncStartSync
packages/hive/src/backend/js/native/storage_backend_js.dart 79:19                 decodeValue
dart-sdk/lib/internal/iterable.dart 425:31                                        elementAt
dart-sdk/lib/internal/iterable.dart 354:26                                        moveNext
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 930:20  next
dart-sdk/lib/async/future.dart 532:16                                             wait
packages/hive/src/backend/js/native/storage_backend_js.dart 136:41                <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 603:19               <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 627:23               <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 525:3                _asyncStartSync
packages/hive/src/backend/js/native/storage_backend_js.dart 136:19                <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 426:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 451:28  dcall
dart-sdk/lib/html/dart2js/html_dart2js.dart 37257:58                              <fn>

Steps to Reproduce:

  1. Initialize the Hive database using the hive_flutter package.
  2. Attempt to open a Hive box with the encryptionCipher parameter set to null.
  3. Observe the runtime error.

Expected Behavior:

Proposed Solution:

Environment:

Additional Information:

This issue might impact developers who prefer conditional encryption based on user settings. Ensuring the encryptionCipher can be safely set to null or handled internally would improve the robustness of the hive_flutter package.

Labels: