After correcting the problem of withdrawal1
This commit is contained in:
parent
4bb1e7485f
commit
e3e9433c4c
@ -17,6 +17,19 @@ export type AccountLedgerMeta = {
|
|||||||
export declare function getAccountLedgerMeta(accountId: string): Promise<AccountLedgerMeta | null>;
|
export declare function getAccountLedgerMeta(accountId: string): Promise<AccountLedgerMeta | null>;
|
||||||
export declare function getSnapshot(accountId: string): Promise<LedgerSnapshotRow | null>;
|
export declare function getSnapshot(accountId: string): Promise<LedgerSnapshotRow | null>;
|
||||||
export declare function upsertSnapshot(meta: AccountLedgerMeta): Promise<void>;
|
export declare function upsertSnapshot(meta: AccountLedgerMeta): Promise<void>;
|
||||||
|
/**
|
||||||
|
* Main accounts whose locked_balance does not equal SUM(withdrawal_locks).
|
||||||
|
* Catches unauthorized available↔locked reshuffles that conserve total balance.
|
||||||
|
*/
|
||||||
|
export declare function listLockedReservationMismatches(): Promise<Array<{
|
||||||
|
account_id: string;
|
||||||
|
user_id: string;
|
||||||
|
wallet_type_id: number;
|
||||||
|
available_balance: string;
|
||||||
|
locked_balance: string;
|
||||||
|
entries_sum: string;
|
||||||
|
locks_sum: string;
|
||||||
|
}>>;
|
||||||
/** Credits with missing or broken references to authorized source tables. */
|
/** Credits with missing or broken references to authorized source tables. */
|
||||||
export declare function listInvalidLedgerReferences(): Promise<Array<{
|
export declare function listInvalidLedgerReferences(): Promise<Array<{
|
||||||
account_id: string;
|
account_id: string;
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
{"version":3,"file":"ledgerIntegrityRepository.d.ts","sourceRoot":"","sources":["../../src/repositories/ledgerIntegrityRepository.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,iBAAiB,GAAG;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,IAAI,GAAG,IAAI,CAAC;IAC3B,WAAW,EAAE,IAAI,CAAC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gerMeta = getAccountLedgerMeta;
|
exports.getAccountLedgerMeta = getAccountLedgerMeta;
|
||||||
exports.getSnapshot = getSnapshot;
|
exports.getSnapshot = getSnapshot;
|
||||||
exports.upsertSnapshot = upsertSnapshot;
|
exports.upsertSnapshot = upsertSnapshot;
|
||||||
|
exports.listLockedReservationMismatches = listLockedReservationMismatches;
|
||||||
exports.listInvalidLedgerReferences = listInvalidLedgerReferences;
|
exports.listInvalidLedgerReferences = listInvalidLedgerReferences;
|
||||||
exports.checksumBalances = checksumBalances;
|
exports.checksumBalances = checksumBalances;
|
||||||
const crypto_1 = require("crypto");
|
const crypto_1 = require("crypto");
|
||||||
@ -42,6 +43,27 @@ async function upsertSnapshot(meta) {
|
|||||||
meta.last_entry_at,
|
meta.last_entry_at,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Main accounts whose locked_balance does not equal SUM(withdrawal_locks).
|
||||||
|
* Catches unauthorized available↔locked reshuffles that conserve total balance.
|
||||||
|
*/
|
||||||
|
async function listLockedReservationMismatches() {
|
||||||
|
return (0, connection_1.query)(`SELECT la.id AS account_id,
|
||||||
|
la.user_id,
|
||||||
|
la.wallet_type_id,
|
||||||
|
la.available_balance::text,
|
||||||
|
la.locked_balance::text,
|
||||||
|
COALESCE((SELECT SUM(le.amount) FROM ledger_entries le WHERE le.account_id = la.id), 0)::text AS entries_sum,
|
||||||
|
COALESCE(wl.locks_sum, 0)::text AS locks_sum
|
||||||
|
FROM ledger_accounts la
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT user_id, wallet_type_id, SUM(amount) AS locks_sum
|
||||||
|
FROM withdrawal_locks
|
||||||
|
GROUP BY user_id, wallet_type_id
|
||||||
|
) wl ON wl.user_id = la.user_id AND wl.wallet_type_id = la.wallet_type_id
|
||||||
|
WHERE la.account_kind = 'main'
|
||||||
|
AND la.locked_balance <> COALESCE(wl.locks_sum, 0)`);
|
||||||
|
}
|
||||||
/** Credits with missing or broken references to authorized source tables. */
|
/** Credits with missing or broken references to authorized source tables. */
|
||||||
async function listInvalidLedgerReferences() {
|
async function listInvalidLedgerReferences() {
|
||||||
return (0, connection_1.query)(`SELECT la.id AS account_id, la.user_id, le.id AS entry_id, le.type, le.amount::text,
|
return (0, connection_1.query)(`SELECT la.id AS account_id, la.user_id, le.id AS entry_id, le.type, le.amount::text,
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
{"version":3,"file":"ledgerIntegrityRepository.js","sourceRoot":"","sources":["../../src/repositories/ledgerIntegrityRepository.ts"],"names":[],"mappings":";;AAqBA,oDAeC;AAED,kCAQC;AAED,wCAkBC;AAGD,kEAmCC;AAED,4CAGC;AA7GD,mCAAoC;AACpC,iDAAyC;AAoBlC,KAAK,UAAU,oBAAoB,CAAC,SAAiB;IAC1D,MAAM,IAAI,GAAG,MAAM,IAAA,kBAAK,EACtB;;;;;;;;;oBASgB,EAChB,CAAC,SAAS,CAAC,CACZ,CAAC;IACF,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AACzB,CAAC;AAEM,KAAK,UAAU,WAAW,CAAC,SAAiB;IACjD,MAAM,IAAI,GAAG,MAAM,IAAA,kBAAK,EACtB;;2DAEuD,EACvD,CAAC,SAAS,CAAC,CACZ,CAAC;IACF,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AACzB,CAAC;AAEM,KAAK,UAAU,cAAc,CAAC,IAAuB;IAC1D,MAAM,IAAA,kBAAK,EACT;;;;;;;2BAOuB,EACvB;QACE,IAAI,CAAC,UAAU;QACf,IAAI,CAAC,iBAAiB;QACtB,IAAI,CAAC,cAAc;QACnB,IAAI,CAAC,WAAW;QAChB,IAAI,CAAC,aAAa;KACnB,CACF,CAAC;AACJ,CAAC;AAED,6EAA6E;AACtE,KAAK,UAAU,2BAA2B;IAG/C,OAAO,IAAA,kBAAK,EACV;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SA6BK,CACN,CAAC;AACJ,CAAC;AAED,SAAgB,gBAAgB,CAAC,IAAuB;IACtD,MAAM,OAAO,GAAG,GAAG,IAAI,CAAC,iBAAiB,IAAI,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;IAC3G,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC"}
|
{"version":3,"file":"ledgerIntegrityRepository.js","sourceRoot":"","sources":["../../src/repositories/ledgerIntegrityRepository.ts"],"names":[],"mappings":";;AAqBA,oDAeC;AAED,kCAQC;AAED,wCAkBC;AAMD,0EA4BC;AAGD,kEAmCC;AAED,4CAGC;AA/ID,mCAAoC;AACpC,iDAAyC;AAoBlC,KAAK,UAAU,oBAAoB,CAAC,SAAiB;IAC1D,MAAM,IAAI,GAAG,MAAM,IAAA,kBAAK,EACtB;;;;;;;;;oBASgB,EAChB,CAAC,SAAS,CAAC,CACZ,CAAC;IACF,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AACzB,CAAC;AAEM,KAAK,UAAU,WAAW,CAAC,SAAiB;IACjD,MAAM,IAAI,GAAG,MAAM,IAAA,kBAAK,EACtB;;2DAEuD,EACvD,CAAC,SAAS,CAAC,CACZ,CAAC;IACF,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AACzB,CAAC;AAEM,KAAK,UAAU,cAAc,CAAC,IAAuB;IAC1D,MAAM,IAAA,kBAAK,EACT;;;;;;;2BAOuB,EACvB;QACE,IAAI,CAAC,UAAU;QACf,IAAI,CAAC,iBAAiB;QACtB,IAAI,CAAC,cAAc;QACnB,IAAI,CAAC,WAAW;QAChB,IAAI,CAAC,aAAa;KACnB,CACF,CAAC;AACJ,CAAC;AAED;;;GAGG;AACI,KAAK,UAAU,+BAA+B;IAWnD,OAAO,IAAA,kBAAK,EACV;;;;;;;;;;;;;;0DAcsD,CACvD,CAAC;AACJ,CAAC;AAED,6EAA6E;AACtE,KAAK,UAAU,2BAA2B;IAG/C,OAAO,IAAA,kBAAK,EACV;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SA6BK,CACN,CAAC;AACJ,CAAC;AAED,SAAgB,gBAAgB,CAAC,IAAuB;IACtD,MAAM,OAAO,GAAG,GAAG,IAAI,CAAC,iBAAiB,IAAI,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;IAC3G,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC"}
|
||||||
9
backend/dist/services/securityService.d.ts
vendored
9
backend/dist/services/securityService.d.ts
vendored
@ -1,6 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Platform security: ledger integrity, alerts, withdrawal suspension, admin notifications.
|
* Platform security: ledger integrity, alerts, withdrawal suspension, admin notifications.
|
||||||
*/
|
*/
|
||||||
|
import * as ledgerIntegrityRepository from '../repositories/ledgerIntegrityRepository';
|
||||||
import type { SecurityAlert, SecurityAlertSeverity, SecurityAlertStatus } from '../models/security.model';
|
import type { SecurityAlert, SecurityAlertSeverity, SecurityAlertStatus } from '../models/security.model';
|
||||||
/** Ledger entry types that may credit or debit user balances. */
|
/** Ledger entry types that may credit or debit user balances. */
|
||||||
export declare const AUTHORIZED_LEDGER_TYPES: Set<string>;
|
export declare const AUTHORIZED_LEDGER_TYPES: Set<string>;
|
||||||
@ -31,6 +32,14 @@ export type IntegrityIssue = {
|
|||||||
entryId?: string;
|
entryId?: string;
|
||||||
amount?: string;
|
amount?: string;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* Detects balance inflation/deflation without a matching ledger entry change.
|
||||||
|
*
|
||||||
|
* Lock/unlock (withdrawal reserve / cancel) reshuffles available↔locked while keeping
|
||||||
|
* (available + locked) and entries_sum unchanged — that must NOT count as tamper.
|
||||||
|
* Real direct tamper changes the total while entries stay the same.
|
||||||
|
*/
|
||||||
|
export declare function isDirectBalanceTamper(snap: Pick<ledgerIntegrityRepository.LedgerSnapshotRow, 'available_balance' | 'locked_balance' | 'entries_sum' | 'last_entry_at'>, meta: Pick<ledgerIntegrityRepository.AccountLedgerMeta, 'available_balance' | 'locked_balance' | 'entries_sum' | 'last_entry_at'>): boolean;
|
||||||
export declare function runLedgerIntegrityCheck(): Promise<IntegrityIssue[]>;
|
export declare function runLedgerIntegrityCheck(): Promise<IntegrityIssue[]>;
|
||||||
export declare function getSecuritySettings(): Promise<{
|
export declare function getSecuritySettings(): Promise<{
|
||||||
alertEmails: string[];
|
alertEmails: string[];
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
{"version":3,"file":"securityService.d.ts","sourceRoot":"","sources":["../../src/services/securityService.ts"],"names":[],"mappings":"AAAA;;GAEG;AAcH,OAAO,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAE1G,iEAAiE;AACjE,eAAO,MAAM,uBAAuB,aAclC,CAAC;AAEH,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,OAAO,EAAE,MAAM;CAI5B;AAED,wBAAsB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAazE;AAED,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,QAAQ,GAAE,qBAA8B,GACvC,OAAO,CAAC,IAAI,CAAC,CAiBf;AAED,wBAAsB,UAAU,CAAC,KAAK,EAAE;IACtC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,QAAQ,EAAE,qBAAqB,CAAC;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,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AM,CAAC,EAAE,MAAM,CAAC;CACjB;;;;;;;;;;;;;;;;;GAsBA;AAED,wBAAgB,2BAA2B,IAAI,MAAM,CAEpD;AAED,qGAAqG;AACrG,wBAAsB,2BAA2B,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsBtF"}
|
||||||
34
backend/dist/services/securityService.js
vendored
34
backend/dist/services/securityService.js
vendored
@ -42,6 +42,7 @@ exports.suspendUserForSecurity = suspendUserForSecurity;
|
|||||||
exports.raiseAlert = raiseAlert;
|
exports.raiseAlert = raiseAlert;
|
||||||
exports.notifyAdminsOfAlert = notifyAdminsOfAlert;
|
exports.notifyAdminsOfAlert = notifyAdminsOfAlert;
|
||||||
exports.resolveAlert = resolveAlert;
|
exports.resolveAlert = resolveAlert;
|
||||||
|
exports.isDirectBalanceTamper = isDirectBalanceTamper;
|
||||||
exports.runLedgerIntegrityCheck = runLedgerIntegrityCheck;
|
exports.runLedgerIntegrityCheck = runLedgerIntegrityCheck;
|
||||||
exports.getSecuritySettings = getSecuritySettings;
|
exports.getSecuritySettings = getSecuritySettings;
|
||||||
exports.updateSecuritySettings = updateSecuritySettings;
|
exports.updateSecuritySettings = updateSecuritySettings;
|
||||||
@ -226,12 +227,20 @@ async function resolveAlert(alertId, adminUserId, status, resolutionNote, reacti
|
|||||||
void (0, securityRealtimeService_1.publishSecurityAdminEvent)({ type: 'security.audit.new', action: 'security_alert_resolved' });
|
void (0, securityRealtimeService_1.publishSecurityAdminEvent)({ type: 'security.audit.new', action: 'security_alert_resolved' });
|
||||||
return alert;
|
return alert;
|
||||||
}
|
}
|
||||||
function snapshotTampered(snap, meta) {
|
/**
|
||||||
const balanceChanged = snap.available_balance !== meta.available_balance ||
|
* Detects balance inflation/deflation without a matching ledger entry change.
|
||||||
snap.locked_balance !== meta.locked_balance;
|
*
|
||||||
|
* Lock/unlock (withdrawal reserve / cancel) reshuffles available↔locked while keeping
|
||||||
|
* (available + locked) and entries_sum unchanged — that must NOT count as tamper.
|
||||||
|
* Real direct tamper changes the total while entries stay the same.
|
||||||
|
*/
|
||||||
|
function isDirectBalanceTamper(snap, meta) {
|
||||||
|
const snapTotal = BigInt(snap.available_balance) + BigInt(snap.locked_balance);
|
||||||
|
const metaTotal = BigInt(meta.available_balance) + BigInt(meta.locked_balance);
|
||||||
|
const totalChanged = snapTotal !== metaTotal;
|
||||||
const entriesUnchanged = snap.entries_sum === meta.entries_sum &&
|
const entriesUnchanged = snap.entries_sum === meta.entries_sum &&
|
||||||
(snap.last_entry_at?.getTime() ?? 0) === (meta.last_entry_at?.getTime() ?? 0);
|
(snap.last_entry_at?.getTime() ?? 0) === (meta.last_entry_at?.getTime() ?? 0);
|
||||||
return balanceChanged && entriesUnchanged;
|
return totalChanged && entriesUnchanged;
|
||||||
}
|
}
|
||||||
async function runLedgerIntegrityCheck() {
|
async function runLedgerIntegrityCheck() {
|
||||||
const accounts = await ledgerRepository.listAllAccountsForIntegrity();
|
const accounts = await ledgerRepository.listAllAccountsForIntegrity();
|
||||||
@ -267,7 +276,7 @@ async function runLedgerIntegrityCheck() {
|
|||||||
const meta = await ledgerIntegrityRepository.getAccountLedgerMeta(account.id);
|
const meta = await ledgerIntegrityRepository.getAccountLedgerMeta(account.id);
|
||||||
if (meta) {
|
if (meta) {
|
||||||
const snap = await ledgerIntegrityRepository.getSnapshot(account.id);
|
const snap = await ledgerIntegrityRepository.getSnapshot(account.id);
|
||||||
if (snap && snapshotTampered(snap, meta)) {
|
if (snap && isDirectBalanceTamper(snap, meta)) {
|
||||||
issues.push({
|
issues.push({
|
||||||
userId: account.user_id,
|
userId: account.user_id,
|
||||||
walletTypeId: account.wallet_type_id,
|
walletTypeId: account.wallet_type_id,
|
||||||
@ -280,6 +289,19 @@ async function runLedgerIntegrityCheck() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const lockMismatches = await ledgerIntegrityRepository.listLockedReservationMismatches();
|
||||||
|
for (const row of lockMismatches) {
|
||||||
|
issues.push({
|
||||||
|
userId: row.user_id,
|
||||||
|
walletTypeId: row.wallet_type_id,
|
||||||
|
accountId: row.account_id,
|
||||||
|
available: row.available_balance,
|
||||||
|
locked: row.locked_balance,
|
||||||
|
entriesSum: row.entries_sum,
|
||||||
|
issue: 'locked_reservation_mismatch',
|
||||||
|
amount: row.locks_sum,
|
||||||
|
});
|
||||||
|
}
|
||||||
const invalidRefs = await ledgerIntegrityRepository.listInvalidLedgerReferences();
|
const invalidRefs = await ledgerIntegrityRepository.listInvalidLedgerReferences();
|
||||||
for (const ref of invalidRefs) {
|
for (const ref of invalidRefs) {
|
||||||
const account = accounts.find((a) => a.id === ref.account_id);
|
const account = accounts.find((a) => a.id === ref.account_id);
|
||||||
@ -311,7 +333,7 @@ async function runLedgerIntegrityCheck() {
|
|||||||
? 'unauthorized_ledger_entry'
|
? 'unauthorized_ledger_entry'
|
||||||
: issue.issue.startsWith('invalid_ledger_reference')
|
: issue.issue.startsWith('invalid_ledger_reference')
|
||||||
? 'orphan_ledger_credit'
|
? 'orphan_ledger_credit'
|
||||||
: issue.issue === 'direct_balance_tamper'
|
: issue.issue === 'direct_balance_tamper' || issue.issue === 'locked_reservation_mismatch'
|
||||||
? 'ledger_balance_tamper'
|
? 'ledger_balance_tamper'
|
||||||
: 'ledger_balance_mismatch';
|
: 'ledger_balance_mismatch';
|
||||||
await suspendUserForSecurity(issue.userId, `Ledger integrity failure (${issue.issue}) on wallet type ${issue.walletTypeId}. ` +
|
await suspendUserForSecurity(issue.userId, `Ledger integrity failure (${issue.issue}) on wallet type ${issue.walletTypeId}. ` +
|
||||||
|
|||||||
2
backend/dist/services/securityService.js.map
vendored
2
backend/dist/services/securityService.js.map
vendored
File diff suppressed because one or more lines are too long
@ -66,6 +66,40 @@ export async function upsertSnapshot(meta: AccountLedgerMeta): Promise<void> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main accounts whose locked_balance does not equal SUM(withdrawal_locks).
|
||||||
|
* Catches unauthorized available↔locked reshuffles that conserve total balance.
|
||||||
|
*/
|
||||||
|
export async function listLockedReservationMismatches(): Promise<
|
||||||
|
Array<{
|
||||||
|
account_id: string;
|
||||||
|
user_id: string;
|
||||||
|
wallet_type_id: number;
|
||||||
|
available_balance: string;
|
||||||
|
locked_balance: string;
|
||||||
|
entries_sum: string;
|
||||||
|
locks_sum: string;
|
||||||
|
}>
|
||||||
|
> {
|
||||||
|
return query(
|
||||||
|
`SELECT la.id AS account_id,
|
||||||
|
la.user_id,
|
||||||
|
la.wallet_type_id,
|
||||||
|
la.available_balance::text,
|
||||||
|
la.locked_balance::text,
|
||||||
|
COALESCE((SELECT SUM(le.amount) FROM ledger_entries le WHERE le.account_id = la.id), 0)::text AS entries_sum,
|
||||||
|
COALESCE(wl.locks_sum, 0)::text AS locks_sum
|
||||||
|
FROM ledger_accounts la
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT user_id, wallet_type_id, SUM(amount) AS locks_sum
|
||||||
|
FROM withdrawal_locks
|
||||||
|
GROUP BY user_id, wallet_type_id
|
||||||
|
) wl ON wl.user_id = la.user_id AND wl.wallet_type_id = la.wallet_type_id
|
||||||
|
WHERE la.account_kind = 'main'
|
||||||
|
AND la.locked_balance <> COALESCE(wl.locks_sum, 0)`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/** Credits with missing or broken references to authorized source tables. */
|
/** Credits with missing or broken references to authorized source tables. */
|
||||||
export async function listInvalidLedgerReferences(): Promise<
|
export async function listInvalidLedgerReferences(): Promise<
|
||||||
Array<{ account_id: string; user_id: string; entry_id: string; type: string; amount: string; issue: string }>
|
Array<{ account_id: string; user_id: string; entry_id: string; type: string; amount: string; issue: string }>
|
||||||
|
|||||||
@ -236,17 +236,30 @@ export type IntegrityIssue = {
|
|||||||
amount?: string;
|
amount?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function snapshotTampered(
|
/**
|
||||||
snap: ledgerIntegrityRepository.LedgerSnapshotRow,
|
* Detects balance inflation/deflation without a matching ledger entry change.
|
||||||
meta: ledgerIntegrityRepository.AccountLedgerMeta
|
*
|
||||||
|
* Lock/unlock (withdrawal reserve / cancel) reshuffles available↔locked while keeping
|
||||||
|
* (available + locked) and entries_sum unchanged — that must NOT count as tamper.
|
||||||
|
* Real direct tamper changes the total while entries stay the same.
|
||||||
|
*/
|
||||||
|
export function isDirectBalanceTamper(
|
||||||
|
snap: Pick<
|
||||||
|
ledgerIntegrityRepository.LedgerSnapshotRow,
|
||||||
|
'available_balance' | 'locked_balance' | 'entries_sum' | 'last_entry_at'
|
||||||
|
>,
|
||||||
|
meta: Pick<
|
||||||
|
ledgerIntegrityRepository.AccountLedgerMeta,
|
||||||
|
'available_balance' | 'locked_balance' | 'entries_sum' | 'last_entry_at'
|
||||||
|
>
|
||||||
): boolean {
|
): boolean {
|
||||||
const balanceChanged =
|
const snapTotal = BigInt(snap.available_balance) + BigInt(snap.locked_balance);
|
||||||
snap.available_balance !== meta.available_balance ||
|
const metaTotal = BigInt(meta.available_balance) + BigInt(meta.locked_balance);
|
||||||
snap.locked_balance !== meta.locked_balance;
|
const totalChanged = snapTotal !== metaTotal;
|
||||||
const entriesUnchanged =
|
const entriesUnchanged =
|
||||||
snap.entries_sum === meta.entries_sum &&
|
snap.entries_sum === meta.entries_sum &&
|
||||||
(snap.last_entry_at?.getTime() ?? 0) === (meta.last_entry_at?.getTime() ?? 0);
|
(snap.last_entry_at?.getTime() ?? 0) === (meta.last_entry_at?.getTime() ?? 0);
|
||||||
return balanceChanged && entriesUnchanged;
|
return totalChanged && entriesUnchanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function runLedgerIntegrityCheck(): Promise<IntegrityIssue[]> {
|
export async function runLedgerIntegrityCheck(): Promise<IntegrityIssue[]> {
|
||||||
@ -290,7 +303,7 @@ export async function runLedgerIntegrityCheck(): Promise<IntegrityIssue[]> {
|
|||||||
const meta = await ledgerIntegrityRepository.getAccountLedgerMeta(account.id);
|
const meta = await ledgerIntegrityRepository.getAccountLedgerMeta(account.id);
|
||||||
if (meta) {
|
if (meta) {
|
||||||
const snap = await ledgerIntegrityRepository.getSnapshot(account.id);
|
const snap = await ledgerIntegrityRepository.getSnapshot(account.id);
|
||||||
if (snap && snapshotTampered(snap, meta)) {
|
if (snap && isDirectBalanceTamper(snap, meta)) {
|
||||||
issues.push({
|
issues.push({
|
||||||
userId: account.user_id,
|
userId: account.user_id,
|
||||||
walletTypeId: account.wallet_type_id,
|
walletTypeId: account.wallet_type_id,
|
||||||
@ -304,6 +317,20 @@ export async function runLedgerIntegrityCheck(): Promise<IntegrityIssue[]> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const lockMismatches = await ledgerIntegrityRepository.listLockedReservationMismatches();
|
||||||
|
for (const row of lockMismatches) {
|
||||||
|
issues.push({
|
||||||
|
userId: row.user_id,
|
||||||
|
walletTypeId: row.wallet_type_id,
|
||||||
|
accountId: row.account_id,
|
||||||
|
available: row.available_balance,
|
||||||
|
locked: row.locked_balance,
|
||||||
|
entriesSum: row.entries_sum,
|
||||||
|
issue: 'locked_reservation_mismatch',
|
||||||
|
amount: row.locks_sum,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const invalidRefs = await ledgerIntegrityRepository.listInvalidLedgerReferences();
|
const invalidRefs = await ledgerIntegrityRepository.listInvalidLedgerReferences();
|
||||||
for (const ref of invalidRefs) {
|
for (const ref of invalidRefs) {
|
||||||
const account = accounts.find((a) => a.id === ref.account_id);
|
const account = accounts.find((a) => a.id === ref.account_id);
|
||||||
@ -336,7 +363,7 @@ export async function runLedgerIntegrityCheck(): Promise<IntegrityIssue[]> {
|
|||||||
? 'unauthorized_ledger_entry'
|
? 'unauthorized_ledger_entry'
|
||||||
: issue.issue.startsWith('invalid_ledger_reference')
|
: issue.issue.startsWith('invalid_ledger_reference')
|
||||||
? 'orphan_ledger_credit'
|
? 'orphan_ledger_credit'
|
||||||
: issue.issue === 'direct_balance_tamper'
|
: issue.issue === 'direct_balance_tamper' || issue.issue === 'locked_reservation_mismatch'
|
||||||
? 'ledger_balance_tamper'
|
? 'ledger_balance_tamper'
|
||||||
: 'ledger_balance_mismatch';
|
: 'ledger_balance_mismatch';
|
||||||
|
|
||||||
|
|||||||
63
backend/src/tests/ledgerIntegrityTamper.test.ts
Normal file
63
backend/src/tests/ledgerIntegrityTamper.test.ts
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { describe, test } from 'node:test';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
|
||||||
|
import { isDirectBalanceTamper } from '../services/securityService';
|
||||||
|
|
||||||
|
const t0 = new Date('2026-01-01T00:00:00.000Z');
|
||||||
|
|
||||||
|
describe('isDirectBalanceTamper', () => {
|
||||||
|
test('ignores available↔locked reshuffle when total and entries stay the same (withdrawal lock/cancel)', () => {
|
||||||
|
const snap = {
|
||||||
|
available_balance: '9840000',
|
||||||
|
locked_balance: '0',
|
||||||
|
entries_sum: '9840000',
|
||||||
|
last_entry_at: t0,
|
||||||
|
};
|
||||||
|
const afterLock = {
|
||||||
|
available_balance: '4840000',
|
||||||
|
locked_balance: '5000000',
|
||||||
|
entries_sum: '9840000',
|
||||||
|
last_entry_at: t0,
|
||||||
|
};
|
||||||
|
const afterCancel = {
|
||||||
|
available_balance: '9840000',
|
||||||
|
locked_balance: '0',
|
||||||
|
entries_sum: '9840000',
|
||||||
|
last_entry_at: t0,
|
||||||
|
};
|
||||||
|
assert.equal(isDirectBalanceTamper(snap, afterLock), false);
|
||||||
|
assert.equal(isDirectBalanceTamper(afterLock, afterCancel), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('flags total balance change without a new ledger entry', () => {
|
||||||
|
const snap = {
|
||||||
|
available_balance: '9840000',
|
||||||
|
locked_balance: '0',
|
||||||
|
entries_sum: '9840000',
|
||||||
|
last_entry_at: t0,
|
||||||
|
};
|
||||||
|
const inflated = {
|
||||||
|
available_balance: '19840000',
|
||||||
|
locked_balance: '0',
|
||||||
|
entries_sum: '9840000',
|
||||||
|
last_entry_at: t0,
|
||||||
|
};
|
||||||
|
assert.equal(isDirectBalanceTamper(snap, inflated), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('does not flag when entries_sum moves with the balance (normal credit/debit)', () => {
|
||||||
|
const snap = {
|
||||||
|
available_balance: '9840000',
|
||||||
|
locked_balance: '0',
|
||||||
|
entries_sum: '9840000',
|
||||||
|
last_entry_at: t0,
|
||||||
|
};
|
||||||
|
const afterDeposit = {
|
||||||
|
available_balance: '10840000',
|
||||||
|
locked_balance: '0',
|
||||||
|
entries_sum: '10840000',
|
||||||
|
last_entry_at: new Date('2026-01-01T01:00:00.000Z'),
|
||||||
|
};
|
||||||
|
assert.equal(isDirectBalanceTamper(snap, afterDeposit), false);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user