Skip to contentSkip to conversation

http.c: prompt for username on 403

PATCH In review Ashlesh Gawande 3 participants 11 messages Oct 14, 2025 – Dec 11, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 http.c: prompt for username on 403

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

---
 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(-)
http.c +1 -1
index 7e3af1e72f..18959f63b9 100644
@@ -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) {
t/lib-httpd.sh +9 -0
index 5091db949b..cdc92b2916 100644
@@ -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}
 
t/lib-httpd/apache.conf +4 -0
index e631ab0eb5..6b8c50a51a 100644
@@ -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$">
t/lib-httpd/passwd +1 -0
index d9c122f348..3bab7b6423 100644
@@ -1 +1,2 @@
 user@host:$apr1$LGPmCZWj$9vxEwj5Z5GzQLBMxp3mCx1
+forbidden-user@host:$apr1$LGPmCZWj$9vxEwj5Z5GzQLBMxp3mCx1
t/t5550-http-fetch-dumb.sh +24 -0
index ed0ad66fad..6c4c1cafb2 100755
@@ -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 &&
Signature · 1 line
2.43.0
#1 lore raw
Quoted 30 lines · Ashlesh Gawande

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

---
 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(-)
http.c +0 -0
index 7e3af1e72f..18959f63b9 100644

http.c static int handle_curl_result(struct slot_results *results) · line 1855

