Skip to content

AspNetCore: Add DataContractSerializer options for configurable reader quotas#578

Draft
Copilot wants to merge 6 commits into
mainfrom
copilot/add-datacontractserializer-options-again
Draft

AspNetCore: Add DataContractSerializer options for configurable reader quotas#578
Copilot wants to merge 6 commits into
mainfrom
copilot/add-datacontractserializer-options-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 19, 2026

Exposes per-format serializer options so hosts can restrict XML reader quotas to mitigate denial-of-service (DoS) risks. By default all quotas remain at Max (current behavior unchanged).

New public API

Options classes (Serialization/DataContractSerializerOptions.cs):

  • DataContractSerializerOptions — base class with ReaderQuotas (XmlDictionaryReaderQuotas, mutable, defaults to max values)
  • BinaryDataContractSerializerOptions : DataContractSerializerOptions
  • XmlDataContractSerializerOptions : DataContractSerializerOptions

Builder methods (OpenRiaServicesOptionsBuilder):

  • AddXmlSerialization(Action<XmlDataContractSerializerOptions>? configure, bool defaultProvider = false) — new overload; existing AddXmlSerialization(bool) delegates to it
  • ConfigureBinarySerialization(Action<BinaryDataContractSerializerOptions> configure) — replaces the default binary provider in-place, preserving shared DataContractCache
builder.Services.AddOpenRiaServices()
    .ConfigureBinarySerialization(o =>
    {
        o.ReaderQuotas = new XmlDictionaryReaderQuotas { MaxStringContentLength = 1 * 1024 * 1024 };
    })
    .AddXmlSerialization(o =>
    {
        o.ReaderQuotas = new XmlDictionaryReaderQuotas { MaxStringContentLength = 1 * 1024 * 1024 };
    });

Internal wiring

  • BinaryMessageReader.Rent() accepts XmlDictionaryReaderQuotas and applies them on every call (thread-static cache remains safe — SetInput reinitializes with the provided quotas each time)
  • DataContractRequestSerializer stores _readerQuotas from its provider's options and passes them through to BinaryMessageReader.Rent()
  • BinaryXmlSerializationProvider / TextXmlSerializationProvider each hold their own options instance and pass it to every created serializer
  • DataContractSerializationProvider.CopyDataContractCacheFrom() replaces direct internal-field access for cache sharing

Copilot AI changed the title [WIP] Add DataContractSerializer options to ASP.NET Core AspNetCore: Add DataContractSerializer options for configurable reader quotas May 19, 2026
Copilot AI requested a review from Daniel-Svensson May 19, 2026 21:32
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AspNetCore: Add DataContractSerializer options

2 participants