Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 3 tasks
manishgautammg3994 opened this issue Aug 16, 2024 · 0 comments
Open
1 of 3 tasks

Comments

@manishgautammg3994
Copy link

manishgautammg3994 commented Aug 16, 2024

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:

  • If encryptionCipher is null, the Hive.openBox method should handle it gracefully, avoiding the runtime error.
  • Alternatively, there should be documentation or validation preventing null from being passed to encryptionCipher.

Proposed Solution:

  • Modify the internal logic to properly handle null values for the encryptionCipher parameter.
  • Add validation in the Hive.openBox method to ensure encryptionCipher is not null or provide a meaningful error message if it is or vice versa add extra null check.

Environment:

  • hive_flutter version: 2.0.0-dev
  • Dart SDK version: Dart 3.6.0 (build 3.6.0-128.0.dev)
  • Flutter version: 3.24.0-1.0.pre.511 • channel master
  • Platform: [Web]

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:

  • Bug
  • Enhancement
  • Documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant