-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathPlaintextCredentialStoreTests.cs
More file actions
203 lines (165 loc) · 8.41 KB
/
Copy pathPlaintextCredentialStoreTests.cs
File metadata and controls
203 lines (165 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.IO;
using System.Text;
using GitCredentialManager.Tests.Objects;
using Xunit;
namespace GitCredentialManager.Tests
{
public class PlaintextCredentialStoreTests
{
private const string TestNamespace = "git-test";
private const string StoreRoot = "/tmp/test-store";
[Fact]
public void PlaintextCredentialStore_ReadWriteDelete()
{
var fs = new TestFileSystem();
var collection = new PlaintextCredentialStore(fs, StoreRoot, TestNamespace);
// Create a service that is guaranteed to be unique
string uniqueGuid = Guid.NewGuid().ToString("N");
string service = $"https://example.com/{uniqueGuid}";
const string userName = "john.doe";
const string password = "letmein123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
string expectedSlug = Path.Combine(
TestNamespace,
"https",
"example.com",
uniqueGuid,
$"{userName}.credential");
string expectedFilePath = Path.Combine(StoreRoot, expectedSlug);
string expectedFileContents = password + Environment.NewLine +
$"service={service}" + Environment.NewLine +
$"account={userName}" + Environment.NewLine;
byte[] expectedFileBytes = Encoding.UTF8.GetBytes(expectedFileContents);
try
{
// Write
collection.AddOrUpdate(service, userName, password);
// Read
ICredential outCredential = collection.Get(service, userName);
Assert.NotNull(outCredential);
Assert.Equal(userName, userName);
Assert.Equal(password, outCredential.Password);
Assert.True(fs.Files.ContainsKey(expectedFilePath));
Assert.Equal(expectedFileBytes, fs.Files[expectedFilePath]);
}
finally
{
// Ensure we clean up after ourselves even in case of 'get' failures
collection.Remove(service, userName);
}
}
[Fact]
public void PlaintextCredentialStore_Get_NotFound_ReturnsNull()
{
var fs = new TestFileSystem();
var collection = new PlaintextCredentialStore(fs, StoreRoot, TestNamespace);
// Unique service; guaranteed not to exist!
string service = $"https://example.com/{Guid.NewGuid():N}";
ICredential credential = collection.Get(service, null);
Assert.Null(credential);
}
[Fact]
public void PlaintextCredentialStore_Remove_NotFound_ReturnsFalse()
{
var fs = new TestFileSystem();
var collection = new PlaintextCredentialStore(fs, StoreRoot, TestNamespace);
// Unique service; guaranteed not to exist!
string service = $"https://example.com/{Guid.NewGuid():N}";
bool result = collection.Remove(service, account: null);
Assert.False(result);
}
[Fact]
public void PlaintextCredentialStore_AccountWithPathSeparators_StoresInServiceDirectory()
{
var fs = new TestFileSystem();
var collection = new PlaintextCredentialStore(fs, StoreRoot, TestNamespace);
string uniqueGuid = Guid.NewGuid().ToString("N");
string service = $"https://example.com/{uniqueGuid}";
// Account name with path traversal characters
const string userName = "../../malicious/account";
const string password = "letmein123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
// Expected: path separators replaced with '_', file stays inside service directory
string safeUserName = ".._.._malicious_account";
string expectedSlug = Path.Combine(
TestNamespace,
"https",
"example.com",
uniqueGuid,
$"{safeUserName}.credential");
string expectedFilePath = Path.Combine(StoreRoot, expectedSlug);
// Write
collection.AddOrUpdate(service, userName, password);
// Verify the file is created inside the expected service directory (no traversal)
Assert.True(fs.Files.ContainsKey(expectedFilePath),
$"Expected credential file at '{expectedFilePath}' but it was not found.");
// Verify no files were created outside the store root
foreach (string filePath in fs.Files.Keys)
{
Assert.True(filePath.StartsWith(StoreRoot, StringComparison.Ordinal),
$"Credential file '{filePath}' was created outside the store root.");
}
// Verify the credential can be retrieved using the original account name
ICredential outCredential = collection.Get(service, userName);
Assert.NotNull(outCredential);
Assert.Equal(password, outCredential.Password);
}
[Fact]
public void PlaintextCredentialStore_AccountWithNullByte_StoresInServiceDirectory()
{
var fs = new TestFileSystem();
var collection = new PlaintextCredentialStore(fs, StoreRoot, TestNamespace);
string uniqueGuid = Guid.NewGuid().ToString("N");
string service = $"https://example.com/{uniqueGuid}";
// Account name with null byte (CWE-158: null byte injection)
string userName = "user\0name";
const string password = "letmein123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
// Expected: null byte replaced with '_'
string safeUserName = "user_name";
string expectedSlug = Path.Combine(
TestNamespace,
"https",
"example.com",
uniqueGuid,
$"{safeUserName}.credential");
string expectedFilePath = Path.Combine(StoreRoot, expectedSlug);
// Write
collection.AddOrUpdate(service, userName, password);
// Verify the file is created inside the expected service directory
Assert.True(fs.Files.ContainsKey(expectedFilePath),
$"Expected credential file at '{expectedFilePath}' but it was not found.");
// Verify no files were created outside the store root
foreach (string filePath in fs.Files.Keys)
{
Assert.True(filePath.StartsWith(StoreRoot, StringComparison.Ordinal),
$"Credential file '{filePath}' was created outside the store root.");
}
// Verify the credential can be retrieved using the original account name
ICredential outCredential = collection.Get(service, userName);
Assert.NotNull(outCredential);
Assert.Equal(password, outCredential.Password);
}
[Theory]
[InlineData(".")]
[InlineData("..")]
public void PlaintextCredentialStore_AccountWithReservedPathComponent_StoresInServiceDirectory(string userName)
{
var fs = new TestFileSystem();
var collection = new PlaintextCredentialStore(fs, StoreRoot, TestNamespace);
string uniqueGuid = Guid.NewGuid().ToString("N");
string service = $"https://example.com/{uniqueGuid}";
const string password = "letmein123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
// Write — must not throw and must stay inside the store root
collection.AddOrUpdate(service, userName, password);
// Verify no files were created outside the store root
foreach (string filePath in fs.Files.Keys)
{
Assert.True(filePath.StartsWith(StoreRoot, StringComparison.Ordinal),
$"Credential file '{filePath}' was created outside the store root.");
}
// Verify the credential can be retrieved using the original account name
ICredential outCredential = collection.Get(service, userName);
Assert.NotNull(outCredential);
Assert.Equal(password, outCredential.Password);
}
}
}