Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

CachedCatalogViewModelService risky? #1055

Open
calbeda opened this issue May 13, 2024 · 4 comments
Open

CachedCatalogViewModelService risky? #1055

calbeda opened this issue May 13, 2024 · 4 comments

Comments

@calbeda
Copy link

calbeda commented May 13, 2024

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?

@michelcedric
Copy link
Contributor

@calbeda
Copy link
Author

calbeda commented May 13, 2024

Thanks for the answer!
You are right, this cache is not trivially useless... is much worse: it is using a service that by specification has http request specific dependencies to get data that will be used in other requests.

This could generate wrong answers and very difficult to detect.
As this project is presented as a model architecture it could be used in many situations where developers probably will not be aware of this details. Is a potentially big security hole if used without much attention.
This "singleton cache in an http scoped service" is a not secure by default. Is a clear security risk.

@ardalis
Copy link
Collaborator

ardalis commented May 13, 2024

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.

@calbeda
Copy link
Author

calbeda commented May 15, 2024

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.
But maybe this could be excessive in many situations... I suppose everything depends of the situation and the size and communication inside the developers team...

@calbeda calbeda changed the title CachedCatalogViewModelService useless? CachedCatalogViewModelService risky? May 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants