mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
A guard on a path an attacker controls fires as fast as they can send. One
document per event meant the database grew with the attack, the Problems page
became a scroll of near-identical lines, and the one event that mattered was
buried under ten thousand that did not. The admin's question is never "list
every attempt" - it is what is happening, how much, since when, and who.
So each problem is ONE row that accumulates:
count how many times this problem has happened
firstAt … at the window it happened in
actors who, each with their own count:
username1 25, 100.100.100.100 30
username/ip the most recent occurrence
WHAT MAKES TWO EVENTS THE SAME PROBLEM is the whole design. Identity is the KIND
of thing that happened - stream, bleed, category, action, source, severity, CWE,
and the database stream's own type/db/kind. The actor is NOT part of it: a
username or an address in the key gives a row per attacker per attempt, which is
the cost being removed.
A USERNAME AND AN ADDRESS ARE TALLIED SEPARATELY, not as a pair. They answer
different questions - which account, and where from - and an unauthenticated
attempt has an address and no name. Pairing them would split one attacker across
a line per address and answer neither.
THE TALLY IS CAPPED at 50 actors, with the rest counted in actorsOverflow.
Otherwise an attacker rotating addresses grows the row with the attack and
reintroduces the same bug one level down - and "and 9,412 others" is itself the
signal that the source is spread rather than single. The cap needs to know
whether an actor is new to the row, so the row's actor keys are cached per
process: reading it on every attempt would double the database work of the thing
being made cheap, and under attack the same few actors repeat.
All three loggers share the fold (server/lib/eventLogFold.js) - a slow query in
a loop and a test failing on every run repeat just as a guard under attack does.
Still fire-and-forget and still unable to throw into the caller.
EXISTING PER-EVENT ROWS ARE FOLDED IN PLACE at startup, in batches, in JS so it
behaves the same on FerretDB as on MongoDB, and idempotently - a row with firstAt
is already a summary. Nothing is lost that the summary does not carry: counts are
summed, the earliest becomes firstAt, the latest becomes at, and the latest
occurrence's actor and detail are what the row keeps.
AND A COVERAGE CHECK, as asked: tests/hallOfFameProblemsCoverage.test.cjs
requires every hall-of-fame vulnerability to be keyed, deliberate (with the
reason, as HashBleed is), or pending. 30 of 66 are keyed; the other 35 predate
the rule and are listed as pending rather than judged here, because deciding
whether a given fix has anything to detect means reading that fix and claiming
otherwise would be worse than the gap. The pending list may shrink and may never
grow, so a NEW vulnerability without a key fails.
It found one real fault immediately: the catalog said OidcBleed and the site says
OIDCBleed, so attempts were grouping under a name nobody could look up.
Thanks to xet7 !
68 lines
2.9 KiB
JavaScript
68 lines
2.9 KiB
JavaScript
// Fold the per-event rows an older WeKan wrote into the summaries Admin Panel →
|
|
// Problems shows now (models/lib/eventLogSummary.js).
|
|
//
|
|
// Before this, every guard firing wrote its own document. An instance that has
|
|
// been attacked - or that ran a chatty CPU governor for a month - arrives with
|
|
// thousands of near-identical rows, and until they are folded the Problems page
|
|
// still shows the scroll this replaced, with the new summaries mixed in among
|
|
// them. So the old rows are folded once, in place, and removed.
|
|
//
|
|
// Nothing is lost that the summary does not carry: the count of every old row is
|
|
// added up, the earliest becomes `firstAt`, the latest becomes `at`, and the
|
|
// latest occurrence's user, address and detail are what the row keeps. What does
|
|
// go is one line per attempt, which is the point.
|
|
//
|
|
// Done in JS rather than an aggregation pipeline so it behaves the same on
|
|
// FerretDB as on MongoDB, and in batches so a large collection does not have to
|
|
// fit in memory at once. Idempotent: a row with `firstAt` is already a summary
|
|
// and is not touched, so a re-run - or a restart in the middle - costs nothing.
|
|
|
|
import EventLog from '/models/eventLog';
|
|
|
|
const { foldEvents, summaryIdentity, summaryUpdate } = require('/models/lib/eventLogSummary');
|
|
|
|
const BATCH = 2000;
|
|
|
|
export async function foldLegacyEventLogRows({ log = console } = {}) {
|
|
let folded = 0;
|
|
let removed = 0;
|
|
for (;;) {
|
|
// A legacy row is one with no `firstAt`: every summary has one.
|
|
const batch = await EventLog.find(
|
|
{ firstAt: { $exists: false } },
|
|
{ limit: BATCH },
|
|
).fetchAsync();
|
|
if (!batch.length) break;
|
|
|
|
for (const summary of foldEvents(batch)) {
|
|
const { count, firstAt, at, ...rest } = summary;
|
|
// Add this batch's occurrences to whatever the row already holds - a
|
|
// summary may already exist, from this migration's earlier batches or from
|
|
// a guard that fired while it was running.
|
|
await EventLog.upsertAsync(
|
|
summaryIdentity(rest),
|
|
summaryUpdate(rest, at, count),
|
|
);
|
|
// $setOnInsert cannot lower a firstAt that a later batch turns out to
|
|
// precede, so the earliest is written explicitly when it is earlier.
|
|
const row = await EventLog.findOneAsync(summaryIdentity(rest));
|
|
if (row && (!row.firstAt || firstAt < row.firstAt)) {
|
|
await EventLog.updateAsync({ _id: row._id }, { $set: { firstAt } });
|
|
}
|
|
folded += 1;
|
|
}
|
|
|
|
const ids = batch.map(d => d._id);
|
|
await EventLog.removeAsync({ _id: { $in: ids } });
|
|
removed += ids.length;
|
|
|
|
// A batch that folds to nothing would loop forever; it cannot, since every
|
|
// row is either folded or removed, but stop on no progress regardless.
|
|
if (!ids.length) break;
|
|
}
|
|
if (removed) {
|
|
log.log(`eventlog: folded ${removed} per-event row(s) into ${folded} problem summary(s).`);
|
|
}
|
|
return { folded, removed };
|
|
}
|