Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+48
-43
@@ -3,9 +3,12 @@
|
||||
* import them without dragging in the route module's server-only deps
|
||||
* (PrismaClient, getDbClient, session secret env vars).
|
||||
*/
|
||||
import { computeAuditHash } from "@hivegrid/db/audit-hash";
|
||||
import { verifyAuditChain } from "@hivegrid/db/audit-hash";
|
||||
import { auditChainKey, type AuditVerificationContext } from "@hivegrid/db/audit-chain-checkpoint.server";
|
||||
import { neutralizeSpreadsheetFormula } from "@hivegrid/foundation/spreadsheet-safety";
|
||||
|
||||
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
const FILTER_ALL = "__all__";
|
||||
|
||||
export const AUDIT_EXPORT_MAX_ROWS = 50_000;
|
||||
|
||||
@@ -69,6 +72,16 @@ export type AuditIntegritySummary = {
|
||||
reason: string | null;
|
||||
};
|
||||
|
||||
export function stripAuditExportFilterSentinels(url: URL): URL {
|
||||
const cleaned = new URL(url.toString());
|
||||
for (const key of Array.from(cleaned.searchParams.keys())) {
|
||||
const values = cleaned.searchParams.getAll(key).filter((value) => value.trim() !== FILTER_ALL);
|
||||
cleaned.searchParams.delete(key);
|
||||
for (const value of values) cleaned.searchParams.append(key, value);
|
||||
}
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
export function parseAuditExportFilters(url: URL): AuditExportFilters {
|
||||
return {
|
||||
from: parseIsoDate(url.searchParams.get("from")),
|
||||
@@ -105,7 +118,19 @@ export function buildAuditExportWhere(filters: AuditExportFilters): Record<strin
|
||||
return where;
|
||||
}
|
||||
|
||||
export function computeAuditIntegritySummary(rows: ReadonlyArray<AuditExportRow>): AuditIntegritySummary {
|
||||
export function hasAuditExportPrefixFilters(filters: AuditExportFilters): boolean {
|
||||
return Boolean(
|
||||
filters.from
|
||||
|| filters.actorType
|
||||
|| filters.outcome
|
||||
|| filters.regulatoryScope.length > 0,
|
||||
);
|
||||
}
|
||||
|
||||
export function computeAuditIntegritySummary(
|
||||
rows: ReadonlyArray<AuditExportRow>,
|
||||
context?: AuditVerificationContext,
|
||||
): AuditIntegritySummary {
|
||||
const chains = new Map<string, AuditExportRow[]>();
|
||||
for (const row of rows) {
|
||||
const key = row.tenantId ?? "__platform__";
|
||||
@@ -114,44 +139,24 @@ export function computeAuditIntegritySummary(rows: ReadonlyArray<AuditExportRow>
|
||||
chains.set(key, list);
|
||||
}
|
||||
let inspected = 0;
|
||||
for (const [, list] of chains) {
|
||||
list.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
|
||||
let lastHash: string | null = null;
|
||||
for (const row of list) {
|
||||
if (row.eventHash === null) {
|
||||
lastHash = null;
|
||||
continue;
|
||||
}
|
||||
inspected += 1;
|
||||
if (row.previousHash !== lastHash) {
|
||||
return { ok: false, inspected, firstBreakId: row.id, reason: "previous-hash-mismatch" };
|
||||
}
|
||||
const recomputed = computeAuditHash({
|
||||
previousHash: row.previousHash,
|
||||
payload: {
|
||||
id: "",
|
||||
createdAt: "",
|
||||
tenantId: row.tenantId,
|
||||
actorType: row.actorType,
|
||||
actorId: row.actorId,
|
||||
actorEmail: row.actorEmail,
|
||||
actorIp: row.actorIp,
|
||||
actorUserAgent: row.actorUserAgent,
|
||||
action: row.action,
|
||||
targetType: row.targetType,
|
||||
targetId: row.targetId,
|
||||
requestId: row.requestId,
|
||||
sourceModule: row.sourceModule,
|
||||
outcome: row.outcome,
|
||||
dataSensitivity: row.dataSensitivity,
|
||||
regulatoryScope: row.regulatoryScope,
|
||||
metadata: row.metadata,
|
||||
},
|
||||
});
|
||||
if (recomputed !== row.eventHash) {
|
||||
return { ok: false, inspected, firstBreakId: row.id, reason: "hash-mismatch" };
|
||||
}
|
||||
lastHash = row.eventHash;
|
||||
for (const [key, list] of chains) {
|
||||
list.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id));
|
||||
const chainKey = auditChainKey(key === "__platform__" ? null : key);
|
||||
if (context?.invalidCheckpointChains.has(chainKey)) {
|
||||
return { ok: false, inspected, firstBreakId: list[0]?.id ?? null, reason: "checkpoint-authentication-failed" };
|
||||
}
|
||||
const result = verifyAuditChain(
|
||||
list.map((row) => ({ ...row, createdAt: row.createdAt.toISOString() })),
|
||||
{ initialPreviousHash: context?.initialPreviousHashes.get(chainKey) ?? null },
|
||||
);
|
||||
inspected += result.inspected;
|
||||
if (!result.ok) {
|
||||
return {
|
||||
ok: false,
|
||||
inspected,
|
||||
firstBreakId: result.firstBreak?.eventId ?? null,
|
||||
reason: result.firstBreak?.reason ?? "unknown",
|
||||
};
|
||||
}
|
||||
}
|
||||
return { ok: true, inspected, firstBreakId: null, reason: null };
|
||||
@@ -196,7 +201,7 @@ export function csvCell(value: unknown): string {
|
||||
if (Array.isArray(value)) return csvCell(value.join("|"));
|
||||
if (value instanceof Date) return value.toISOString();
|
||||
if (typeof value === "object") return csvCell(JSON.stringify(value));
|
||||
const s = String(value);
|
||||
if (/[",\n\r]/.test(s)) return `"${s.replace(/"/g, '""')}"`;
|
||||
return s;
|
||||
const safeValue = neutralizeSpreadsheetFormula(String(value));
|
||||
if (/[",\n\r]/.test(safeValue)) return `"${safeValue.replace(/"/g, '""')}"`;
|
||||
return safeValue;
|
||||
}
|
||||
Reference in new issue
Block a user