Files
wekan/server/lib/scrumHistoryRestoreWriter.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

53 lines
3.0 KiB
JavaScript

const { sha256, canonical, hashHistoryRow, rowHashIsValid } = require('../../models/lib/changeHistoryIntegrity');
const { appendLegacyHistoryRow } = require('../../models/lib/historyLegacyAppend');
// Only restoration events: ordinary edits retain the model's best-effort writer
// and redo invalidation. The caller retains its recovery journal until this
// strict writer has acknowledged the exact event for each affected author.
async function recordScrumRestoreOnce(history, options) {
const { boardId, batchId, userId, entityType, entityId, restoredFromId, restoredByUserId } = options;
if (![boardId, batchId, userId, entityType, entityId, restoredFromId, restoredByUserId]
.every(value => typeof value === 'string' && value.length > 0) || options.changeType !== 'restored') {
throw new Error('Invalid Scrum History restoration identity');
}
const event = {
boardId, swimlaneId: options.swimlaneId ?? null, listId: options.listId ?? null,
cardId: options.cardId ?? null, entityType, entityId, group: options.group ?? null,
changeType: 'restored', previousContent: options.previousContent ?? null,
newContent: options.newContent ?? null, userId, batchId, restoredFromId,
restoredByUserId, isCheckpoint: options.isCheckpoint ?? false,
};
const id = `scrum-restore-${sha256(canonical([boardId, batchId, userId]))}`;
const matches = row => {
if (!rowHashIsValid(row) || Object.keys(event).some(key => canonical(row[key]) !== canonical(event[key]))) {
throw new Error('Conflicting Scrum History restoration record');
}
return row._id;
};
// Pre-upgrade journals used random row IDs. Validate those too, without
// rewriting their timestamp or chain link, before acknowledging the journal.
const existing = await history.findOneAsync({ boardId, batchId, userId });
if (existing) return matches(existing);
const row = { ...event, _id: id, createdAt: new Date(), undone: false,
undoneAt: null, superseded: false };
// After the chain's tip, one append at a time per board (historyLegacyAppend.js).
const legacy = document => appendLegacyHistoryRow({ history, boardId, document, hash: hashHistoryRow });
const write = async append => {
let writeError;
try { await append(row); } catch (error) { writeError = error; }
// Confirm inside writer admission so a lost insert reply with a verified
// row can release its token. A failed confirmation retains recovery evidence.
let committed;
try { committed = await history.findOneAsync(id); }
catch (error) { throw writeError || error; }
if (committed) {
if (committed._id !== id) throw new Error('Conflicting Scrum History restoration record');
return matches(committed);
}
throw writeError || new Error('Unconfirmed Scrum History restoration record');
};
return typeof history.withHistoryWriter === 'function'
? history.withHistoryWriter({ boardId, row, write, legacy }) : write(legacy);
}
module.exports = { recordScrumRestoreOnce };