From: Ashlesh Gawande <git@ashlesh.me>
To: git@vger.kernel.org
Cc: "Ashlesh Gawande" <git@ashlesh.me>,
"Patrick Steinhardt" <ps@pks.im>,
"Junio C Hamano" <gitster@pobox.com>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
"brian m. carlson" <sandals@crustytoothpaste.net>
Subject: [PATCH] http.c: prompt for username on 403
Date: Tue, 14 Oct 2025 20:13:52 +0530 [thread overview]
Message-ID: <20251014144354.1457818-2-git@ashlesh.me> (raw)
Scenario:
- There are a few pre-production systems that a lot of testers and
developers need to time share because of low availability
- Devops generates a GitHub token with pull only access
and adds it to the netrc file on these systems
(Pull only as we don't want testers/others to be able to push)
- Testers log in and do a git pull for the latest changes
(via netrc credentials - though testers may not be aware)
- Developers login to debug issues and may make fixes to the test repo
- Now when developers try to push their changes they receive:
fatal: unable to access 'https://github.com/<org>/<project>/':
The requested URL returned error: 403
- The developer is not given the chance to supply an authorized token
and either needs to comment the netrc file or copy the changes over
to their own machine
Signed-off-by: Ashlesh Gawande <git@ashlesh.me>
---
http.c | 2 +-
t/lib-httpd.sh | 9 +++++++++
t/lib-httpd/apache.conf | 4 ++++
t/lib-httpd/passwd | 1 +
t/t5550-http-fetch-dumb.sh | 24 ++++++++++++++++++++++++
5 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/http.c b/http.c
index 7e3af1e72f..18959f63b9 100644
--- a/http.c
+++ b/http.c
@@ -1852,7 +1852,7 @@ static int handle_curl_result(struct slot_results *results)
return HTTP_NOMATCHPUBLICKEY;
} else if (missing_target(results))
return HTTP_MISSING_TARGET;
- else if (results->http_code == 401) {
+ else if (results->http_code == 401 || results->http_code == 403) {
if ((http_auth.username && http_auth.password) ||\
(http_auth.authtype && http_auth.credential)) {
if (http_auth.multistage) {
diff --git a/t/lib-httpd.sh b/t/lib-httpd.sh
index 5091db949b..cdc92b2916 100644
--- a/t/lib-httpd.sh
+++ b/t/lib-httpd.sh
@@ -325,6 +325,15 @@ set_askpass() {
echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
}
+set_netrc() {
+ # $HOME=$TRASH_DIRECTORY
+ echo "machine $1 login $2 password $3" > $TRASH_DIRECTORY/.netrc
+}
+
+clear_netrc() {
+ rm "$TRASH_DIRECTORY/.netrc"
+}
+
expect_askpass() {
dest=$HTTPD_DEST${3+/$3}
diff --git a/t/lib-httpd/apache.conf b/t/lib-httpd/apache.conf
index e631ab0eb5..6b8c50a51a 100644
--- a/t/lib-httpd/apache.conf
+++ b/t/lib-httpd/apache.conf
@@ -238,6 +238,10 @@ SSLEngine On
AuthName "git-auth"
AuthUserFile passwd
Require valid-user
+
+ # return 403 for authenticated user: forbidden-user@host
+ RewriteCond "%{REMOTE_USER}" "^forbidden-user@host"
+ RewriteRule ^ - [F]
</Location>
<LocationMatch "^/auth-push/.*/git-receive-pack$">
diff --git a/t/lib-httpd/passwd b/t/lib-httpd/passwd
index d9c122f348..3bab7b6423 100644
--- a/t/lib-httpd/passwd
+++ b/t/lib-httpd/passwd
@@ -1 +1,2 @@
user@host:$apr1$LGPmCZWj$9vxEwj5Z5GzQLBMxp3mCx1
+forbidden-user@host:$apr1$LGPmCZWj$9vxEwj5Z5GzQLBMxp3mCx1
diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
index ed0ad66fad..6c4c1cafb2 100755
--- a/t/t5550-http-fetch-dumb.sh
+++ b/t/t5550-http-fetch-dumb.sh
@@ -102,6 +102,30 @@ test_expect_success 'cloning password-protected repository can fail' '
expect_askpass both wrong
'
+test_expect_success 'using credentials from netrc to clone successfully' '
+ set_askpass wrong &&
+ set_netrc 127.0.0.1 user@host pass@host &&
+ git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-netrc &&
+ expect_askpass none
+'
+clear_netrc
+
+test_expect_success 'netrc unauthorized credentials (prompt after 401)' '
+ set_askpass wrong &&
+ set_netrc 127.0.0.1 user@host pass@wrong &&
+ test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-netrc-401 &&
+ expect_askpass both wrong
+'
+clear_netrc
+
+test_expect_success 'netrc authorized but forbidden credentials (prompt after 403)' '
+ set_askpass wrong &&
+ set_netrc 127.0.0.1 forbidden-user@host pass@host &&
+ test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-netc-403 &&
+ expect_askpass both wrong
+'
+clear_netrc
+
test_expect_success 'http auth can use user/pass in URL' '
set_askpass wrong &&
git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
--
2.43.0
next reply other threads:[~2025-10-14 14:55 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-14 14:43 Ashlesh Gawande [this message]
2025-10-14 21:29 ` [PATCH] http.c: prompt for username on 403 brian m. carlson
2025-10-15 14:12 ` Ashlesh Gawande
2025-10-15 22:31 ` brian m. carlson
2025-12-09 8:22 ` Ashlesh Gawande
2025-12-10 2:05 ` brian m. carlson
2025-12-10 12:30 ` Ashlesh Gawande
2025-12-10 17:48 ` rsbecker
2025-12-10 22:28 ` brian m. carlson
2025-12-11 6:05 ` Ashlesh Gawande
2025-12-10 12:32 ` Ashlesh Gawande
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251014144354.1457818-2-git@ashlesh.me \
--to=git@ashlesh.me \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ps@pks.im \
--cc=sandals@crustytoothpaste.net \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).