fix(push): sign and push from a private bare repository

pkgmgr push copies HEAD and tags into a private temporary bare repository and signs (git commit-tree -S) and pushes from there, so the checkout config, hooks, filters, merge drivers, replace refs, bundle URIs, submodule configs and allowed-signers never take part. The checkout is only read via upload-pack and updated by a fetch of the signed commit plus an update-ref guarded by the old HEAD. Push and fetch URLs must be https, ssh or user@host:path. Codacy tightened (D413, Lizard CCN 8, no new defaults, B607 via shutil.which); prose docstrings removed for quality clean.comment.

| 🌿 Branch | 📊 Diff stat | 🎯 Fix | 💥 Break | 💸 Tokens | ⏱️ Velocity | 🛡️ Security | 🏆 Score | Gate |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| fix/push-trusted-gitdir | 4 files +1112/-233 | 90 % | 8 % | – | – | 100 % | 83 | ⛔ break 8 % > 5 |

Evidence: host run 86 tests OK, full unit suite 527 OK, ruff clean, lizard -C 8 clean, MEDUSA 100 % critical 0. Break 8 %: a checkout whose origin fetch URL is a local path or http:// is refused; repository-level http.* and credential.* settings and a GIT_CONFIG_GLOBAL override no longer apply to push. Operator review a41d30aa (re-card of 39022e85).
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 19:44:39 +00:00
1 parent 895ec2f0d4
commit 6c7ca613fc
4 files changed
+1114 -235

No files matched your search

