Files
wekan/server/lib/storedHistoryChain.js
T
Lauri Ojansivu 66dd9b0938 History: two changes at once no longer fork a board's History chain.
Admin Panel -> Problems showed HistoryIntegrity, critical, "history-fork",
89,062 times. Each History row names the row before it, and the legacy
append (every board not migrated to the coordinated chain) read "the
newest row" and inserted after it as two separate steps: two changes
saved at the same moment on one board - two members, or one action whose
hooks record twice at once - both named the same row. Nothing was
tampered with, but the audit called it tampering, and undo, redo and
restore were refused for both rows. The writer gate around the legacy
append counts its writers for a migration; it does not make them take
turns.

- models/lib/historyLegacyAppend.js: a board's appends take turns in
  this server, and each links after the chain's tip - the newest row,
  then its successors, as Sync History already did - so a createdAt tie
  does not fork either. The ordinary recorder, the Scrum restore writer
  and Sync's legacy append all use it. Several servers writing one board
  at the same moment can still race; the coordinated chain is the answer
  to that.
- The hourly audit reported every broken row again every hour, which is
  where 89,062 came from; each finding is now reported once
  (historyIntegrityReported). It reads the History board by board instead
  of all at once, and a fork of two intact rows saved within a minute of
  each other is reported as what it is - integrity.historyConcurrentFork,
  medium, "a History writer race, not tampering" - while anything else
  stays critical.

