From 7ca0ecb8b3f2cde6a30aea4bc14f92e0f9c82ae1 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Sat, 25 May 2024 03:34:11 -0300 Subject: [PATCH] Make sure we honor credentials namespace/scope A missing test uncovers the issue: we're not filtering credentials at all and are instead just defaulting to the 'git' default namespace. We ensure now that the credential store uses our context and settings wrappers to override this value. Fixes #103 --- src/CredentialManager/CredentialManager.cs | 3 +-- src/Tests/EndToEnd.cs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/CredentialManager/CredentialManager.cs b/src/CredentialManager/CredentialManager.cs index fa6955c..61423c1 100644 --- a/src/CredentialManager/CredentialManager.cs +++ b/src/CredentialManager/CredentialManager.cs @@ -22,10 +22,9 @@ public static class CredentialManager /// The . public static ICredentialStore Create(string? @namespace = default) { - using var context = new CommandContextWrapper(new CommandContext(), @namespace); // The context already does the check for the platform and configured store to initialize. // By overriding the settings with our wrapper, we ensure just the namespace is overriden. - return context.CredentialStore; + return new CredentialStore(new CommandContextWrapper(new CommandContext(), @namespace)); } class CommandContextWrapper(CommandContext context, string? @namespace) : ICommandContext diff --git a/src/Tests/EndToEnd.cs b/src/Tests/EndToEnd.cs index ac903ec..e538038 100644 --- a/src/Tests/EndToEnd.cs +++ b/src/Tests/EndToEnd.cs @@ -56,6 +56,24 @@ public void LinuxSecretService() Run(); } + [WindowsFact] + public void SavedOneNamespaceCannotRetrieveAnother() + { + var ns1 = Guid.NewGuid().ToString("N"); + var ns2 = Guid.NewGuid().ToString("N"); + + var store1 = CredentialManager.Create(ns1); + var store2 = CredentialManager.Create(ns2); + + var usr = Guid.NewGuid().ToString("N"); + var pwd = Guid.NewGuid().ToString("N"); + + store1.AddOrUpdate("https://test.com", usr, pwd); + + Assert.Null(store2.Get("https://test.com", usr)); + Assert.Empty(store2.GetAccounts("https://test.com")); + } + void Run() { var store = CredentialManager.Create(Guid.NewGuid().ToString("N"));