Close two token-leak paths in the git mirror and harden the supply chain per the OWASP audit (audit/2026-08-11/security-report.md). Runtime fixes: - mirror GitError masks the embedded oauth2:<token> in both the failing command line and git's stderr before raising; cli now catches GitError with a clean message instead of an uncaught traceback that printed the live token. - Scrub token-bearing remote URLs from the bare config at the end of each run so tokens aren't persisted in .git/config. - Default mirror workdir is per-user 0700 (/tmp/cb2gitea-<uid>), not a world-readable /tmp/cb2gitea. Supply chain: - Commit uv.lock; CI installs via uv sync --frozen (hash-verified). - Pin Gitea Actions to commit SHAs, not mutable @vN tags. - Pin the ruff rule set and exclude audit/ for reproducible linting. Adds tests/test_mirror.py for the redaction helpers.
This commit is contained in:
1 parent
7d26e2d4ca
commit
299c061f26
10 files changed
+633
-11
No files matched your search
@@ -9,6 +9,7 @@ repo.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
@@ -30,12 +31,51 @@ def _authed(url: str, token: str) -> str:
|
||||
return urlunsplit(parts._replace(netloc=f"{userinfo}@{parts.netloc}"))
|
||||
|
||||
|
||||
_TOKEN_RE = re.compile(r"(oauth2:)[^@]+(@)")
|
||||
|
||||
|
||||
def _redact(text: str) -> str:
|
||||
"""Mask any embedded ``oauth2:<token>`` so secrets never reach logs/exceptions."""
|
||||
return _TOKEN_RE.sub(r"\1***\2", text)
|
||||
|
||||
|
||||
def _bare(url: str) -> str:
|
||||
"""Strip an ``oauth2:<token>@`` userinfo, returning the bare URL."""
|
||||
return _TOKEN_RE.sub("", url)
|
||||
|
||||
|
||||
def _scrub_remote_tokens(bare: Path) -> None:
|
||||
"""Rewrite token-bearing remote URLs in the bare config to bare form.
|
||||
|
||||
The mirror is left on disk for inspection / re-push; strip the embedded
|
||||
``oauth2:<token>`` so it isn't persisted in plaintext in ``.git/config``.
|
||||
A re-run re-injects auth via :func:`_authed`, so scrubbing is safe. Best-effort.
|
||||
"""
|
||||
listing = subprocess.run( # noqa: S603 - argv list, no shell
|
||||
["git", "--git-dir", str(bare), "remote", "-v"], capture_output=True, text=True
|
||||
)
|
||||
for line in listing.stdout.splitlines():
|
||||
parts = line.split("\t")
|
||||
if len(parts) < 2:
|
||||
continue
|
||||
name = parts[0]
|
||||
url = parts[1].split(" ")[0]
|
||||
if "oauth2:" in url:
|
||||
subprocess.run( # noqa: S603 - argv list, no shell
|
||||
["git", "--git-dir", str(bare), "remote", "set-url", name, _bare(url)],
|
||||
capture_output=True,
|
||||
)
|
||||
|
||||
|
||||
def _run(cmd: list[str], cwd: Path | None = None) -> None:
|
||||
proc = subprocess.run( # noqa: S603 - argv list, no shell
|
||||
cmd, cwd=cwd, capture_output=True, text=True
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
raise GitError(f"git failed ({proc.returncode}): {' '.join(cmd)}\n{proc.stderr.strip()}")
|
||||
raise GitError(
|
||||
f"git failed ({proc.returncode}): {' '.join(_redact(a) for a in cmd)}"
|
||||
f"\n{_redact(proc.stderr.strip())}"
|
||||
)
|
||||
|
||||
|
||||
def _run_capture(cmd: list[str], cwd: Path | None = None) -> str:
|
||||
@@ -44,7 +84,10 @@ def _run_capture(cmd: list[str], cwd: Path | None = None) -> str:
|
||||
cmd, cwd=cwd, capture_output=True, text=True
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
raise GitError(f"git failed ({proc.returncode}): {' '.join(cmd)}\n{proc.stderr.strip()}")
|
||||
raise GitError(
|
||||
f"git failed ({proc.returncode}): {' '.join(_redact(a) for a in cmd)}"
|
||||
f"\n{_redact(proc.stderr.strip())}"
|
||||
)
|
||||
return proc.stdout
|
||||
|
||||
|
||||
@@ -125,6 +168,10 @@ def mirror_repo(
|
||||
clone is left there (useful for inspection / re-push).
|
||||
"""
|
||||
workdir.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
workdir.chmod(0o700) # private even on a shared /tmp
|
||||
except OSError:
|
||||
pass
|
||||
bare = workdir / (Path(source_url).name.removesuffix(".git") + ".git")
|
||||
|
||||
src = _authed(source_url, source_token)
|
||||
@@ -173,11 +220,14 @@ def mirror_repo(
|
||||
_run(["git", "--git-dir", str(bare), "lfs", "push", "--all", "target"])
|
||||
lfs_pushed = True
|
||||
|
||||
_scrub_remote_tokens(bare)
|
||||
return {"bare": str(bare), "lfs_pushed": lfs_pushed, "pr_head_branches": len(pull_heads)}
|
||||
|
||||
|
||||
if __name__ == "__main__": # self-check — URL auth only, no network
|
||||
if __name__ == "__main__": # self-check — URL auth + redaction, no network
|
||||
url = _authed("https://codeberg.org/AlexKucera/helm.git", "t ok@en")
|
||||
assert url == "https://oauth2:t%20ok%40en@codeberg.org/AlexKucera/helm.git", url
|
||||
assert _authed("https://x/o.git", "") == "https://x/o.git"
|
||||
assert _redact(url) == "https://oauth2:***@codeberg.org/AlexKucera/helm.git", _redact(url)
|
||||
assert _bare(url) == "https://codeberg.org/AlexKucera/helm.git", _bare(url)
|
||||
print("mirror self-check ok:", url)
|
||||
Reference in new issue
Block a user