mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
THE PDF THAT WAS AN HTML PAGE. Not a broken PDF - WeKan's own page, saved as
`<card>.pdf`. Every export in the interface is a download from an `/api/...`
address, and server/apiMiddleware.js refuses every /api request unless WITH_API
is exactly "true". It refused by answering `301 Location: /`, so the browser
followed it to the front page and the download link's `download="<card>.pdf"`
wrote the HTML it got there to that name. Reproduced against the running
instance: `GET /api/boards/x/exportPDF` -> 301.
Two faults, and either alone is enough:
* The bundle launchers did not set WITH_API. The snap has defaulted it to true
for years (snap-src/bin/config) and every docker-compose*.yml in this
repository sets it, so the bundle was the ONE platform where exporting was
off by default - which is why it was the one platform where an export came
back as HTML. start-wekan.sh and start-wekan.bat now default it to true,
overridable, and say WHY: the name reads like a developer feature, and
somebody switching it off to harden an instance should know they are turning
off every export in the interface.
* The refusal was a redirect. Whatever the setting, "the API is off" must not
arrive as a page: it is now 403 text/plain saying that exports use the API
too and naming the variable to set. A 403 with a sentence in it cannot be
mistaken for the file that was asked for.
CHECKBOXES ARE SQUARE, WHEREVER THEY ARE PUT. The export popup's unchecked box
drew a thin vertical sliver beside a long label and a proper square beside a
short one: the row is a flex container, the box is a flex item, and a flex item
shrinks. That is the THIRD time this shape has broken - settingBody.css already
carries a fix for a 41px min-height that "turns a 13px box into a tall
rectangle", and another for a `height: 100%` that stretched it - so the fix goes
in the rule that DEFINES the checkbox: `flex: none` in forms.css, for all 90 of
them across 19 templates, rather than one more local patch.
The local patch is removed with it. It had set `box-sizing: border-box`, which
would have made the export popup's boxes 13px INCLUDING their border while every
other checkbox in WeKan is 13px plus 2px - a fix for one page that made that
page the odd one out.
The Admin Panel's checkboxes are native inputs STYLED to be the material box,
and a guard already compares the two rules declaration by declaration, so they
take the new declaration too - written the same way, since `flex-shrink: 0` and
`flex: none` mean the same thing and only one of them can be compared.
Audited the rest rather than assuming: every CSS rule in the client that sizes a
checkbox gives it equal width and height, including the two "clean" board themes
that deliberately draw theirs at 24px and 18px. tests/checkboxesAreSquare.test.cjs
pins that invariant for every rule, and tests/exportNeedsTheApi.test.cjs pins
that the launchers turn the API on, that the refusal is never a redirect, and
that the exports really are /api addresses - which is what made the gate their
business in the first place.
Thanks to Heart1010 and xet7 !
158 lines
6.5 KiB
JavaScript
158 lines
6.5 KiB
JavaScript
// API middleware: body parsing, auth token handling, API gate, and JSON response helper.
|
|
// Replaces communitypackages:json-routes middleware chain.
|
|
// Must be imported before model files that register API routes.
|
|
|
|
const { Meteor } = require('meteor/meteor');
|
|
const { Accounts } = require('meteor/accounts-base');
|
|
const { WebApp } = require('meteor/webapp');
|
|
const bodyParser = require('body-parser');
|
|
const { safeJsonStringify } = require('/server/lib/apiResponseHelpers');
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 1. Body parsing (previously registered by json-routes)
|
|
// ---------------------------------------------------------------------------
|
|
WebApp.handlers.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));
|
|
WebApp.handlers.use(bodyParser.json({ limit: '50mb' }));
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 2. API gate — check WITH_API env var (previously in models/users.js)
|
|
// ---------------------------------------------------------------------------
|
|
WebApp.handlers.use(function apiGate(req, res, next) {
|
|
const api = req.url.startsWith('/api');
|
|
if ((api && process.env.WITH_API === 'true') || !api) {
|
|
return next();
|
|
}
|
|
// REFUSED, IN WORDS - not a redirect to the front page.
|
|
//
|
|
// This used to answer `301 Location: /`, and every export in the interface is
|
|
// a download from an `/api/...` address: a board or a card to PDF, Excel,
|
|
// JSON, .zip, CSV. The browser followed the redirect, got WeKan's own HTML
|
|
// page, and wrote it to the file the download link had named - so "the API is
|
|
// off" arrived as a PDF that no reader could open, with nothing anywhere
|
|
// saying why. A 403 with a sentence in it cannot be mistaken for the file
|
|
// that was asked for, and it says what to change.
|
|
res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
return res.end(
|
|
'WeKan API is disabled, so this address answers nothing.\n'
|
|
+ '\n'
|
|
+ 'Exports use it too - a board or card exported to PDF, Excel, JSON, .zip\n'
|
|
+ 'or CSV is a download from /api/..., so with the API off none of them can\n'
|
|
+ 'work. Set the environment variable WITH_API=true and restart WeKan.\n',
|
|
);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 3. Bearer token parser (replaces communitypackages:rest-bearer-token-parser)
|
|
// ---------------------------------------------------------------------------
|
|
WebApp.handlers.use(function parseBearerToken(req, res, next) {
|
|
// Check Authorization header first
|
|
const authHeader = req.headers.authorization;
|
|
if (authHeader) {
|
|
const match = authHeader.match(/^Bearer\s+(.+)$/i);
|
|
if (match) {
|
|
req.authToken = match[1];
|
|
}
|
|
}
|
|
|
|
// Fallback to access_token query param
|
|
if (!req.authToken && req.query && req.query.access_token) {
|
|
req.authToken = req.query.access_token;
|
|
}
|
|
|
|
next();
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 4. User authentication (replaces communitypackages:authenticate-user-by-token)
|
|
// ---------------------------------------------------------------------------
|
|
WebApp.handlers.use(async function authenticateByToken(req, res, next) {
|
|
if (req.authToken) {
|
|
try {
|
|
const hashedToken = Accounts._hashLoginToken(req.authToken);
|
|
const user = await Meteor.users.findOneAsync(
|
|
{ 'services.resume.loginTokens.hashedToken': hashedToken },
|
|
{ fields: { _id: 1 } },
|
|
);
|
|
if (user) {
|
|
req.userId = user._id;
|
|
}
|
|
} catch (e) {
|
|
// Ignore auth errors — routes handle missing userId themselves
|
|
}
|
|
}
|
|
next();
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 4b. API usage counting (Admin Panel -> Problems -> API)
|
|
// ---------------------------------------------------------------------------
|
|
// After authentication, so a call has an account, and in front of the routes, so
|
|
// a route cannot be added without being counted. It counts on the response's
|
|
// `finish` event - by then Express has filled req.route.path, which is the route
|
|
// PATTERN and therefore one row per endpoint rather than one per board id.
|
|
WebApp.handlers.use(require('/server/lib/apiUsageLog').apiUsageMiddleware);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 5. sendJsonResult — drop-in replacement for JsonRoutes.sendResult
|
|
// ---------------------------------------------------------------------------
|
|
function sendJsonResult(res, options) {
|
|
options = options || {};
|
|
|
|
// Default response headers (matching json-routes behavior)
|
|
res.setHeader('Cache-Control', 'no-store');
|
|
res.setHeader('Pragma', 'no-cache');
|
|
|
|
// Custom headers
|
|
if (options.headers) {
|
|
Object.entries(options.headers).forEach(function ([key, value]) {
|
|
res.setHeader(key, value);
|
|
});
|
|
}
|
|
|
|
// Status code
|
|
res.statusCode = options.code || 200;
|
|
|
|
// JSON body
|
|
if (options.data !== undefined) {
|
|
const shouldPrettyPrint = process.env.NODE_ENV === 'development';
|
|
const spacer = shouldPrettyPrint ? 2 : null;
|
|
res.setHeader('Content-Type', 'application/json');
|
|
// Use a crash-proof serializer: some error payloads (e.g. a circular
|
|
// SimpleSchema validation error) would otherwise throw inside
|
|
// JSON.stringify and surface as a generic HTTP 500. See #5804.
|
|
res.write(safeJsonStringify(options.data, spacer));
|
|
}
|
|
|
|
res.end();
|
|
}
|
|
|
|
// GHSA-3gcg-g6rf-w2rx: an export route that throws must not escape as an unhandled
|
|
// promise rejection. This app turns one of those into a full process crash - the
|
|
// note in server/ldapGroupSync.js explains why - so a single crafted request could
|
|
// take the server down for everyone. Every route body wrapped in this answers with a
|
|
// 500 instead, and the request that caused it is logged.
|
|
//
|
|
// The guards inside the handlers are still the real fix; this is the net under them,
|
|
// so the next missing null check is one broken request rather than an outage.
|
|
function safeRoute(handler) {
|
|
return async function safeRouteHandler(req, res, ...rest) {
|
|
try {
|
|
return await handler.call(this, req, res, ...rest);
|
|
} catch (error) {
|
|
console.error('[api] unhandled error while serving',
|
|
(req && req.url) || 'a request', error);
|
|
try {
|
|
if (!res.headersSent) {
|
|
res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
}
|
|
res.end('Internal server error');
|
|
} catch (_) {
|
|
// The response is already gone; nothing left to answer with.
|
|
}
|
|
return undefined;
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = { sendJsonResult, safeRoute };
|