Skip to content

Commit 896b82e

Browse files
committed
fix(input): resolve longest valid ref for GitHub tree URLs
GitHub branch names may contain slashes, so the first /tree/ segment is not necessarily the complete ref. Resolve the longest advertised branch/tag name via git ls-remote before splitting ref from subdirectory, and reject URLs that name no known ref. Adds slash-ref regressions. Signed-off-by: Deepak Jain <[email protected]>
1 parent 032436d commit 896b82e

2 files changed

Lines changed: 122 additions & 13 deletions

File tree

‎src/skillspector/input_handler.py‎

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,24 +1025,78 @@ def _is_git_url(self, path: str) -> bool:
10251025
return True
10261026
return False
10271027

1028-
@staticmethod
1029-
def _github_tree_target(path: str) -> tuple[str, str, PurePosixPath] | None:
1030-
"""Return a canonical clone target for a GitHub ``/tree/<ref>/<dir>`` URL."""
1028+
def _github_tree_target(self, path: str) -> tuple[str, str, PurePosixPath] | None:
1029+
"""Return a canonical clone target for a GitHub ``/tree/<ref>/<dir>`` URL.
1030+
1031+
The ref itself may contain ``/`` (for example ``feature/foo``), so the
1032+
split between ref and subdirectory is resolved against the remote's
1033+
advertised refs: the longest ``refs/heads/`` or ``refs/tags/`` name
1034+
that prefixes the ``/tree/`` segments wins. Without this, a URL for
1035+
branch ``feature/foo`` would clone branch ``feature`` and treat
1036+
``foo`` as part of the subdirectory.
1037+
"""
10311038
parsed = urlparse(path)
10321039
if parsed.scheme != "https" or parsed.hostname != "github.com":
10331040
return None
10341041
parts = [unquote(part) for part in parsed.path.split("/") if part]
1035-
if len(parts) < 5 or parts[2] != "tree":
1042+
if len(parts) < 4 or parts[2] != "tree":
10361043
return None
1037-
owner, repository, _tree, branch, *subdirectory = parts
1038-
if any(part in {"", ".", ".."} or "/" in part or "\\" in part for part in subdirectory):
1044+
owner, repository = parts[0], parts[1]
1045+
segments = parts[3:]
1046+
if any(part in {"", ".", ".."} or "/" in part or "\\" in part for part in segments):
10391047
raise ValueError("Git URL subdirectory must stay within the repository")
1040-
return (
1041-
f"https://github.com/{owner}/{repository}.git",
1042-
branch,
1043-
PurePosixPath(*subdirectory),
1048+
repository_url = f"https://github.com/{owner}/{repository}.git"
1049+
ref, subdirectory = self._resolve_tree_ref(repository_url, segments)
1050+
return (repository_url, ref, PurePosixPath(*subdirectory))
1051+
1052+
def _resolve_tree_ref(self, repository_url: str, segments: list[str]) -> tuple[str, list[str]]:
1053+
"""Split ``/tree/`` *segments* into ``(ref, subdirectory)``.
1054+
1055+
Uses the longest remote branch/tag name that prefixes the segments, so
1056+
refs containing ``/`` resolve to the intended tree. Raises ValueError
1057+
when no advertised ref matches the URL.
1058+
"""
1059+
remote_refs = self._list_remote_refs(repository_url)
1060+
for end in range(len(segments), 0, -1):
1061+
candidate = "/".join(segments[:end])
1062+
if candidate in remote_refs:
1063+
return candidate, segments[end:]
1064+
raise ValueError(
1065+
"GitHub tree URL does not name a known branch or tag: "
1066+
f"{repository_url} ({'/'.join(segments)})"
10441067
)
10451068

1069+
def _list_remote_refs(self, repository_url: str) -> set[str]:
1070+
"""Return the branch/tag names advertised by the remote repository.
1071+
1072+
Bounded by the ingest deadline; the host allowlist and private-IP
1073+
checks from URL validation apply.
1074+
"""
1075+
self._validate_url_host(repository_url, ALLOWED_GIT_HOSTS)
1076+
deadline = self._deadline()
1077+
self._check_deadline(deadline, "git")
1078+
timeout = max(1.0, deadline - monotonic())
1079+
try:
1080+
process = subprocess.run(
1081+
["git", "ls-remote", repository_url],
1082+
stdout=subprocess.PIPE,
1083+
stderr=subprocess.DEVNULL,
1084+
timeout=timeout,
1085+
check=False,
1086+
)
1087+
except subprocess.TimeoutExpired as exc:
1088+
raise IngestLimitExceededError("Git ref listing exceeded its time limit") from exc
1089+
if process.returncode != 0:
1090+
raise ValueError(f"Could not list refs for GitHub tree URL: {repository_url}")
1091+
refs: set[str] = set()
1092+
for line in process.stdout.decode("utf-8", errors="replace").splitlines():
1093+
_, _, ref = line.partition("\t")
1094+
for prefix in ("refs/heads/", "refs/tags/"):
1095+
if ref.startswith(prefix):
1096+
refs.add(ref[len(prefix) :])
1097+
break
1098+
return refs
1099+
10461100
def _is_file_url(self, path: str) -> bool:
10471101
"""Check if path is a direct file URL."""
10481102
if not path.startswith("https://"):

‎tests/unit/test_input_handler.py‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import os
2020
import sys
2121
from errno import ENOENT
22-
from pathlib import Path
22+
from pathlib import Path, PurePosixPath
2323
from types import SimpleNamespace
2424
from unittest.mock import MagicMock, patch
2525

@@ -459,7 +459,10 @@ def test_github_tree_url_resolves_a_checked_out_subdirectory(tmp_path: Path) ->
459459
handler = InputHandler()
460460
clone = tmp_path / "repo"
461461
(clone / "skills" / "biome-gritql").mkdir(parents=True)
462-
with patch.object(handler, "_clone_git", return_value=clone) as clone_git:
462+
with (
463+
patch.object(handler, "_clone_git", return_value=clone) as clone_git,
464+
patch.object(handler, "_list_remote_refs", return_value={"main"}),
465+
):
463466
resolved, source_type = handler.resolve(
464467
"https://github.com/somtougeh/somto-dev-toolkit/tree/main/skills/biome-gritql"
465468
)
@@ -470,6 +473,55 @@ def test_github_tree_url_resolves_a_checked_out_subdirectory(tmp_path: Path) ->
470473
)
471474

