All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Marc Becker via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Marc Becker <becm@gmx.de>, Marc Becker <becm@gmx.de>
Subject: [PATCH v5] wincred: refactor credential blob processing
Date: Wed, 14 Oct 2026 20:00:58 +0000	[thread overview]
Message-ID: <pull.2251.v5.git.1792008058394.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2251.git.1791553518774.gitgitgadget@gmail.com>

From: Marc Becker <becm@gmx.de>

Implementation for splitting secret blob into separate lines and
parsing those into key/value pairs has a couple of issues:
- invalid size limit to `wcsncpy_s` (missed during d22a4884)
- inconsistent detection of line separator (matches _CR_ and _LF_)
- failing to advance position after initial password line
- deep code nesting in loop body.

Line end detection needs to be deterministic and
code can be split up into smaller functional blocks.

Create new methods for processing the matching credential and each
supported key/value pair of a multi-line secret.
Reduce variable scope and nesting level. Use early continue/return to
improve code readability.
Use `wmemchr` to reliably detect wide-character linefeed in immutable
blob data without need to create a further copy.

Signed-off-by: Marc Becker <becm@gmx.de>
---
    wincred: fix line split of secret blob content
    
    Changes since v4:
    
     * rename method to parse/format/write secret data to
       write_credential_secret
     * pass only secret blob data to write_credential_secret

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2251%2Fbecm%2Ffix-wincred-secret-linesplit-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2251/becm/fix-wincred-secret-linesplit-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/2251

