.NET 10 Preview: AllowDuplicateProperties defaults to true (insecure)
I've been testing .NET 10 Preview 6 and noticed that the new AllowDuplicateProperties option defaults to true. Since this is explicitly documented as a security concern, shouldn't the default be false?
From the XML comments in JsonSerializerOptions.cs:
"Duplicate property names can create security vulnerabilities since they introduce ambiguity"
Right now:
var options = new JsonSerializerOptions();
Console.WriteLine(options.AllowDuplicateProperties); // true - insecure by default
Only JsonSerializerOptions.Strict sets it to false. Most developers won't know to use Strict mode, so they'll be vulnerable by default.
Why this matters: Different JSON parsers handle duplicates differently (first-wins vs last-wins). This creates attacks where validation sees one value but the app uses another:
{"userId": "normalUser", "isAdmin": false, "isAdmin": true}
Two specific issues:
- The default value is backwards - security should be opt-out, not opt-in. Line 105 in JsonSerializerOptions.cs:
private bool _allowDuplicateProperties = true;
JsonDocument.ParseValue(ref reader)hardcodesallowDuplicateProperties: truewith no public overload to disable it (line 434-435). The internal overload exists but isn't exposed.
Since this is still preview, could you consider:
- Changing the default to
false - Adding a public overload for ParseValue with the parameter exposed
I understand backward compatibility concerns, but this is preview - exactly when breaking changes are acceptable for security improvements.
Is there a specific reason the insecure default is required that I'm missing?
I'm a bot. Here are possible related and/or duplicate issues (I may be wrong):
- https://github.com/dotnet/runtime/issues/108521
- https://github.com/dotnet/runtime/pull/115856
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
To further the point of the default being false, the JSON spec, section 4, states:
The names within an object SHOULD be unique.
The semantics of "SHOULD" imply that an implementation needs a very good reason why it's choosing to not follow this requirement.
The spec effectively recommends support for duplicates to be disabled by default.
Is there a specific reason the insecure default is required that I'm missing?
Because it's a significant behavioral breaking change for code using System.Text.Json prior to .NET 10. Effectively the default has always been true, it's just that the knob is now exposed to change it.
I've been testing .NET 10 Preview 6
.NET 10 was released in November.