diff --git a/models/lib/fileStoreStrategy.js b/models/lib/fileStoreStrategy.js index 85cbc6d715..62e1c0c7c3 100644 --- a/models/lib/fileStoreStrategy.js +++ b/models/lib/fileStoreStrategy.js @@ -43,7 +43,7 @@ const MAX_FILENAME_BYTES = 200; * @param {number} maxBytes * @return {string} the (possibly) shortened filename */ -function truncateFilenameToBytes(filename, maxBytes) { +export function truncateFilenameToBytes(filename, maxBytes) { if (Buffer.byteLength(filename, 'utf8') <= maxBytes) { return filename; } @@ -74,7 +74,7 @@ function truncateFilenameToBytes(filename, maxBytes) { * @param {string} filename - User-provided filename * @return {string} Sanitized filename safe for filesystem operations */ -function sanitizeFilename(filename) { +export function sanitizeFilename(filename) { if (!filename || typeof filename !== 'string') { return 'unnamed'; } diff --git a/server/lib/tests/filenameTruncation.tests.js b/server/lib/tests/filenameTruncation.tests.js new file mode 100644 index 0000000000..37b8f046b8 --- /dev/null +++ b/server/lib/tests/filenameTruncation.tests.js @@ -0,0 +1,70 @@ +/* eslint-env mocha */ +import { expect } from 'chai'; +import { + truncateFilenameToBytes, + sanitizeFilename, +} from '/models/lib/fileStoreStrategy'; + +// Regression tests for #6412: very long attachment filenames produced an on-disk +// "--" component longer than the filesystem's 255-byte limit +// and failed with ENAMETOOLONG (worse with multibyte UTF-8 like German umlauts). +// sanitizeFilename now caps the name at 200 UTF-8 *bytes*, measured in bytes and +// never splitting a multibyte codepoint, while preserving the file extension. +describe('attachment filename truncation (#6412)', function() { + const isValidUtf8 = s => Buffer.from(s, 'utf8').toString('utf8') === s; + + describe('truncateFilenameToBytes', function() { + it('leaves a short name unchanged', function() { + expect(truncateFilenameToBytes('short.png', 200)).to.equal('short.png'); + }); + + it('truncates a long ASCII name to the byte budget and keeps the extension', function() { + const name = 'a'.repeat(300) + '.png'; + const out = truncateFilenameToBytes(name, 200); + expect(Buffer.byteLength(out, 'utf8')).to.be.at.most(200); + expect(out.endsWith('.png')).to.equal(true); + }); + + it('truncates a multibyte (umlaut) name by bytes without splitting a codepoint', function() { + // The actual filename from the issue report (German, umlauts = 2 bytes each). + const name = + 'Förderprogramme helfen bei deiner Unternehmensgründung. Man unterscheidet ' + + 'auf drei Ebenen. Absicherung des Lebensunterhalt bei Gründung aus ' + + 'Arbeitslosigkeit Beratungskostenzuschüsse für die Beratung vor oder während .png'; + expect(Buffer.byteLength(name, 'utf8')).to.be.above(200); + const out = truncateFilenameToBytes(name, 200); + expect(Buffer.byteLength(out, 'utf8')).to.be.at.most(200); + expect(isValidUtf8(out), 'must remain valid UTF-8 (no split codepoint)').to.equal(true); + expect(out.endsWith('.png')).to.equal(true); + }); + + it('clips the whole name when the extension itself is absurdly long', function() { + const name = 'x.' + 'y'.repeat(300); + const out = truncateFilenameToBytes(name, 200); + expect(Buffer.byteLength(out, 'utf8')).to.be.at.most(200); + expect(isValidUtf8(out)).to.equal(true); + }); + }); + + describe('sanitizeFilename', function() { + it('caps a very long name so the on-disk "--" stays under 255 bytes', function() { + const out = sanitizeFilename('Ü'.repeat(300) + '.png'); + expect(Buffer.byteLength(out, 'utf8')).to.be.at.most(200); + const id = 'aBcD1234eFgH5678i'; // typical Random.id() + const component = `${id}-original-${out}`; + expect(Buffer.byteLength(component, 'utf8')).to.be.below(255); + }); + + it('still strips path traversal AND caps length together', function() { + const out = sanitizeFilename('../../' + 'n'.repeat(300) + '.txt'); + expect(out.includes('..'), 'path traversal removed').to.equal(false); + expect(Buffer.byteLength(out, 'utf8')).to.be.at.most(200); + expect(out.endsWith('.txt')).to.equal(true); + }); + + it('falls back to "unnamed" for empty/invalid input', function() { + expect(sanitizeFilename('')).to.equal('unnamed'); + expect(sanitizeFilename(null)).to.equal('unnamed'); + }); + }); +}); diff --git a/tests/playwright/specs/36-fixed-bug-regressions.e2e.js b/tests/playwright/specs/36-fixed-bug-regressions.e2e.js index aee011dd38..c181dc79f7 100644 --- a/tests/playwright/specs/36-fixed-bug-regressions.e2e.js +++ b/tests/playwright/specs/36-fixed-bug-regressions.e2e.js @@ -145,4 +145,39 @@ test.describe('Fixed-bug regressions', () => { ) .toBeLessThanOrEqual(40); }); + + test('#3897 a guest can open a public board without the isBoardAdmin null crash', async ({ + page, + board, + }) => { + // Make the seeded board public so an anonymous guest can open it. + db.updateOne('boards', { _id: board.boardId }, { $set: { permission: 'public' } }); + + // Capture client-side errors BEFORE navigating. The bug threw + // "Cannot read property 'isBoardAdmin' of null" from template helpers for a + // logged-out user, aborting the Tracker render so lists stopped loading. + const errors = []; + page.on('pageerror', e => errors.push(String(e.message || e))); + page.on('console', m => { + if (m.type() === 'error') errors.push(m.text()); + }); + + // Visit as a guest (the `page` fixture is NOT logged in). + await page.goto(`/b/${board.boardId}/${board.slug}`, { waitUntil: 'commit' }); + + // The public board must still render its lists for the guest... + await expect(page.locator('.js-list:not(.js-list-composer)').first()).toBeVisible({ + timeout: 30_000, + }); + // give any deferred Tracker computations a moment to run and (previously) throw + await page.waitForTimeout(1_000); + + // ...and there must be no "isBoardAdmin of null/undefined" crash (#3897). + const crashes = errors.filter( + e => /isBoardAdmin|isWorker/.test(e) && /null|undefined/.test(e), + ); + expect(crashes, `no isBoardAdmin/isWorker null crash for guest:\n${crashes.join('\n')}`).toEqual( + [], + ); + }); });