diff --git a/src/core/helpers.ts b/src/core/helpers.ts index 99496c5..54004b5 100644 --- a/src/core/helpers.ts +++ b/src/core/helpers.ts @@ -72,7 +72,7 @@ export function assertUint8Array( value: unknown, path: string ): asserts value is Uint8Array { - if (!(value instanceof Uint8Array)) { + if (!isUint8Array(value)) { throw new XdrError(`${path}: expected Uint8Array`); } // Check if the Uint8Array is detached or out of bounds by attempting to access an element. diff --git a/test/unit/opaque.test.ts b/test/unit/opaque.test.ts index f568c13..388a757 100644 --- a/test/unit/opaque.test.ts +++ b/test/unit/opaque.test.ts @@ -1,3 +1,4 @@ +import { runInNewContext } from 'node:vm'; import { describe, it, expect } from 'vitest'; import { opaque } from '../../src/index.js'; import { bytes, toArray, encodeInvalid } from './_helpers.js'; @@ -27,6 +28,12 @@ describe('opaque (fixed-length)', () => { /expected Uint8Array/i ); }); + + it('accepts a Uint8Array from another realm', () => { + const foreign = runInNewContext('new Uint8Array([1, 2, 3, 4])'); + expect(toArray(opaque(4).encode(foreign))).toEqual([1, 2, 3, 4]); + expect(opaque(4).validate(foreign)).toBe(true); + }); }); describe('decode', () => { diff --git a/test/unit/string.test.ts b/test/unit/string.test.ts index a9e3a6f..74d9e00 100644 --- a/test/unit/string.test.ts +++ b/test/unit/string.test.ts @@ -1,3 +1,4 @@ +import { runInNewContext } from 'node:vm'; import { describe, it, expect } from 'vitest'; import { string } from '../../src/index.js'; import { bytes, toArray, roundTrip, encodeInvalid } from './_helpers.js'; @@ -32,6 +33,14 @@ describe('string', () => { expect(() => encodeInvalid(schema, 'Hi')).toThrow(/expected Uint8Array/i); expect(() => encodeInvalid(schema, 123)).toThrow(/expected Uint8Array/i); }); + + it('accepts a Uint8Array from another realm', () => { + const foreign = runInNewContext('new Uint8Array([104, 105])'); + expect(toArray(schema.encode(foreign))).toEqual([ + 0, 0, 0, 2, 104, 105, 0, 0 + ]); + expect(schema.validate(foreign)).toBe(true); + }); }); describe('decode', () => { diff --git a/test/unit/struct.test.ts b/test/unit/struct.test.ts index b4c6995..db22cc5 100644 --- a/test/unit/struct.test.ts +++ b/test/unit/struct.test.ts @@ -1,5 +1,6 @@ +import { runInNewContext } from 'node:vm'; import { describe, it, expect } from 'vitest'; -import { struct, int32, bool } from '../../src/index.js'; +import { struct, int32, bool, varOpaque } from '../../src/index.js'; import { bytes, toArray, roundTrip, encodeInvalid } from './_helpers.js'; const Range = struct('Range', { @@ -61,6 +62,15 @@ describe('struct', () => { Range.encode({ begin: 2147483648, end: 0, inclusive: false }) ).toThrow(/range/i); }); + + it('accepts a byte field holding a Uint8Array from another realm', () => { + const Blob = struct('Blob', { data: varOpaque(10) }); + const foreign = runInNewContext('new Uint8Array([1, 2, 3])'); + expect(toArray(Blob.encode({ data: foreign }))).toEqual([ + 0, 0, 0, 3, 1, 2, 3, 0 + ]); + expect(Blob.validate({ data: foreign })).toBe(true); + }); }); describe('decode', () => { diff --git a/test/unit/var-opaque.test.ts b/test/unit/var-opaque.test.ts index 40b3645..4044cac 100644 --- a/test/unit/var-opaque.test.ts +++ b/test/unit/var-opaque.test.ts @@ -1,3 +1,4 @@ +import { runInNewContext } from 'node:vm'; import { describe, it, expect } from 'vitest'; import { varOpaque } from '../../src/index.js'; import { bytes, toArray, encodeInvalid } from './_helpers.js'; @@ -30,6 +31,25 @@ describe('varOpaque (variable-length)', () => { /expected Uint8Array/i ); }); + + it('accepts a Uint8Array from another realm', () => { + const foreign = runInNewContext('new Uint8Array([1, 2, 3])'); + expect(toArray(schema.encode(foreign))).toEqual([0, 0, 0, 3, 1, 2, 3, 0]); + expect(schema.validate(foreign)).toBe(true); + }); + + it('rejects values that only look like a Uint8Array', () => { + const spoof = { [Symbol.toStringTag]: 'Uint8Array', length: 0 }; + expect(() => encodeInvalid(schema, spoof)).toThrow( + /expected Uint8Array/i + ); + expect(() => encodeInvalid(schema, new Proxy(bytes([1]), {}))).toThrow( + /expected Uint8Array/i + ); + expect(() => + encodeInvalid(schema, runInNewContext('new Uint16Array(1)')) + ).toThrow(/expected Uint8Array/i); + }); }); describe('decode', () => {