Skip to content

DCS: Add more formal BOM-detection to Json encoding stream wrappers.#130635

Open
StephenMolloy wants to merge 5 commits into
dotnet:mainfrom
StephenMolloy:80160-DCS-JsonEncodingStreamWrapper-Fix-BOM-Detection
Open

DCS: Add more formal BOM-detection to Json encoding stream wrappers.#130635
StephenMolloy wants to merge 5 commits into
dotnet:mainfrom
StephenMolloy:80160-DCS-JsonEncodingStreamWrapper-Fix-BOM-Detection

Conversation

@StephenMolloy

@StephenMolloy StephenMolloy commented Jul 13, 2026

Copy link
Copy Markdown
Member

This pull request improves the handling of JSON byte order marks (BOM) in the DataContractJsonSerializer by centralizing and enhancing encoding detection logic, and adds comprehensive tests to ensure correct behavior when processing streams and buffers with BOMs.

Encoding detection improvements:

  • Introduced a new DetectEncoding method to reliably detect the encoding of a JSON document by examining its leading bytes for BOMs or inferring from initial bytes when no BOM is present, consolidating logic previously duplicated across code paths.
  • Updated the buffer and stream processing logic in JsonEncodingStreamWrapper to use the new DetectEncoding method, ensuring BOMs are correctly skipped and only the actual JSON payload is processed. [1] [2]

Test coverage enhancements:

  • Added new parameterized tests to verify that deserialization works correctly for streams and buffers containing UTF-8, UTF-16LE, and UTF-16BE BOMs, both when encoding is auto-detected and when explicitly specified.
  • Provided a helper method to generate JSON byte arrays with the appropriate BOM for use in the new tests.

Fixes #80160

@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Jul 13, 2026
@StephenMolloy StephenMolloy self-assigned this Jul 13, 2026
Copilot AI review requested due to automatic review settings July 13, 2026 16:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates JsonEncodingStreamWrapper to centralize JSON encoding detection (including BOM handling) across both buffer- and stream-based read paths, and adds regression tests ensuring BOM-prefixed JSON payloads deserialize correctly.

Changes:

  • Added a shared DetectEncoding(ReadOnlySpan<byte>, out int bomLength) helper and used it from both buffer and stream paths.
  • Updated buffer and stream reading logic to skip BOM bytes so only the JSON payload is processed.
  • Added new [Theory] tests covering UTF-8/UTF-16LE/UTF-16BE BOM scenarios for both stream and buffer deserialization.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEncodingStreamWrapper.cs Centralizes encoding detection and skips BOMs for both buffer and stream read paths.
src/libraries/System.Runtime.Serialization.Json/tests/DataContractJsonSerializer.cs Adds BOM-focused theory tests and a helper to construct BOM-prefixed JSON byte payloads.

…ort input

ReadEncoding used ReadAtLeast, which loops until the requested minimum number of bytes is accumulated and therefore blocks a consumer waiting for bytes an untrusted producer may never send. Replace it with a single Stream.Read that performs one underlying read and decides the encoding from however many bytes were available, defaulting to UTF-8 when there are too few. This recognizes every byte order mark whenever the leading bytes are present while allowing valid short documents (for example a single-byte JSON value) to proceed without waiting for a fixed minimum.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 13, 2026 19:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Copilot AI review requested due to automatic review settings July 14, 2026 05:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Copilot AI review requested due to automatic review settings July 14, 2026 07:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Copilot AI review requested due to automatic review settings July 14, 2026 07:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@StephenMolloy StephenMolloy requested a review from mangod9 July 14, 2026 16:41

@mangod9 mangod9 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

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.

JsonEncodingStreamWrapper cannot handle UTF16 BOMs

3 participants