LiteDB may deserialize bad JSON on object type using _type
Published 24 Feb 2023 · Updated 8 Nov 2023
Is your project on an affected LiteDB?
Look after several .NET apps? Indie watches up to 15 repos, private ones too, for $5 a month. See Indie
Affected package
- Affected
- <5.0.13
- Fixed in
- 5.0.13
dotnet add package LiteDB --version 5.0.13Details
Impact
LiteDB use a special field in JSON documents to cast diferent types from BsonDocument do POCO classes. When instance of an object are not the same of class, BsonMapper use a special field _type string info with full class name with assembly to be loaded and fit in your model.
If your end-user can send to your app a plain JSON string, deserialization can load an unsafe object to fit in your model.
Patches
Version >= 5.0.13 add some basic fixes to avoid this, but is not 100% guaranteed when using Object type
Next major version will contains a allow-list to select what king of Assembly can be loaded
Workarounds
- Avoid users send to your app a JSON string to be direct insert/update into database
- Avoid use classes with
Objecttype - try use an interface when possible
If your app send a plain JSON string to be insert/update into database, prefer this:
// Bad
public class Customer {
public int Id { get; set; }
public string Name { get; set; }
public Object AnyData { get; set; } // <= Avoid use `Object` base type
}
// Good
public class Customer {
public int Id { get; set; }
public string Name { get; set; }
public IDictionary<string, string> AnyData { get; set; } // Will accept only key/value strings
}
References
See this workaround fix on this commit:
https://github.com/mbdavid/LiteDB/commit/4382ff4dd0dd8b8b16a4e37dfd29727c5f70f93f
Is GHSA-3x49-g6rc-c284 in any of your client repos?
Checking one package by hand is fine. Checking it across forty client projects is not. NPMscan reads the lockfiles in every GitHub repo you pick, finds each project with an affected LiteDB, and gives you the command that fixes it.
References
- Linkgithub.com/mbdavid/LiteDB/security/advisories/GHSA-3x49-g6rc-c284
- Advisorynvd.nist.gov/vuln/detail/CVE-2022-23535
- Linkgithub.com/mbdavid/LiteDB/commit/4382ff4dd0dd8b8b16a4e37dfd29727c5f70f93f
- Linkgithub.com/mbdavid/LiteDB/commit/d72c6774e6a13de2cfcd7d477d3575efeb75c8f2
- Packagegithub.com/mbdavid/LiteDB
- Linkgithub.com/mbdavid/LiteDB/releases/tag/v5.0.13
Advisory data from OSV.dev (opens OSV.dev in a new tab), refreshed daily.