isar / isar/hive

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

Open
#1,304 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read packages/hive/src/backend/js/native/storage_backend_js.dart and packages/hive/src/binary/binary_reader_impl.dart, then reproduce the provided Hive.openBox call on Web with encryptionCipher: null. Clarify whether null should be accepted or rejected, and consider the issue complete only when the chosen behavior is covered by a regression check and the runtime error is no longer produced.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.