-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_bitwarden_export.py
More file actions
524 lines (433 loc) · 19.7 KB
/
validate_bitwarden_export.py
File metadata and controls
524 lines (433 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/usr/bin/env python3
"""
Bitwarden Export Validation Script
This script validates the integrity and quality of Bitwarden exports by comparing
the original input file with the organized output file. It performs various checks
to ensure data preservation and quality improvements.
"""
import json
import sys
import argparse
from pathlib import Path
from typing import Dict, List, Any, Set, Tuple
from collections import defaultdict, Counter
import hashlib
class BitwardenValidator:
"""Validates Bitwarden export files for integrity and quality."""
def __init__(self, input_file: str, output_file: str, verbose: bool = False):
self.input_file = Path(input_file)
self.output_file = Path(output_file)
self.verbose = verbose
self.input_data = None
self.output_data = None
self.validation_results = {}
def load_files(self) -> bool:
"""Load and parse both JSON files."""
try:
with open(self.input_file, 'r', encoding='utf-8') as f:
self.input_data = json.load(f)
print(f"✓ Loaded input file: {self.input_file}")
with open(self.output_file, 'r', encoding='utf-8') as f:
self.output_data = json.load(f)
print(f"✓ Loaded output file: {self.output_file}")
return True
except FileNotFoundError as e:
print(f"❌ File not found: {e}")
return False
except json.JSONDecodeError as e:
print(f"❌ Invalid JSON: {e}")
return False
except Exception as e:
print(f"❌ Error loading files: {e}")
return False
def validate_basic_structure(self) -> Dict[str, Any]:
"""Validate basic JSON structure and required fields."""
results = {
'valid': True,
'errors': [],
'warnings': []
}
# Check required top-level fields
required_fields = ['items']
for field in required_fields:
if field not in self.input_data:
results['errors'].append(f"Missing required field in input: {field}")
results['valid'] = False
if field not in self.output_data:
results['errors'].append(f"Missing required field in output: {field}")
results['valid'] = False
# Check if items is a list
if 'items' in self.input_data and not isinstance(self.input_data['items'], list):
results['errors'].append("Input 'items' field is not a list")
results['valid'] = False
if 'items' in self.output_data and not isinstance(self.output_data['items'], list):
results['errors'].append("Output 'items' field is not a list")
results['valid'] = False
# Check for organization vs personal vault
if 'collections' in self.input_data:
if 'collections' not in self.output_data:
results['warnings'].append("Input has collections but output is missing them")
elif 'folders' in self.input_data:
if 'folders' not in self.output_data:
results['warnings'].append("Input has folders but output is missing them")
return results
def validate_item_count(self) -> Dict[str, Any]:
"""Validate that the number of items is preserved."""
results = {
'valid': True,
'input_count': 0,
'output_count': 0,
'errors': []
}
input_count = len(self.input_data.get('items', []))
output_count = len(self.output_data.get('items', []))
results['input_count'] = input_count
results['output_count'] = output_count
if input_count != output_count:
results['errors'].append(
f"Item count mismatch: input has {input_count}, output has {output_count}"
)
results['valid'] = False
return results
def extract_credentials(self, items: List[Dict[str, Any]]) -> List[Tuple[str, str, str]]:
"""Extract username/password pairs from items."""
credentials = []
for item in items:
if item.get('type') == 1: # Login type
login = item.get('login', {})
username = login.get('username', '')
password = login.get('password', '')
name = item.get('name', 'Unknown')
if username and password: # Only include items with both username and password
credentials.append((username, password, name))
return credentials
def validate_credentials_preservation(self) -> Dict[str, Any]:
"""Validate that all username/password pairs are preserved."""
results = {
'valid': True,
'input_credentials': 0,
'output_credentials': 0,
'missing_credentials': [],
'errors': []
}
input_creds = self.extract_credentials(self.input_data.get('items', []))
output_creds = self.extract_credentials(self.output_data.get('items', []))
results['input_credentials'] = len(input_creds)
results['output_credentials'] = len(output_creds)
# Create sets for comparison (username, password) pairs
input_set = {(u, p) for u, p, n in input_creds}
output_set = {(u, p) for u, p, n in output_creds}
# Find missing credentials
missing = input_set - output_set
if missing:
results['valid'] = False
for username, password in missing:
# Find the item name for this credential
item_name = next((n for u, p, n in input_creds if u == username and p == password), 'Unknown')
results['missing_credentials'].append({
'username': username,
'password': password[:8] + '...' if len(password) > 8 else password,
'item_name': item_name
})
results['errors'].append(f"Missing {len(missing)} credential pairs")
return results
def validate_item_integrity(self) -> Dict[str, Any]:
"""Validate that individual items maintain their core data."""
results = {
'valid': True,
'input_items': 0,
'output_items': 0,
'modified_items': [],
'errors': []
}
input_items = self.input_data.get('items', [])
output_items = self.output_data.get('items', [])
results['input_items'] = len(input_items)
results['output_items'] = len(output_items)
# Create lookup dictionaries for comparison
input_lookup = {}
for item in input_items:
if item.get('type') == 1: # Login type
login = item.get('login', {})
username = login.get('username', '')
password = login.get('password', '')
if username and password:
key = (username, password)
input_lookup[key] = item
output_lookup = {}
for item in output_items:
if item.get('type') == 1: # Login type
login = item.get('login', {})
username = login.get('username', '')
password = login.get('password', '')
if username and password:
key = (username, password)
output_lookup[key] = item
# Compare items with the same credentials
for key in input_lookup:
if key in output_lookup:
input_item = input_lookup[key]
output_item = output_lookup[key]
# Check if critical fields are preserved
critical_fields = ['type', 'login.username', 'login.password', 'login.uris']
modifications = []
for field in critical_fields:
if '.' in field:
# Handle nested fields
parts = field.split('.')
input_val = input_item
output_val = output_item
for part in parts:
input_val = input_val.get(part, {}) if isinstance(input_val, dict) else None
output_val = output_val.get(part, {}) if isinstance(output_val, dict) else None
if input_val != output_val:
modifications.append(f"{field}: {input_val} → {output_val}")
else:
if input_item.get(field) != output_item.get(field):
modifications.append(f"{field}: {input_item.get(field)} → {output_item.get(field)}")
if modifications:
results['modified_items'].append({
'item_name': input_item.get('name', 'Unknown'),
'modifications': modifications
})
return results
def validate_organization_improvements(self) -> Dict[str, Any]:
"""Validate that organization improvements were made."""
results = {
'valid': True,
'folders_created': 0,
'collections_created': 0,
'items_with_folders': 0,
'items_with_collections': 0,
'items_with_tags': 0,
'warnings': []
}
# Check folders
if 'folders' in self.output_data:
results['folders_created'] = len(self.output_data['folders'])
# Count items assigned to folders
for item in self.output_data.get('items', []):
if item.get('folderId'):
results['items_with_folders'] += 1
# Check collections
if 'collections' in self.output_data:
results['collections_created'] = len(self.output_data['collections'])
# Count items assigned to collections
for item in self.output_data.get('items', []):
if item.get('collectionIds'):
results['items_with_collections'] += len(item['collectionIds'])
# Check tags
for item in self.output_data.get('items', []):
if item.get('tags'):
results['items_with_tags'] += 1
# Validate that organization actually happened
if results['folders_created'] == 0 and results['collections_created'] == 0:
results['warnings'].append("No folders or collections were created")
return results
def validate_metadata_preservation(self) -> Dict[str, Any]:
"""Validate that important metadata is preserved."""
results = {
'valid': True,
'items_with_notes': 0,
'items_with_uris': 0,
'items_with_creation_date': 0,
'items_with_revision_date': 0,
'errors': []
}
input_items = self.input_data.get('items', [])
output_items = self.output_data.get('items', [])
# Count metadata in input
for item in input_items:
if item.get('notes'):
results['items_with_notes'] += 1
if item.get('login', {}).get('uris'):
results['items_with_uris'] += 1
if item.get('creationDate'):
results['items_with_creation_date'] += 1
if item.get('revisionDate'):
results['items_with_revision_date'] += 1
# Verify metadata is preserved in output
for item in output_items:
if not item.get('notes') and item.get('login', {}).get('uris'):
# Check if this item had notes in input
input_item = self._find_matching_input_item(item)
if input_item and input_item.get('notes'):
results['errors'].append(f"Notes lost for item: {item.get('name', 'Unknown')}")
results['valid'] = False
return results
def _find_matching_input_item(self, output_item: Dict[str, Any]) -> Dict[str, Any]:
"""Find the corresponding input item for an output item."""
output_login = output_item.get('login', {})
output_username = output_login.get('username', '')
output_password = output_login.get('password', '')
for input_item in self.input_data.get('items', []):
input_login = input_item.get('login', {})
input_username = input_login.get('username', '')
input_password = input_login.get('password', '')
if (input_username == output_username and
input_password == output_password):
return input_item
return None
def generate_summary_report(self) -> str:
"""Generate a comprehensive summary report."""
report = []
report.append("=" * 60)
report.append("BITWARDEN EXPORT VALIDATION REPORT")
report.append("=" * 60)
report.append(f"Input file: {self.input_file}")
report.append(f"Output file: {self.output_file}")
report.append("")
# Basic validation
basic = self.validation_results.get('basic_structure', {})
report.append("📋 BASIC STRUCTURE VALIDATION")
report.append("-" * 30)
if basic.get('valid'):
report.append("✓ Basic structure is valid")
else:
report.append("❌ Basic structure has errors:")
for error in basic.get('errors', []):
report.append(f" - {error}")
for warning in basic.get('warnings', []):
report.append(f"⚠️ {warning}")
report.append("")
# Item count validation
count = self.validation_results.get('item_count', {})
report.append("🔢 ITEM COUNT VALIDATION")
report.append("-" * 30)
if count.get('valid'):
report.append(f"✓ Item count preserved: {count['input_count']} → {count['output_count']}")
else:
report.append("❌ Item count mismatch:")
for error in count.get('errors', []):
report.append(f" - {error}")
report.append("")
# Credentials validation
creds = self.validation_results.get('credentials_preservation', {})
report.append("🔐 CREDENTIALS VALIDATION")
report.append("-" * 30)
if creds.get('valid'):
report.append(f"✓ All credentials preserved: {creds['input_credentials']} pairs")
else:
report.append("❌ Missing credentials:")
for missing in creds.get('missing_credentials', []):
report.append(f" - {missing['item_name']}: {missing['username']}")
report.append("")
# Item integrity validation
integrity = self.validation_results.get('item_integrity', {})
report.append("🔍 ITEM INTEGRITY VALIDATION")
report.append("-" * 30)
if not integrity.get('modified_items'):
report.append("✓ All items maintain their core data")
else:
report.append(f"⚠️ {len(integrity['modified_items'])} items have modifications:")
for item in integrity['modified_items'][:5]: # Show first 5
report.append(f" - {item['item_name']}: {', '.join(item['modifications'][:3])}")
if len(integrity['modified_items']) > 5:
report.append(f" ... and {len(integrity['modified_items']) - 5} more")
report.append("")
# Organization improvements
org = self.validation_results.get('organization_improvements', {})
report.append("📁 ORGANIZATION IMPROVEMENTS")
report.append("-" * 30)
report.append(f"Folders created: {org.get('folders_created', 0)}")
report.append(f"Collections created: {org.get('collections_created', 0)}")
report.append(f"Items with folders: {org.get('items_with_folders', 0)}")
report.append(f"Items with collections: {org.get('items_with_collections', 0)}")
report.append(f"Items with tags: {org.get('items_with_tags', 0)}")
for warning in org.get('warnings', []):
report.append(f"⚠️ {warning}")
report.append("")
# Metadata preservation
metadata = self.validation_results.get('metadata_preservation', {})
report.append("📝 METADATA PRESERVATION")
report.append("-" * 30)
report.append(f"Items with notes: {metadata.get('items_with_notes', 0)}")
report.append(f"Items with URIs: {metadata.get('items_with_uris', 0)}")
report.append(f"Items with creation date: {metadata.get('items_with_creation_date', 0)}")
report.append(f"Items with revision date: {metadata.get('items_with_revision_date', 0)}")
if not metadata.get('valid'):
report.append("❌ Some metadata was lost:")
for error in metadata.get('errors', []):
report.append(f" - {error}")
report.append("")
# Overall assessment
report.append("🎯 OVERALL ASSESSMENT")
report.append("-" * 30)
all_valid = all(
result.get('valid', False)
for result in self.validation_results.values()
if isinstance(result, dict) and 'valid' in result
)
if all_valid:
report.append("✅ VALIDATION PASSED - Export is ready for import")
else:
report.append("❌ VALIDATION FAILED - Review issues before importing")
report.append("=" * 60)
return "\n".join(report)
def run_validation(self) -> bool:
"""Run all validation checks."""
print("🔍 Starting Bitwarden export validation...")
print(f"Input: {self.input_file}")
print(f"Output: {self.output_file}")
print()
# Load files
if not self.load_files():
return False
# Run all validation checks
self.validation_results = {
'basic_structure': self.validate_basic_structure(),
'item_count': self.validate_item_count(),
'credentials_preservation': self.validate_credentials_preservation(),
'item_integrity': self.validate_item_integrity(),
'organization_improvements': self.validate_organization_improvements(),
'metadata_preservation': self.validate_metadata_preservation()
}
# Generate and display report
report = self.generate_summary_report()
print(report)
# Return overall validation status
all_valid = all(
result.get('valid', False)
for result in self.validation_results.values()
if isinstance(result, dict) and 'valid' in result
)
return all_valid
def main():
"""Main function."""
parser = argparse.ArgumentParser(
description="Validate Bitwarden export files for integrity and quality",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python validate_bitwarden_export.py input.json output.json
python validate_bitwarden_export.py bw.json bw_organized.json --verbose
"""
)
parser.add_argument(
'input_file',
help='Path to the original Bitwarden export JSON file'
)
parser.add_argument(
'output_file',
help='Path to the organized Bitwarden export JSON file'
)
parser.add_argument(
'--verbose', '-v',
action='store_true',
help='Enable verbose output'
)
args = parser.parse_args()
# Validate input files exist
if not Path(args.input_file).exists():
print(f"❌ Input file not found: {args.input_file}")
sys.exit(1)
if not Path(args.output_file).exists():
print(f"❌ Output file not found: {args.output_file}")
sys.exit(1)
# Run validation
validator = BitwardenValidator(args.input_file, args.output_file, args.verbose)
success = validator.run_validation()
# Exit with appropriate code
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()