Skip to content

A 40-character GitHub branch name can yield a mixed-revision folder read #494

Closed
1 of 1 issue completed

Description

@ephico2real2

Part of #13. Found by Codex (reviewer 1) in the review of PR #492. Reviewer 2 confirmed it predates the PR: github.py is not touched by #492. A ready fix and test are in workbench/cmapi/mayday/i492-review/r2/followup-F07.diff.

F07 · Medium · A 40-character GitHub branch can yield a mixed-revision folder

cmapi/src/cmapi/plugins/namespaces/router.py:200,207–215, cmapi/src/cmapi/core/git/github.py:69–80. The handler parses and hashes one mapping. On GitHub REST, however, any 40-character branch name is treated as an immutable commit ID. If the configured default branch is 40 characters and moves between two Contents requests, the mapping contains the namespace from A and the quota from B. Its ETag identifies neither Git folder. This is an inherited provider bug reachable from the new detail contract. The usual main path resolves the commit before reading the directory. Gitea checks hexadecimal characters and the workspace reads blobs from one tree under its lock. Resolve the configured default as a branch, and validate explicit object IDs before skipping branch lookup.

Fix

diff --git a/cmapi/src/cmapi/core/git/github.py b/cmapi/src/cmapi/core/git/github.py
--- a/cmapi/src/cmapi/core/git/github.py
+++ b/cmapi/src/cmapi/core/git/github.py
@@ -21,6 +21,7 @@
 from __future__ import annotations
 
 import base64
+import re
 from typing import Any, Iterable, Mapping
 
 from cmapi.core.git.base import (BranchRequest, CheckState, GitUnavailable, HeadMoved, HostGitProvider, MergeCheck, NotMergeable,
@@ -66,10 +67,13 @@
     def read_folder(self, path: str, ref: str | None = None) -> tuple[dict[str, str], str]:
         if self.workspace is not None:
             return self.workspace.read_folder(path, ref)
-        ref = ref or self.default_branch
-        head = ref if len(ref) == 40 else self.branch_head(ref)
-        if head is None:
-            raise GitUnavailable(f"branch {ref} does not exist")
+        if ref is not None and re.fullmatch(r"[0-9a-fA-F]{40}", ref):
+            head = ref
+        else:
+            branch = ref or self.default_branch
+            head = self.branch_head(branch)
+            if head is None:
+                raise GitUnavailable(f"branch {branch} does not exist")
         r = self._call("read_folder", "GET", f"{self._repo()}/contents/{path.strip('/')}", params={"ref": head}, ok=(200, 404))
         files: dict[str, str] = {}
         if r.status_code == 200:

Regression tests

diff --git a/cmapi/tests/core/test_review492_snapshot.py b/cmapi/tests/core/test_review492_snapshot.py
new file mode 100644
--- /dev/null
+++ b/cmapi/tests/core/test_review492_snapshot.py
@@ -0,0 +1,31 @@
+import base64
+from unittest.mock import Mock
+
+from cmapi.core.git.github import GitHubProvider
+from cmapi.core.pipeline import folder_etag
+
+
+def test_github_reads_every_file_at_one_commit_when_default_branch_is_40_characters(monkeypatch):
+    branch = "production".ljust(40, "-")
+    git = GitHubProvider("https://example.invalid", "o/r", "", default_branch=branch)
+    first, second = "a" * 40, "b" * 40
+    current = first
+    paths = ["folder/namespace.yaml", "folder/quota.yaml"]
+
+    def answer(operation, method, path, **kw):
+        nonlocal current
+        if operation == "branch_head":
+            return Mock(status_code=200, json=lambda: {"commit": {"sha": current}})
+        ref = kw["params"]["ref"]
+        revision = current if ref == branch else ref
+        if operation == "read_folder":
+            return Mock(status_code=200, json=lambda: [
+                {"path": p, "name": p.split("/")[-1], "type": "file"} for p in paths])
+        current = second
+        blob = base64.b64encode(revision.encode()).decode()
+        return Mock(status_code=200, json=lambda: {"content": blob})
+
+    monkeypatch.setattr(git, "_call", answer)
+    files, sha = git.read_folder("folder")
+    assert files == dict.fromkeys(paths, first)
+    assert sha == first and folder_etag(files) == folder_etag(dict.fromkeys(paths, first))

Command and observed output

PYTHONPATH=src python -m pytest -q --tb=short tests/core/test_review492_snapshot.py
HEAD (selected failure lines and pytest summary)
E   AssertionError: assert {'folder/name...bbbbbbbbbbbb'} == {'folder/name...aaaaaaaaaaaa'}
1 failed in 1.31s
With fixes
1 passed in 1.16s

Research mandate. Check GitHub's branch and SHA handling in its documentation, confirm the cause, and fix it with the test that fails before.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added
    bug
    status:open
    sev:medium
    Backlog
    on Nov 20, 2026
  2. added
    In-Progress
    and removed
    status:open
    Backlog
    on Nov 20, 2026
  3. added 2 commits that reference this issue on Nov 20, 2026
  4. added a commit that references this issue on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-Progressbugsev:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions