Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only match credential entries with correct namespace in the Windows Credential Manager #1328

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,70 @@ public void WindowsCredentialManager_RemoveUriUserInfo(string input, string expe
Assert.Equal(expected, actual);
}

[PlatformFact(Platforms.Windows)]
public void WindowsCredentialManager_IsMatch_NoNamespace_NotMatched()
{
var win32Cred = new Win32Credential
{
UserName = "test",
TargetName = $"{WindowsCredentialManager.TargetNameLegacyGenericPrefix}https://example.com"
};

var credManager = new WindowsCredentialManager(TestNamespace);

bool result = credManager.IsMatch("https://example.com", null, win32Cred);

Assert.False(result);
}

[PlatformFact(Platforms.Windows)]
public void WindowsCredentialManager_IsMatch_DifferentNamespace_NotMatched()
{
var win32Cred = new Win32Credential
{
UserName = "test",
TargetName = $"{WindowsCredentialManager.TargetNameLegacyGenericPrefix}:random-namespace:https://example.com"
};

var credManager = new WindowsCredentialManager(TestNamespace);

bool result = credManager.IsMatch("https://example.com", null, win32Cred);

Assert.False(result);
}

[PlatformFact(Platforms.Windows)]
public void WindowsCredentialManager_IsMatch_CaseSensitiveNamespace_NotMatched()
{
var win32Cred = new Win32Credential
{
UserName = "test",
TargetName = $"{WindowsCredentialManager.TargetNameLegacyGenericPrefix}:nAmEsPaCe:https://example.com"
};

var credManager = new WindowsCredentialManager("namespace");

bool result = credManager.IsMatch("https://example.com", null, win32Cred);

Assert.False(result);
}

[PlatformFact(Platforms.Windows)]
public void WindowsCredentialManager_IsMatch_NoNamespaceInQuery_IsMatched()
{
var win32Cred = new Win32Credential
{
UserName = "test",
TargetName = $"{WindowsCredentialManager.TargetNameLegacyGenericPrefix}https://example.com"
};

var credManager = new WindowsCredentialManager();

bool result = credManager.IsMatch("https://example.com", null, win32Cred);

Assert.True(result);
}

[PlatformTheory(Platforms.Windows)]
[InlineData("https://example.com", null, "https://example.com")]
[InlineData("https://example.com", "bob", "https://bob@example.com")]
Expand Down
12 changes: 10 additions & 2 deletions src/shared/Core/Interop/Windows/WindowsCredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,19 @@ private WindowsCredential CreateCredentialFromStructure(Win32Credential credenti
return false;
}

// Trim the "LegacyGeneric" prefix Windows adds and any namespace we have been filtered with
// Trim the "LegacyGeneric" prefix Windows adds
string targetName = credential.TargetName.TrimUntilIndexOf(TargetNameLegacyGenericPrefix);

// Only match credentials with the namespace we have been configured with (if any)
if (!string.IsNullOrWhiteSpace(_namespace))
{
targetName = targetName.TrimUntilIndexOf($"{_namespace}:");
string nsPrefix = $"{_namespace}:";
if (!targetName.StartsWith(nsPrefix, StringComparison.Ordinal))
{
return false;
}

targetName = targetName.Substring(nsPrefix.Length);
}

// If the target name matches the service name exactly then return 'match'
Expand Down