mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
The board export REST endpoints look a user up by the login token in
`?authToken=`. A token that matches nothing makes that lookup answer `undefined`,
and the next line dereferenced it:
user = await ReactiveCache.getUser({ 'services.resume.loginTokens.hashedToken': … });
adminId = user._id.toString(); // TypeError on an unknown token
The handlers are async and had no try/catch, so the TypeError escaped as an
unhandled promise rejection - which this app turns into a full process crash (the
note in server/ldapGroupSync.js explains why). One crafted GET against a private
board id took the server down for every user. Reachable by anyone who can obtain a
private board id, which on an open-registration instance means anyone at all.
It was an incomplete fix: models/exportPDF.js and models/exportExcelCard.js
already had the `if (!user)` guard; three handlers in models/export.js and one in
models/exportExcel.js were missed.
Fixed on both levels:
* Every token lookup in the export models is now followed by the guard - 401
"Invalid token" and return - including the two handlers that did not crash
because they hand the user to canExport() instead of dereferencing it. One
rule for every lookup is what stops the next handler being the missed one.
* Every export route body is wrapped in safeRoute() (server/apiMiddleware.js),
which awaits the handler, answers 500 once and logs the request that failed.
A throw from any other cause is now one broken request instead of an outage.
tests/exportTokenGuard.test.cjs does not check four places by hand: it finds every
token lookup in the export models and requires a guard on each, that nothing
dereferences the user before it, that every route is wrapped, and that the two
handlers which were already guarded stay guarded.
Thanks to laijunyue for the coordinated disclosure, and xet7 !
117 lines
4.1 KiB
JavaScript
117 lines
4.1 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { TAPi18n } from '/imports/i18n';
|
|
import { runOnServer } from './runOnServer';
|
|
import ImpersonatedUsers from '/models/impersonatedUsers';
|
|
|
|
runOnServer(function() {
|
|
// the ExporterExcel class is only available on server and in order to import
|
|
// it here we use runOnServer to have it inside a function instead of an
|
|
// if (Meteor.isServer) block
|
|
const { ExporterExcel } = require('./server/ExporterExcel');
|
|
const { WebApp } = require('meteor/webapp');
|
|
const { safeRoute } = require('/server/apiMiddleware');
|
|
const { Authentication } = require('/server/authentication');
|
|
|
|
// todo XXX once we have a real API in place, move that route there
|
|
// todo XXX also share the route definition between the client and the server
|
|
// so that we could use something like
|
|
// `ApiRoutes.path('boards/exportExcel', boardId)``
|
|
// on the client instead of copy/pasting the route path manually between the
|
|
// client and the server.
|
|
/**
|
|
* @operation exportExcel
|
|
* @tag Boards
|
|
*
|
|
* @summary This route is used to export the board Excel.
|
|
*
|
|
* @description If user is already logged-in, pass loginToken as param
|
|
* "authToken": '/api/boards/:boardId/exportExcel?authToken=:token'
|
|
*
|
|
* See https://blog.kayla.com.au/server-side-route-authentication-in-meteor/
|
|
* for detailed explanations
|
|
*
|
|
* @param {string} boardId the ID of the board we are exporting
|
|
* @param {string} authToken the loginToken
|
|
*/
|
|
WebApp.handlers.get('/api/boards/:boardId/exportExcel', safeRoute(async function (req, res) {
|
|
const boardId = req.params.boardId;
|
|
let user = null;
|
|
let impersonateDone = false;
|
|
let adminId = null;
|
|
|
|
// First check if board exists and is public to avoid unnecessary authentication
|
|
const board = await ReactiveCache.getBoard(boardId);
|
|
if (!board) {
|
|
res.end('Board not found');
|
|
return;
|
|
}
|
|
|
|
// If board is public, skip expensive authentication operations
|
|
if (board.isPublic()) {
|
|
// Public boards don't require authentication - skip hash operations
|
|
const exporterExcel = new ExporterExcel(boardId, 'en');
|
|
exporterExcel.build(res);
|
|
return;
|
|
}
|
|
|
|
// Only perform expensive authentication for private boards
|
|
const loginToken = req.query.authToken;
|
|
if (loginToken) {
|
|
// Validate token length to prevent resource abuse
|
|
if (loginToken.length > 10000) {
|
|
if (process.env.DEBUG === 'true') {
|
|
console.warn('Suspiciously long auth token received, rejecting to prevent resource abuse');
|
|
}
|
|
res.end('Invalid token');
|
|
return;
|
|
}
|
|
|
|
const hashToken = Accounts._hashLoginToken(loginToken);
|
|
user = await ReactiveCache.getUser({
|
|
'services.resume.loginTokens.hashedToken': hashToken,
|
|
});
|
|
if (!user) {
|
|
// GHSA-3gcg-g6rf-w2rx - see the note in models/export.js: an unknown token
|
|
// answers `undefined`, and dereferencing it crashed the server.
|
|
res.writeHead(401, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
res.end('Invalid token');
|
|
return;
|
|
}
|
|
adminId = user._id.toString();
|
|
impersonateDone = await ReactiveCache.getImpersonatedUser({ adminId: adminId });
|
|
} else if (!Meteor.settings.public.sandstorm) {
|
|
try {
|
|
await Authentication.checkUserId(req.userId);
|
|
} catch (error) {
|
|
res.writeHead(error.statusCode || 403, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
res.end('Unauthorized');
|
|
return;
|
|
}
|
|
user = await ReactiveCache.getUser({
|
|
_id: req.userId,
|
|
isAdmin: true,
|
|
});
|
|
}
|
|
|
|
let userLanguage = 'en';
|
|
if(user && user.profile){
|
|
userLanguage = user.profile.language
|
|
}
|
|
|
|
const exporterExcel = new ExporterExcel(boardId, userLanguage);
|
|
if ((await exporterExcel.canExport(user))) {
|
|
if (impersonateDone) {
|
|
await ImpersonatedUsers.insertAsync({
|
|
adminId: adminId,
|
|
boardId: boardId,
|
|
reason: 'exportExcel',
|
|
});
|
|
}
|
|
exporterExcel.build(res);
|
|
} else {
|
|
res.end(TAPi18n.__('user-can-not-export-excel'));
|
|
}
|
|
}));
|
|
});
|