-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: prevent Zip Slip path traversal in backup restore #1938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -533,7 +533,16 @@ Future<MigrationData> _restoreTask(RootIsolateToken token) async { | |
| final dir = Directory(restoreDirPath); | ||
| await dir.create(recursive: true); | ||
| for (final file in archive.files) { | ||
| final outPath = join(restoreDirPath, posix.normalize(file.name)); | ||
| final outPath = canonicalize(join(restoreDirPath, file.name)); | ||
| final canonicalRestoreDir = canonicalize(restoreDirPath); | ||
| if (!outPath.startsWith('$canonicalRestoreDir${Platform.pathSeparator}') && | ||
| outPath != canonicalRestoreDir) { | ||
| throw 'Invalid zip entry: path traversal detected in "${file.name}"'; | ||
| } | ||
| final parent = Directory(dirname(outPath)); | ||
| if (!await parent.exists()) { | ||
| await parent.create(recursive: true); | ||
| } | ||
| final outputStream = OutputFileStream(outPath); | ||
| file.writeContent(outputStream); | ||
| await outputStream.close(); | ||
|
Comment on lines
+542
to
548
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
canonicalRestoreDiris recomputed for every archive entry, even though it’s constant for the whole restore. Compute it once before the loop (and consider usingpath.isWithin(canonicalRestoreDir, outPath)for the containment check) to avoid repeated canonicalization and reduce the chance of subtle separator/edge-case bugs in the stringstartsWithlogic.