tests: add #6412 filename-truncation unit test and #3897 guest-board regression

- Export truncateFilenameToBytes / sanitizeFilename from fileStoreStrategy and
  add server/lib/tests/filenameTruncation.tests.js (#6412): byte-budget
  truncation preserves the extension, never splits a multibyte codepoint, caps
  the on-disk component under 255 bytes, and still strips path traversal.
- Spec 36: a guest opening a public board renders lists with no
  "isBoardAdmin of null" crash (#3897).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lauri OjansivuandClaude Opus 4.8 committed 2026-06-22 12:09:19 +03:00
1 parent e9e5bbf132
commit 5709854ed5
3 files changed
+107 -2

No files matched your search

+2 -2
View File
@@ -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';
}
@@ -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
// "<id>-<version>-<name>" 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 "<id>-<version>-<name>" 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');
});
});
});
@@ -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(
[],
);
});
});