Range-diff vs v4:

 1:  5fbd3987cf ! 1:  7a0b1d4d2d wincred: refactor credential blob processing
     @@ contrib/credential/wincred/git-credential-wincred.c: static void write_item(cons
      + * Write single credential block
      + * consisting of password and further (accepted) credential items.
      + */
     -+static void write_credential(const CREDENTIALW *cred)
     ++static void write_credential_secret(LPCWSTR blob, DWORD wlen)
      +{
      +	LPCWSTR end;
      +	DWORD length;
     -+	LPCWSTR blob = (LPCWSTR)cred->CredentialBlob;
     -+	DWORD wlen = cred->CredentialBlobSize / sizeof(WCHAR);
      +
      +	/* check if content is single line (retain trailing carriage return) */
      +	if ((end = wmemchr(blob, L'\n', wlen)) == NULL) {
     @@ contrib/credential/wincred/git-credential-wincred.c: static void get_credential(
      +	/* search for the first credential that matches target and username */
      +	for (int i = 0; i < num_creds; ++i) {
      +		LPCWSTR username;
     ++		LPCWSTR blob;
      +
      +		if (!match_cred(creds[i], 0))
      +			continue;
     @@ contrib/credential/wincred/git-credential-wincred.c: static void get_credential(
      +		username = creds[i]->UserName;
      +		write_item("username", username, username ? wcslen(username) : 0);
      +
     -+		write_credential(creds[i]);
     ++		blob = (LPCWSTR)creds[i]->CredentialBlob;
     ++		write_credential_secret(blob, creds[i]->CredentialBlobSize / sizeof(*blob));
      +
      +		for (int j = 0; j < creds[i]->AttributeCount; j++) {
      +			CREDENTIAL_ATTRIBUTEW *attr = creds[i]->Attributes + j;


 .../wincred/git-credential-wincred.c          | 124 ++++++++++++------
 1 file changed, 85 insertions(+), 39 deletions(-)

diff --git a/contrib/credential/wincred/git-credential-wincred.c b/contrib/credential/wincred/git-credential-wincred.c
index 22eb27ca31..e2b40ef8c8 100644
--- a/contrib/credential/wincred/git-credential-wincred.c
+++ b/contrib/credential/wincred/git-credential-wincred.c
@@ -6,6 +6,7 @@
 #include <stdio.h>
 #include <io.h>
 #include <fcntl.h>
+#include <wchar.h>
 #include <wincred.h>
 
 /* common helpers */
@@ -69,6 +70,68 @@ static void write_item(const char *what, LPCWSTR wbuf, int wlen)
 	free(buf);
 }
 
+/*
+ * Write known credential item.
+ */
+static void write_credential_item(LPCWSTR data, int wlen)
+{
+	static const LPCWSTR refresh_token = L"oauth_refresh_token";
+	LPCWSTR value;
+	DWORD klen;
+	DWORD vlen;
+
+	/* find key/value separator for credential item */
+	if ((value = wmemchr(data, L'=', wlen)) == NULL)
+		return;
+	klen = value++ - data;
+	vlen = wlen - klen - 1;
+
+	/* write items known to git credential protocol */
+	if (klen == wcslen(refresh_token) && wmemcmp(data, refresh_token, klen) == 0)
+		write_item("oauth_refresh_token", value, vlen);
+}
+
+/*
+ * Write single credential block
+ * consisting of password and further (accepted) credential items.
+ */
+static void write_credential_secret(LPCWSTR blob, DWORD wlen)
+{
+	LPCWSTR end;
+	DWORD length;
+
+	/* check if content is single line (retain trailing carriage return) */
+	if ((end = wmemchr(blob, L'\n', wlen)) == NULL) {
+		write_item("password", blob, wlen);
+		return;
+	}
+	/* determine current line length and remaining data size */
+	length = end++ - blob;
+	wlen -= length + 1;
+
+	/* trim carriage return at line end */
+	if (length && blob[length - 1] == L'\r')
+		--length;
+	write_item("password", blob, length);
+	blob = end;
+
+	/* find line separator */
+	while ((end = wmemchr(blob, L'\n', wlen)) != NULL) {
+		length = end++ - blob;
+		wlen -= length + 1;
+
+		/* trim carriage return at line end */
+		if (length && blob[length - 1] == L'\r')
+			--length;
+
+		/* write item and advance blob position */
+		write_credential_item(blob, length);
+		blob = end;
+	}
+	/* process final line (retain trailing carriage return) */
+	write_credential_item(blob, wlen);
+}
+
 /*
  * Match an (optional) expected string and a delimiter in the target string,
  * consuming the matched text by updating the target pointer.
@@ -148,51 +211,34 @@ static void get_credential(void)
 {
 	CREDENTIALW **creds;
 	DWORD num_creds;
-	int i;
-	CREDENTIAL_ATTRIBUTEW *attr;
-	WCHAR *secret;
-	WCHAR *line;
-	WCHAR *remaining_lines;
-	WCHAR *part;
-	WCHAR *remaining_parts;
 
 	if (!CredEnumerateW(L"git:*", 0, &num_creds, &creds))
 		return;
 
-	/* search for the first credential that matches username */
-	for (i = 0; i < num_creds; ++i)
-		if (match_cred(creds[i], 0)) {
-			write_item("username", creds[i]->UserName,
-				creds[i]->UserName ? wcslen(creds[i]->UserName) : 0);
-			if (creds[i]->CredentialBlobSize > 0) {
-				secret = xmalloc(creds[i]->CredentialBlobSize + sizeof(WCHAR));
-				wcsncpy_s(secret, creds[i]->CredentialBlobSize, (LPCWSTR)creds[i]->CredentialBlob, creds[i]->CredentialBlobSize / sizeof(WCHAR));
-				line = wcstok_s(secret, L"\r\n", &remaining_lines);
-				write_item("password", line, line ? wcslen(line) : 0);
-				while(line != NULL) {
-					part = wcstok_s(line, L"=", &remaining_parts);
-					if (!wcscmp(part, L"oauth_refresh_token")) {
-						write_item("oauth_refresh_token", remaining_parts, remaining_parts ? wcslen(remaining_parts) : 0);
-					}
-					line = wcstok_s(NULL, L"\r\n", &remaining_lines);
-				}
-				free(secret);
-			} else {
-				write_item("password",
-						(LPCWSTR)creds[i]->CredentialBlob,
-						creds[i]->CredentialBlobSize / sizeof(WCHAR));
-			}
-			for (int j = 0; j < creds[i]->AttributeCount; j++) {
-				attr = creds[i]->Attributes + j;
-				if (!wcscmp(attr->Keyword, L"git_password_expiry_utc")) {
-					write_item("password_expiry_utc", (LPCWSTR)attr->Value,
-					attr->ValueSize / sizeof(WCHAR));
-					break;
-				}
+	/* search for the first credential that matches target and username */
+	for (int i = 0; i < num_creds; ++i) {
+		LPCWSTR username;
+		LPCWSTR blob;
+
+		if (!match_cred(creds[i], 0))
+			continue;
+
+		username = creds[i]->UserName;
+		write_item("username", username, username ? wcslen(username) : 0);
+
+		blob = (LPCWSTR)creds[i]->CredentialBlob;
+		write_credential_secret(blob, creds[i]->CredentialBlobSize / sizeof(*blob));
+
+		for (int j = 0; j < creds[i]->AttributeCount; j++) {
+			CREDENTIAL_ATTRIBUTEW *attr = creds[i]->Attributes + j;
+
+			if (!wcscmp(attr->Keyword, L"git_password_expiry_utc")) {
+				write_item("password_expiry_utc", (LPCWSTR)attr->Value, attr->ValueSize / sizeof(WCHAR));
+				break;
 			}
-			break;
 		}
-
+		break;
+	}
 	CredFree(creds);
 }
 

base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd
-- 
gitgitgadget

      parent reply	other threads:[~2026-10-14 20:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-09 13:45 [PATCH] wincred: fix line split of secret blob content Marc Becker via GitGitGadget
2026-10-09 21:16 ` Junio C Hamano
2026-10-10 11:47 ` [PATCH v2] wincred: refactor credential blob processing Marc Becker via GitGitGadget
2026-10-12 21:27 ` [PATCH v3] " Marc Becker via GitGitGadget
2026-10-12 21:57   ` Junio C Hamano
2026-10-14 18:16 ` [PATCH v4] " Marc Becker via GitGitGadget
2026-10-14 20:00 ` Marc Becker via GitGitGadget [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2251.v5.git.1792008058394.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=becm@gmx.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.