Files
wekan/models/lib/backupPaths.js
T
Lauri Ojansivu f1c89548e0 ZipBleed: a restored backup archive could write outside the files directory.
CRITICAL SECURITY ISSUE (CWE-22 Improper Limitation of a Pathname to a Restricted
Directory, "zip-slip").

A zip entry carries its own path, chosen by whoever built the archive, and path.join()
RESOLVES `..` segments instead of rejecting them. The restore checked only that an
entry's FIRST path segment was `attachments` or `avatars`, then joined the rest onto
the matching directory:

  const rel = entry.path.split('/').slice(1);
  const kind = rel[0];
  if (kind !== 'attachments' && kind !== 'avatars') continue;
  const destPath = path.join(attachmentsDir(), ...rel.slice(1));

So an entry named

  2026-07-25_12-00-00/attachments/../../../../etc/cron.d/wekan

satisfied that check - its first segment really is `attachments` - and joined its way
clean out of the files directory. With attachmentsDir() = /data/files/attachments it
resolves to /etc/cron.d/wekan, and the entry's contents were streamed there. A crafted
backup.zip could drop or overwrite a file anywhere the WeKan process could write.

restoreBackup is behind requireAdmin(), which is less of a barrier than it sounds:
that is exactly how a hostile archive arrives - offered as a backup to restore, at the
one moment nobody inspects the file they were handed, and nothing in an archive is
visible before it is unpacked.

Fixed in models/lib/backupPaths.js, next to the other pure backup helpers so it is
unit-testable without Meteor: safeEntryPath() resolves the candidate to an absolute
path and requires it to sit under the target directory. It compares against the base
plus a separator, because a bare startsWith(base) also accepts a sibling directory
whose name merely begins with the same letters (/data/files/attachments-evil). An
absolute segment is caught by the same check, since path.resolve discards everything
before it.

The data half of an archive names the Mongo collection to restore into, and that name
came from the entry too, so safeCollectionName() constrains it to a plain
[A-Za-z0-9_-]+ name - which also keeps a restore out of the database's internal
system.* collections, excluded from backups in the first place.

A refused entry is skipped and logged rather than thrown on, so one hostile entry in
an otherwise good archive cannot abort a genuine restore.

This is WeKan's own code, not a dependency: it is the same class as the unzipper
advisory GHSA-884w-698f-927f, but WeKan runs unzipper 0.12.x, which that advisory does
not affect. Found while reviewing the open dependency pull requests.

tests/zipbleed.test.cjs asserts the exact traversal above is refused, that a plain
entry still restores where it belongs, that absolute segments and prefix-matching
siblings are refused, and that the restore really calls both guards. One assertion
reproduces what the OLD code did with the attack path - path.join returning
/etc/cron.d/wekan - so the suite states the danger, not just the remedy.

Tests: 1359 passing.

Thanks to xet7 !
2026-07-25 23:52:31 +03:00

78 lines
3.5 KiB
JavaScript

'use strict';
// Pure, Meteor-free helpers for the Admin Panel / Attachments / Backup feature.
// Unit-tested in tests/backupPaths.test.cjs. Used by server/methods/backup.js.
const path = require('path');
// Resolve the files root from a writable-path base. WeKan's WRITABLE_PATH is
// either the parent of "files" (docker/dev: /data -> /data/files) or already
// ends in "files" (snap: $SNAP_COMMON/files -> unchanged). Handles both the '/'
// and '\\' separator so a Windows path is not double-suffixed.
function filesRootFrom(base) {
const b = base || '';
return b.endsWith('/files') || b.endsWith('\\files') ? b : path.join(b, 'files');
}
// Build the synced-cron schedule text for a saved backup schedule.
// daily -> "every day at HH:MM"
// weekly -> "on <Day> at HH:MM" (default Sunday)
// monthly -> "on the <N> day of the month at HH:MM" (default 1)
// Any other/absent frequency falls back to the daily form.
function scheduleText(s) {
const settings = s || {};
const [hh, mm] = (settings.time || '04:00').split(':');
const at = `at ${hh}:${mm}`;
if (settings.frequency === 'weekly') return `on ${settings.dayOfWeek || 'Sunday'} ${at}`;
if (settings.frequency === 'monthly') return `on the ${settings.dayOfMonth || 1} day of the month ${at}`;
return `every day ${at}`;
}
// ZipBleed: resolve a path for ONE entry of a restored backup archive, and refuse
// any that escapes the directory it is supposed to land in.
//
// A zip entry carries its own path, chosen by whoever made the archive, and
// `path.join()` RESOLVES `..` segments rather than rejecting them. So an entry named
//
// 2026-07-25_12-00-00/attachments/../../../../etc/cron.d/x
//
// passed the "is this under attachments/" check - its first segment really is
// `attachments` - and then joined its way clean out of the files directory, writing
// wherever the WeKan process could write. That is the classic zip-slip
// (CWE-22 path traversal), and restoring a backup is exactly when you are least
// inclined to inspect the file you were handed.
//
// The rule is the only one that holds: resolve the candidate to an absolute path and
// require it to be the base directory itself or something beneath it. Comparing
// against `base + sep` matters - a plain `startsWith(base)` also accepts a sibling
// directory whose name merely begins with the same letters (`/data/files-evil`).
//
// Returns the absolute destination, or null when the entry must be skipped.
function safeEntryPath(baseDir, segments) {
if (!baseDir) return null;
const parts = (Array.isArray(segments) ? segments : []).filter(
s => typeof s === 'string' && s !== '');
if (!parts.length) return null;
const base = path.resolve(baseDir);
// path.resolve also swallows an ABSOLUTE segment ('/etc/passwd' would discard the
// base entirely), which the containment check below catches as well.
const dest = path.resolve(base, ...parts);
if (dest === base) return null;
return dest.startsWith(base + path.sep) ? dest : null;
}
// The same problem for the data half of an archive: the entry names the Mongo
// collection to restore into. Anything but a plain name is refused - `system.*`
// collections are internal to the database and are excluded from backups in the
// first place, and a name with a separator or a `$` in it is not a collection
// WeKan ever writes.
function safeCollectionName(name) {
return typeof name === 'string'
&& /^[A-Za-z0-9_-]+$/.test(name)
&& !name.startsWith('system')
? name
: null;
}
module.exports = { filesRootFrom, scheduleText, safeEntryPath, safeCollectionName };