472475

476+
def test_github_tree_url_resolves_slash_containing_ref(tmp_path: Path) -> None:
477+
"""A branch name containing / must not be split into ref + subdirectory."""
478+
handler = InputHandler()
479+
clone = tmp_path / "repo"
480+
(clone / "skills" / "demo").mkdir(parents=True)
481+
with (
482+
patch.object(handler, "_clone_git", return_value=clone) as clone_git,
483+
patch.object(handler, "_list_remote_refs", return_value={"main", "feature", "feature/foo"}),
484+
):
485+
resolved, source_type = handler.resolve(
486+
"https://github.com/example/repo/tree/feature/foo/skills/demo"
487+
)
488+
assert resolved == clone / "skills" / "demo"
489+
assert source_type == "git"
490+
clone_git.assert_called_once_with("https://github.com/example/repo.git", branch="feature/foo")
491+
492+
493+
def test_github_tree_url_prefers_shorter_ref_when_longest_absent() -> None:
494+
"""The longest *advertised* ref wins, not the longest URL prefix."""
495+
handler = InputHandler()
496+
with patch.object(handler, "_list_remote_refs", return_value={"feature"}):
497+
repository_url, ref, subdirectory = handler._github_tree_target(
498+
"https://github.com/example/repo/tree/feature/sub"
499+
)
500+
assert repository_url == "https://github.com/example/repo.git"
501+
assert ref == "feature"
502+
assert subdirectory == PurePosixPath("sub")
503+
504+
505+
def test_github_tree_url_rejects_unknown_ref() -> None:
506+
handler = InputHandler()
507+
with (
508+
patch.object(handler, "_list_remote_refs", return_value={"main"}),
509+
pytest.raises(ValueError, match="does not name a known branch or tag"),
510+
):
511+
handler._github_tree_target("https://github.com/example/repo/tree/nope/sub")
512+
513+
514+
def test_github_tree_url_supports_ref_without_subdirectory() -> None:
515+
handler = InputHandler()
516+
with patch.object(handler, "_list_remote_refs", return_value={"main"}):
517+
repository_url, ref, subdirectory = handler._github_tree_target(
518+
"https://github.com/example/repo/tree/main"
519+
)
520+
assert repository_url == "https://github.com/example/repo.git"
521+
assert ref == "main"
522+
assert subdirectory == PurePosixPath(".")
523+
524+
473525
@pytest.mark.parametrize("segment", ["%2Fetc", "%2E%2E%2Frepo", "%5Coutside"])
474526
def test_github_tree_url_rejects_encoded_path_escapes(segment: str) -> None:
475527
with pytest.raises(ValueError, match="stay within the repository"):
@@ -487,7 +539,10 @@ def test_github_tree_url_selection_failure_cleans_owned_clone(tmp_path: Path, ta
487539
if target == "SKILL.md":
488540
(clone / target).write_text("# skill\n")
489541
handler._temp_dir = tmp_path
490-
with patch.object(handler, "_clone_git", return_value=clone):
542+
with (
543+
patch.object(handler, "_clone_git", return_value=clone),
544+
patch.object(handler, "_list_remote_refs", return_value={"main"}),
545+
):
491546
with pytest.raises(ValueError):
492547
handler.resolve(f"https://github.com/example/repo/tree/main/{target}")
493548
assert not tmp_path.exists()

0 commit comments

Comments
 (0)