Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harden uint8arrayFromHexString #30

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions auth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ <h2>Message log</h2>
* @returns {Uint8Array}
*/
var uint8arrayFromHexString = function(hexString) {
var hexRegex = /^[0-9A-Fa-f]+$/;
if (!hexString || hexString.length % 2 != 0 || !hexRegex.test(hexString)) {
throw new Error('cannot create uint8array from invalid hex string: "' + hexString + '"');
}
return new Uint8Array(hexString.match(/../g).map(h=>parseInt(h,16)));
}

Expand Down
17 changes: 17 additions & 0 deletions auth/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ describe("TKHQ", () => {

it("contains uint8arrayFromHexString", () => {
expect(TKHQ.uint8arrayFromHexString("627566666572").toString()).toEqual("98,117,102,102,101,114");

// Error case: bad value
expect(() => {
TKHQ.uint8arrayFromHexString({});
}).toThrow('cannot create uint8array from invalid hex string: "[object Object]"');
// Error case: empty string
expect(() => {
TKHQ.uint8arrayFromHexString("");
}).toThrow('cannot create uint8array from invalid hex string: ""');
// Error case: odd number of characters
expect(() => {
TKHQ.uint8arrayFromHexString("123");
}).toThrow('cannot create uint8array from invalid hex string: "123"');
// Error case: bad characters outside of hex range
expect(() => {
TKHQ.uint8arrayFromHexString("oops");
}).toThrow('cannot create uint8array from invalid hex string: "oops"');
})

it("contains bigIntToHex", () => {
Expand Down
9 changes: 7 additions & 2 deletions export/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,13 @@ <h2>Message log</h2>
* @param {string} hexString
* @returns {Uint8Array}
*/
const uint8arrayFromHexString = hexString =>
new Uint8Array(hexString.match(/../g).map(h=>parseInt(h,16)));
const uint8arrayFromHexString = function(hexString) {
var hexRegex = /^[0-9A-Fa-f]+$/;
if (!hexString || hexString.length % 2 != 0 || !hexRegex.test(hexString)) {
throw new Error('cannot create uint8array from invalid hex string: "' + hexString + '"');
}
return new Uint8Array(hexString.match(/../g).map(h=>parseInt(h,16)));
}

/**
* Takes a Uint8Array and returns a hex string
Expand Down
17 changes: 17 additions & 0 deletions export/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ describe("TKHQ", () => {

it("contains uint8arrayFromHexString", () => {
expect(TKHQ.uint8arrayFromHexString("627566666572").toString()).toEqual("98,117,102,102,101,114");

// Error case: bad value
expect(() => {
TKHQ.uint8arrayFromHexString({});
}).toThrow('cannot create uint8array from invalid hex string: "[object Object]"');
// Error case: empty string
expect(() => {
TKHQ.uint8arrayFromHexString("");
}).toThrow('cannot create uint8array from invalid hex string: ""');
// Error case: odd number of characters
expect(() => {
TKHQ.uint8arrayFromHexString("123");
}).toThrow('cannot create uint8array from invalid hex string: "123"');
// Error case: bad characters outside of hex range
expect(() => {
TKHQ.uint8arrayFromHexString("oops");
}).toThrow('cannot create uint8array from invalid hex string: "oops"');
})

it("logs messages and sends messages up", async () => {
Expand Down
Loading