Skip to content
Open
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
18 changes: 18 additions & 0 deletions WindowsCredentialManager.Tests/CredentialTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;

namespace WindowsCredentialManager.Tests
{
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -70,6 +72,22 @@ public void PromptCredentialsTest()
Assert.That (credentialsPromptResult, Is.Not.Null);
}

[Test]
public void ArgumentExceptionThrownWhenSecretIsTooBig()
{
var genericCredentials = new GenericCredentials ("CRED_TEST");

genericCredentials.UserName = "my user";
genericCredentials.Password = new SecureString();

for (var i = 0; i < 2561; i++)
{
genericCredentials.Password.AppendChar('x');
}

Assert.That(() => genericCredentials.Save(), Throws.ArgumentException);
}

private static string SecureStringToString (SecureString value)
{
var ptr = Marshal.SecureStringToGlobalAllocUnicode (value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net6</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
6 changes: 6 additions & 0 deletions WindowsCredentialManager/Credential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace WindowsCredentialManager

public abstract class Credential
{
private const int WindowsCredentialManagerMaxSize = 2560;
public string TargetName { get; }

internal CredentialType Type { get; }
Expand Down Expand Up @@ -107,6 +108,11 @@ public void Save()
blob = credentialW.Blob;
credentialW.BlobSize = blob?.Size ?? 0;

if (credentialW.BlobSize > WindowsCredentialManagerMaxSize)
{
throw new ArgumentException("Secret is too big for Windows Credential Manager");
}

Debug.Assert (credentialW.Type != default, "credentialW.Type != default");
Debug.Assert (credentialW.TargetName != null, "credentialW.TargetName != null");

Expand Down
4 changes: 3 additions & 1 deletion WindowsCredentialManager/Win32/Types/CREDENTIALW.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.InteropServices.ComTypes;

namespace WindowsCredentialManager.Win32.Types
{
using System;
Expand All @@ -10,7 +12,7 @@ internal struct CREDENTIALW
public CREDENTIAL_TYPE Type;
[MarshalAs (UnmanagedType.LPWStr)] public string TargetName;
[MarshalAs (UnmanagedType.LPWStr)] public string? Comment;
public DateTimeOffset LastWritten;
public FILETIME LastWritten;
public int BlobSize;
public SecureBlob? Blob;
public CREDENTIAL_PERSIST Persist;
Expand Down