Skip to content

Commit f275826

Browse files
committed
fixed Arbitrary Directory Creation and File Manipulation via Backup Handler
- coauthored by @jeremyHOT - GHSA-c64m-cx97-6rc9 - before this change, an attacker could add ../ to the backup path, which let them create unwanted directories and files
1 parent 1b542e9 commit f275826

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/ipc/storage.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,16 @@ function register() {
204204
const backupDir = settings.path;
205205
if (!backupDir) return { ok: false, error: "No backup path set" };
206206

207-
fs.mkdirSync(backupDir, { recursive: true });
207+
const resolvedDir = path.resolve(backupDir);
208+
209+
fs.mkdirSync(resolvedDir, { recursive: true });
208210

209211
const timestamp = new Date()
210212
.toISOString()
211213
.replace(/[:.]/g, "-")
212214
.slice(0, 19);
213215
const filename = `streambert-backup-${timestamp}.json`;
214-
const fullPath = path.join(backupDir, filename);
216+
const fullPath = path.join(resolvedDir, filename);
215217
fs.writeFileSync(
216218
fullPath,
217219
JSON.stringify(
@@ -229,19 +231,19 @@ function register() {
229231

230232
// Prune old backups
231233
const keepCount = Math.max(1, Number(settings.keepCount) || 5);
232-
fs.readdirSync(backupDir)
234+
fs.readdirSync(resolvedDir)
233235
.filter(
234236
(f) => f.startsWith("streambert-backup-") && f.endsWith(".json"),
235237
)
236238
.map((f) => ({
237239
name: f,
238-
mtime: fs.statSync(path.join(backupDir, f)).mtimeMs,
240+
mtime: fs.statSync(path.join(resolvedDir, f)).mtimeMs,
239241
}))
240242
.sort((a, b) => b.mtime - a.mtime)
241243
.slice(keepCount)
242244
.forEach(({ name }) => {
243245
try {
244-
fs.unlinkSync(path.join(backupDir, name));
246+
fs.unlinkSync(path.join(resolvedDir, name));
245247
} catch {}
246248
});
247249

0 commit comments

Comments
 (0)