Files
wekan/models/server/metrics.js
T
Lauri Ojansivu a4b524a5fc Compare the /metrics access token in constant time, and refuse an empty one.
METRICS_ACCESS_TOKEN set but empty let ?access_token= through, and the loose
early-exit comparison leaked how much of a guess was right through timing.

Thanks to xet7 !
2026-10-02 11:38:24 +03:00

305 lines
12 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import Users from '../users';
function acceptedIpAddress(ipAddress) {
const trustedIpAddress = process.env.METRICS_ACCEPTED_IP_ADDRESS;
return (
trustedIpAddress !== undefined &&
trustedIpAddress.split(',').includes(ipAddress)
);
}
// Resolve the client IP used for the METRICS_ACCEPTED_IP_ADDRESS allowlist.
//
// Security fix (reported by meifukun): the endpoint previously trusted the
// client-supplied `X-Forwarded-For` header UNCONDITIONALLY, so anyone who could
// reach /metrics directly could send `X-Forwarded-For: <whitelisted-ip>` and pass
// the allowlist. By default we now use ONLY the real socket peer address.
//
// Operators genuinely behind reverse proxies can opt back in with
// METRICS_TRUST_PROXY set to the number of trusted proxy hops (e.g. `1`). The
// client IP is then read from the Nth position from the RIGHT of the XFF chain —
// the address your own proxy appended, which a client cannot forge — so a forged
// left-most entry is ignored.
function metricsClientIp(req) {
const remote = req.socket.remoteAddress;
const trust = process.env.METRICS_TRUST_PROXY;
if (!trust || trust === 'false' || trust === '0') {
return remote;
}
const xff = req.headers['x-forwarded-for'];
if (!xff) return remote;
const list = String(xff).split(',').map(s => s.trim()).filter(Boolean);
if (!list.length) return remote;
const hops = parseInt(trust, 10);
const n = Number.isInteger(hops) && hops > 0 ? hops : 1;
const idx = list.length - n;
return idx >= 0 ? list[idx] : remote;
}
function accessToken(req) {
const valid_token = process.env.METRICS_ACCESS_TOKEN;
let token;
if (req.headers && req.headers.authorization) {
var parts = req.headers.authorization.split(" ");
if (parts.length === 2) {
var scheme = parts[0];
var credentials = parts[1];
if (/^Bearer$/i.test(scheme)) {
token = credentials;
}
}
}
if (!token && req.query && req.query.access_token) {
token = req.query.access_token;
}
// An unset or EMPTY METRICS_ACCESS_TOKEN is no token: `?access_token=` would
// otherwise equal ''. Compare digests in constant time, so the response time
// does not tell a guesser how much of the token was right.
if (typeof token !== 'string' || typeof valid_token !== 'string' || valid_token === '') {
return false;
}
const digest = value => require('crypto').createHash('sha256').update(value).digest();
return require('crypto').timingSafeEqual(digest(token), digest(valid_token));
}
const getBoardTitleWithMostActivities = async (dateWithXdaysAgo, nbLimit) => {
return await Activities.rawCollection()
.aggregate([
{
$match: { modifiedAt: { $gte: dateWithXdaysAgo } },
},
{
$group: { _id: '$boardId', count: { $sum: 1 } },
},
{
$sort: { count: -1 },
},
{
$lookup: { from: 'boards', localField: '_id', foreignField: '_id', as: 'lookup' },
},
{
$project: { 'lookup.title': 1, count: 1 },
},
])
.limit(nbLimit)
.toArray();
};
const getBoards = async (boardIds) => {
const ret = await ReactiveCache.getBoards({ _id: { $in: boardIds } });
return ret;
};
Meteor.startup(() => {
WebApp.handlers.use('/metrics', async (req, res, next) => {
try {
// Client-supplied X-Forwarded-For is trusted ONLY when METRICS_TRUST_PROXY
// is set (see metricsClientIp); by default the real socket address is used
// so a forged XFF cannot bypass METRICS_ACCEPTED_IP_ADDRESS.
const ipAddress = metricsClientIp(req);
// List of trusted ip adress will be found in environment variable "METRICS_ACCEPTED_IP_ADDRESS" (separeted with commas)
if (acceptedIpAddress(ipAddress) || (accessToken(req))) {
let metricsRes = '';
let resCount = 0;
//connected users
metricsRes += '# Number of connected users\n';
// Get number of connected user by using meteor socketJs
const allOpenedSockets = Meteor.server.stream_server.open_sockets;
let connectedUserIds = [];
allOpenedSockets.forEach(
(socket) =>
socket._meteorSession.userId !== null &&
connectedUserIds.push(socket._meteorSession.userId),
);
resCount = connectedUserIds.length; // KPI 1
metricsRes += 'wekan_connectedUsers ' + resCount + '\n';
//registered users
metricsRes += '# Number of registered users\n';
// Get number of registered user
resCount = (await ReactiveCache.getUsers({})).length; // KPI 2
metricsRes += 'wekan_registeredUsers ' + resCount + '\n';
resCount = 0;
//board numbers
metricsRes += '# Number of registered boards\n';
// Get number of registered boards
resCount = (await ReactiveCache.getBoards({ archived: false, type: 'board' })).length; // KPI 3
metricsRes += 'wekan_registeredboards ' + resCount + '\n';
resCount = 0;
//board numbers by registered users
metricsRes += '# Number of registered boards by registered users\n';
// Get number of registered boards by registered users
resCount =
(await ReactiveCache.getBoards({ archived: false, type: 'board' })).length /
(await ReactiveCache.getUsers({})).length; // KPI 4
metricsRes +=
'wekan_registeredboardsBysRegisteredUsers ' + resCount + '\n';
resCount = 0;
//board numbers with only one member
metricsRes += '# Number of registered boards\n';
// Get board numbers with only one member
resCount = (await ReactiveCache.getBoards({
archived: false,
type: 'board',
members: { $size: 1 },
})).length; // KPI 5
metricsRes +=
'wekan_registeredboardsWithOnlyOneMember ' + resCount + '\n';
resCount = 0;
// KPI 6 : - store last login date
// KPI 6 = count where date of last connection > x days
// Cutting in label since 5 days / 10 days / 20 days / 30 days
//Number of users with last connection dated 5 days ago
metricsRes +=
'# Number of users with last connection dated 5 days ago\n';
// Get number of users with last connection dated 5 days ago
let xdays = 5;
let dateWithXdaysAgo = new Date(
new Date() - xdays * 24 * 60 * 60 * 1000,
);
resCount = (await ReactiveCache.getUsers({
lastConnectionDate: { $gte: dateWithXdaysAgo },
})).length; // KPI 5
metricsRes +=
'wekan_usersWithLastConnectionDated5DaysAgo ' + resCount + '\n';
resCount = 0;
metricsRes +=
'# Number of users with last connection dated 10 days ago\n';
// Get number of users with last connection dated 10 days ago
xdays = 10;
dateWithXdaysAgo = new Date(new Date() - xdays * 24 * 60 * 60 * 1000);
resCount = (await ReactiveCache.getUsers({
lastConnectionDate: { $gte: dateWithXdaysAgo },
})).length; // KPI 5
metricsRes +=
'wekan_usersWithLastConnectionDated10DaysAgo ' + resCount + '\n';
resCount = 0;
metricsRes +=
'# Number of users with last connection dated 20 days ago\n';
// Get number of users with last connection dated 20 days ago
xdays = 20;
dateWithXdaysAgo = new Date(new Date() - xdays * 24 * 60 * 60 * 1000);
resCount = (await ReactiveCache.getUsers({
lastConnectionDate: { $gte: dateWithXdaysAgo },
})).length; // KPI 5
metricsRes +=
'wekan_usersWithLastConnectionDated20DaysAgo ' + resCount + '\n';
resCount = 0;
metricsRes +=
'# Number of users with last connection dated 30 days ago\n';
// Get number of users with last connection dated 20 days ago
xdays = 30;
dateWithXdaysAgo = new Date(new Date() - xdays * 24 * 60 * 60 * 1000);
resCount = (await ReactiveCache.getUsers({
lastConnectionDate: { $gte: dateWithXdaysAgo },
})).length; // KPI 5
metricsRes +=
'wekan_usersWithLastConnectionDated30DaysAgo ' + resCount + '\n';
resCount = 0;
// TO DO:
// connection average: ((disconnection date - last connection date) + (last average)) / 2
// KPI 7 : sum of connection average / number of users (ignore users with 0 average)
metricsRes +=
'# Top 10 boards with most activities dated 30 days ago\n';
//Get top 10 table with most activities in current month
const boardTitleWithMostActivities = await getBoardTitleWithMostActivities(
dateWithXdaysAgo,
xdays,
);
const boardWithMostActivities = boardTitleWithMostActivities.map(
(board) => board.lookup[0].title,
);
boardWithMostActivities.forEach((title, index) => {
metricsRes +=
`wekan_top10BoardsWithMostActivities{n="${title}"} ${
index + 1
}` + '\n';
});
res.writeHead(200); // HTTP status
res.end(metricsRes);
} else {
// A denied request that carried an X-Forwarded-For header MIGHT be a
// forged-whitelisted-IP attempt (MetricsBleed) — but only if the header
// claimed an address that is actually on the allowlist.
//
// "Nothing legitimate sends a forwarded-for header here" turned out to
// be wrong, and an admin's Security Report is where it showed: every
// reverse proxy adds X-Forwarded-For to everything it forwards. A
// Prometheus scrape through a local proxy, on a server whose allowlist
// simply does not cover it, was recorded as a medium-severity spoofing
// attempt from 127.0.0.1, over and over. A report full of the proxy
// doing its job is a report nobody reads.
//
// The spoof has a signature, so ask for it: the header names an
// allowlisted address while the connection itself is not from one. That
// is somebody claiming to be a whitelisted host. Anything else is an
// ordinary refusal, and the 401 is the whole of the answer.
const forwardedFor = req.headers['x-forwarded-for'];
if (forwardedFor) {
try {
const claimed = String(forwardedFor)
.split(',')
.map(s => s.trim())
.filter(Boolean);
const impersonating =
claimed.some(addr => acceptedIpAddress(addr)) &&
!acceptedIpAddress(req.socket.remoteAddress);
if (impersonating) {
// A canary, with the REQUEST in hand. The 401 below is unchanged
// (docs/Security/Remediation/WeKan.md §12).
require('/server/lib/canary').tripCanary('spoof.forwarded-header', {
req,
detail: 'denied /metrics with a forwarded-for header naming an allowed address',
});
}
} catch (e) { /* logging must never break the endpoint */ }
}
res.writeHead(401); // HTTP status
res.end(
'IpAddress: ' +
ipAddress +
' is not authorized to perform this action !!\n' +
// The address above is the SOCKET peer unless METRICS_TRUST_PROXY
// says how many proxies to trust, so behind a reverse proxy this is
// the proxy and never the scraper. Somebody staring at a 401 from
// their own Prometheus deserves to be told that here, in the answer
// they are actually looking at, rather than in the server log.
(forwardedFor && !process.env.METRICS_TRUST_PROXY
? 'This request came through a proxy (X-Forwarded-For present) and the ' +
'address above is the proxy. Add it to METRICS_ACCEPTED_IP_ADDRESS, or ' +
'set METRICS_TRUST_PROXY to the number of trusted proxy hops so the ' +
"scraper's own address is used.\n"
: ''),
);
}
} catch (e) {
res.writeHead(500); // HTTP status
res.end(e.toString());
}
});
});