Files
wekan/server/crossSiteRequestGuard.js
T
Lauri Ojansivu 48a2e97ea3 A state-changing request that logs in only with the cookie must come from WeKan itself.
POST /import-trello and other HTTP routes accept the login cookie as the
login, so a page on another site could make a signed-in browser send one.
The cookie is SameSite=Strict, which keeps it off such a request in
today's browsers; this is the second line, for older browsers, same-site
sibling hosts and routes added later. A POST, PUT, PATCH or DELETE whose
only login is the cookie must carry an Origin, or Referer, naming
ROOT_URL's host or the Host / X-Forwarded-Host it was sent to, which on
Sandstorm is the grain's own host. Token-authenticated API calls, Meteor's
cookie routes and DDP are not affected. A refusal is recorded in Admin
Panel / Problems as csrf.cross-site-cookie and never blocks the account
whose cookie it was.

Thanks to xet7 !
2026-10-30 23:52:09 +02:00

30 lines
1.2 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
// A state-changing request that carries the login only as a cookie must come
// from WeKan's own origin (models/lib/crossSiteRequest.js says which requests
// and why). A rawHandler, so it runs before every route, whichever file
// registered the route and whenever it was imported.
const { crossSiteCookieRequest } = require('/models/lib/crossSiteRequest');
WebApp.rawHandlers.use((req, res, next) => {
let decision;
try {
decision = crossSiteCookieRequest(req, process.env.ROOT_URL || Meteor.absoluteUrl());
} catch (e) {
decision = { block: false };
}
if (!decision.block) return next();
try {
// Nobody's account is named: the cookie is the victim's, not the sender's.
require('/server/lib/securityLog').record({
key: 'csrf.cross-site-cookie', action: 'blocked', req,
source: `${req.method} ${String(req.url || '').split('?')[0]}`,
detail: `Cross-site request refused: ${decision.reason} ${decision.origin}`,
});
} catch (e) { /* logging must never break the guard */ }
res.writeHead(403, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'cross-site-request' }));
return undefined;
});