Quoted 5 lines
http.c +1 -1
@@ -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) {

I don't think this is a good idea. Existing servers send a 401 when no credentials are available and 403 if credentials are sent but are not valid for a repository. The former case causes credentials to be erased, but the latter does not.

Your proposal will cause someone's credentials to be erased just because they don't have access to a repository, which would be bad because it's not that the credentials are invalid (that would be a 401) but that the credentials are not usable for that repository or for that operation.

So if I attempt to push to https://github.com/git/git.git, then my credentials will be erased even though there are no valid credentials that could possibly grant me access to that repository (because I'm not Junio). Then _none_ of my pushes work because my token is gone.

I agree that it's inconvenient that netrc credential override other credentials, but the proper thing to do would be to (a) not share working trees among users (since Git's security model doesn't allow for that), (b) not use netrc for this purpose and use a credential helper,
(c) add functionality to disable netrc via config, or (d) use an SSH deploy key for automated systems with GIT_SSH_COMMAND and ssh -i and have developers forward their SSH agent to push.

Signature · 2 lines
brian m. carlson (they/them)
Toronto, Ontario, CA
#2 lore raw
Quoted 51 lines · brian m. carlson
Quoted 30 lines · Ashlesh Gawande

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

---
  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(-)
http.c +0 -0
index 7e3af1e72f..18959f63b9 100644

http.c static int handle_curl_result(struct slot_results *results) · line 1855

Quoted 5 lines
http.c +1 -1
@@ -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) {

I don't think this is a good idea. Existing servers send a 401 when no credentials are available and 403 if credentials are sent but are not valid for a repository. The former case causes credentials to be erased, but the latter does not.

Your proposal will cause someone's credentials to be erased just because they don't have access to a repository, which would be bad because it's not that the credentials are invalid (that would be a 401) but that the credentials are not usable for that repository or for that operation.

So if I attempt to push to https://github.com/git/git.git, then my credentials will be erased even though there are no valid credentials that could possibly grant me access to that repository (because I'm not Junio). Then _none_ of my pushes work because my token is gone.

Quoted 7 lines

I agree that it's inconvenient that netrc credential override other credentials, but the proper thing to do would be to (a) not share working trees among users (since Git's security model doesn't allow for that), (b) not use netrc for this purpose and use a credential helper,
(c) add functionality to disable netrc via config, or (d) use an SSH deploy key for automated systems with GIT_SSH_COMMAND and ssh -i and have developers forward their SSH agent to push.

Oh I see - yeah don't want to erase the credentials.
Was trying to figure why 403 was happening instead of a prompt (as I was not aware of netrc file being used). Thanks for the detailed explanation and suggestions Brian!

Is it worth it to include the netrc tests in git that I wrote as part of this

(if so I can email them as a separate patch)?

Thanks
Ashlesh

#3 lore raw
Quoted 7 lines · Ashlesh Gawande

Oh I see - yeah don't want to erase the credentials.
Was trying to figure why 403 was happening instead of a prompt (as I was not aware of netrc file being used).
Thanks for the detailed explanation and suggestions Brian!

Is it worth it to include the netrc tests in git that I wrote as part of this

Yes, I think if you have patches to test our netrc handling, those would be very welcome. I was complaining a couple months ago about how we had no tests for netrc after I accidentally broke the code that makes it work, so I would very much appreciate any tests we could add to make that less likely in the future.

Signature · 2 lines
brian m. carlson (they/them)
Toronto, Ontario, CA
#4 lore raw
Quoted 6 lines · brian m. carlson
Quoted 5 lines · Ashlesh Gawande

Oh I see - yeah don't want to erase the credentials.
Was trying to figure why 403 was happening instead of a prompt (as I was not aware of netrc file being used).
Thanks for the detailed explanation and suggestions Brian!

Quoted 7 lines
Quoted 2 lines

Is it worth it to include the netrc tests in git that I wrote as part of this

Yes, I think if you have patches to test our netrc handling, those would be very welcome. I was complaining a couple months ago about how we had no tests for netrc after I accidentally broke the code that makes it work, so I would very much appreciate any tests we could add to make that less likely in the future.

I was working on separating the tests and thought about the original proposal a bit more. To stop the credentials from being erased on 403 could something like the following be acceptable?

        else if (results->http_code == 401 || results->http_code == 403) { if ((http_auth.username && http_auth.password) ||\
                    (http_auth.authtype && http_auth.credential)) { +                       // Do not override existing credentials on 403 +                       if (results->http_code == 403) {
+                               return HTTP_ERROR;
+                       }
+
                        if (http_auth.multistage) {

So then we would prompt on 403 only if credentials are not configured.

#5 lore raw
Quoted 15 lines · Ashlesh Gawande

I was working on separating the tests and thought about the original proposal a bit more.
To stop the credentials from being erased on 403 could something like the following be acceptable?

        else if (results->http_code == 401 || results->http_code == 403) { if ((http_auth.username && http_auth.password) ||\
                    (http_auth.authtype && http_auth.credential)) { +                       // Do not override existing credentials on 403 +                       if (results->http_code == 403) {
+                               return HTTP_ERROR;
+                       }
+
                        if (http_auth.multistage) {

Quoted 1 line

So then we would prompt on 403 only if credentials are not configured.

Can you tell me what file you see this in? I don't actually see any place in the code that has "http_code == 403" in the latest version of the main branch.

I wonder if your issue may already be fixed in a newer version than you have.

Signature · 2 lines
brian m. carlson (they/them)
Toronto, Ontario, CA
#6 lore raw
Quoted 16 lines · brian m. carlson
Quoted 15 lines · Ashlesh Gawande

I was working on separating the tests and thought about the original proposal a bit more.
To stop the credentials from being erased on 403 could something like the following be acceptable?

        else if (results->http_code == 401 || results->http_code == 403) { if ((http_auth.username && http_auth.password) ||\
                    (http_auth.authtype && http_auth.credential)) {
+                       // Do not override existing credentials on 403 +                       if (results->http_code == 403) {
+                               return HTTP_ERROR;
+                       }
+
                        if (http_auth.multistage) {

Quoted 7 lines
Quoted 1 line

So then we would prompt on 403 only if credentials are not configured.

Can you tell me what file you see this in? I don't actually see any place in the code that has "http_code == 403" in the latest version of the main branch.

I wonder if your issue may already be fixed in a newer version than you have.

Oh, that http_code == 403 is my original proposal to prompt for username/password on 403 (I did the diff on top of that instead of base).
But you pointed out that it would wipe out existing credentials. This is an attempt to fix that by not prompting on 403 if git credentials are set. So when credentials are provided through default netrc file (such that http_auth.* are not set; git credential helper is not set) then we can still get the prompt on 403.

#7 lore raw
Quoted 26 lines · Ashlesh Gawande
Quoted 18 lines · brian m. carlson
Quoted 17 lines · Ashlesh Gawande

I was working on separating the tests and thought about the original proposal a bit more.
To stop the credentials from being erased on 403 could something like the following be acceptable?

         else if (results->http_code == 401 || results->http_code ==
403) {
                 if ((http_auth.username && http_auth.password) ||\
                     (http_auth.authtype && http_auth.credential)) {
(quoted diff) +6 -0
+                       // Do not override existing credentials on
+403
+                       if (results->http_code == 403) {
+                               return HTTP_ERROR;
+                       }
+
                         if (http_auth.multistage) {
Quoted 7 lines
Quoted 1 line

So then we would prompt on 403 only if credentials are not configured.

Can you tell me what file you see this in? I don't actually see any place in the code that has "http_code == 403" in the latest version of the main branch.

I wonder if your issue may already be fixed in a newer version than you have.

Quoted 7 lines

Oh, that http_code == 403 is my original proposal to prompt for username/password on 403 (I did the diff on top of that instead of base). But you pointed out that it would wipe out existing credentials. This is an attempt to fix that by not prompting on 403 if git credentials are set.
So when credentials are provided through default netrc file (such that http_auth.* are not set; git credential helper is not set) then we can still get the prompt on 403.

Please make sure that any existing git credential helpers, including custom helpers are not impacted by this change. This would have serious negative consequences and would be a blocker for many in my community who use both the standard git credential helpers and custom ones. If you are going to force a credential wipe this should cause an update to the credential protocol to inform the helper that a wipe has occurred or is requested. The .netrc approach is most limited to Linux implementations and is not available on or applicable to some other platforms.

Thank you for your attention.
Randall

#8 lore raw
In reply to Ashlesh Gawande
Quoted 7 lines · Ashlesh Gawande

Oh, that http_code == 403 is my original proposal to prompt for username/password on 403 (I did the diff on top of that instead of base). But you pointed out that it would wipe out existing credentials. This is an attempt to fix that by not prompting on 403 if git credentials are set. So when credentials are provided through default netrc file (such that http_auth.* are not set; git credential helper is not set) then we can still get the prompt on 403.

As Randall said, I don't think it's a good idea to do this. It's a major change in how functionality works and it will probably break users.

I did mention before that a better approach is to add a config to decide whether to honour the netrc and I think that would be the right choice here. That lets people opt into different behaviour if they want it (and, to be honest, I _do_ very much want to skip netrc for Git credentials since I have similar problems as the ones you're describing) and avoids breaking things for existing users.

Signature · 2 lines
brian m. carlson (they/them)
Toronto, Ontario, CA
#9 lore raw
Quoted 12 lines · brian m. carlson
Quoted 7 lines · Ashlesh Gawande

Oh, that http_code == 403 is my original proposal to prompt for username/password on 403 (I did the diff on top of that instead of base). But you pointed out that it would wipe out existing credentials. This is an attempt to fix that by not prompting on 403 if git credentials are set. So when credentials are provided through default netrc file (such that http_auth.* are not set; git credential helper is not set) then we can still get the prompt on 403.

As Randall said, I don't think it's a good idea to do this. It's a major change in how functionality works and it will probably break users.

Quoted 6 lines

I did mention before that a better approach is to add a config to decide whether to honour the netrc and I think that would be the right choice here. That lets people opt into different behaviour if they want it (and, to be honest, I _do_ very much want to skip netrc for Git credentials since I have similar problems as the ones you're describing) and avoids breaking things for existing users.

Hmm, okay I understand. Yes probably good idea to skip netrc for Git credentials. Thank you for your input Brian and Randall!

#10 lore raw
In reply to brian m. carlson
Quoted 16 lines · brian m. carlson
Quoted 15 lines · Ashlesh Gawande

I was working on separating the tests and thought about the original proposal a bit more.
To stop the credentials from being erased on 403 could something like the following be acceptable?

        else if (results->http_code == 401 || results->http_code == 403) { if ((http_auth.username && http_auth.password) ||\
                    (http_auth.authtype && http_auth.credential)) {
+                       // Do not override existing credentials on 403 +                       if (results->http_code == 403) {
+                               return HTTP_ERROR;
+                       }
+
                        if (http_auth.multistage) {

Quoted 7 lines
Quoted 1 line

So then we would prompt on 403 only if credentials are not configured.

Can you tell me what file you see this in? I don't actually see any place in the code that has "http_code == 403" in the latest version of the main branch.

I wonder if your issue may already be fixed in a newer version than you have.

Oh, that http_code == 403 is my original proposal to prompt for username/password on 403 (I did the diff on top of that instead of base).
But you pointed out that it would wipe out existing credentials. This is an attempt to fix that by not prompting on 403 if git credentials are set. So when credentials are provided through default netrc file (such that http_auth.* are not set; git credential helper is not set) then we can still get the prompt on 403.