Summary
Anyone who can write documents an app later reads as plain objects can make the driver build types the safety allow-list was supposed to block.
Impact
An attacker who can shape stored or wire BSON defeats the driver's deserialization type allow-list and gets non-allow-listed compiler-generated types instantiated with attacker-chosen field values inside the victim application, enabling type confusion, application-state corruption, and exceptions in consuming code.
Severity: high
Exploitability: likely_exploitable — The bypass is deterministic once a matching gadget type exists (any generic async/iterator method with 'Anon' in its name, e.g. anonymous-user handling code); attacker fully controls the _t type name and the field values, but harmful downstream impact depends on how the app consumes the unexpected object.
Location
- src/MongoDB.Bson/Serialization/TypeExtensions.cs:24 in IsAnonymousType
- src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:576 in AllowedTypesImplementation.IsAllowedGenericType
- src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:375 in DeserializeDiscriminatedValue
- src/MongoDB.Bson/Serialization/BsonClassMap.cs:74 in BsonClassMap..ctor
- src/MongoDB.Bson/Serialization/TypeNameDiscriminator.cs:59 in GetActualType
Reproduction / trigger path
Analyzed trigger path from static analysis — not an executed PoC. Confirm with a concrete repro before handing off.
Attacker controls: Full control of the _t discriminator string (hence the resolved Type, including constructed generics with attacker-chosen type arguments) and of all BSON field values used to populate the instantiated object's public fields.
- (source) src/MongoDB.Bson/Serialization/Conventions/ObjectDiscriminatorConvention.cs:130 in GetActualType — Reads the attacker-controlled _t discriminator element from BSON being deserialized into an object-typed member.
- (hop) src/MongoDB.Bson/Serialization/BsonSerializer.cs:377 in LookupActualType — Unregistered string discriminators fall back to type-name resolution; assignability check at line 386 is vacuous for nominal type object.
- (hop) src/MongoDB.Bson/Serialization/TypeNameDiscriminator.cs:59 in GetActualType — Type.GetType resolves the attacker string to any loadable type, including nested compiler-generated types; line 94 constructs generics from attacker-chosen arguments.
- (hop) src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:375 in DeserializeDiscriminatedValue — Default deserialization type allow-list gate is consulted on the attacker-resolved Type.
- (hop) src/MongoDB.Bson/Serialization/TypeExtensions.cs:30 in IsAnonymousType — False positive: CompilerGeneratedAttribute + generic + name-contains-'Anon' matches async/iterator state machines of methods named Anon, not just true anonymous types.
- (hop) src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:576 in AllowedTypesImplementation.IsAllowedGenericType — IsAnonymousType() is an OR-alternative to the curated allow-list set, so the misclassified type is approved; primitive generic args pass line 577.
- (sink) src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:399 in DeserializeDiscriminatedValue — LookupSerializer auto-maps the non-allow-listed type (BsonClassMap.cs:74 marks it anonymous) and deserialization instantiates it, populating public fields from attacker BSON (lines 403/420).
Root cause
- IsAnonymousType (src/MongoDB.Bson/Serialization/TypeExtensions.cs:28-30) classifies a type as anonymous if it merely carries CompilerGeneratedAttribute, is generic, and has the substring 'Anon' anywhere in its name — async state machines and iterators of methods named Anon (e.g. <GetAnonymousUsersAsync>d_7 in a generic context) satisfy all three, unlike the real anonymous-type prefixes <>f_AnonymousType / VB$AnonymousType.
- The deserialization allow-list treats IsAnonymousType() as an OR-alternative to the curated generic-type set (src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs:576), and the residual generic-argument check (line 577) passes trivially for primitive arguments.
- The Type under test is attacker-resolved: the _t discriminator string from the BSON document is passed to Type.GetType and MakeGenericType (src/MongoDB.Bson/Serialization/TypeNameDiscriminator.cs:59,94 via src/MongoDB.Bson/Serialization/BsonSerializer.cs:377), and the assignability check (BsonSerializer.cs:386) is vacuous for nominal type object.
- After the gate passes (ObjectSerializer.cs:375), BsonSerializer.LookupSerializer auto-maps and instantiates the type and populates its public fields from attacker BSON (ObjectSerializer.cs:399-420); BsonClassMap additionally misclassifies it as anonymous (src/MongoDB.Bson/Serialization/BsonClassMap.cs:74), switching it onto the anonymous-type constructor-mapping path (src/MongoDB.Bson/Serialization/Conventions/ImmutableTypeClassMapConvention.cs:93).
Ownership
Assigned teams: @mongodb/dbx-csharp-dotnet
Filed from Aegis finding bd0a66a32af6 (scan scan-e837ccfa32ab) · primitive: Attacker-named compiler-generated generic types pass the deserialization type allow-list and get instantiated with attacker-controlled field values. · categories: Isolation Bypass, Data Corruption, Crash