abhigyanpatwari/GitNexus · error · ValueError

clone contains an unsafe reference name

Error message

clone contains an unsafe reference name

What it means

Each ref returned by for-each-ref must start with 'refs/' and contain no whitespace. The guard prevents argument injection into the subsequent `git update-ref --no-deref -d <ref>` and protects the bounded parser. A ref failing this check is treated as unsafe rather than being passed to update-ref.

Source

Thrown at eval/workflow_bench/oracle_assets.py:365

        timeout=60,
        env=deterministic_git_env,
    )
    _git_checked(
        root,
        ["update-ref", "--no-deref", "HEAD", sanitized_head, original_head],
        timeout=60,
    )

    refs_output = _git_checked(
        root,
        ["for-each-ref", f"--count={MAX_CLONE_REFS + 1}", "--format=%(refname)"],
        timeout=60,
    )
    refs = refs_output.splitlines() if refs_output else []
    if len(refs) > MAX_CLONE_REFS:
        raise ValueError(f"clone has more than {MAX_CLONE_REFS} references; refusing incomplete sanitization")
    if any(not ref.startswith("refs/") or any(character.isspace() for character in ref) for ref in refs):
        raise ValueError("clone contains an unsafe reference name")
    for ref in refs:
        _git_checked(root, ["update-ref", "--no-deref", "-d", ref], timeout=60)

    remote_output = _git_checked(root, ["remote"], timeout=60)
    remotes = remote_output.splitlines() if remote_output else []
    if len(remotes) > MAX_CLONE_REFS or any(
        re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._/-]{0,255}", remote) is None or ".." in remote for remote in remotes
    ):
        raise ValueError("clone contains unsafe or unbounded remote metadata")
    for remote in remotes:
        _git_checked(root, ["remote", "remove", remote], timeout=60)

    _git_checked(
        root,
        ["reflog", "expire", "--expire=now", "--expire-unreachable=now", "--all"],
        timeout=60,
    )
    git_dir = root / ".git"

View on GitHub (pinned to d540b00184)

Solutions

  1. List refs: `git -C <clone> for-each-ref --format='%(refname)'` and find the offender.
  2. Delete the offending ref carefully with a literal SHA arg or by removing the loose ref file under .git/refs.
  3. Re-clone from a trusted remote to discard the corrupted ref store.
Defensive patterns

Strategy: validation

Validate before calling

import re
from pathlib import Path
from eval.workflow_bench.process_control import run_checked

def ref_names_are_safe(clone: Path) -> bool:
    out = run_checked(["git", "-C", str(clone), "for-each-ref", "--format=%(refname)"], timeout=60).stdout_tail.strip()
    for ref in out.splitlines():
        if not ref.startswith("refs/") or re.search(r"\s", ref):
            return False
    return True

Type guard

def is_unsafe_ref_name(exc: BaseException) -> bool:
    return isinstance(exc, ValueError) and "unsafe reference name" in str(exc)

Try / catch

try:
    oracle_assets.sanitize_clone_for_hidden_oracles(clone)
except ValueError as exc:
    quarantine(clone)
    raise AbortTask(str(exc)) from exc

Prevention

When it happens

Trigger: Triggered when any ref name does not begin with refs/ or contains whitespace. Git itself normally forbids these, so this fires on corrupted or hand-crafted repos where refs have been written through low-level commands.

Common situations: A repo whose .git/refs has been edited by hand; a malicious or corrupted clone; refs created via `git update-ref` with unusual names bypassing the porcelain checks.

Related errors


AI-assisted analysis of abhigyanpatwari/GitNexus@d540b00184 (2026-08-12). Data as JSON: /api/errors/b34f630750c778ef. Report an issue: GitHub.