Skip to content

Commit

Permalink
Make sure we honor credentials namespace/scope
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kzu committed May 25, 2024
1 parent eaf384a commit 7ca0ecb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/CredentialManager/CredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public static class CredentialManager
/// <returns>The <see cref="ICredentialStore"/>.</returns>
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
Expand Down
18 changes: 18 additions & 0 deletions src/Tests/EndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 7ca0ecb

Please sign in to comment.