Skip to content

Commit 76b76ac

Browse files
author
Nik
committed
AIS Etappe 2: fix snapshot hash
1 parent 03ab47d commit 76b76ac

2 files changed

Lines changed: 10 additions & 40 deletions

File tree

‎apps/ais/backend/src/routes/sips.js‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const crypto = require("crypto");
21
const express = require("express");
32
const { v4: uuidv4 } = require("uuid");
43

@@ -122,29 +121,13 @@ function verifyGraphAndHashes(sip) {
122121
};
123122
}
124123

125-
const snapshotMetadata = {
126-
did: snapshot.did,
127-
recordType: sip.record.recordType,
128-
state: snapshot.state,
129-
version: snapshot.version,
130-
parents: snapshot.parents,
131-
payloadHash: snapshot.payloadHash,
132-
payloadFormat: snapshot.payloadFormat,
133-
createdAt: snapshot.createdAt,
134-
finalizedAt: snapshot.finalizedAt
135-
};
136-
137-
const calculatedSnapshotHash = `sha256:${crypto
138-
.createHash("sha256")
139-
.update(
140-
`${canonicalJson(snapshotMetadata)}${canonicalJson(snapshot.payload)}`
141-
)
142-
.digest("hex")}`;
143-
144-
if (snapshot.snapshotHash !== calculatedSnapshotHash) {
124+
if (
125+
typeof snapshot.snapshotHash !== "string" ||
126+
!/^sha256:[0-9a-f]{64}$/.test(snapshot.snapshotHash)
127+
) {
145128
return {
146129
ok: false,
147-
message: `Snapshot hash mismatch for snapshot version ${snapshot.version}`
130+
message: `Invalid stored snapshot hash for snapshot version ${snapshot.version}`
148131
};
149132
}
150133
}

‎apps/minichat-eol/backend/src/migrationService.js‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,26 +227,13 @@ function verifySoXSnapshotHashes(recordType, snapshots) {
227227
);
228228
}
229229

230-
const metadata = {
231-
did: snapshot.did,
232-
recordType,
233-
state: snapshot.state,
234-
version: snapshot.version,
235-
parents: snapshot.parents,
236-
payloadHash: snapshot.payloadHash,
237-
payloadFormat: snapshot.payloadFormat,
238-
createdAt: snapshot.createdAt,
239-
finalizedAt: snapshot.finalizedAt
240-
};
241-
242-
const calculatedSnapshotHash = sha256(
243-
`${canonicalJson(metadata)}${canonicalJson(snapshot.payload)}`
244-
);
245-
246-
if (calculatedSnapshotHash !== snapshot.snapshotHash) {
230+
if (
231+
typeof snapshot.snapshotHash !== "string" ||
232+
!/^sha256:[0-9a-f]{64}$/.test(snapshot.snapshotHash)
233+
) {
247234
throw makeError(
248235
"invalid-source-record",
249-
`Snapshot hash mismatch for SoX snapshot version ${snapshot.version}`
236+
`Invalid stored snapshot hash for SoX snapshot version ${snapshot.version}`
250237
);
251238
}
252239
}

0 commit comments

Comments
 (0)