Existing forks stay as they are (a row's hash cannot be changed), and
undo of their two rows is still refused.

tests/historyLegacyAppend.test.cjs: the old append forks under
concurrency (negative control), twenty appends at once on two boards
make two straight chains, a createdAt tie follows the chain, a failed
append does not stop the next, the audit tells a race from a changed or
late row, and no append in models/ or server/ links by "newest" without
taking turns.

Thanks to xet7 !
2026-10-14 22:39:36 +03:00

142 lines
8.2 KiB
JavaScript

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import ChangeHistory from '/models/changeHistory';
import { ensureIndex } from '/server/lib/mongoStartup';
const { EJSON } = require('bson');
const { appendHistoryChain, historyChainId, validateHistoryChainHead } = require('./historyChainAppend');
const { initializeHistoryChain } = require('./historyChainBootstrap');
const { withHistoryWriter, recoverExpiredHistoryWriters, historyWriterLeasePolicy } = require('./historyWriterGate');
const { hashHistoryRow } = require('../../models/lib/changeHistoryIntegrity');
const { serializeHistoryAppend, historyChainTip } = require('../../models/lib/historyLegacyAppend');
const { migrateHistoryChain } = require('./historyChainMigration');
export const HistoryWriterGates = new Mongo.Collection('historyWriterGates');
HistoryWriterGates.deny({ insert: () => true, update: () => true, remove: () => true });
export const HistoryChainHeads = new Mongo.Collection('historyChainHeads');
HistoryChainHeads.deny({ insert: () => true, update: () => true, remove: () => true });
// One lease per legacy writer token (server/lib/historyWriterGate.js), so a
// writer that died can be taken over online.
export const HistoryWriterLeases = new Mongo.Collection('historyWriterLeases');
HistoryWriterLeases.deny({ insert: () => true, update: () => true, remove: () => true });
Meteor.startup(async () => {
await ensureIndex(HistoryChainHeads, { boardId: 1 }, { unique: true });
await ensureIndex(HistoryWriterGates, { boardId: 1 }, { unique: true });
await ensureIndex(HistoryWriterLeases, { boardId: 1 });
});
const { leaseMs, graceMs } = historyWriterLeasePolicy();
const writerStorage = () => ({ gates: HistoryWriterGates.rawCollection(), leases: HistoryWriterLeases.rawCollection(),
history: ChangeHistory.rawCollection(), leaseMs });
// Internal initialization only: the caller must exclude all legacy writers.
export function initializeStoredHistoryChain({ boardId, assertExclusive }) {
return initializeHistoryChain({ heads: HistoryChainHeads.rawCollection(),
history: ChangeHistory.rawCollection(), boardId, assertExclusive });
}
// Prepare the COMPLETE schema-normalized snapshot before reserving it. A row
// that fails ordinary validation must not poison the durable pending slot.
export function prepareStoredHistoryRow(input) {
if (!input || typeof input._id !== 'string' || !input._id ||
!(input.createdAt instanceof Date) || !Number.isFinite(input.createdAt.getTime()) ||
Object.hasOwn(input, 'previousHash') || Object.hasOwn(input, 'integrityHash')) {
throw new Error('history-chain-input-invalid');
}
const row = EJSON.parse(EJSON.stringify({ boardId: null, swimlaneId: null, listId: null,
cardId: null, group: null, previousContent: null, newContent: null, undone: false,
undoneAt: null, isCheckpoint: false, batchId: null, restoredFromId: null,
restoredByUserId: null, superseded: false, ...input }), { relaxed: true });
const { _id, ...document } = row;
const validation = ChangeHistory.simpleSchema().newContext();
if (!validation.validate(document)) throw new Error('history-chain-schema-invalid');
return row;
}
export async function appendStoredHistoryChain({ row, assertCurrent }) {
row = prepareStoredHistoryRow(row);
if (typeof assertCurrent !== 'function') throw new Error('history-chain-ownership-required');
await assertCurrent();
const heads = HistoryChainHeads.rawCollection();
const head = await heads.findOne({ _id: historyChainId(row.boardId) });
await assertCurrent();
if (!head) throw new Error('history-chain-not-initialized');
validateHistoryChainHead(head, row.boardId);
return appendHistoryChain({ row, initialHash: head.hash, assertCurrent,
heads: { findOne: (...args) => heads.findOne(...args), replaceOne: (...args) => heads.replaceOne(...args),
// Never silently recreate a deleted head from a stale initialHash.
insertOne: async () => { throw new Error('history-chain-not-initialized'); } },
history: { findOne: selector => ChangeHistory.findOneAsync(selector, { transform: null }),
insertOne: async document => ({ insertedId: await ChangeHistory.insertAsync(document,
{ removeEmptyStrings: false, trimStrings: false }) }) } });
}
// Ordinary recording participates immediately, but boards remain on their old
// append path until explicitly migrated. Schema errors precede admission.
ChangeHistory.withHistoryWriter = async ({ boardId, row, write, legacy }) => {
const prepared = prepareStoredHistoryRow({ ...row, _id: row._id ?? Random.id() });
return withHistoryWriter({ ...writerStorage(), boardId,
// Each legacy insert is claimed under the writer's lease, so a writer that
// dies mid-insert can be fenced and taken over online.
writeLegacy: ({ assertCurrent, fencedInsert }) => write(async document => {
const doc = { ...document, _id: document._id ?? prepared._id };
const id = await fencedInsert(doc._id, () => legacy(doc)); await assertCurrent(); return id;
}),
writeCoordinated: () => write(() => appendStoredHistoryChain({ row: prepared, assertCurrent: async () => {} })) });
};
// Sync History (server/lib/syncHistoryBatch.js) plans rows as content and links
// each one when it is appended (maintainer decision of 2026-09-30). On a
// legacy board the whole batch holds one writer token, so a migration drains
// it; on a coordinated board each row goes through the chain head, like an
// ordinary edit, so nothing is refused and nothing forks.
ChangeHistory.admitHistoryWriter = ({ boardId, work }) => withHistoryWriter({
...writerStorage(), boardId,
writeLegacy: ({ assertCurrent, fencedInsert }) => work({ mode: 'legacy', assertCurrent, fencedInsert }),
writeCoordinated: () => work({ mode: 'coordinated', assertCurrent: async () => {} }) });
// Append one planned Sync row, idempotently by its _id: a retry that finds
// the row returns it, and the caller checks it is the planned content.
ChangeHistory.appendSyncHistoryRow = async ({ row, mode, assertCurrent, fencedInsert }) => {
if (mode === 'coordinated') return appendStoredHistoryChain({ row, assertCurrent });
if (mode !== 'legacy') throw new Error('history-chain-mode-invalid');
if (typeof fencedInsert !== 'function') throw new Error('history-writer-fence-required');
const prepared = prepareStoredHistoryRow(row);
await assertCurrent();
const existing = await ChangeHistory.findOneAsync(prepared._id, { transform: null });
// A fence tombstone holds this id: an earlier attempt was taken over.
if (existing) {
if (existing.boardId !== prepared.boardId) throw new Error('history-writer-fenced');
return prepared._id;
}
// After the chain's TIP: from the newest hashed row, follow its successors.
// A Sync batch's rows share one planned createdAt, so "newest" alone is a
// tie and would give an earlier batch row a second successor - a fork. And
// one append at a time per board, like every legacy append
// (models/lib/historyLegacyAppend.js).
return serializeHistoryAppend(prepared.boardId, async () => {
const previous = await historyChainTip(ChangeHistory, prepared.boardId);
const saved = { ...prepared, previousHash: previous ? previous.integrityHash : null };
saved.integrityHash = hashHistoryRow(saved);
await assertCurrent();
try { await fencedInsert(prepared._id, () => ChangeHistory.insertAsync(saved, { removeEmptyStrings: false, trimStrings: false })); }
catch (error) {
const found = await ChangeHistory.findOneAsync(prepared._id, { transform: null });
if (!found || found.boardId !== prepared.boardId) throw error;
}
return prepared._id;
});
};
// No automatic rollout: callers must prove older server versions cannot write.
// Drain/resume uses the same durable migration UUID, never a timed takeover.
export function migrateStoredHistoryChain({ boardId, migrationId, assertDeploymentExclusive }) {
return migrateHistoryChain({ ...writerStorage(), heads: HistoryChainHeads.rawCollection(),
boardId, migrationId, assertDeploymentExclusive, graceMs });
}
// Take every abandoned writer of a board online (Admin/CLI use).
export function recoverStoredHistoryWriters({ boardId }) {
return recoverExpiredHistoryWriters({ ...writerStorage(), boardId, graceMs });
}