mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -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 !
135 lines
4.9 KiB
JavaScript
135 lines
4.9 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() {
|
||
const { ExporterExcelCard, ALL_FIELDS } = require('./server/ExporterExcelCard');
|
||
const { WebApp } = require('meteor/webapp');
|
||
const { safeRoute } = require('/server/apiMiddleware');
|
||
const { Authentication } = require('/server/authentication');
|
||
|
||
/**
|
||
* @operation exportExcelCard
|
||
* @tag Cards
|
||
*
|
||
* @summary Export a single card to Excel (.xlsx), formatted for DIN A4 Portrait printing.
|
||
*
|
||
* @description If user is already logged-in, pass loginToken as param
|
||
* "authToken": '/api/boards/:boardId/lists/:listId/cards/:cardId/exportExcel?authToken=:token'
|
||
*
|
||
* Optional query param "fields" is a comma-separated list of sections to include.
|
||
* Valid values: people, board-info, dates, description, checklists, subtasks, comments
|
||
* Omitting "fields" includes all sections.
|
||
*
|
||
* @param {string} boardId the ID of the board
|
||
* @param {string} listId the ID of the list
|
||
* @param {string} cardId the ID of the card to export
|
||
* @param {string} authToken the loginToken
|
||
* @param {string} fields comma-separated list of sections to include
|
||
*/
|
||
WebApp.handlers.get(
|
||
'/api/boards/:boardId/lists/:listId/cards/:cardId/exportExcel',
|
||
safeRoute(async function (req, res) {
|
||
const boardId = req.params.boardId;
|
||
const paramListId = req.params.listId;
|
||
const paramCardId = req.params.cardId;
|
||
let user = null;
|
||
let impersonateDone = false;
|
||
let adminId = null;
|
||
|
||
const board = await ReactiveCache.getBoard(boardId);
|
||
if (!board) {
|
||
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Board not found');
|
||
return;
|
||
}
|
||
|
||
// Public boards skip authentication
|
||
if (board.isPublic()) {
|
||
const fieldsParam = req.query.fields;
|
||
const fields = fieldsParam
|
||
? fieldsParam.split(',').map(f => f.trim()).filter(f => ALL_FIELDS.includes(f))
|
||
: null;
|
||
const exporter = new ExporterExcelCard(boardId, paramListId, paramCardId, 'en', fields);
|
||
await exporter.build(res);
|
||
return;
|
||
}
|
||
|
||
// Authenticate for private boards
|
||
const loginToken = req.query.authToken;
|
||
if (loginToken) {
|
||
if (loginToken.length > 10000) {
|
||
res.writeHead(400, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Invalid token');
|
||
return;
|
||
}
|
||
|
||
const hashToken = Accounts._hashLoginToken(loginToken);
|
||
user = await ReactiveCache.getUser({
|
||
'services.resume.loginTokens.hashedToken': hashToken,
|
||
});
|
||
if (!user) {
|
||
res.writeHead(401, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Invalid token');
|
||
return;
|
||
}
|
||
adminId = user._id.toString();
|
||
impersonateDone = await ReactiveCache.getImpersonatedUser({ 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,
|
||
});
|
||
}
|
||
|
||
// Determine language: prefer the active UI language sent by the client
|
||
// (?lang=fi), then the profile setting, then English. The client-side
|
||
// param is necessary because WeKan may use the browser locale without
|
||
// ever writing it to profile.language.
|
||
let userLanguage =
|
||
req.query.lang ||
|
||
(user && user.profile && user.profile.language) ||
|
||
'en';
|
||
|
||
// Ensure the chosen language bundle is loaded into i18next.
|
||
try {
|
||
await TAPi18n.loadLanguage(userLanguage);
|
||
} catch (_) {
|
||
// Unknown / unsupported language – fall back to English silently.
|
||
userLanguage = 'en';
|
||
}
|
||
|
||
const dateFormat = (user && user.profile && user.profile.dateFormat) || 'YYYY-MM-DD';
|
||
|
||
// Parse optional ?fields=people,dates,... query param
|
||
const fieldsParam = req.query.fields;
|
||
const fields = fieldsParam
|
||
? fieldsParam.split(',').map(f => f.trim()).filter(f => ALL_FIELDS.includes(f))
|
||
: null;
|
||
|
||
const exporter = new ExporterExcelCard(boardId, paramListId, paramCardId, userLanguage, fields, dateFormat);
|
||
if ((await exporter.canExport(user))) {
|
||
if (impersonateDone) {
|
||
await ImpersonatedUsers.insertAsync({
|
||
adminId,
|
||
boardId,
|
||
reason: 'exportExcelCard',
|
||
});
|
||
}
|
||
await exporter.build(res);
|
||
} else {
|
||
res.end(TAPi18n.__('user-can-not-export-card-to-excel'));
|
||
}
|
||
}),
|
||
);
|
||
});
|