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

fix: Test for invalid byte array sizes and ranges in v1(), v4(), and v7() #845

Merged
merged 5 commits into from
Jan 5, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: throw for array size and range errors in v1, v4, and v7
broofa committed Dec 24, 2024
commit 7f840155b9ed119ad0fd9a0b2d40a82c9f18c694
24 changes: 24 additions & 0 deletions src/test/v1.test.ts
Original file line number Diff line number Diff line change
@@ -168,4 +168,28 @@ describe('v1', () => {
assert.deepStrictEqual(updateV1State(state, now, RFC_RANDOM), expected, `Failed: ${title}`);
}
});

test('throws when option.random is too short', () => {
const random = Uint8Array.of(16);
const buffer = new Uint8Array(16).fill(0);
assert.throws(() => {
v1({ random }, buffer);
});
});

test('throws when options.rng() is too short', () => {
const buffer = new Uint8Array(16);
const rng = () => Uint8Array.of(0); // length = 1
assert.throws(() => {
v1({ rng }, buffer);
});
});

test('throws RangeError for out-of-range indexes', () => {
const buf15 = new Uint8Array(15);
const buf30 = new Uint8Array(30);
assert.throws(() => v1({}, buf15));
assert.throws(() => v1({}, buf30, -1));
assert.throws(() => v1({}, buf30, 15));
});
});
40 changes: 22 additions & 18 deletions src/test/v4.test.ts
Original file line number Diff line number Diff line change
@@ -2,9 +2,6 @@ import * as assert from 'assert';
import test, { describe } from 'node:test';
import native from '../native.js';
import v4 from '../v4.js';
import parse from '../parse.js';
import version from '../version.js';
import { unsafeStringify } from '../stringify.js';

const randomBytesFixture = Uint8Array.of(
0x10,
@@ -24,7 +21,7 @@ const randomBytesFixture = Uint8Array.of(
0x58,
0x36
);
const randomBytesFixtureLess = Uint8Array.of(16);

const expectedBytes = Uint8Array.of(
16,
145,
@@ -117,21 +114,28 @@ describe('v4', () => {

assert.deepEqual(buffer, expectedBuf);
});
test('fills three UUID into a buffer as expected', () => {

test('throws when option.random is too short', () => {
const random = Uint8Array.of(16);
const buffer = new Uint8Array(16).fill(0);
assert.throws(() => {
v4({ random }, buffer);
});
});

test('throws when options.rng() is too short', () => {
const buffer = new Uint8Array(16);
const resultBuffer = v4({ random: randomBytesFixtureLess }, buffer);
const id = unsafeStringify(resultBuffer);
const v = version(id);
parse(id);
assert.deepEqual(buffer[0], expectedBytes[0]);
assert.strictEqual(buffer[0], resultBuffer[0]);
assert.strictEqual(v, 4);
const rng = () => Uint8Array.of(0); // length = 1
assert.throws(() => {
v4({ rng }, buffer);
});
});
test('v4 UUID generation with insufficient random bytes should match expected bytes', () => {
const id = v4({ random: randomBytesFixtureLess });
const v = version(id);
const buffer = parse(id);
assert.deepEqual(buffer[0], expectedBytes[0]);
assert.strictEqual(v, 4);

test('throws RangeError for out-of-range indexes', () => {
const buf15 = new Uint8Array(15);
const buf30 = new Uint8Array(30);
assert.throws(() => v4({}, buf15));
assert.throws(() => v4({}, buf30, -1));
assert.throws(() => v4({}, buf30, 15));
});
});
26 changes: 25 additions & 1 deletion src/test/v7.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as assert from 'assert';
import test, { describe } from 'node:test';
import { Version7Options } from '../types.js';
import parse from '../parse.js';
import stringify from '../stringify.js';
import { Version7Options } from '../types.js';
import v7, { updateV7State } from '../v7.js';

// Fixture values for testing with the rfc v7 UUID example:
@@ -267,4 +267,28 @@ describe('v7', () => {
assert.notStrictEqual(stringify(buf), id);
}
});

test('throws when option.random is too short', () => {
const random = Uint8Array.of(16);
const buffer = new Uint8Array(16).fill(0);
assert.throws(() => {
v7({ random }, buffer);
});
});

test('throws when options.rng() is too short', () => {
const buffer = new Uint8Array(16);
const rng = () => Uint8Array.of(0); // length = 1
assert.throws(() => {
v7({ rng }, buffer);
});
});

test('throws RangeError for out-of-range indexes', () => {
const buf15 = new Uint8Array(15);
const buf30 = new Uint8Array(30);
assert.throws(() => v7({}, buf15));
assert.throws(() => v7({}, buf30, -1));
assert.throws(() => v7({}, buf30, 15));
});
});
11 changes: 10 additions & 1 deletion src/v1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UUIDTypes, Version1Options } from './types.js';
import rng from './rng.js';
import { unsafeStringify } from './stringify.js';
import { UUIDTypes, Version1Options } from './types.js';

