Skip to content

tlock: harden hex helpers and add seal/open input validation - #196

Closed
karagozemin wants to merge 1 commit into
mainfrom
tlock/hex-seal-validation
Closed

tlock: harden hex helpers and add seal/open input validation#196
karagozemin wants to merge 1 commit into
mainfrom
tlock/hex-seal-validation

Conversation

@karagozemin

Copy link
Copy Markdown
Collaborator

Closes #191

- fromHex now rejects non-hex characters instead of producing NaN bytes
- add isValidHex helper and re-export it
- sealBid guards against a non-positive round and wrong-length nonce
- openBid guards against an empty ciphertext
- add unit tests for all of the above
Copilot AI lite review requested due to automatic review settings August 25, 2026 20:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens tlock input handling by adding strict hex validation helpers and by introducing early guard clauses in bid sealing/opening paths, aligning behavior with Issue #191’s goal of failing loudly on malformed inputs.

Changes:

  • Added isValidHex and tightened fromHex validation to reject non-hex characters.
  • Added input guard clauses to sealBid (round/nonce) and openBid (empty ciphertext).
  • Added unit tests covering the new validation behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/tlock/src/seal.ts Adds guard clauses for round, nonce length, and empty ciphertext before crypto operations.
packages/tlock/src/seal.test.ts Adds tests ensuring new sealBid/openBid validation rejects invalid inputs.
packages/tlock/src/index.ts Re-exports isValidHex from the package entry point.
packages/tlock/src/commitment.ts Introduces isValidHex and adds strict character validation to fromHex.
packages/tlock/src/commitment.test.ts Adds tests for isValidHex and fromHex validation behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +45 to +47
if (round < 1) {
throw new RangeError(`round must be a positive integer, got ${round}`);
}
Comment on lines 88 to +93
export function fromHex(hex: string): Uint8Array {
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
if (clean.length % 2 !== 0) throw new Error("odd hex length");
if (!HEX_CHARS_RE.test(clean)) {
throw new Error("invalid hex string: expected only [0-9a-fA-F] characters");
}
Comment on lines +60 to +63
await assert.rejects(
() => sealBid({ value: 1n, nonce: new Uint8Array(16), round: 5, client }),
/nonce must be 32 bytes/,
);
Comment on lines +71 to +75
test("fromHex rejects non-hex characters and odd length", () => {
assert.throws(() => fromHex("zz"), /invalid hex/);
assert.throws(() => fromHex("12g4"), /invalid hex/);
assert.throws(() => fromHex("abc"), /odd hex length/);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tlock: harden hex helpers and add seal/open input validation

2 participants