Skip to content

Commit f766535

Browse files
Guard popup import flow against invalid backups and read errors
Importing data in the popup assumed file reads, JSON parsing, and storage writes would always succeed, leaving failures unhandled and giving users no feedback. Catch errors across the import flow and validate that parsed backup data is a plain object before writing it to storage, so invalid files fail clearly without changing the successful restore path.
1 parent 5a59aa0 commit f766535

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/popup/sections/GeneralPart.jsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,22 @@ export function GeneralPart({ config, updateConfig, setTabIndex }) {
568568
input.click()
569569
})
570570
if (!file) return
571-
const data = await new Promise((resolve) => {
572-
const reader = new FileReader()
573-
reader.onload = (e) => resolve(JSON.parse(e.target.result))
574-
reader.readAsText(file)
575-
})
576-
await importDataIntoStorage(Browser.storage.local, data)
577-
window.location.reload()
571+
try {
572+
const parsedData = JSON.parse(await file.text())
573+
const isPlainObject =
574+
parsedData !== null && typeof parsedData === 'object' && !Array.isArray(parsedData)
575+
576+
if (!isPlainObject) {
577+
throw new Error('Invalid backup file')
578+
}
579+
580+
await importDataIntoStorage(Browser.storage.local, parsedData)
581+
window.location.reload()
582+
} catch (error) {
583+
console.error('[popup] Failed to import data', error)
584+
const message = error instanceof Error ? error.message : String(error ?? t('Error'))
585+
window.alert(`${t('Error')}: ${message}`)
586+
}
578587
}}
579588
>
580589
{t('Import All Data')}

0 commit comments

Comments
 (0)