Skip to content

Commit cb626f8

Browse files
hickfordgitster
authored andcommitted
credential/wincred: erase matching creds only
The credential erase request typically includes protocol, host, username and password. credential-wincred erases stored credentials that match protocol, host and username, regardless of password. This is confusing in the case the stored password differs from that in the request. This case can occur when multiple credential helpers are configured. Only erase credential if stored password matches request (or request omits password). This fixes test "helper (wincred) does not erase a password distinct from input" when t0303 is run with GIT_TEST_CREDENTIAL_HELPER set to "wincred". This test was added in aeb21ce (credential: avoid erasing distinct password, 2023-06-13). Signed-off-by: M Hickford <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7144dee commit cb626f8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

contrib/credential/wincred/git-credential-wincred.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,18 @@ static int match_part_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
109109
return match_part_with_last(ptarget, want, delim, 1);
110110
}
111111

112-
static int match_cred(const CREDENTIALW *cred)
112+
static int match_cred_password(const CREDENTIALW *cred) {
113+
int ret;
114+
WCHAR *cred_password = xmalloc(cred->CredentialBlobSize);
115+
wcsncpy_s(cred_password, cred->CredentialBlobSize,
116+
(LPCWSTR)cred->CredentialBlob,
117+
cred->CredentialBlobSize / sizeof(WCHAR));
118+
ret = !wcscmp(cred_password, password);
119+
free(cred_password);
120+
return ret;
121+
}
122+
123+
static int match_cred(const CREDENTIALW *cred, int match_password)
113124
{
114125
LPCWSTR target = cred->TargetName;
115126
if (wusername && wcscmp(wusername, cred->UserName ? cred->UserName : L""))
@@ -119,7 +130,8 @@ static int match_cred(const CREDENTIALW *cred)
119130
match_part(&target, protocol, L"://") &&
120131
match_part_last(&target, wusername, L"@") &&
121132
match_part(&target, host, L"/") &&
122-
match_part(&target, path, L"");
133+
match_part(&target, path, L"") &&
134+
(!match_password || match_cred_password(cred));
123135
}
124136

125137
static void get_credential(void)
@@ -134,7 +146,7 @@ static void get_credential(void)
134146

135147
/* search for the first credential that matches username */
136148
for (i = 0; i < num_creds; ++i)
137-
if (match_cred(creds[i])) {
149+
if (match_cred(creds[i], 0)) {
138150
write_item("username", creds[i]->UserName,
139151
creds[i]->UserName ? wcslen(creds[i]->UserName) : 0);
140152
write_item("password",
@@ -196,7 +208,7 @@ static void erase_credential(void)
196208
return;
197209

198210
for (i = 0; i < num_creds; ++i) {
199-
if (match_cred(creds[i]))
211+
if (match_cred(creds[i], password != NULL))
200212
CredDeleteW(creds[i]->TargetName, creds[i]->Type, 0);
201213
}
202214

0 commit comments

Comments
 (0)