git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3] t5550: add netrc tests for http 401/403

From
Jeff King <peff@peff.net>
Date
Feb 6, 2026, 09:38 UTC
Message-ID
<20260206093840.GC2761602@coredump.intra.peff.net>
In-Reply-To
<xmqqms1mihqo.fsf@gitster.g>
On Thu, Feb 05, 2026 at 09:05:51PM -0800, Junio C Hamano wrote:
Show 13 quoted lines
> >   - Third test case checks that the git clone fails when the .netrc file
> >     provides credentials that are valid but do not have permission for
> >     this user. For example one may have multiple tokens in GitHub
> >     and uses the one which was not authorized for cloning this repo.
> >     In such a case the HTTP server returns 403 Forbidden.
> >     For this test, the apache.conf is modified to return a 403
> >     on finding a forbidden-user. No prompt for username/password is
> >     expected after the 403 (unlike 401). This is because prompting may wipe
> >     out existing credentials or conflict with custom credential helpers.
> 
> Nicely summarised.  So we say 401 when we do not know you, while we
> say 403 when we know you and do not want you to be accessing the
> resource.  We test for both.

I think it is fine to check the 403 handling, but note that this _isn't_ how GitHub would respond. If you try to fetch from a repository you don't have access to, it will return a 401 first (so you try to log in) and then a 404. The idea being to avoid revealing the existence of the repository to unauthorized users.

> Just out of curiosity, do we test for these codes with other
> credential helpers or is this only relevant for .netrc users?

The netrc support here should not involve credential helpers at all. It is all being done internally by curl. So in this (third and final) test:

Show 8 quoted lines
> > +test_expect_success 'netrc authorized but forbidden credentials (fail on 403)' '
> > +	test_when_finished clear_netrc &&
> > +	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-netrc-403 2>err &&
> > +	expect_askpass none &&
> > +	grep "The requested URL returned error: 403" err
> > +'
...what is happening is roughly:
  - curl sends the first request with no credentials, which gets a 401
  - curl internally, without returning a response to Git, looks up the
    netrc value and repeats the request with an Authorization header
  - curl returns the resulting 403 to Git
  - Git calls this an error (just like it would a 404) and bails

But from Git's perspective the use of netrc here is not really interesting. We don't even know it happened! And if the server did return a 401, we'd happily try to get credentials (from the user or from a helper) in the usual way. And that's what happens in the second test:

Show 7 quoted lines
> > +test_expect_success 'netrc unauthorized credentials (prompt after 401)' '
> > +	test_when_finished clear_netrc &&
> > +	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
> > +'

Curl tries the credential under the hood, but we have no idea, and we process a 401 in the usual way.

And in the first one:
Show 7 quoted lines
> > +test_expect_success 'using credentials from netrc to clone successfully' '
> > +	test_when_finished clear_netrc &&
> > +	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
> > +'

We do not ever even see the 401, and curl just magically handles it for us. We see only the successful 200 code, just as if authentication was not required in the first place.

So really, none of this is testing anything novel in Git at all that is not covered elsewhere, except for the fact that we pass the flag to curl that says "you may use netrc". And so there's some value in adding it in that case. But trying to answer your question about other credential helpers, no, they're not even entering the picture here.

-Peff
Previous: Junio C HamanoNext: Ashlesh Gawande
Message 9 of 14 in “t5550: add netrc tests for http 401/403”
  1. t5550: add netrc tests for http 401/403Ashlesh Gawande, Jan 6, 2026
  2. Junio C HamanoJan 6, 2026
  3. Ashlesh GawandeJan 6, 2026
  4. t5550: add netrc tests for http 401/403Ashlesh Gawande, Jan 6, 2026
  5. Junio C HamanoJan 7, 2026
  6. t5550: add netrc tests for http 401/403Ashlesh Gawande, Jan 7, 2026
  7. Ashlesh GawandeJan 31, 2026
  8. Junio C HamanoFeb 6, 2026
  9. Jeff KingFeb 6, 2026
  10. Ashlesh GawandeFeb 6, 2026
  11. Ashlesh GawandeFeb 6, 2026
  12. Jeff KingFeb 6, 2026
  13. Junio C HamanoFeb 6, 2026
  14. Jeff KingFeb 6, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.