mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
GHSA-w3qg-pf27-g68r: attachments were answered with Cache-Control: public, max-age=31536000 and no Vary after the board access check, so a shared cache (CDN, caching proxy) could store a member's copy and serve it to anyone without credentials for a year, also after the member was removed from the board. The same header was in setFileHeaders, both ?download=1 branches, both avatar routes, and Meteor-Files' own /cdn/storage route sent its default public, s-maxage one-year header. Every file response, its 304 and the thumbnail now send Cache-Control: private, no-cache and Vary: Cookie, Authorization, X-Auth-Token through models/lib/fileCacheHeaders.js, for public boards too, because a board made private later would leave a public copy behind. Both FilesCollections pass the same cacheControl. The ETag keeps revalidation a bodiless 304. tests/fileCacheHeaders.test.cjs drives the policy against the shared-cache rules the report used, checks each route, and fails on any public Cache-Control, a FilesCollection left on the Meteor-Files default, or an ETag-sending file without the policy, anywhere in the tree. The thumbnail Playwright spec checks the headers on every route. Nothing is refused, so there is no Problems key. Thanks to alham-rizvi and xet7 !
78 lines
3.4 KiB
JavaScript
78 lines
3.4 KiB
JavaScript
export const httpStreamOutput = function(readStream, name, http, downloadFlag, cacheControl, fileObj) {
|
|
const {
|
|
fileNameIsBrowserExecutable,
|
|
fileResponsePolicy,
|
|
} = require('./fileResponseSafety');
|
|
const storedType = fileObj?.versions?.original?.type || fileObj?.type;
|
|
const policy = fileResponsePolicy(storedType, fileNameIsBrowserExecutable(name));
|
|
// Sanitize known exploits from EXISTING files on the fly, straight from the
|
|
// storage backend, without buffering whole files: the sanitizer sniffs the
|
|
// start of the stream and only rewrites documents that begin like dangerous
|
|
// markup (SVG/XML with <script>, an <!DOCTYPE/<!ENTITY XML loop, inline event
|
|
// handlers, ...); everything else streams through unchanged. See
|
|
// models/lib/serveFileSanitizer.js. When it DOES rewrite something, log it to
|
|
// Admin Panel / Problems with the uploader + location context (viewing path).
|
|
let outStream = readStream;
|
|
try {
|
|
const { createServeSanitizer } = require('./serveFileSanitizer');
|
|
const onSanitized = kinds => {
|
|
try {
|
|
require('/server/lib/filenameSanitizeLog').logContentSanitized({
|
|
fileObj: fileObj || { name }, source: 'fileView', kinds,
|
|
});
|
|
} catch (e) { /* best effort */ }
|
|
};
|
|
const sanitizer = createServeSanitizer(name, onSanitized);
|
|
readStream.on('error', err => sanitizer.destroy(err));
|
|
outStream = readStream.pipe(sanitizer);
|
|
} catch (e) {
|
|
console.error('serveFileSanitizer unavailable, streaming file as-is:', e);
|
|
outStream = readStream;
|
|
}
|
|
|
|
outStream.on('data', data => {
|
|
http.response.write(data);
|
|
});
|
|
|
|
outStream.on('end', () => {
|
|
// don't pass parameters to end() or it will be attached to the file's binary stream
|
|
http.response.end();
|
|
});
|
|
|
|
outStream.on('error', (err) => {
|
|
console.error(`Download stream error for file '${name}':`, err);
|
|
http.response.statusCode = 404;
|
|
http.response.end('not found');
|
|
});
|
|
|
|
// CacheBleed (GHSA-w3qg-pf27-g68r): whatever cacheControl the collection
|
|
// passes, a file behind an access check is never shared-cacheable.
|
|
require('./fileCacheHeaders').setPrivateFileCacheHeaders(http.response);
|
|
|
|
// This path is the original Meteor-Files /cdn/storage route. Apply the same
|
|
// fail-closed policy as WeKan's universal and legacy routes before any
|
|
// storage strategy writes bytes to the response.
|
|
http.response.setHeader('Content-Type', policy.contentType);
|
|
http.response.setHeader(
|
|
'Content-Disposition',
|
|
getContentDisposition(name, policy.forceDownload ? 'true' : downloadFlag),
|
|
);
|
|
for (const [header, value] of Object.entries(policy.headers)) {
|
|
http.response.setHeader(header, value);
|
|
}
|
|
};
|
|
|
|
/** will initiate download, if links are called with ?download="true" queryparam */
|
|
const getContentDisposition = (name, downloadFlag) => {
|
|
// Force attachment disposition for SVG files to prevent XSS attacks
|
|
const forceAttachment = downloadFlag === 'true';
|
|
const dispositionType = forceAttachment ? 'attachment;' : 'inline;';
|
|
|
|
const { sanitizeDownloadFileName } = require('/imports/lib/fileNameDisplay');
|
|
const encodedName = encodeURIComponent(sanitizeDownloadFileName(name));
|
|
const dispositionName = `filename="${encodedName}"; filename=*UTF-8"${encodedName}";`;
|
|
const dispositionEncoding = 'charset=utf-8';
|
|
|
|
return `${dispositionType} ${dispositionName} ${dispositionEncoding}`;
|
|
};
|