Files
hermes-agent/tests/tools/test_skills_hub_github_fetch.py
Adolanium f9c79425a7 fix(security): re-check every Skills Hub redirect hop against URL and website policy
7d793348e8 routed hub GETs inside skills_hub_http_session() through the
connect-time SSRF client, which refuses private-IP hops but never consults
the website blocklist, and callers still passed follow_redirects=True
(browse.sh skillMdUrl and detail, GitHub API GETs, the skills.sh root
scan, the Hermes index). Outside a session _skills_hub_http_get fell back
to plain httpx.get with no SSRF check at all.

_skills_hub_http_get is now the one redirect loop: every hop runs
is_safe_url and check_website_access, then goes out through the session
client (or a one-shot SSRF-safe client when no session is active).
Params apply to the first hop only, credential headers stay on the
origin, and a refused hop raises SkillsHubFetchBlocked, an httpx
transport error, so every caller's existing HTTPError handling covers
it. The GitHub and ClawHub retry loops don't retry a blocked hop.
_guarded_http_get becomes the None-returning wrapper over the same loop.

Fixes #63919
2026-11-12 21:17:47 +01:00

55 lines
2.5 KiB
Python

"""GitHubSource file downloads: which host serves a skill's files."""
from unittest.mock import MagicMock
import httpx
import pytest
from tools.skills_hub_github import GitHubAuth, GitHubSource
@pytest.mark.parametrize("raw_status", [200, 404])
def test_pinned_file_comes_from_raw_and_spends_no_api_call(monkeypatch, raw_status):
"""Every file of a skill folder was one Contents API call: an anonymous user (60/h) could
never install a skill with ~58+ files (remotion-best-practices: 143 calls, rate_limited).
A pinned file now comes from raw.githubusercontent.com; a raw miss (private repo) still
falls back to the API."""
import tools.skills_hub as hub
hosts = []
def fake_get(url, **_kw):
hosts.append(url.split("/")[2])
ok = "raw.githubusercontent.com" not in url or raw_status == 200
return httpx.Response(200 if ok else raw_status, content=b"body" if ok else b"")
monkeypatch.setattr(hub, "_skills_hub_http_get", fake_get)
auth = MagicMock(spec=GitHubAuth)
auth.get_headers.return_value = {}
assert GitHubSource(auth)._fetch_file_bytes("o/r", "s/a b.md", ref="c" * 40) == b"body"
assert hosts == (["raw.githubusercontent.com"] if raw_status == 200
else ["raw.githubusercontent.com", "api.github.com"])
def test_unreachable_raw_host_falls_back_to_the_api_and_is_tried_once(monkeypatch):
"""A DNS-poisoned or NXDOMAIN raw.githubusercontent.com (api.github.com fine) raises
SSRFConnectionBlocked, a ValueError the raw attempt did not catch: every install failed before
the API fallback. A dropped-packet firewall costs a 15 s timeout per file, so stop after one."""
import tools.skills_hub as hub
from tools.url_safety import SSRFConnectionBlocked
hosts = []
def fake_get(url, **_kw):
hosts.append(url.split("/")[2])
if "raw.githubusercontent.com" in url:
raise SSRFConnectionBlocked("DNS resolution failed for: raw.githubusercontent.com")
return httpx.Response(200, content=b"body")
monkeypatch.setattr(hub, "is_safe_url", lambda _url: True)
monkeypatch.setattr(hub, "check_website_access", lambda _url: None)
monkeypatch.setattr(hub, "_ssrf_safe_http_get", fake_get)
auth = MagicMock(spec=GitHubAuth)
auth.get_headers.return_value = {}
source = GitHubSource(auth)
assert [source._fetch_file_bytes("o/r", f"s/{i}.md", ref="c" * 40) for i in range(3)] == [b"body"] * 3
assert hosts == ["raw.githubusercontent.com"] + ["api.github.com"] * 3