isar / hive

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

[Web] Issue with "Invalid argument(s): Invalid or corrupted pad block" in Flutter #1272

Open BokDevTeam opened 9 months ago

BokDevTeam commented 9 months ago

When using Hive in the web version, I encounter an issue in the following scenario.

The problem arises when the security key changes in the middle of using secureStorage, resulting in the error "Invalid argument(s): Invalid or corrupted pad block."

The issue occurs when calling Hive.deleteBoxFromDisk or any other Hive function during this situation. The code gets locked, and there is no further response.

I attempted to resolve the problem by using try-catch, intending to call Hive.deleteBoxFromDisk to handle the issue and then reload the webpage. However, I face an issue where the code freezes when attempting to call Hive.deleteBoxFromDisk, preventing the problem from being resolved. Refreshing the page resolves the issue, and it starts functioning normally again.

To reproduce the issue, follow these steps after running the code once. Uncomment either "test key 1" or "test key 2" to alternate between the security keys.

Please note that when running the code in Flutter, make sure to use the --web-port 3000 option to fix the port. This is essential to maintain the same repository and reproduce the problem.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  Log.initialize();

  await Hive.initFlutter();
  // await PersistentData().initialize();

  const secureStorage = FlutterSecureStorage(
    aOptions: AndroidOptions(
      encryptedSharedPreferences: true,
    ),
  );

  // if key not exists return null
  final encryptionKeyString = await secureStorage.read(key: 'secureKey');
  if (encryptionKeyString == null) {
    final key = Hive.generateSecureKey();
    await secureStorage.write(
      key: 'secureKey',
      value: base64UrlEncode(key),
    );
  }

  try {
    // test key 1
    // await secureStorage.write(
    //   key: 'secureKey',
    //   value: 'R9LRQSXERnYA6at85jsf4zpJCBE3HcKfGeoKbSCs-U0=',
    // );

    // test key 2
    // await secureStorage.write(
    //   key: 'secureKey',
    //   value: 'Pe-J9mcSNI-MHcCxt6MxqH8kAt8GD-UN0HKNIrpsnJs=',
    // );

    final key = await secureStorage.read(key: 'secureKey');
    print('Encryption key: $key');
    final encryptionKeyUint8List = base64Url.decode(key!);
    print('Encryption key Uint8List: $encryptionKeyUint8List');

    final encryptedBox = await Hive.openBox('vaultBox', encryptionCipher: HiveAesCipher(encryptionKeyUint8List));

    print(encryptedBox.get('secret'));
    encryptedBox.put('secret', 'Hive is cool');
    print(encryptedBox.get('secret'));
  } catch (e) {
    print(e);

    try {
      await Hive.deleteBoxFromDisk('vaultBox');
    } catch (e) {
      print(e);
    }
    print('--- delete vaultBox ---');
  }

  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}
hpelitebook745G2 commented 7 months ago

any updates?

bombo774 commented 7 months ago

Getting the same error

valtbarbos commented 7 months ago

Same error here

ElGaleon commented 5 months ago

I have encountered the same error. For me worked delete the IndexedDB of browser and refresh the page! You can find it under Application section of browser Inspector image

Hope this will work fine also for you

MattyBoy4444 commented 1 week ago

We are having a similar issue. Has anyone found a workaround?