Switching server database at runtime exposes data to wrong user
See original GitHub issueOur customers are companies and each company has their own database on our server. We have rolled out a version of our app that uses DMS to sync SQLite on the client to their database via https.
The client passes a token in the header httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(...
The sync client is configured
WebClientOrchestrator webClientOrchestrator = new WebClientOrchestrator(serviceUri, client: httpClient, maxDownladingDegreeOfParallelism: 4);
On the server the SyncController uses the token to lookup which database to use for that user.
string databaseName = DatabaseHelper.GetDatabaseName(token);
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(orchestrator.Provider.ConnectionString);
builder.InitialCatalog = databaseName;
orchestrator.Provider.ConnectionString = builder.ConnectionString;
We’ve rolled out this version of our app to less than 100 customers as beta testers. We’ve had one report of a customer seeing some data that was not theirs, mixed in with theirs. We immediately shut down the beta test.
Where is the flaw in our strategy for switching databases at runtime. Is there recommended practice?
Issue Analytics
- State:
- Created 2 years ago
- Comments:18 (17 by maintainers)

Top Related StackOverflow Question
We’ve released this beta to 50+ beta testers and the issue does appear to be resolved. Thank you.
We’d like to incorporate this package into one of our commercial releases. When do you expect this fix to be out of beta?
I have changed the way the server is handling information for each client.
Breaking changes
I’ve removed the
WebServerManagerinstance, and I’m injecting directly aWebServerOrchestratorin the pipeline. Be careful, the code on the controller is slightly different (see example below)I’ve removed all the server cache usages, and replace them with the client session.
And for your particular need, I’ve changed a little bit the
WebServerOrchestratorto let you create one dynamically in the controller.So far, if you want to test it, just clone the last commit from the master branch and test it.
It’s not mandatory to migrate your clients, since the changes are only occurring on the server side (if your clients are already on version
v0.8.0Since we are using now the session, you need to add the session service and middleware. See here for more info : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0#configure-session-state.
Sample
Startup.cs:
Be careful, order is important here :
UseSessionis AFTERUseRoutingandUseAuthorizationand BEFOREUseEndpointsThe SyncController class has changed too:
And for your particular use case, where you want to create a new
WebServerOrchestratorfor each request, with a different connection string:SyncController.cs:
Let me know if it’s resolving your issue. It will be quite complicated for you, I know, since you have to redeploy and retests with 100 customers, but it’s complicated for me as well since I’m not able to reproduce your bug, and once again, you don’t share a lot of your code to help me figure out what can be the issue in your code…
With this fix, I’m almost 100% sure that nothing is shared between sessions.