isar / isar/hive

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

Open
#1,272 5 comments 1 reaction 0 assignees View on GitHub
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

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.
```dart
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!'),
),
),
);
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the supplied main entry point and reproduce it in Flutter web using --web-port 3000. Alternate the two shown secureStorage keys, then exercise Hive.openBox and Hive.deleteBoxFromDisk for vaultBox. Done means the corrupted-key case reports or recovers without freezing, while normal access continues to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
database, security, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.