mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
The last item of docs/Compliance/README.md's P3 list, on the maintainer's decision: while ATTACHMENTS_PASSPHRASE is set (at least 16 characters), a site administrator gets Admin Panel / Attachments / Encrypt, which encrypts or decrypts every attachment on this server's disk in the background with progress, and turns on encryption of new attachments. - AES-256-CTR (server/lib/attachmentEncryption.js), so a download can start anywhere: the counter for any byte is the IV plus its block. The key is stretched from the passphrase with scrypt once per salt and kept in memory. Each file starts with WKATT1, the salt and its own IV; a file without the header is plain and read as before. - Every read goes through FileStoreStrategyFilesystem.getReadStream, which decrypts - byte ranges included - so every address, preview, export and storage move is unchanged; the board export and the WebDAV move, which read the file directly, use the same decrypting read. - New uploads are encrypted after their validation and indexing, which need the plain file; imports, e-mail to card and the REST API by an hourly sweep. Each changed attachment's meta.updatedAt is set, so the filesystem integrity scan sees the change explained. The runs and the switch are in the Audit log. - On every platform, the snap and the Helm chart (from a Secret). Tested against a live server (tests/playwright/specs/attachment-encryption.e2e.js): an attachment encrypted on disk downloads at the same address, whole and as a byte range, and decrypts back to the same bytes. Seven new strings, translated in every language. Low confidence: ace, ak, ay, bi, bm, bo, bua, chr, csb, dz, ee, ff, fj, gn, gv, haw, iu, kl, kok, ks, kw, lg, lld, nah, nap, nd, nso, fur, mg, tlh, tig, ti, wal, om, ve-PP, ve-CC, rup, zgh, uz-AR, sah, vo, wuu-Hans, se, qu, to, sm, ss, ts, ve, ny, rn, tn, st, wa, sc, scn. Thanks to xet7 !
80 lines
4.6 KiB
JavaScript
80 lines
4.6 KiB
JavaScript
'use strict';
|
|
|
|
// ATTACHMENTS_PASSPHRASE (docs/Compliance/README.md P3): attachments on the
|
|
// disk encrypted by WeKan, read back plain - all of a file or any byte range -
|
|
// so every URL stays the same.
|
|
// Run: node --test tests/attachmentEncryption.test.cjs
|
|
|
|
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const crypto = require('node:crypto');
|
|
|
|
const ROOT = path.join(__dirname, '..');
|
|
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
|
|
const TMP = fs.mkdtempSync(path.join(process.env.TMPDIR || path.join(ROOT, '.tools', 'tmp'), 'att-enc-'));
|
|
process.env.ATTACHMENTS_PASSPHRASE = 'a passphrase for the attachment test';
|
|
const E = require('../server/lib/attachmentEncryption');
|
|
|
|
const collect = async stream => { const parts = []; for await (const c of stream) parts.push(c); return Buffer.concat(parts); };
|
|
|
|
test('encrypted in place, read back plain - the whole file and any range, plain files as before', async () => {
|
|
const data = crypto.randomBytes(100003);
|
|
const file = path.join(TMP, 'a.bin');
|
|
fs.writeFileSync(file, data);
|
|
assert.ok((await collect(E.createPlainReadStream(file))).equals(data), 'a plain file reads as before');
|
|
assert.equal(await E.encryptFileInPlace(file), true);
|
|
assert.equal(await E.isEncryptedFile(file), true);
|
|
assert.equal(fs.statSync(file).size, data.length + E.HEADER);
|
|
assert.ok(!fs.readFileSync(file).includes(data.subarray(500, 564)), 'nothing of the plain file on the disk');
|
|
assert.ok((await collect(E.createPlainReadStream(file))).equals(data));
|
|
for (const [start, end] of [[0, 0], [1, 15], [15, 16], [17, 4096], [65535, 70000], [100000, 100002]]) {
|
|
assert.ok((await collect(E.createPlainReadStream(file, { start, end }))).equals(data.subarray(start, end + 1)), `${start}-${end}`);
|
|
}
|
|
assert.equal(await E.encryptFileInPlace(file), false, 'encrypting twice does nothing');
|
|
assert.equal(await E.decryptFileInPlace(file), true);
|
|
assert.ok(fs.readFileSync(file).equals(data), 'decrypted back to the same bytes');
|
|
});
|
|
|
|
test('the counter crosses byte boundaries correctly', () => {
|
|
const iv = Buffer.alloc(16, 0xff);
|
|
assert.equal(E.ivAt(iv, 16).toString('hex'), '00'.repeat(16), 'carries all the way');
|
|
assert.equal(E.ivAt(Buffer.alloc(16), 16 * 256).toString('hex'), `${'00'.repeat(14)}0100`);
|
|
});
|
|
|
|
test('negative: without the passphrase an encrypted file is refused, not served as garbage', async () => {
|
|
const file = path.join(TMP, 'b.bin');
|
|
fs.writeFileSync(file, Buffer.from('secret contents'));
|
|
await E.encryptFileInPlace(file);
|
|
const saved = process.env.ATTACHMENTS_PASSPHRASE;
|
|
delete process.env.ATTACHMENTS_PASSPHRASE;
|
|
try {
|
|
assert.throws(() => E.createPlainReadStream(file), /set ATTACHMENTS_PASSPHRASE/);
|
|
} finally {
|
|
process.env.ATTACHMENTS_PASSPHRASE = saved;
|
|
}
|
|
assert.equal(E.attachmentsPassphrase({ ATTACHMENTS_PASSPHRASE: 'short' }), '', 'a short passphrase is not used');
|
|
});
|
|
|
|
test('every reader of an attachment on the disk goes through the decrypting read', () => {
|
|
assert.match(read('models/lib/fileStoreStrategy.js'), /require\('\/server\/lib\/attachmentEncryption'\)\.createPlainReadStream\(chosen, range \|\| undefined\)/);
|
|
const exporter = read('models/exporter.js');
|
|
assert.equal((exporter.match(/createPlainReadStream\(/g) || []).length, 2, 'both attachment reads of the board export');
|
|
assert.match(read('server/lib/webdavAdapter.js'), /createPlainReadStream\(origPath\)/);
|
|
const upload = read('models/attachments.server.js');
|
|
assert.ok(upload.indexOf('encryptNewAttachment(fileObjId)') > upload.indexOf('indexNewAttachment(fileObjId)'), 'after indexing, which reads the plain file');
|
|
});
|
|
|
|
test('the pane: only while the passphrase is set, site administrators only, audited', () => {
|
|
const run = read('server/lib/attachmentEncryptionRun.js');
|
|
assert.match(run, /if \(!enc\.attachmentsPassphrase\(\)\) throw new Meteor\.Error\('attachment-encryption-off'/);
|
|
assert.match(run, /if \(!user \|\| user\.loginDisabled \|\| !isSiteAdmin\(user\)\) throw new Meteor\.Error\('not-authorized'\);/);
|
|
const audited = require('../models/lib/adminAudit').AUDITED_METHODS;
|
|
for (const m of ['setEncryptNewAttachments', 'encryptAllAttachments', 'decryptAllAttachments']) assert.ok(audited.includes(m), m);
|
|
assert.match(read('client/components/settings/attachments.js'), /\.\.\.\(encryptionAvailable\.get\(\) \? \[\{ id: 'encrypt'/);
|
|
assert.match(run, /'meta\.updatedAt': new Date\(\)/, 'the integrity scan sees the change explained');
|
|
});
|
|
|
|
test.after(() => fs.rmSync(TMP, { recursive: true, force: true }));
|