Files
wekan/server/lib/webdavAdapter.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

95 lines
4.0 KiB
JavaScript

// #687: WebDAV attachment storage - Nextcloud Files, ownCloud, or any WebDAV
// server - as one more cloud provider beside S3, Azure and GCS
// (models/lib/cloudStorage.js). It implements the adapter interface those
// use: getFileAsStream, addFileFromPath, addFileFromStream, removeFile,
// listFiles - each answering { value } or { error }. Files go to
// `<endpoint>/<folder>/<key>`; the folders of a key are made with MKCOL.
// The endpoint is set by a site administrator in Admin Panel / Attachments
// (like an S3 endpoint), so it may be on the internal network.
const fs = require('fs');
const { Readable } = require('stream');
const { webdavObjectUrl, webdavCollectionUrls } = require('/models/lib/webdavStorage');
class AdapterWebDav {
constructor(config) {
this.config = config || {};
this.configError = this.config.endpoint ? null : 'WebDAV endpoint missing';
}
headers(extra = {}) {
const { username, password } = this.config;
return username || password
? { Authorization: `Basic ${Buffer.from(`${username || ''}:${password || ''}`).toString('base64')}`, ...extra } : { ...extra };
}
async request(url, options = {}) {
return fetch(url, { ...options, headers: this.headers(options.headers), redirect: 'error', signal: AbortSignal.timeout(120000) });
}
async ensureFolders(bucketName, key) {
for (const url of webdavCollectionUrls(this.config.endpoint, bucketName, key)) {
const res = await this.request(url, { method: 'MKCOL' });
// 201 made, 405 already there; anything else is reported by the PUT.
if (![201, 405, 301].includes(res.status)) break;
}
}
async addFileFromPath({ origPath, bucketName, targetPath }) {
try {
await this.ensureFolders(bucketName, targetPath);
const res = await this.request(webdavObjectUrl(this.config.endpoint, bucketName, targetPath), {
// ATTACHMENTS_PASSPHRASE: the copy in WebDAV is the plain file, as in
// every other storage WeKan moves attachments to.
method: 'PUT', body: Readable.toWeb(require('/server/lib/attachmentEncryption').createPlainReadStream(origPath)), duplex: 'half' });
return res.ok ? { value: targetPath } : { error: `WebDAV PUT ${res.status}` };
} catch (error) {
return { error: error.message };
}
}
async addFileFromStream({ stream, bucketName, targetPath }) {
try {
await this.ensureFolders(bucketName, targetPath);
const res = await this.request(webdavObjectUrl(this.config.endpoint, bucketName, targetPath), {
method: 'PUT', body: Readable.toWeb(stream), duplex: 'half' });
return res.ok ? { value: targetPath } : { error: `WebDAV PUT ${res.status}` };
} catch (error) {
return { error: error.message };
}
}
async getFileAsStream(bucketName, key, range) {
try {
const headers = range ? { Range: `bytes=${range.start}-${range.end === undefined ? '' : range.end}` } : {};
const res = await this.request(webdavObjectUrl(this.config.endpoint, bucketName, key), { method: 'GET', headers });
if (!res.ok || !res.body) return { error: `WebDAV GET ${res.status}` };
return { value: Readable.fromWeb(res.body) };
} catch (error) {
return { error: error.message };
}
}
async removeFile(bucketName, key) {
try {
const res = await this.request(webdavObjectUrl(this.config.endpoint, bucketName, key), { method: 'DELETE' });
return res.ok || res.status === 404 ? { value: key } : { error: `WebDAV DELETE ${res.status}` };
} catch (error) {
return { error: error.message };
}
}
// Used to test the connection: the folder can be listed.
async listFiles(bucketName) {
try {
await this.ensureFolders(bucketName, 'x');
const res = await this.request(webdavObjectUrl(this.config.endpoint, bucketName, ''), { method: 'PROPFIND', headers: { Depth: '1' } });
return res.status === 207 ? { value: [] } : { error: `WebDAV PROPFIND ${res.status}` };
} catch (error) {
return { error: error.message };
}
}
}
module.exports = { AdapterWebDav };