// **`v1()` - Generate time-based UUID**
//
@@ -139,11 +139,20 @@ function v1Bytes(
buf?: Uint8Array,
offset = 0
) {
if (rnds.length < 16) {
throw new Error('Random bytes length must be >= 16');
}

// Defaults
if (!buf) {
buf = new Uint8Array(16);
offset = 0;
} else {
if (offset < 0 || offset + 16 > buf.length) {
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
}
}

msecs ??= Date.now();
nsecs ??= 0;
clockseq ??= ((rnds[8] << 8) | rnds[9]) & 0x3fff;
18 changes: 6 additions & 12 deletions src/v4.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UUIDTypes, Version4Options } from './types.js';
import native from './native.js';
import rng from './rng.js';
import { unsafeStringify } from './stringify.js';
import { UUIDTypes, Version4Options } from './types.js';

function v4(options?: Version4Options, buf?: undefined, offset?: number): string;
function v4(options: Version4Options | undefined, buf: Uint8Array, offset?: number): Uint8Array;
@@ -12,15 +12,9 @@ function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): UUIDT

options = options || {};

let rnds = options.random || (options.rng || rng)();
const rnds = options.random ?? options.rng?.() ?? rng();
if (rnds.length < 16) {
const newRnds = rng()
for (let i = 0; i < 16; i++) {
if (rnds[i]) {
newRnds[i] = rnds[i]
}
}
rnds = newRnds;
throw new Error('Random bytes length must be >= 16');
}

// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
@@ -29,10 +23,10 @@ function v4(options?: Version4Options, buf?: Uint8Array, offset?: number): UUIDT

// Copy bytes to buffer, if provided
if (buf) {
if (buf.length < 16) {
throw new Error('The buffer length must not be less than 16');
}
offset = offset || 0;
if (offset < 0 || offset + 16 > buf.length) {
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
}

for (let i = 0; i < 16; ++i) {
buf[offset + i] = rnds[i];
10 changes: 9 additions & 1 deletion src/v7.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UUIDTypes, Version7Options } from './types.js';
import rng from './rng.js';
import { unsafeStringify } from './stringify.js';
import { UUIDTypes, Version7Options } from './types.js';

type V7State = {
msecs?: number; // time, milliseconds
@@ -62,9 +62,17 @@ export function updateV7State(state: V7State, now: number, rnds: Uint8Array) {
}

function v7Bytes(rnds: Uint8Array, msecs?: number, seq?: number, buf?: Uint8Array, offset = 0) {
if (rnds.length < 16) {
throw new Error('Random bytes length must be >= 16');
}

if (!buf) {
buf = new Uint8Array(16);
offset = 0;
} else {
if (offset < 0 || offset + 16 > buf.length) {
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
}
}

// Defaults