Files
wekan/server/lib/attachmentEncryptionRun.js
T
Lauri Ojansivu 6cde02380a Admin Panel / Attachments / Encrypt: attachments on disk encrypted with ATTACHMENTS_PASSPHRASE, at the same addresses.
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 !
2026-11-24 13:19:44 +02:00

143 lines
6.1 KiB
JavaScript

// Admin Panel / Attachments / Encrypt (server/lib/attachmentEncryption.js):
// encrypt or decrypt every attachment stored on this server's disk, in the
// background with progress, and encrypt new ones as they arrive. Only while
// ATTACHMENTS_PASSPHRASE is set - without it the pane is not shown and every
// method refuses. Site administrators only; the runs and the switch are in
// the Audit log (models/lib/adminAudit.js).
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
const enc = require('/server/lib/attachmentEncryption');
const { isSiteAdmin } = require('/models/lib/tenantAdmin');
// { _id: 'settings', encryptNew: Boolean }
export const AttachmentEncryptionSettings = new Mongo.Collection('attachmentEncryption');
const HOUR_MS = 60 * 60 * 1000;
const progress = { running: false, action: '', done: 0, total: 0, failed: 0, error: '' };
async function requireSiteAdmin(userId) {
const user = userId ? await Meteor.users.findOneAsync(userId, { fields: { isAdmin: 1, loginDisabled: 1, orgs: 1, teams: 1 } }) : null;
if (!user || user.loginDisabled || !isSiteAdmin(user)) throw new Meteor.Error('not-authorized');
if (!enc.attachmentsPassphrase()) throw new Meteor.Error('attachment-encryption-off', 'ATTACHMENTS_PASSPHRASE is not set.');
}
function attachmentsRaw() {
return require('/models/attachments').default.collection.rawCollection();
}
// The file paths of an attachment that are on this server's disk.
function filesystemPaths(doc) {
return Object.values((doc && doc.versions) || {})
.filter(v => v && typeof v.path === 'string' && (!v.storage || v.storage === 'fs'))
.map(v => v.path);
}
async function encryptNewEnabled() {
const s = await AttachmentEncryptionSettings.findOneAsync('settings');
return !!(s && s.encryptNew === true);
}
// One attachment: each of its files on disk, then a note on its document so
// the filesystem integrity scan sees the change explained.
async function convertOne(doc, action) {
const fs = require('node:fs');
let changed = false;
for (const file of filesystemPaths(doc)) {
if (!fs.existsSync(file)) continue;
// eslint-disable-next-line no-await-in-loop
changed = (action === 'encrypt' ? await enc.encryptFileInPlace(file) : await enc.decryptFileInPlace(file)) || changed;
}
if (changed) {
await attachmentsRaw().updateOne({ _id: doc._id }, { $set: { 'meta.encrypted': action === 'encrypt', 'meta.updatedAt': new Date() } });
}
return changed;
}
export async function runAll(action) {
if (progress.running) throw new Meteor.Error('already-running');
Object.assign(progress, { running: true, action, done: 0, total: 0, failed: 0, error: '' });
try {
const selector = { 'versions.original.path': { $exists: true } };
progress.total = await attachmentsRaw().countDocuments(selector);
for await (const doc of attachmentsRaw().find(selector, { projection: { versions: 1 } })) {
try {
await convertOne(doc, action);
} catch (e) {
progress.failed += 1;
progress.error = String(e && e.message ? e.message : e).slice(0, 300);
}
progress.done += 1;
}
} finally {
progress.running = false;
}
}
// A new attachment, after its validation (models/attachments.server.js
// onAfterUpload): encrypted when encryption of new attachments is on.
export async function encryptNewAttachment(fileObjId) {
if (!enc.attachmentsPassphrase() || !(await encryptNewEnabled())) return false;
const doc = await attachmentsRaw().findOne({ _id: fileObjId }, { projection: { versions: 1 } });
return doc ? convertOne(doc, 'encrypt') : false;
}
// Attachments that arrive without onAfterUpload - imports, e-mail to card,
// the REST API - are encrypted by this hourly sweep while the switch is on.
async function sweep() {
if (progress.running || !enc.attachmentsPassphrase() || !(await encryptNewEnabled())) return;
const docs = await attachmentsRaw().find({ 'versions.original.path': { $exists: true }, 'meta.encrypted': { $ne: true } },
{ projection: { versions: 1 }, limit: 5000 }).toArray();
for (const doc of docs) {
try { await convertOne(doc, 'encrypt'); } catch (e) { /* the next sweep tries again */ }
}
}
async function counts() {
const fs = require('node:fs');
const out = { encrypted: 0, plain: 0, missing: 0 };
for await (const doc of attachmentsRaw().find({ 'versions.original.path': { $exists: true } }, { projection: { versions: 1 } })) {
const file = doc.versions.original.path;
if (!fs.existsSync(file)) { out.missing += 1; continue; }
if (await enc.isEncryptedFile(file)) out.encrypted += 1; else out.plain += 1;
}
return out;
}
Meteor.methods({
// Whether the pane is shown at all: only while the passphrase is set.
async attachmentEncryptionAvailable() {
if (!this.userId) return false;
const user = await Meteor.users.findOneAsync(this.userId, { fields: { isAdmin: 1, loginDisabled: 1, orgs: 1, teams: 1 } });
return !!(user && !user.loginDisabled && isSiteAdmin(user) && enc.attachmentsPassphrase());
},
async attachmentEncryptionStatus() {
await requireSiteAdmin(this.userId);
return { encryptNew: await encryptNewEnabled(), progress: { ...progress }, counts: progress.running ? null : await counts() };
},
async setEncryptNewAttachments(on) {
check(on, Boolean);
await requireSiteAdmin(this.userId);
await AttachmentEncryptionSettings.upsertAsync({ _id: 'settings' }, { $set: { encryptNew: on, updatedAt: new Date() } });
return on;
},
async encryptAllAttachments() {
await requireSiteAdmin(this.userId);
runAll('encrypt').catch(e => console.error('[attachment-encryption]', e && e.message));
return true;
},
async decryptAllAttachments() {
await requireSiteAdmin(this.userId);
runAll('decrypt').catch(e => console.error('[attachment-encryption]', e && e.message));
return true;
},
});
Meteor.startup(() => {
if (!enc.attachmentsPassphrase() && process.env.ATTACHMENTS_PASSPHRASE) {
console.error('[attachment-encryption] ATTACHMENTS_PASSPHRASE is shorter than 16 characters; encryption is off.');
}
Meteor.setInterval(() => { sweep().catch(() => {}); }, HOUR_MS);
});