mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
- server/lib/attachmentApiAccess.js: the size limits and the board write check that the attachment Meteor methods (server/attachmentApi.js) and REST routes (server/routes/attachmentApi.js) each defined, identically - seven functions. tests/assignedbleed reads the write check there and fails if either API defines its own again. - server/lib/contentDisposition.js: the Content-Disposition filename functions of server/routes/universalFileServer.js and legacyAttachments.js. - models/lib/fileStoreStrategy.js: the GridFS, filesystem and cloud strategies had the same interceptDownload. The base class now has it, and still fails closed: a strategy whose getReadStream() gives nothing - the base one gives nothing - answers 404 and never hands the file to Meteor-Files unfiltered. tests/attachmentServeFailsClosed holds any override to the same rules. - models/lib/uploadFileId.js: uploadNameParts() reads the id and name of an upload for both Attachments' and Avatars' namingFunction, which each had the same 25 lines. tests/uploadFileId, which the module's comment named but did not exist, covers it and safeUploadFileId. Thanks to xet7 !
54 lines
2.5 KiB
JavaScript
54 lines
2.5 KiB
JavaScript
// The Content-Disposition header of a served file, shared by the file routes
|
|
// (server/routes/universalFileServer.js, server/routes/legacyAttachments.js),
|
|
// which each used to define both functions.
|
|
const { sanitizeDownloadFileName } = require('/imports/lib/fileNameDisplay');
|
|
|
|
/**
|
|
* Encode a filename for the Content-Disposition header.
|
|
* Removes invalid characters (control chars, newlines, etc.) that would break HTTP headers.
|
|
* For non-ASCII filenames, uses RFC 5987 encoding to preserve the original filename.
|
|
* This prevents ERR_INVALID_CHAR errors when filenames contain control characters.
|
|
*
|
|
* Example:
|
|
* - ASCII filename: sanitizeFilenameForHeader('test.txt') => 'test.txt'
|
|
* - Non-ASCII: sanitizeFilenameForHeader('現有檔案.odt') => '____.odt|RFC5987:%E7%8F%BE%E6%9C%89%E6%AA%94%E6%A1%88.odt'
|
|
* - Control chars: sanitizeFilenameForHeader('test\nfile.txt') => 'testfile.txt'
|
|
*/
|
|
export function sanitizeFilenameForHeader(filename) {
|
|
if (!filename || typeof filename !== 'string') {
|
|
return 'download';
|
|
}
|
|
|
|
// Decode a URL-encoded name and REMOVE all invisible/format characters (incl.
|
|
// control chars, zero-width, bidi, BOM) so the downloaded file gets a clean,
|
|
// unambiguous name — matching how filenames are shown in the UI.
|
|
let sanitized = sanitizeDownloadFileName(filename);
|
|
|
|
// If the filename is all ASCII printable characters (0x20-0x7E), use it directly
|
|
if (/^[\x20-\x7E]*$/.test(sanitized)) {
|
|
// Escape any quotes and backslashes in the filename
|
|
sanitized = sanitized.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
|
return sanitized;
|
|
}
|
|
|
|
// For non-ASCII filenames, provide a fallback and RFC 5987 encoded version
|
|
const fallback = sanitized.replace(/[^\x20-\x7E]/g, '_').slice(0, 100) || 'download';
|
|
const encoded = encodeURIComponent(sanitized);
|
|
|
|
// Return special marker format that will be handled by buildContentDispositionHeader
|
|
// Format: "fallback|RFC5987:encoded"
|
|
return `${fallback}|RFC5987:${encoded}`;
|
|
}
|
|
|
|
/**
|
|
* Build a complete Content-Disposition header value with RFC 5987 support.
|
|
* Handles the special format returned by sanitizeFilenameForHeader for non-ASCII filenames.
|
|
*/
|
|
export function buildContentDispositionHeader(disposition, sanitizedFilename) {
|
|
if (sanitizedFilename.includes('|RFC5987:')) {
|
|
const [fallback, encoded] = sanitizedFilename.split('|RFC5987:');
|
|
return `${disposition}; filename="${fallback}"; filename*=UTF-8''${encoded}`;
|
|
}
|
|
return `${disposition}; filename="${sanitizedFilename}"`;
|
|
}
|