-
Notifications
You must be signed in to change notification settings - Fork 5.5k
CachedCatalogViewModelService risky? #1055
Comments
The service
use IMemoryCache https://learn.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-8.0 This one is registered in https://github.com/dotnet-architecture/eShopOnWeb/blob/2414014bfa0f4d2021b5bc9061429a98d232f440/src/Web/Program.cs#L66C1-L66C28 |
Thanks for the answer! This could generate wrong answers and very difficult to detect. |
How exactly would you implement a memory cache in a monolithic application with a single production instance? Yes, one could use Redis or a similar out of process cache to resolve some of the concerns you're mentioning, but in this case in-memory is the chosen approach (which is fully supported by dotnet). What security risk are you implying? Please demonstrate an attack vector and mitigation. |
Ok, probably I have exaggerated the risk... you are using a request scoped service to get data that will be used in others requests. This is ok if you use it carefully, the risk is only if you use request context data in the methods used by the cache. For example accessing a 3rd part service with current user credentials that maybe will return data only valid for that user, etc. This request scoped service could in turn use other services to perform some actions and if some developer thinks that everything is request scoped they could for example inject request context information in the service constructor making not clear what methods use this information... real business services could become very complex. Here they propose using IServiceScopeFactory and creating scoped services inside the singleton using that. |
CachedCatalogViewModelService is registered as a Scoped, so one instance is created per http request.
As the cache is a property of service instance (not static) it will be empty for each request.
This makes all the cache trivially useless... I'm missing something?
The text was updated successfully, but these errors were encountered: