mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
A file chosen on the Import page for the generalized importer's sources is POSTed to /api/import/upload/:source and written to WRITABLE_PATH/import-uploads a part at a time, instead of being read in the browser and sent whole, as text or base64, through importBoard. The size the browser announces is checked before the first byte against the largest import set in Admin Panel / Settings / Security (none: no limit) and against the free space of that disk less a reserve, and the space is checked again every 64 MB while it is written; a refused or interrupted upload leaves no file behind. importBoard then reads the file by its id: a .zip export (Slack, Google Takeout, Vikunja, Notion, Plane) is opened from the disk, its entries inflating up to the limit or the free space, and a mailbox is read a message at a time, also inside a Takeout zip. The upload is removed once read, or after an hour. The HTML-only mail text is read through the parsed document rather than stripped with a pattern. TODO.md says what is left of large files, and which locale files hold another language. Thanks to xet7 !
159 lines
7.9 KiB
JavaScript
159 lines
7.9 KiB
JavaScript
'use strict';
|
|
// Server-only: the .zip exports of Slack and Google Takeout, opened into what
|
|
// models/lib/chatExportFormats.js and models/lib/googleTakeoutFormat.js read.
|
|
//
|
|
// An upload is the user's file, so a zip is read with the guards of the other
|
|
// zip imports (server/lib/vikunjaArchive.js, ZipBombBleed): the upload and the
|
|
// number of entries are capped before anything inflates, the size an entry
|
|
// declares is checked first, and every entry read inflates through
|
|
// readZipEntryBounded, counting what actually comes out against one budget
|
|
// for the whole archive. Nothing is written to disk and an entry's name is
|
|
// only matched, never used as a path. Attachments and images are not read.
|
|
const { declaredZipEntrySize, readZipEntryBounded } = require('./boundedZipEntry');
|
|
|
|
const MAX_ZIP_BYTES = 128 * 1024 * 1024;
|
|
const MAX_ZIP_ENTRIES = 50000;
|
|
const MAX_INFLATED_BYTES = 256 * 1024 * 1024;
|
|
const MAX_ENTRY_BYTES = 128 * 1024 * 1024;
|
|
|
|
// value: { zipBase64 } from the page, or { zipFile, inflateLimit } - an
|
|
// upload on the local disk (server/lib/importUpload.js), opened from there so
|
|
// the archive is never in memory whole. What its entries inflate to is counted
|
|
// against MAX_INFLATED_BYTES, or for an upload against inflateLimit: the
|
|
// administrator's limit, or with none the free disk space, as if it were
|
|
// unpacked there (models/import.js).
|
|
async function openZip(value, what) {
|
|
const unzipper = require('unzipper');
|
|
let directory;
|
|
if (value && typeof value.zipFile === 'string') {
|
|
const fd = await require('fs').promises.open(value.zipFile, 'r');
|
|
const head = Buffer.alloc(4);
|
|
try { await fd.read(head, 0, 4, 0); } finally { await fd.close(); }
|
|
const magic = head.readUInt32LE(0);
|
|
if (magic !== 0x04034b50 && magic !== 0x06054b50) throw new Error(`${what} is not a .zip file`);
|
|
try {
|
|
directory = await unzipper.Open.file(value.zipFile);
|
|
} catch (error) {
|
|
throw new Error(`${what} is not a readable .zip file`);
|
|
}
|
|
} else {
|
|
const base64 = value && value.zipBase64;
|
|
if (typeof base64 !== 'string' || !base64) throw new Error(`${what} is empty`);
|
|
if (base64.length > Math.ceil(MAX_ZIP_BYTES / 3) * 4 + 4) throw new Error(`${what} is larger than ${MAX_ZIP_BYTES} bytes`);
|
|
const buffer = Buffer.from(base64, 'base64');
|
|
const magic = buffer.length >= 4 ? buffer.readUInt32LE(0) : 0;
|
|
if (magic !== 0x04034b50 && magic !== 0x06054b50) throw new Error(`${what} is not a .zip file`);
|
|
try {
|
|
directory = await unzipper.Open.buffer(buffer);
|
|
} catch (error) {
|
|
throw new Error(`${what} is not a readable .zip file`);
|
|
}
|
|
}
|
|
const entries = directory.files.filter(entry => entry.type === 'File');
|
|
if (entries.length > MAX_ZIP_ENTRIES) throw new Error(`${what} has more than ${MAX_ZIP_ENTRIES} files`);
|
|
const inflateLimit = value && value.zipFile && Number(value.inflateLimit) > 0 ? Number(value.inflateLimit) : MAX_INFLATED_BYTES;
|
|
const budget = { remaining: inflateLimit };
|
|
const read = async entry => {
|
|
if (declaredZipEntrySize(entry) > MAX_ENTRY_BYTES) throw new Error(`${what}: ${entry.path} is too large`);
|
|
try {
|
|
return (await readZipEntryBounded(entry, MAX_ENTRY_BYTES, budget)).toString('utf8');
|
|
} catch (error) {
|
|
if (/zip-(entry-)?too-large/.test(String(error && error.message))) throw new Error(`${what} is too large once unpacked`);
|
|
throw new Error(`${what}: ${entry.path} could not be read`);
|
|
}
|
|
};
|
|
const json = async entry => {
|
|
try { return JSON.parse((await read(entry)).replace(/^\uFEFF/, '')); } catch (error) {
|
|
if (/too large/.test(error.message)) throw error;
|
|
return undefined;
|
|
}
|
|
};
|
|
// An entry streamed on rather than read - an mbox - under the same budget,
|
|
// with no size of its own beyond it.
|
|
const stream = entry => require('./boundedZipEntry').boundedZipEntryStream(entry, inflateLimit, budget);
|
|
return { entries, read, json, stream };
|
|
}
|
|
|
|
const parts = path => String(path).split('/').filter(Boolean);
|
|
const base = path => parts(path).pop() || '';
|
|
|
|
// Slack: users.json, channels.json, groups.json, dms.json, mpims.json at the
|
|
// top, and each conversation's days as <folder>/<YYYY-MM-DD>.json.
|
|
async function readSlackExport(value) {
|
|
const { entries, json } = await openZip(value, 'Slack export');
|
|
const top = entries.filter(e => parts(e.path).length === 1);
|
|
const named = name => top.find(e => e.path === name);
|
|
if (!named('channels.json') && !named('dms.json') && !named('groups.json') && !named('mpims.json')) {
|
|
throw new Error('Slack export has no channels.json');
|
|
}
|
|
const data = { messages: {} };
|
|
for (const key of ['users', 'channels', 'groups', 'dms', 'mpims']) {
|
|
const entry = named(`${key}.json`);
|
|
// eslint-disable-next-line no-await-in-loop
|
|
data[key] = entry ? (await json(entry)) || [] : [];
|
|
}
|
|
const days = entries.filter(e => parts(e.path).length === 2 && /^\d{4}-\d{2}-\d{2}\.json$/.test(base(e.path)))
|
|
.sort((a, b) => a.path.localeCompare(b.path));
|
|
for (const entry of days) {
|
|
const folder = parts(entry.path)[0];
|
|
// eslint-disable-next-line no-await-in-loop
|
|
const day = await json(entry);
|
|
if (Array.isArray(day)) (data.messages[folder] = data.messages[folder] || []).push(...day);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
// Google Takeout: each product is recognised by what a file IS, not by its
|
|
// folder name, which Takeout writes in the account's language.
|
|
async function readGoogleTakeout(value) {
|
|
const { entries, read, json, stream } = await openZip(value, 'Google Takeout export');
|
|
const found = { tasks: [], keep: [], mail: [], calendars: [], chat: [], skipped: [] };
|
|
const spaces = new Map();
|
|
// Product folders seen, and those something was read from.
|
|
const products = new Set();
|
|
const used = new Set();
|
|
for (const entry of entries) {
|
|
const path = entry.path;
|
|
const name = base(path).toLowerCase();
|
|
// The product is the folder under Takeout/; a file beside them is none.
|
|
const offset = parts(path)[0] === 'Takeout' ? 1 : 0;
|
|
const product = parts(path).length > offset + 1 ? parts(path)[offset] : '';
|
|
if (product) products.add(product);
|
|
if (name.endsWith('.mbox')) {
|
|
used.add(product);
|
|
// Streamed, a message at a time: a Takeout mailbox can be many gigabytes.
|
|
// eslint-disable-next-line no-await-in-loop
|
|
const { messages } = await require('./mailImport').decodeMailStream(stream(entry));
|
|
found.mail.push(...messages);
|
|
} else if (name.endsWith('.ics')) {
|
|
used.add(product);
|
|
// eslint-disable-next-line no-await-in-loop
|
|
found.calendars.push(await read(entry));
|
|
} else if (name === 'messages.json' || name === 'group_info.json') {
|
|
used.add(product);
|
|
const folder = parts(path).slice(0, -1).join('/');
|
|
// eslint-disable-next-line no-await-in-loop
|
|
const data = await json(entry);
|
|
const space = spaces.get(folder) || { folder: base(folder), name: '', members: [], messages: [] };
|
|
if (data && name === 'messages.json' && Array.isArray(data.messages)) space.messages.push(...data.messages);
|
|
if (data && name === 'group_info.json') {
|
|
space.name = typeof data.name === 'string' ? data.name : '';
|
|
space.members = Array.isArray(data.members) ? data.members : [];
|
|
}
|
|
spaces.set(folder, space);
|
|
} else if (name.endsWith('.json')) {
|
|
if (declaredZipEntrySize(entry) > 32 * 1024 * 1024) continue;
|
|
// eslint-disable-next-line no-await-in-loop
|
|
const data = await json(entry);
|
|
if (data && data.kind === 'tasks#taskLists') { found.tasks.push(data); used.add(product); }
|
|
else if (data && typeof data === 'object' && ('textContent' in data || 'listContent' in data)
|
|
&& ('isTrashed' in data || 'isArchived' in data || 'userEditedTimestampUsec' in data)) { found.keep.push(data); used.add(product); }
|
|
}
|
|
}
|
|
found.chat = [...spaces.values()].filter(space => space.messages.length);
|
|
found.skipped = [...products].filter(p => !used.has(p)).sort();
|
|
return found;
|
|
}
|
|
|
|
module.exports = { readSlackExport, readGoogleTakeout, MAX_ZIP_BYTES, MAX_INFLATED_BYTES };
|