Skip to contentSkip to conversation

t5550: add netrc tests for http 401/403

PATCH Superseded Ashlesh Gawande 3 participants 14 messages Jan 6 – Feb 6

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 t5550: add netrc tests for http 401/403

---
Sending netrc test patches as suggested in: https://lore.kernel.org/git/[email protected]

 t/lib-httpd.sh             |  9 +++++++++
 t/lib-httpd/apache.conf    |  4 ++++
 t/lib-httpd/passwd         |  1 +
 t/t5550-http-fetch-dumb.sh | 25 +++++++++++++++++++++++++
 4 files changed, 39 insertions(+)
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 +25 -0
index ed0ad66fad..e002c7357d 100755
@@ -102,6 +102,31 @@ 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 (fail on 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-netrc-403 2>err &&
+	expect_askpass none &&
+	grep "The requested URL returned error: 403" err
+'
+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 &&

base-commit: e0bfec3dfc356f7d808eb5ee546a54116b794397

Signature · 1 line
2.43.0
#1 lore raw
Quoted 3 lines · Ashlesh Gawande

---
Sending netrc test patches as suggested in: https://lore.kernel.org/git/[email protected]

At the conceptual level, I am happy to have tests for features that we claim to support. It is a different matter if we want to support netrc, though ;-).

There are some nits.

Quoted 1 line

+set_netrc() {

Style. SP on both sides of (). I.e.

    set_netrc () {

t/lib-httpd.sh set_askpass() { · line 330

Quoted 2 lines
t/lib-httpd.sh +2 -0
+	# $HOME=$TRASH_DIRECTORY
+	echo "machine $1 login $2 password $3" > $TRASH_DIRECTORY/.netrc

Style. No space between the redirection operator ">" and redirection target.

Style. Enclose the redirection target inside a pair of double quotes if it involves variable interpolation. I.e.

	echo ... >"$TRASH_DIRECTORY/.netrc"

t/lib-httpd.sh set_askpass() { · line 333

Quoted 3 lines
t/lib-httpd.sh +3 -0
+}
+
+clear_netrc() {

Ditto.

t/lib-httpd.sh set_askpass() { · line 335

Quoted 2 lines
t/lib-httpd.sh +2 -0
+	rm "$TRASH_DIRECTORY/.netrc"
+}

Should this fail if .netrc did not exist in the first place, or is the primary purpose of this helper to ensure the file does not exist after it returns (in which case it would be desirable not to fail if the file did not exist when it was called, with "rm -f")?

Quoted 1 line

expect_askpass() {

Ditto.

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 111

Quoted 7 lines
t/t5550-http-fetch-dumb.sh +7 -0
+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

We try not to run random shell functions outside the test_expect_* blocks. A clean-up function like this is better called at the end of each piece, arranged with the test_when_finished helper.

	test_expect_success 'do random thing' '
		test_when_finished clear_netrc &&
		set_askpass wrong &&
		set_netrc ... &&
		...
	'
#2 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Ashlesh Gawande

---
Sending netrc test patches as suggested in: https://lore.kernel.org/git/[email protected]

At the conceptual level, I am happy to have tests for features that we claim to support. It is a different matter if we want to support netrc, though ;-).

There are some nits.

Thanks for the quick review!
I think Brian also suggested getting rid of netrc in the future. I have sent v2 to address your comments.

Quoted 21 lines
Quoted 2 lines

+set_netrc() {

Style. SP on both sides of (). I.e.

     set_netrc () {

t/lib-httpd.sh set_askpass() { · line 330

Quoted 2 lines
t/lib-httpd.sh +2 -0
+	# $HOME=$TRASH_DIRECTORY
+	echo "machine $1 login $2 password $3" > $TRASH_DIRECTORY/.netrc

Style. No space between the redirection operator ">" and redirection target.

Style. Enclose the redirection target inside a pair of double quotes if it involves variable interpolation. I.e.

	echo ... >"$TRASH_DIRECTORY/.netrc"

t/lib-httpd.sh set_askpass() { · line 333

Quoted 3 lines
t/lib-httpd.sh +3 -0
+}
+
+clear_netrc() {

Ditto.

Quoted 6 lines

t/lib-httpd.sh set_askpass() { · line 335

Quoted 2 lines
t/lib-httpd.sh +2 -0
+	rm "$TRASH_DIRECTORY/.netrc"
+}

Should this fail if .netrc did not exist in the first place, or is the primary purpose of this helper to ensure the file does not exist after it returns (in which case it would be desirable not to fail if the file did not exist when it was called, with "rm -f")?

Yes, the primary purpose is to just clear the file as it might potentially break other tests (added -f in v2).

Quoted 22 lines
Quoted 1 line
  expect_askpass() {

Ditto.

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 111

Quoted 7 lines
t/t5550-http-fetch-dumb.sh +7 -0
+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

We try not to run random shell functions outside the test_expect_* blocks. A clean-up function like this is better called at the end of each piece, arranged with the test_when_finished helper.

	test_expect_success 'do random thing' '
		test_when_finished clear_netrc &&
		set_askpass wrong &&
		set_netrc ... &&
		...
	'
#3 lore raw

PATCH v2 1/1 t5550: add netrc tests for http 401/403

In reply to Ashlesh Gawande

---
Range-diff against v1:
1: 27e112ea42 ! 1: 0b68f1d1af t5550: add netrc tests for http 401/403

    @@ Commit message
         Signed-off-by: Ashlesh Gawande <[email protected]>
     
      ## t/lib-httpd.sh ##
    -@@ t/lib-httpd.sh: set_askpass() {
    +@@ t/lib-httpd.sh: setup_askpass_helper() {
    + 	'
    + }
    + 
    +-set_askpass() {
    ++set_askpass () {
    + 	>"$TRASH_DIRECTORY/askpass-query" &&
    + 	echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
      	echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
      }
      
    -+set_netrc() {
    +-expect_askpass() {
    ++set_netrc () {
     +	# $HOME=$TRASH_DIRECTORY
    -+	echo "machine $1 login $2 password $3" > $TRASH_DIRECTORY/.netrc
    ++	echo "machine $1 login $2 password $3" >"$TRASH_DIRECTORY/.netrc"
     +}
     +
    -+clear_netrc() {
    -+	rm "$TRASH_DIRECTORY/.netrc"
    ++clear_netrc () {
    ++	rm -f "$TRASH_DIRECTORY/.netrc"
     +}
     +
    - expect_askpass() {
    ++expect_askpass () {
      	dest=$HTTPD_DEST${3+/$3}
      
    + 	{
     
      ## t/lib-httpd/apache.conf ##
     @@ t/lib-httpd/apache.conf: SSLEngine On
    @@ t/t5550-http-fetch-dumb.sh: test_expect_success 'cloning password-protected repo
      '
      
     +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
     +'
    -+clear_netrc
     +
     +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
     +'
    -+clear_netrc
     +
     +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
     +'
    -+clear_netrc
     +
      test_expect_success 'http auth can use user/pass in URL' '
      	set_askpass wrong &&
 t/lib-httpd.sh             | 13 +++++++++++--
 t/lib-httpd/apache.conf    |  4 ++++
 t/lib-httpd/passwd         |  1 +
 t/t5550-http-fetch-dumb.sh | 25 +++++++++++++++++++++++++
 4 files changed, 41 insertions(+), 2 deletions(-)
t/lib-httpd.sh +11 -2
index 5091db949b..5f42c311c2 100644
@@ -319,13 +319,22 @@ setup_askpass_helper() {
 	'
 }
 
-set_askpass() {
+set_askpass () {
 	>"$TRASH_DIRECTORY/askpass-query" &&
 	echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
 	echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
 }
 
-expect_askpass() {
+set_netrc () {
+	# $HOME=$TRASH_DIRECTORY
+	echo "machine $1 login $2 password $3" >"$TRASH_DIRECTORY/.netrc"
+}
+
+clear_netrc () {
+	rm -f "$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 +25 -0
index ed0ad66fad..9530f01b9e 100755
@@ -102,6 +102,31 @@ test_expect_success 'cloning password-protected repository can fail' '
 	expect_askpass both wrong
 '
 
+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
+'
+
+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
+'
+
+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
+'
+
 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
#4 lore raw

Ashlesh Gawande <[email protected]> writes:

Here between the title and your sign-off is a space to explain why it makes sense to add these new tests. One way to do so may be to explain that some cases were missing in the existing tests, and the new ones are added to cover those cases, i.e., what the new tests try to see under what situation, what behaviour do we expect out of the system, and why do we expect that behaviour?

Thanks.

Quoted 169 lines

---
Range-diff against v1:
1: 27e112ea42 ! 1: 0b68f1d1af t5550: add netrc tests for http 401/403

    @@ Commit message
         Signed-off-by: Ashlesh Gawande <[email protected]>
     
      ## t/lib-httpd.sh ##
    -@@ t/lib-httpd.sh: set_askpass() {
    +@@ t/lib-httpd.sh: setup_askpass_helper() {
    + 	'
    + }
    + 
    +-set_askpass() {
    ++set_askpass () {
    + 	>"$TRASH_DIRECTORY/askpass-query" &&
    + 	echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
      	echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
      }
      
    -+set_netrc() {
    +-expect_askpass() {
    ++set_netrc () {
     +	# $HOME=$TRASH_DIRECTORY
    -+	echo "machine $1 login $2 password $3" > $TRASH_DIRECTORY/.netrc
    ++	echo "machine $1 login $2 password $3" >"$TRASH_DIRECTORY/.netrc"
     +}
     +
    -+clear_netrc() {
    -+	rm "$TRASH_DIRECTORY/.netrc"
    ++clear_netrc () {
    ++	rm -f "$TRASH_DIRECTORY/.netrc"
     +}
     +
    - expect_askpass() {
    ++expect_askpass () {
      	dest=$HTTPD_DEST${3+/$3}
      
    + 	{
     
      ## t/lib-httpd/apache.conf ##
     @@ t/lib-httpd/apache.conf: SSLEngine On
    @@ t/t5550-http-fetch-dumb.sh: test_expect_success 'cloning password-protected repo
      '
      
     +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
     +'
    -+clear_netrc
     +
     +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
     +'
    -+clear_netrc
     +
     +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
     +'
    -+clear_netrc
     +
      test_expect_success 'http auth can use user/pass in URL' '
      	set_askpass wrong &&
 t/lib-httpd.sh             | 13 +++++++++++--
 t/lib-httpd/apache.conf    |  4 ++++
 t/lib-httpd/passwd         |  1 +
 t/t5550-http-fetch-dumb.sh | 25 +++++++++++++++++++++++++
 4 files changed, 41 insertions(+), 2 deletions(-)
t/lib-httpd.sh +11 -2
index 5091db949b..5f42c311c2 100644
@@ -319,13 +319,22 @@ setup_askpass_helper() {
 	'
 }
 
-set_askpass() {
+set_askpass () {
 	>"$TRASH_DIRECTORY/askpass-query" &&
 	echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
 	echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
 }
 
-expect_askpass() {
+set_netrc () {
+	# $HOME=$TRASH_DIRECTORY
+	echo "machine $1 login $2 password $3" >"$TRASH_DIRECTORY/.netrc"
+}
+
+clear_netrc () {
+	rm -f "$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 +25 -0
index ed0ad66fad..9530f01b9e 100755
@@ -102,6 +102,31 @@ test_expect_success 'cloning password-protected repository can fail' '
 	expect_askpass both wrong
 '
 
+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
+'
+
+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
+'
+
+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
+'
+
 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 &&
#5 lore raw

PATCH v3 1/1 t5550: add netrc tests for http 401/403

In reply to Ashlesh Gawande

git allows using .netrc file to supply credentials for HTTP auth. Three test cases are added in this patch to provide missing coverage when cloning over HTTP using .netrc file:

- First test case checks that the git clone is successful when credentials are provided via .netrc file
- Second test case checks that the git clone fails when the .netrc file provides invalid credentials. The HTTP server is expected to return 401 Unauthorized in such a case. The test checks that the user is provided with a prompt for username/password on 401 to provide the valid ones.
- 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.

---
Range-diff against v2:
1: 0b68f1d1af ! 1: 25ef751f28 t5550: add netrc tests for http 401/403
@@ Metadata
## Commit message ##
t5550: add netrc tests for http 401/403

+ git allows using .netrc file to supply credentials for HTTP auth.
+ Three test cases are added in this patch to provide missing coverage
+ when cloning over HTTP using .netrc file:
+
+ - First test case checks that the git clone is successful when credentials
+ are provided via .netrc file
+ - Second test case checks that the git clone fails when the .netrc file
+ provides invalid credentials. The HTTP server is expected to return
+ 401 Unauthorized in such a case. The test checks that the user is
+ provided with a prompt for username/password on 401 to provide
+ the valid ones.
+ - 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.
+

## t/lib-httpd.sh ##

 t/lib-httpd.sh             | 13 +++++++++++--
 t/lib-httpd/apache.conf    |  4 ++++
 t/lib-httpd/passwd         |  1 +
 t/t5550-http-fetch-dumb.sh | 25 +++++++++++++++++++++++++
 4 files changed, 41 insertions(+), 2 deletions(-)
t/lib-httpd.sh +11 -2
index 5091db949b..5f42c311c2 100644
@@ -319,13 +319,22 @@ setup_askpass_helper() {
 	'
 }
 
-set_askpass() {
+set_askpass () {
 	>"$TRASH_DIRECTORY/askpass-query" &&
 	echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
 	echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
 }
 
-expect_askpass() {
+set_netrc () {
+	# $HOME=$TRASH_DIRECTORY
+	echo "machine $1 login $2 password $3" >"$TRASH_DIRECTORY/.netrc"
+}
+
+clear_netrc () {
+	rm -f "$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 +25 -0
index ed0ad66fad..9530f01b9e 100755
@@ -102,6 +102,31 @@ test_expect_success 'cloning password-protected repository can fail' '
 	expect_askpass both wrong
 '
 
+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
+'
+
+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
+'
+
+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
+'
+
 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
#6 lore raw

Any other comments or suggestions on this patch that I can address? (re-sending because the mailing list rejected my previous email for not being plain text).

Thanks
Ashlesh

Quoted 147 lines · Ashlesh Gawande

git allows using .netrc file to supply credentials for HTTP auth. Three test cases are added in this patch to provide missing coverage when cloning over HTTP using .netrc file:

- First test case checks that the git clone is successful when credentials are provided via .netrc file
- Second test case checks that the git clone fails when the .netrc file provides invalid credentials. The HTTP server is expected to return 401 Unauthorized in such a case. The test checks that the user is provided with a prompt for username/password on 401 to provide the valid ones.
- 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.

---
Range-diff against v2:
1: 0b68f1d1af ! 1: 25ef751f28 t5550: add netrc tests for http 401/403
@@ Metadata
## Commit message ##
t5550: add netrc tests for http 401/403

+ git allows using .netrc file to supply credentials for HTTP auth.
+ Three test cases are added in this patch to provide missing coverage
+ when cloning over HTTP using .netrc file:
+
+ - First test case checks that the git clone is successful when credentials
+ are provided via .netrc file
+ - Second test case checks that the git clone fails when the .netrc file
+ provides invalid credentials. The HTTP server is expected to return
+ 401 Unauthorized in such a case. The test checks that the user is
+ provided with a prompt for username/password on 401 to provide
+ the valid ones.
+ - 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.
+

## t/lib-httpd.sh ##

  t/lib-httpd.sh             | 13 +++++++++++--
  t/lib-httpd/apache.conf    |  4 ++++
  t/lib-httpd/passwd         |  1 +
  t/t5550-http-fetch-dumb.sh | 25 +++++++++++++++++++++++++
  4 files changed, 41 insertions(+), 2 deletions(-)
t/lib-httpd.sh +11 -2
index 5091db949b..5f42c311c2 100644
@@ -319,13 +319,22 @@ setup_askpass_helper() {
  	'
  }
 
-set_askpass() {
+set_askpass () {
  	>"$TRASH_DIRECTORY/askpass-query" &&
  	echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
  	echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
  }
 
-expect_askpass() {
+set_netrc () {
+	# $HOME=$TRASH_DIRECTORY
+	echo "machine $1 login $2 password $3" >"$TRASH_DIRECTORY/.netrc"
+}
+
+clear_netrc () {
+	rm -f "$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 +25 -0
index ed0ad66fad..9530f01b9e 100755
@@ -102,6 +102,31 @@ test_expect_success 'cloning password-protected repository can fail' '
  	expect_askpass both wrong
  '
 
+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
+'
+
+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
+'
+
+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
+'
+
  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 &&
#7 lore raw
In reply to Ashlesh Gawande
Quoted 4 lines · Ashlesh Gawande

git allows using .netrc file to supply credentials for HTTP auth. Three test cases are added in this patch to provide missing coverage when cloning over HTTP using .netrc file:

Quoted 16 lines

- First test case checks that the git clone is successful when credentials are provided via .netrc file
- Second test case checks that the git clone fails when the .netrc file provides invalid credentials. The HTTP server is expected to return 401 Unauthorized in such a case. The test checks that the user is provided with a prompt for username/password on 401 to provide the valid ones.
- 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.

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

Quoted 28 lines
t/t5550-http-fetch-dumb.sh +25 -0
+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
+'
+
+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
+'
+
+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
+'
+
 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 &&
#8 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 9 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.

Quoted 3 lines

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.

Quoted 2 lines

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:

Quoted 8 lines

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 128

Quoted 8 lines
t/t5550-http-fetch-dumb.sh +8 -0
+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:

Quoted 7 lines

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 119

Quoted 7 lines
t/t5550-http-fetch-dumb.sh +7 -0
+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:

Quoted 7 lines

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 111

Quoted 7 lines
t/t5550-http-fetch-dumb.sh +7 -0
+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

#9 lore raw
Quoted 11 lines · Jeff King
Quoted 9 lines · Junio C Hamano
Quoted 9 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.

Quoted 8 lines
Quoted 3 lines

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.

In the case of fine-grained access token such that the token has read access to the repository but not write access GitHub does return a 403.
(I think this is correct behavior as the token has read access so user is authorized/knows about the repository).

Quoted 61 lines
Quoted 2 lines

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:

Quoted 8 lines

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 128

Quoted 8 lines
t/t5550-http-fetch-dumb.sh +8 -0
+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:

Quoted 7 lines

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 119

Quoted 7 lines
t/t5550-http-fetch-dumb.sh +7 -0
+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:

Quoted 7 lines

t/t5550-http-fetch-dumb.sh test_expect_success 'cloning password-protected repository can fail' ' · line 111

Quoted 7 lines
t/t5550-http-fetch-dumb.sh +7 -0
+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

#10 lore raw
Quoted 16 lines · Ashlesh Gawande
Quoted 14 lines · Jeff King
Quoted 12 lines · Junio C Hamano
Quoted 9 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.

Quoted 8 lines
Quoted 5 lines

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.

In the case of fine-grained access token such that the token has read access to the repository but not write access GitHub does return a 403.
(I think this is correct behavior as the token has read access so user is authorized/knows about the repository).

So should I modify that test case to do a push instead for this specific scenario (and update the description)?

Quoted 67 lines
Quoted 66 lines
Quoted 2 lines

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:

Quoted 10 lines
Quoted 10 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:

Quoted 9 lines
Quoted 9 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:

Quoted 8 lines
Quoted 8 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

#11 lore raw
Quoted 10 lines · Ashlesh Gawande
Quoted 10 lines
Quoted 5 lines

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.

In the case of fine-grained access token such that the token has read access to the repository
but not write access GitHub does return a 403.
(I think this is correct behavior as the token has read access so user is authorized/knows about the repository).

Ah, that makes sense.

Quoted 2 lines

So should I modify that test case to do a push instead for this specific scenario (and update the description)?

No, I think what you have is fine. From the client's perspective, they know only that they got a 403 for some reason. So there's no need for complex modeling of what the server thinks is going on.

-Peff

#12 lore raw
In reply to Jeff King
Quoted 5 lines · Jeff King

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.

That is a sensible thing to do on the server side. Presumably when we talk with such a server we would report 404, right? It is not like we behave all that differently with either type of errors---as long as we just give up and do not fall into an infinite loop of asking "oops, that password did not work, try again", it would be OK.

Quoted 5 lines
Quoted 2 lines

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.

Yeah, I phrased my question in a wrong way. As the code paths involving credential helpers are separate, I wondered if we have similar test coverage there as well.

Quoted 5 lines

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.

#13 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Jeff King

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.

Quoted 6 lines

That is a sensible thing to do on the server side. Presumably when we talk with such a server we would report 404, right? It is not like we behave all that differently with either type of errors---as long as we just give up and do not fall into an infinite loop of asking "oops, that password did not work, try again", it would be OK.

Right, we'd report the 404. We never loop on trying to authenticate, but do a maximum of two tries (and then only if we get a 401 on the first request and did not already provide a credential ourselves to curl). Curl might make multiple requests under the hood for each "try", but we won't even know about them.

And all of that is independent of which HTTP error code was returned (except for 401, obviously). We do eventually produce a different message for 404 vs a 403, but that's at the top-level of remote-curl.c.

The interesting bits are in http_request_reauth(), though some of the logic is in handle_curl_result().

Quoted 6 lines
Quoted 2 lines

The netrc support here should not involve credential helpers at all. It is all being done internally by curl.

Yeah, I phrased my question in a wrong way. As the code paths involving credential helpers are separate, I wondered if we have similar test coverage there as well.

The workings are hopefully covered by the explanation above. As far as test coverage, I think t5550 covers this already. When we provide the wrong password, we bail rather than asking repeatedly (e.g., in "cloning password-protected repository can fail").

-Peff