Repository navigation
fix: preserve bitlist input when reusing bytes #560
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import {describe, expect, it, vi} from "vitest"; | ||
| import { | ||
| BitArray, | ||
| BitListType, | ||
| BitVectorType, | ||
| ByteListType, | ||
| ContainerType, | ||
| ProgressiveBitListType, | ||
| deserializeUint8ArrayBitListFromBytes, | ||
| } from "../../src/index.ts"; | ||
|
|
||
| for (const type of [new BitListType(16), new ProgressiveBitListType({limit: 16})]) { | ||
| for (const inputKind of ["Uint8Array", "Buffer"] as const) { | ||
| const createInput = (bytes: number[]): Uint8Array => | ||
| inputKind === "Buffer" ? Buffer.from(bytes) : Uint8Array.from(bytes); | ||
|
|
||
| describe(`${type.typeName} reuseBytes with ${inputKind}`, () => { | ||
| it("preserves the serialized input when removing the padding bit", () => { | ||
| const bytes = createInput([0x00, 0x03]); | ||
| const value = type.deserialize(bytes, {reuseBytes: true}); | ||
|
|
||
| expect(value.bitLen).to.equal(9); | ||
| expect(Array.from(value.uint8Array)).to.deep.equal([0x00, 0x01]); | ||
| expect(Array.from(bytes)).to.deep.equal([0x00, 0x03]); | ||
| }); | ||
|
|
||
| it("returns the same bit length when decoding the input twice", () => { | ||
| const bytes = createInput([0x00, 0x03]); | ||
| const first = type.deserialize(bytes, {reuseBytes: true}); | ||
| const second = type.deserialize(bytes, {reuseBytes: true}); | ||
|
|
||
| expect(first.bitLen).to.equal(9); | ||
| expect(second.bitLen).to.equal(9); | ||
| }); | ||
|
|
||
| it("preserves the container input while reusing other fields", () => { | ||
| const container = new ContainerType({bits: type, payload: new ByteListType(32), flags: new BitVectorType(8)}); | ||
| const expected = { | ||
| bits: BitArray.fromBoolArray([true, false, true]), | ||
| payload: Uint8Array.of(0xab, 0xcd), | ||
| flags: BitArray.fromBoolArray([true, false, false, false, false, false, false, false]), | ||
| }; | ||
| const serialized = container.serialize(expected); | ||
| const bytes = createInput(Array.from(serialized)); | ||
| const value = container.deserialize(bytes, {reuseBytes: true}); | ||
|
|
||
| expect(value.bits.toBoolArray()).to.deep.equal(expected.bits.toBoolArray()); | ||
| expect(Array.from(value.payload)).to.deep.equal(Array.from(expected.payload)); | ||
| expect(value.flags.toBoolArray()).to.deep.equal(expected.flags.toBoolArray()); | ||
| expect(value.payload.buffer).to.equal(bytes.buffer); | ||
| expect(value.flags.uint8Array.buffer).to.equal(bytes.buffer); | ||
| expect(Array.from(bytes)).to.deep.equal(Array.from(serialized)); | ||
| expect(container.serialize(value)).to.deep.equal(serialized); | ||
| }); | ||
|
|
||
| it("preserves the input with the default deserialization options", () => { | ||
| const bytes = createInput([0x00, 0x03]); | ||
| const value = type.deserialize(bytes); | ||
|
|
||
| expect(value.bitLen).to.equal(9); | ||
| expect(Array.from(bytes)).to.deep.equal([0x00, 0x03]); | ||
| }); | ||
|
|
||
| it("reuses byte-aligned data without modifying the input", () => { | ||
| const bytes = createInput([0xab, 0x01]); | ||
| const value = type.deserialize(bytes, {reuseBytes: true}); | ||
|
|
||
| expect(value.bitLen).to.equal(8); | ||
| expect(value.uint8Array.buffer).to.equal(bytes.buffer); | ||
| expect(value.uint8Array.byteOffset).to.equal(bytes.byteOffset); | ||
| expect(Array.from(value.uint8Array)).to.deep.equal([0xab]); | ||
| expect(Array.from(bytes)).to.deep.equal([0xab, 0x01]); | ||
| }); | ||
|
|
||
| it.each([0, 1, 7, 8, 9, 15, 16])( | ||
| "round-trips %i bits from a subarray without changing its backing bytes", | ||
| (bitLen) => { | ||
| const expected = BitArray.fromBoolArray(Array.from({length: bitLen}, (_, i) => i % 2 === 0)); | ||
| const serialized = type.serialize(expected); | ||
| const backing = createInput([0xff, ...serialized, 0xff]); | ||
| const bytes = backing.subarray(1, backing.length - 1); | ||
|
|
||
| for (let i = 0; i < 2; i++) { | ||
| const value = type.deserialize(bytes, {reuseBytes: true}); | ||
|
|
||
| expect(value.toBoolArray()).to.deep.equal(expected.toBoolArray()); | ||
| expect(type.serialize(value)).to.deep.equal(serialized); | ||
| expect(type.hashTreeRoot(value)).to.deep.equal(type.hashTreeRoot(expected)); | ||
| expect(Array.from(backing)).to.deep.equal([0xff, ...serialized, 0xff]); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| it("preserves the input when the bit length exceeds the limit", () => { | ||
| const bytes = createInput([0x00, 0x00, 0x03]); | ||
|
|
||
| expect(() => type.deserialize(bytes, {reuseBytes: true})).to.throw("bitLen over limit 17 > 16"); | ||
| expect(Array.from(bytes)).to.deep.equal([0x00, 0x00, 0x03]); | ||
| }); | ||
|
|
||
| for (const bitLen of [17, 24]) { | ||
| it.each(["copy", "reuse", "view", "viewDU"] as const)( | ||
| `rejects ${bitLen} bits before slicing the input (%s)`, | ||
| (mode) => { | ||
| const bytes = createInput(bitLen === 17 ? [0x00, 0x00, 0x02] : [0x00, 0x00, 0x00, 0x01]); | ||
| const copy = vi.spyOn(Uint8Array.prototype, "slice"); | ||
| const view = vi.spyOn(Uint8Array.prototype, "subarray"); | ||
|
|
||
| try { | ||
| expect(() => { | ||
| if (mode === "view") type.deserializeToView(bytes); | ||
| else if (mode === "viewDU") type.deserializeToViewDU(bytes); | ||
| else type.deserialize(bytes, {reuseBytes: mode === "reuse"}); | ||
| }).to.throw(`bitLen over limit ${bitLen} > 16`); | ||
| expect(copy).not.toHaveBeenCalled(); | ||
| expect(view).not.toHaveBeenCalled(); | ||
| } finally { | ||
| copy.mockRestore(); | ||
| view.mockRestore(); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| it("keeps the shared decoder usable without a limit", () => { | ||
| const bytes = Uint8Array.of(0x00, 0x03); | ||
| const value = deserializeUint8ArrayBitListFromBytes(bytes, 0, bytes.length, true); | ||
|
|
||
| expect(value.bitLen).to.equal(9); | ||
| expect(Array.from(value.uint8Array)).to.deep.equal([0x00, 0x01]); | ||
| expect(Array.from(bytes)).to.deep.equal([0x00, 0x03]); | ||
| }); | ||
|
|
||
| it("accepts only an empty bitlist with a zero progressive limit", () => { | ||
| const type = new ProgressiveBitListType({limit: 0}); | ||
|
|
||
| expect(type.deserialize(Uint8Array.of(0x01), {reuseBytes: true}).bitLen).to.equal(0); | ||
| expect(() => type.deserialize(Uint8Array.of(0x02), {reuseBytes: true})).to.throw("bitLen over limit 1 > 0"); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
reuseBytesis enabled and an untrusted non-byte-aligned bitlist exceeds the configured limit, this unconditional copy duplicates the entire field beforeBitListTypeorProgressiveBitListTypechecksbitLenafter the helper returns. A caller using reuse specifically to avoid allocations can therefore be forced into an input-sized allocation (and possible OOM) for data that is immediately rejected; compute and validate the bit length or serialized bound before taking this copy.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 083ea0f. Both bitlist types now pass their limit to the shared decoder, which checks the bit length before slicing. Added tests for value and tree decoding that verify over-limit inputs never reach slice or subarray.