mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 17:45:18 -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 !
127 lines
6.7 KiB
JavaScript
127 lines
6.7 KiB
JavaScript
'use strict';
|
|
|
|
// #6745: big attachments are streamed, never held whole in server memory where
|
|
// no limit requires it. Run: node tests/attachmentRangeStreaming.test.cjs
|
|
//
|
|
// 1. Range requests: the download route ignored Range and always sent the
|
|
// whole file; it now streams just the requested bytes from fs, GridFS or
|
|
// the cloud backend (models/lib/httpRange.js).
|
|
// 2. A file moved to S3/Azure/GCS was collected into ONE Buffer before its
|
|
// upload (no cap); it is now spooled to a temporary file and uploaded as a
|
|
// stream of known length.
|
|
// 3. The attachment copy endpoints read the whole source file and only then
|
|
// compared it with the API limit; they now refuse an over-limit source
|
|
// first and stop reading at the limit.
|
|
// 4. Migrating a legacy CollectionFS attachment read it into a Buffer - without
|
|
// await, storing "[object Promise]"; it now streams it.
|
|
// 5. The board export appended every chunk with Buffer.concat - quadratic.
|
|
// tests/playwright/specs/attachment-range-streaming.e2e.js drives 1 over HTTP.
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { parseRange, partialContentHeaders, ifRangeAllows } = require('../models/lib/httpRange.js');
|
|
|
|
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
|
let passed = 0;
|
|
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
|
|
|
console.log('1. Range');
|
|
|
|
test('single ranges, open ranges and suffix ranges', () => {
|
|
assert.deepStrictEqual(parseRange('bytes=0-99', 1000), { start: 0, end: 99 });
|
|
assert.deepStrictEqual(parseRange('bytes=100-', 1000), { start: 100, end: 999 });
|
|
assert.deepStrictEqual(parseRange('bytes=-10', 1000), { start: 990, end: 999 });
|
|
assert.deepStrictEqual(parseRange('bytes=-5000', 1000), { start: 0, end: 999 });
|
|
assert.deepStrictEqual(parseRange('bytes=990-5000', 1000), { start: 990, end: 999 }, 'end is clamped');
|
|
assert.deepStrictEqual(parseRange('BYTES = 1 - 2', 10), { start: 1, end: 2 });
|
|
});
|
|
|
|
test('negative: unsatisfiable ranges', () => {
|
|
assert.strictEqual(parseRange('bytes=1000-', 1000), 'unsatisfiable');
|
|
assert.strictEqual(parseRange('bytes=5000-6000', 1000), 'unsatisfiable');
|
|
assert.strictEqual(parseRange('bytes=-0', 1000), 'unsatisfiable');
|
|
assert.strictEqual(parseRange('bytes=0-', 0), 'unsatisfiable');
|
|
});
|
|
|
|
test('negative: anything else is answered with the whole file', () => {
|
|
for (const header of [undefined, '', 'bytes=', 'bytes=-', 'items=0-1', 'bytes=0-1,5-9',
|
|
'bytes=9-1', 'bytes=a-b', 'bytes=0-1;x']) {
|
|
assert.strictEqual(parseRange(header, 1000), null, String(header));
|
|
}
|
|
assert.strictEqual(parseRange('bytes=0-1', -1), null);
|
|
assert.strictEqual(parseRange('bytes=0-1', NaN), null);
|
|
});
|
|
|
|
test('206 headers and If-Range', () => {
|
|
assert.deepStrictEqual(partialContentHeaders({ start: 100, end: 199 }, 1000), {
|
|
'Content-Range': 'bytes 100-199/1000', 'Content-Length': '100', 'Accept-Ranges': 'bytes',
|
|
});
|
|
assert.strictEqual(ifRangeAllows(undefined, '"a"'), true);
|
|
assert.strictEqual(ifRangeAllows('"a"', '"a"'), true);
|
|
assert.strictEqual(ifRangeAllows('"b"', '"a"'), false);
|
|
assert.strictEqual(ifRangeAllows('Wed, 21 Oct 2015 07:28:00 GMT', '"a"'), false);
|
|
});
|
|
|
|
test('the download route and every storage strategy pass the range to the backend', () => {
|
|
const route = read('server/routes/universalFileServer.js');
|
|
assert.match(route, /range = parseRange\(req\.headers\.range, size\)/);
|
|
assert.match(route, /res\.writeHead\(416, \{ 'Content-Range': `bytes \*\/\$\{size\}`/);
|
|
assert.match(route, /strategy\.getReadStream\(range \|\| undefined\)/);
|
|
assert.match(route, /res\.statusCode = 206;/);
|
|
const strategies = read('models/lib/fileStoreStrategy.js');
|
|
assert.match(strategies, /openDownloadStream\(gfsId, \{ start: range\.start, end: range\.end \+ 1 \}\)/);
|
|
// Since ATTACHMENTS_PASSPHRASE the range goes to the decrypting read, which
|
|
// streams the same plain bytes of a plain file (tests/attachmentEncryption.test.cjs).
|
|
assert.match(strategies, /createPlainReadStream\(chosen, range \|\| undefined\)/);
|
|
assert.match(strategies, /getFileAsStream\(adapter\.bucketName, key, \.\.\.rangeArgs\)/);
|
|
});
|
|
|
|
console.log('2. cloud uploads');
|
|
|
|
test('a cloud upload is spooled to disk and streamed, never one Buffer', () => {
|
|
const src = read('models/lib/fileStoreStrategy.js');
|
|
const at = src.indexOf('getWriteStream(filePath) {', src.indexOf('getReadStream(range) {\n const pass'));
|
|
const body = src.slice(at, src.indexOf('waitUntilStored() {', at));
|
|
assert.match(body, /fs\.createWriteStream\(spoolPath\)/);
|
|
assert.match(body, /adapter\.storage\.addFileFromPath\(\{\s*origPath: spoolPath,/);
|
|
// The adapter argument carries the S3 server-side encryption the
|
|
// administrator chose (tests/s3Encryption.test.cjs); the size is still passed.
|
|
assert.match(body, /options: cloudUploadOptions\(this\.provider, stat\.size, adapter\)/);
|
|
assert.match(body, /\.then\(removeSpool\)/);
|
|
// negative: no whole-file buffer remains
|
|
assert.doesNotMatch(body, /Buffer\.concat|addFileFromBuffer|chunks\.push/);
|
|
// S3 only gets the length (and its encryption); the other providers nothing.
|
|
assert.match(src, /if \(provider !== STORAGE_NAME_S3\) return \{\};/);
|
|
assert.match(src, /if \(size !== undefined\) options\.ContentLength = size;/);
|
|
});
|
|
|
|
console.log('3-5. copies, migration, export');
|
|
|
|
test('attachment copies refuse an over-limit source before reading it', () => {
|
|
for (const rel of ['server/attachmentApi.js', 'server/routes/attachmentApi.js']) {
|
|
const src = read(rel);
|
|
const sizeCheck = src.indexOf('if (Number(sourceAttachment.size) > effectiveApiUploadMaxBytes)');
|
|
const firstRead = src.indexOf("getFileStrategy(sourceAttachment, 'original');\n");
|
|
assert.ok(sizeCheck > 0 && sizeCheck < firstRead, `${rel}: size checked before the read`);
|
|
assert.match(src, /bytesRead \+= chunk\.length;\s*if \(bytesRead > effectiveApiUploadMaxBytes\)/);
|
|
}
|
|
});
|
|
|
|
test('legacy attachment migration streams the file', () => {
|
|
const src = read('server/migrations/migrateAttachments.js');
|
|
assert.match(src, /const readStream = await getOldAttachmentStream\(attachmentId\);/);
|
|
assert.match(src, /await addAttachmentFromStream\(readStream, \{/);
|
|
assert.doesNotMatch(src.replace(/\/\/.*$/gm, ''), /getOldAttachmentDataBuffer\(/);
|
|
assert.doesNotMatch(src.replace(/\/\/.*$/gm, ''), /new File\(\[fileData\]/);
|
|
});
|
|
|
|
test('the board export joins an attachment\'s chunks once', () => {
|
|
const src = read('models/exporter.js');
|
|
assert.doesNotMatch(src, /buffer = Buffer\.concat\(\[buffer, chunk\]\)/);
|
|
assert.match(src, /callback\(null, Buffer\.concat\(chunks\)\.toString\('base64'\)\)/);
|
|
assert.doesNotMatch(src, /tmpexport/);
|
|
});
|
|
|
|
console.log(`attachmentRangeStreaming: ${passed} passed`);
|