fix: validate SteamVR preference backups

This commit is contained in:
XxJSONDeruloxX committed 2026-10-15 20:14:22 -04:00
1 parent fd7cb249b1
commit d8de6895c1
2 files changed
+45 -2

No files matched your search

@@ -28,10 +28,27 @@ def write(path, value):
os.replace(temporary, path)
def saved_preferences(path):
try:
backup = read(path)
if not isinstance(backup.get('section'), bool) or not isinstance(backup.get('values'), dict):
raise ValueError('invalid backup object')
for key in OVERRIDES:
entry = backup['values'].get(key)
if not isinstance(entry, list) or len(entry) != 2 or not isinstance(entry[0], bool):
raise ValueError('invalid saved preference: ' + key)
return backup
except ValueError as error:
raise ValueError(f'Invalid preference backup {path}; repair the saved section/values and retry restore. '
'The backup and current preferences have been preserved.') from error
def update(operation, settings_path, backup_path):
settings = read(settings_path)
if operation == 'apply':
if not backup_path.exists():
if backup_path.exists():
saved_preferences(backup_path)
else:
section = settings.get('steamvr', {})
write(backup_path, {'section': 'steamvr' in settings,
'values': {key: [key in section, section.get(key)] for key in OVERRIDES}})
@@ -40,7 +57,7 @@ def update(operation, settings_path, backup_path):
elif operation == 'restore':
if not backup_path.exists():
return
backup = read(backup_path)
backup = saved_preferences(backup_path)
section = settings.setdefault('steamvr', {})
for key in OVERRIDES:
present, value = backup['values'][key]
+26
View File
@@ -134,6 +134,32 @@ class SteamVrHelperTest(unittest.TestCase):
self.assertEqual(json.loads(path.read_text()), {'steamvr': {'motionSmoothing': True, 'somePreference': 42},
'dashboard': {'changedDuringSession': True}})
def test_invalid_backup_preserves_preferences_until_repaired(self):
settings = self.root / 'steamvr.vrsettings'
backup = self.root / 'quest-backup.json'
helper = SOURCE.with_name('droiddeck-steamvr-settings')
original = {'steamvr': {'motionSmoothing': True}, 'dashboard': {'unchanged': 42}}
settings.write_text(json.dumps(original))
subprocess.run([str(helper), 'apply', str(settings), str(backup)], check=True)
valid_backup = backup.read_bytes()
applied = settings.read_bytes()
invalid = ['truncated {', '{}', '{"section": true, "values": []}',
'{"section": false, "values": {"motionSmoothing": [false]}}']
for content in invalid:
for operation in ('apply', 'restore'):
with self.subTest(content=content, operation=operation):
backup.write_text(content)
result = subprocess.run([str(helper), operation, str(settings), str(backup)],
capture_output=True, text=True)
self.assertNotEqual(result.returncode, 0)
self.assertIn('repair the saved section/values and retry restore', result.stderr)
self.assertEqual(backup.read_text(), content)
self.assertEqual(settings.read_bytes(), applied)
backup.write_bytes(valid_backup)
subprocess.run([str(helper), 'restore', str(settings), str(backup)], check=True)
self.assertEqual(json.loads(settings.read_text()), original)
self.assertFalse(backup.exists())
def test_quest_update_preserves_previous_cvp_backup(self):
self.environment['DROIDDECK_VR_PRESENT'] = 'questwsi'
cvp = self.steamvr / 'drivers/cv/bin/linuxarm64/libArcturusPerception.so'