+89 -35
View File
@@ -21,50 +21,90 @@ from pkgmgr.cli.context import CLIContext
from pkgmgr.cli.parser import create_parser
from pkgmgr.cli.proxy import maybe_handle_proxy
_REAL_RUN = subprocess.run
GIT = shutil.which("git")
SSH_KEYGEN = shutil.which("ssh-keygen")
def git(cwd: str, *args: str) -> str:
return subprocess.run(
["git", *args], cwd=cwd, check=True, capture_output=True, text=True
return _REAL_RUN(
[GIT, *args], cwd=cwd, check=True, capture_output=True, text=True
).stdout.strip()
@unittest.skipUnless(shutil.which("ssh-keygen"), "ssh-keygen signs the commits")
class TestPushMirrorLeases(unittest.TestCase):
def keygen(path: Path) -> None:
_REAL_RUN(
[SSH_KEYGEN, "-q", "-t", "ed25519", "-N", "", "-f", str(path)],
check=True,
)
def ssh_url(path: str) -> str:
return f"t@localhost:{path}"
@unittest.skipUnless(GIT and SSH_KEYGEN, "git and ssh-keygen sign the commits")
class SignedRemotesCase(unittest.TestCase):
def setUp(self) -> None:
self._tmp = tempfile.TemporaryDirectory()
self.addCleanup(self._tmp.cleanup)
root = Path(os.path.realpath(self._tmp.name))
self.root = str(root)
key = root / "key"
subprocess.run(
["ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-f", str(key)],
check=True,
)
self.key = root / "key"
keygen(self.key)
(root / "allowed").write_text(f"t@example.org {(root / 'key.pub').read_text()}")
(root / "gitconfig").write_text(
self.gitconfig = root / ".gitconfig"
self._write_gitconfig(root)
self.env = {
**os.environ,
"GIT_CONFIG_GLOBAL": str(self.gitconfig),
"GIT_CONFIG_NOSYSTEM": "1",
}
for patcher in (
patch.dict(os.environ, self.env),
patch("pkgmgr.core.git.run._passwd_home", return_value=self.root),
):
patcher.start()
self.addCleanup(patcher.stop)
self._init_remotes(root)
def _write_gitconfig(self, root: Path) -> None:
fakessh = root / "fakessh"
fakessh.write_text(
"#!/bin/sh\n"
"shift $(($# - 1))\n"
f'exec env -i PATH="$PATH" HOME={root} sh -c "$1"\n'
)
fakessh.chmod(0o755)
self.gitconfig.write_text(
"[user]\n\tname = t\n\temail = t@example.org\n"
f"\tsigningkey = {key}\n"
f"\tsigningkey = {self.key}\n"
"[gpg]\n\tformat = ssh\n"
f'[gpg "ssh"]\n\tallowedSignersFile = {root / "allowed"}\n'
"[commit]\n\tgpgsign = false\n"
"[init]\n\tdefaultBranch = main\n"
f"[core]\n\tsshCommand = {fakessh}\n"
"[ssh]\n\tvariant = simple\n"
)
env = patch.dict(
os.environ,
{"GIT_CONFIG_GLOBAL": str(root / "gitconfig"), "GIT_CONFIG_NOSYSTEM": "1"},
)
env.start()
self.addCleanup(env.stop)
def _init_remotes(self, root: Path) -> None:
self.canonical = str(root / "canonical.git")
self.mirror = str(root / "mirror.git")
self.repo = str(root / "repo")
git(self.root, "init", "-q", "--bare", self.canonical)
git(self.root, "init", "-q", "--bare", self.mirror)
git(self.root, "init", "-q", self.repo)
git(self.repo, "remote", "add", "origin", self.canonical)
git(self.repo, "remote", "set-url", "--add", "--push", "origin", self.canonical)
git(self.repo, "remote", "set-url", "--add", "--push", "origin", self.mirror)
git(self.repo, "remote", "add", "origin", ssh_url(self.canonical))
for bare in (self.canonical, self.mirror):
git(
self.repo,
"remote",
"set-url",
"--add",
"--push",
"origin",
ssh_url(bare),
)
self.init = self._commit("init")
git(self.repo, "push", "-q", self.canonical, "main")
git(self.repo, "push", "-q", self.mirror, "main")
@@ -78,6 +118,17 @@ class TestPushMirrorLeases(unittest.TestCase):
git(cwd, "commit", "-q", *flags, "-m", name)
return git(cwd, "rev-parse", "HEAD")
def _tip(self, bare: str, ref: str = "refs/heads/main") -> str:
return _REAL_RUN(
[GIT, "rev-parse", "--verify", "--quiet", ref],
cwd=bare,
check=False,
capture_output=True,
text=True,
).stdout.strip()
class TestPushMirrorLeases(SignedRemotesCase):
def _push(self, rewrite: bool = False) -> str:
with redirect_stdout(io.StringIO()) as out:
sign_unpushed(self.repo)
@@ -102,15 +153,6 @@ class TestPushMirrorLeases(unittest.TestCase):
self.assertEqual(cm.exception.code, 0)
def _tip(self, bare: str, ref: str = "refs/heads/main") -> str:
return subprocess.run(
["git", "rev-parse", "--verify", "--quiet", ref],
cwd=bare,
check=False,
capture_output=True,
text=True,
).stdout.strip()
def _assert_shipped_signed(self) -> None:
head = git(self.repo, "rev-parse", "HEAD")
self.assertEqual(self._tip(self.canonical), head)
@@ -226,7 +268,7 @@ class TestPushMirrorLeases(unittest.TestCase):
git(self.repo, "fetch", "-q", "origin")
self._commit("new")
upstream = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "@{u}"],
[GIT, "rev-parse", "--abbrev-ref", "@{u}"],
cwd=self.repo,
check=False,
capture_output=True,
@@ -302,12 +344,24 @@ class TestPushMirrorLeases(unittest.TestCase):
head = git(self.repo, "rev-parse", "HEAD")
git(self.repo, "config", "diff.ignoreSubmodules", "all")
git(self.repo, "update-index", "--add", "--cacheinfo", f"160000,{head},sub")
tree = git(self.repo, "rev-parse", "HEAD^{tree}")
with self.assertRaises(PushRefusedError) as cm:
self._push()
self._push()
self.assertIn("staged changes", str(cm.exception))
self.assertEqual(git(self.repo, "rev-parse", "HEAD"), head)
self._assert_shipped_signed()
self.assertNotEqual(git(self.repo, "rev-parse", "HEAD"), head)
self.assertEqual(git(self.repo, "rev-parse", "HEAD^{tree}"), tree)
self.assertEqual(
git(
self.repo,
"diff",
"--cached",
"--name-only",
"--ignore-submodules=none",
"HEAD",
),
"sub",
)
def test_unstaged_edit_stays_out_of_the_signed_head(self) -> None:
self._commit("feature")
@@ -364,7 +418,7 @@ class TestPushMirrorLeases(unittest.TestCase):
self.assertFalse(marker.exists())
def test_remote_named_like_an_option_is_pushed(self) -> None:
git(self.repo, "config", "remote.-r.url", self.canonical)
git(self.repo, "config", "remote.-r.url", ssh_url(self.canonical))
git(self.repo, "config", "remote.-r.fetch", "+refs/heads/*:refs/remotes/-r/*")
git(self.repo, "config", "branch.main.pushRemote", "-r")
git(self.repo, "fetch", "-q", "--", "-r")