Skip to content

Commit 9d27ddf

Browse files
committed
fix: backport fix for GHSA-w5hq-g745-h8pq
1 parent 24c1238 commit 9d27ddf

5 files changed

Lines changed: 29 additions & 3 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/v35.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ describe('v35', () => {
158158
assert.throws(() => v3('hello.example.com', null, new Uint8Array(16)));
159159
});
160160

161+
test('v3 throws RangeError for out-of-range indexes', () => {
162+
const buf15 = new Uint8Array(15);
163+
const buf30 = new Uint8Array(30);
164+
165+
assert.throws(() => v3('hello.example.com', v3.DNS, buf15), RangeError);
166+
assert.throws(() => v3('hello.example.com', v3.DNS, buf30, -1), RangeError);
167+
assert.throws(() => v3('hello.example.com', v3.DNS, buf30, 15), RangeError);
168+
});
169+
161170
test('v5', () => {
162171
// Expect to get the same results as http://tools.adjet.org/uuid-v5
163172
assert.strictEqual(v5('hello.example.com', v5.DNS), 'fdda765f-fc57-5604-a269-52a7df8164ec');

src/test/v6.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,13 @@ describe('v6', () => {
8787
const id = v6ToV1(V6_ID);
8888
assert.equal(id, V1_ID);
8989
});
90+
91+
test('throws RangeError for out-of-range indexes', () => {
92+
const buf15 = new Uint8Array(15);
93+
const buf30 = new Uint8Array(30);
94+
95+
assert.throws(() => v6({}, buf15), RangeError);
96+
assert.throws(() => v6({}, buf30, -1), RangeError);
97+
assert.throws(() => v6({}, buf30, 15), RangeError);
98+
});
9099
});

src/v35.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { UUIDTypes } from './types.js';
21
import parse from './parse.js';
32
import { unsafeStringify } from './stringify.js';
3+
import { UUIDTypes } from './types.js';
44

55
export function stringToBytes(str: string) {
66
// TODO: Use TextEncoder (see https://stackoverflow.com/a/48762658/109538)
@@ -53,6 +53,10 @@ export default function v35<TBuf extends Uint8Array = Uint8Array>(
5353
if (buf) {
5454
offset = offset || 0;
5555

56+
if (offset < 0 || offset + 16 > buf.length) {
57+
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
58+
}
59+
5660
for (let i = 0; i < 16; ++i) {
5761
buf[offset + i] = bytes[i];
5862
}

src/v6.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { UUIDTypes, Version6Options } from './types.js';
21
import { unsafeStringify } from './stringify.js';
2+
import { UUIDTypes, Version6Options } from './types.js';
33
import v1 from './v1.js';
44
import v1ToV6 from './v1ToV6.js';
55

@@ -27,6 +27,10 @@ function v6<TBuf extends Uint8Array = Uint8Array>(
2727

2828
// Return as a byte array if requested
2929
if (buf) {
30+
if (offset < 0 || offset + 16 > buf.length) {
31+
throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
32+
}
33+
3034
for (let i = 0; i < 16; i++) {
3135
buf[offset + i] = bytes[i];
3236
}

0 commit comments

Comments
 (0)