Skip to content

Commit

Permalink
feat(validation): use regex to sanity check block hashes
Browse files Browse the repository at this point in the history
The format sanity check functions are supposed to be lightweight and
performant and should therefore not exhaustively check the format of
transactions and blocks. However, we recently encountered an issue (in a
branch) where invalid blocks were being retrieved from a cache. A regex
check on any of the base64 encoded fields would have caught it. To
address that, this commit adds regex check on the block 'indep_hash' and
'previous_block' fields.
  • Loading branch information
djwhitt committed Nov 15, 2023
1 parent 1538561 commit a8b639b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/lib/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,37 @@ import {
PartialJsonTransaction,
} from '../types.js';

export const isValidTx = (id: string): boolean => {
export const isValidBlockIndepHash = (id: string): boolean => {
return !!id.match(/^[a-zA-Z0-9_-]{64}$/);
};

export const isValidTxId = (id: string): boolean => {
return !!id.match(/^[a-zA-Z0-9_-]{43}$/);
};

export const isValidDataId = isValidTx;
export const isValidDataId = isValidTxId;

export function sanityCheckBlock(block: PartialJsonBlock) {
if (!block.indep_hash) {
throw new Error("Invalid block: missing 'indep_hash'");
}

if (!isValidBlockIndepHash(block.indep_hash)) {
throw new Error("Invalid block: invalid 'indep_hash' format");
}

if (!block.height === undefined) {
throw new Error("Invalid block: missing 'height'");
}

if (typeof block.height !== 'number') {
throw new Error("Invalid block: 'height' must be a number");
}

if (
block.height !== 0 &&
(typeof block.previous_block !== 'string' || block.previous_block === '')
(typeof block.previous_block !== 'string' ||
!isValidBlockIndepHash(block.previous_block))
) {
throw new Error("Invalid block: missing or invalid 'previous_block'");
}
Expand All @@ -53,7 +66,7 @@ export function sanityCheckTx(tx: PartialJsonTransaction) {
throw new Error("Invalid transaction: missing 'id'");
}

if (!isValidTx(tx.id)) {
if (!isValidTxId(tx.id)) {
throw new Error("Invalid transaction: invalid 'id' format");
}
}
Expand Down

0 comments on commit a8b639b

Please sign in to comment.