fix(push): redact URL credentials and close the review findings of PR 18

quality clean.default-value: trusted_repo passes --object-format only when the checkout sets extensions.objectFormat; _old_tip refuses an unsigned HEAD only when HEAD is in the pushed range, without a .get fallback. CodeRabbit: _config_all reads git config -z --get-all and splits on NUL, so one pushurl value with a newline stays one value and url_allowed rejects it; file_url uses Path.as_uri; redact_urls replaces URL userinfo with *** in the GitRunError text of _tgit_run and in every push.py refusal, failure, preview and status line while the argv given to git is unchanged; fetch_heads splits ls-remote records at the TAB; _track sets and reads the upstream of the validated branch. Tests: the mirror fixture drops GIT_DIR, GIT_WORK_TREE and GIT_INDEX_FILE, the planted-after-check test asserts the second check ran, and test_push_trusted_review.py covers each fix.

| 🌿 Branch | 📊 Diff stat | 🎯 Fix | 💥 Break | 💸 Tokens | ⏱️ Velocity | 🛡️ Security | 🏆 Score | Gate |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| fix/push-trusted-gitdir | 5 files +173/-22 | 90 % | 5 % | – | – | 100 % | 86 | ✅ 90/5, tokens and velocity <= 100, security 100 % |

Evidence: tests/unit + tests/integration with PYTHONPATH=src 860 passed; the 33 fake-SSH push fixture tests fail identically on the previous HEAD in the agent sandbox and pass in PR CI, so PR CI is the proof for the push paths; 2 known gpg-in-sandbox failures in test_publish_integration. quality check --staged 0 findings, ruff clean, MEDUSA 100 %, critical 0. Hub review f31edb8114253cb6.

Refs https://github.com/kevinveenbirkenbach/package-manager/pull/18

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-30 23:34:02 +00:00
1 parent 6c7ca613fc
commit db7532f9b4
5 files changed
+173 -22

No files matched your search

+6 -2
View File
@@ -56,12 +56,16 @@ class SignedRemotesCase(unittest.TestCase):
self.gitconfig = root / ".gitconfig"
self._write_gitconfig(root)
self.env = {
**os.environ,
**{
key: value
for key, value in os.environ.items()
if key not in ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE")
},
"GIT_CONFIG_GLOBAL": str(self.gitconfig),
"GIT_CONFIG_NOSYSTEM": "1",
}
for patcher in (
patch.dict(os.environ, self.env),
patch.dict(os.environ, self.env, clear=True),
patch("pkgmgr.core.git.run._passwd_home", return_value=self.root),
):
patcher.start()
@@ -95,7 +95,7 @@ class TestPushTrustedGitdir(SignedRemotesCase):
if e["sid"] in pushes and _repo_param(e, prefixes)
]
def _push_with_config_planted_after_check(self, check: int) -> None:
def _push_with_config_planted_after_check(self, check: int) -> int:
self._commit("feature")
real = push_module.local_config
calls: list[str] = []
@@ -110,6 +110,7 @@ class TestPushTrustedGitdir(SignedRemotesCase):
with patch.object(push_module, "local_config", check_then_plant):
self._push()
return len(calls)
def test_vector1_repository_hooks_never_run(self) -> None:
hooks = (
@@ -315,8 +316,9 @@ class TestPushTrustedGitdir(SignedRemotesCase):
self.assertEqual(self._tip(self.mirror), self.init)
def test_vector10_config_planted_after_the_push_check_never_runs(self) -> None:
self._push_with_config_planted_after_check(2)
checks = self._push_with_config_planted_after_check(2)
self.assertGreaterEqual(checks, 2, "the config was never planted")
self._assert_nothing_ran()
self._assert_landed()
@@ -0,0 +1,118 @@
from __future__ import annotations
import io
import os
import shutil
import subprocess
import tempfile
import unittest
from contextlib import redirect_stdout
from unittest.mock import MagicMock, patch
from pkgmgr.actions.repository import _trusted
from pkgmgr.actions.repository import push as push_module
from pkgmgr.core.git import GitRunError
GIT = shutil.which("git")
CREDENTIAL = "pw-value"
OID = "a" * 40
def _target(branch: str) -> _trusted.Target:
return _trusted.Target(
repo_dir="/nonexistent/repo",
tdir="/nonexistent/tdir",
branch=branch,
head=OID,
remote="origin",
fetch_url="t@localhost:canonical.git",
push_urls=("t@localhost:canonical.git",),
)
class TestTrustedReviewFixes(unittest.TestCase):
@unittest.skipUnless(GIT, "git reads the config")
def test_config_value_with_a_newline_stays_one_value(self) -> None:
value = "https://a.example/x\nhttps://b.example/y"
env = {
key: val
for key, val in os.environ.items()
if key not in ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE")
}
with tempfile.TemporaryDirectory() as repo:
subprocess.run([GIT, "init", "-q", repo], check=True, env=env)
subprocess.run(
[GIT, "config", "--add", "remote.origin.pushurl", value],
cwd=repo,
check=True,
env=env,
)
values = _trusted._config_all(repo, "remote.origin.pushurl")
self.assertEqual(values, [value])
def test_file_url_percent_encodes_the_path(self) -> None:
self.assertEqual(
_trusted.file_url("/srv/repo%20name"), "file:///srv/repo%2520name"
)
def test_failed_trusted_call_redacts_url_userinfo(self) -> None:
failed = subprocess.CompletedProcess([], 128, b"", b"fatal: refused")
url = f"https://user:{CREDENTIAL}@example.org/x.git"
with (
patch.object(_trusted.subprocess, "run", return_value=failed),
self.assertRaises(GitRunError) as cm,
):
_trusted.tgit("/nonexistent/tdir", "ls-remote", "--", url)
self.assertNotIn(CREDENTIAL, str(cm.exception))
self.assertIn("https://***@example.org/x.git", str(cm.exception))
def test_failed_mirror_line_redacts_url_userinfo(self) -> None:
plan = push_module._Push((), (), False, False, None)
url = f"https://user:{CREDENTIAL}@example.org/mirror.git"
refused = _trusted.PushRefusedError("lease failed")
with (
patch.object(push_module, "_lease", side_effect=refused),
redirect_stdout(io.StringIO()) as out,
):
pushed = push_module._push_url(_target("main"), plan, 1, url)
self.assertFalse(pushed)
self.assertNotIn(CREDENTIAL, out.getvalue())
def test_ls_remote_line_without_a_tab_lists_no_ref(self) -> None:
with patch.object(
_trusted, "tgit", return_value=f"{OID} refs/heads/main"
) as fake:
tips = _trusted.fetch_heads(
_target("main"), "t@localhost:x.git", ["main"], "refs/x"
)
self.assertEqual(tips, {})
fake.assert_called_once()
def test_track_uses_the_validated_branch(self) -> None:
run = MagicMock(return_value="")
target = _target("feature")
with (
patch.object(push_module, "run", run),
patch.object(push_module, "try_git", return_value=None) as probe,
):
push_module._track(target)
probe.assert_called_once_with(
target.repo_dir, "rev-parse", "--abbrev-ref", "feature@{upstream}"
)
self.assertEqual(
run.call_args_list[-1].args[0],
["branch", "--set-upstream-to=origin/feature", "feature"],
)
if __name__ == "__main__":
unittest.main()