Skip to content

fix(core): handle Uint8Array subarrays correctly in createFile and encodeByteArray#38144

Open
deyaaeldeen wants to merge 1 commit intomainfrom
fix/core-subarray-bugs
Open

fix(core): handle Uint8Array subarrays correctly in createFile and encodeByteArray#38144
deyaaeldeen wants to merge 1 commit intomainfrom
fix/core-subarray-bugs

Conversation

@deyaaeldeen
Copy link
Copy Markdown
Member

@deyaaeldeen deyaaeldeen commented Apr 14, 2026

Problem

createFile and encodeByteArray silently corrupt data when given a Uint8Array subarray (via .subarray()). Both read from the full backing ArrayBuffer instead of the subarray range.

Fix

  • createFile (core-rest-pipeline): copy subarrays that do not span the full buffer
  • encodeByteArray (core-client): pass byteOffset/byteLength to Buffer.from()

Tests added for both.

@deyaaeldeen deyaaeldeen requested a review from a team as a code owner April 14, 2026 18:35
Copilot AI review requested due to automatic review settings April 14, 2026 18:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes silent data corruption when Uint8Array.subarray() is passed through file creation and base64 encoding by ensuring byte offsets/lengths are respected.

Changes:

  • Update createFile to return correct ArrayBuffer ranges and avoid using a subarray’s full backing buffer.
  • Update encodeByteArray to pass byteOffset/byteLength to Buffer.from.
  • Add tests covering subarray handling for multipart file upload and ByteArray serialization.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
sdk/core/core-rest-pipeline/test/internal/formDataPolicy.spec.ts Adds regression test ensuring createFile handles subarray content correctly.
sdk/core/core-rest-pipeline/src/util/file.ts Fixes createFile.arrayBuffer() and toArrayBuffer() to respect subarray bounds.
sdk/core/core-client/test/public/serializer.spec.ts Adds regression test ensuring ByteArray subarrays serialize correctly.
sdk/core/core-client/src/base64.ts Fixes base64 encoding to respect subarray offsets/lengths.

…codeByteArray

createFile's arrayBuffer() returned the full backing ArrayBuffer via
content.buffer, ignoring byteOffset/byteLength when content is a subarray.
Similarly, toArrayBuffer() returned the original view as-is, causing Blob
and File constructors to use the full buffer.

encodeByteArray in core-client had the same issue: Buffer.from(value.buffer)
ignored the subarray's byteOffset and byteLength.

Fixes:
- createFile: slice the buffer to the correct range in arrayBuffer()
- toArrayBuffer: copy subarrays that don't span the full buffer
- encodeByteArray: pass byteOffset and byteLength to Buffer.from()

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@deyaaeldeen deyaaeldeen force-pushed the fix/core-subarray-bugs branch from 997c301 to 2386ff3 Compare April 14, 2026 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants