gitweb: Use Git credential helper when cloning URLs with credentials

This prevents logging usernames and passwords to the journal logs and to the
Git repo configuration. Also, avoids usernames and passwords appear in the
process list when cloning a repository.

Tests performed:
- Create a new repository by cloning an existing repository URL with basic
auth credentials. Check that:
  - Cloning succeeds.
  - Journal logs don't contain URLs with credential info.
  - The configuration of the cloned repository doesn't contain credential info.
- Try to clone a non-existing repository URL that contains credential
info. Cloning fails and there are no credential info in the journal logs.
- Cloning a public git repository without credential info succeeds.
- All the gitweb module tests pass.

Signed-off-by: Veiko Aasa <veiko17@disroot.org>
[sunil: Add/fix some more type hints]
[sunil: Add tests for URL parsing]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Veiko Aasa authored and Sunil Mohan Adapa committed 2025-09-29 16:42:17 -07:00
1 parent e38ac648fe
commit dc837bd6b8
3 files changed
+81 -24

No files matched your search

@@ -2,6 +2,8 @@
"""Test module for gitweb module operations."""
import pathlib
import subprocess
from unittest.mock import call, patch
import pytest
from django.forms import ValidationError
@@ -121,3 +123,26 @@ def test_action_create_repo_with_invalid_urls(url):
with pytest.raises(ValidationError):
privileged.create_repo(url=url, description='', owner='',
keep_ownership=True)
@patch('plinth.action_utils.run')
def test_setup_git_creentials(run):
"""Test that setting up git credentials works."""
url = 'https://user:pass@host.example/path?key=value'
safe_url = privileged._setup_git_credentials(url)
assert safe_url == 'https://host.example/path?key=value'
input_ = b'protocol=https\nhost=host.example\nusername=user\n' \
b'password=pass\n\n'
env = run.mock_calls[0].kwargs.pop('env')
assert env['GIT_TERMINAL_PROMPT'] == '0'
assert run.mock_calls == [
call(['git', 'credential', 'approve'], input=input_,
stdout=subprocess.DEVNULL, check=True)
]
run.reset_mock()
url = 'https://host2.example/path?key=value'
safe_url = privileged._setup_git_credentials(url)
assert safe_url == 'https://host2.example/path?key=value'
run.assert_not_called()