mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
61 lines
2.5 KiB
JavaScript
61 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const crypto = require('crypto');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const {
|
|
canonical, sha256, hashHistoryRow, rowHashIsValid, verifyHistoryRows,
|
|
} = require('../models/lib/changeHistoryIntegrity');
|
|
|
|
for (const value of ['', 'abc', 'WeKan ® 🐙', 'x'.repeat(1000)]) {
|
|
assert.equal(sha256(value), crypto.createHash('sha256').update(value).digest('hex'));
|
|
}
|
|
const helperSource = fs.readFileSync(
|
|
path.join(__dirname, '../models/lib/changeHistoryIntegrity.js'), 'utf8',
|
|
);
|
|
assert.doesNotMatch(helperSource, /require\(['"](?:node:)?crypto['"]\)/,
|
|
'the isomorphic history helper must not pull Node crypto into the browser');
|
|
|
|
const first = {
|
|
boardId: 'b1', entityType: 'card', entityId: 'c1', group: 'title',
|
|
changeType: 'edited', previousContent: { value: 'before', field: 'title' },
|
|
newContent: { field: 'title', value: 'after' }, userId: 'u1',
|
|
createdAt: new Date('2026-09-05T12:00:00Z'), previousHash: null,
|
|
};
|
|
first.integrityHash = hashHistoryRow(first);
|
|
assert.ok(rowHashIsValid(first));
|
|
|
|
const second = { ...first, entityId: 'c2', previousHash: first.integrityHash,
|
|
createdAt: new Date('2026-09-05T12:00:01Z') };
|
|
second.integrityHash = hashHistoryRow(second);
|
|
assert.ok(rowHashIsValid(second));
|
|
|
|
for (const mutate of [
|
|
row => { row.userId = 'attacker'; },
|
|
row => { row.boardId = 'other'; },
|
|
row => { row.newContent.value = 'tampered'; },
|
|
row => { row.previousHash = '0'.repeat(64); },
|
|
row => { row.createdAt = new Date('2026-09-05T12:00:02Z'); },
|
|
]) {
|
|
const row = structuredClone(second);
|
|
mutate(row);
|
|
assert.equal(rowHashIsValid(row), false);
|
|
}
|
|
|
|
const mutable = { ...second, undone: true, undoneAt: new Date(), superseded: true };
|
|
assert.ok(rowHashIsValid(mutable), 'undo/redo stack state is intentionally outside immutable hash');
|
|
assert.equal(canonical({ b: 1, a: 2 }), canonical({ a: 2, b: 1 }),
|
|
'object insertion order cannot change a canonical hash');
|
|
assert.throws(() => canonical({ bad: Infinity }), /non-finite/);
|
|
|
|
assert.deepEqual(verifyHistoryRows([first, second]), []);
|
|
const orphan = { ...second, previousHash: 'f'.repeat(64) };
|
|
orphan.integrityHash = hashHistoryRow(orphan);
|
|
assert.equal(verifyHistoryRows([first, orphan])[0].reason, 'predecessor-missing');
|
|
const fork = { ...second, entityId: 'c3' };
|
|
fork.integrityHash = hashHistoryRow(fork);
|
|
assert.ok(verifyHistoryRows([first, second, fork]).some(item => item.reason === 'history-fork'));
|
|
|
|
console.log('changeHistoryIntegrity: chain and tamper checks passed');
|