From 18c005c41f116c8eb7fc26b7d7ba0f322fe24f9b Mon Sep 17 00:00:00 2001 From: tronyx Date: Sat, 21 Nov 2026 07:32:53 -0500 Subject: [PATCH 1/2] Let a probe send the host name that an application expects Snipe-IT 8.8.4 answers 400 to any Host header other than the host of its APP_URL, so the internal probe, which uses the IP address of the server, failed. A probe can now set host, which it sends in the Host header. A redirect to the same scheme, host and port keeps it, and a redirect to another host does not. The snipe-it check of the example configuration tries /health instead of /login, and its internal probe sends snipe-it.{domain}. The README and the changelog describe the setting. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 +++++++- README.md | 2 ++ healthchecks.example.toml | 6 ++++-- healthchecks_linux.py | 30 +++++++++++++++++++++--------- tests/test_config.py | 15 +++++++++++++++ tests/test_http.py | 27 +++++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a41e102..5231602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,13 @@ The project doesn't use version numbers. Each entry is one merge into `master`, ## Unreleased -Nothing yet. +### Added + +- A probe can have a `host` setting, for example `host = "snipe-it.{domain}"`. The probe sends it in the `Host` header instead of the host of its URL. A redirect to the same scheme, host and port keeps it, and a redirect to another host does not. Use it for an application that answers only to its public name. + +### Changed + +- The `snipe-it` check of the example configuration tries `/health` instead of `/login`, and its internal probe sends `host = "snipe-it.{domain}"`. Since 8.8.4, Snipe-IT answers `400` to the IP address of the server. ## 2026-11-18 · [#8](https://github.com/tronyx/HealthChecks-Linux/pull/8) diff --git a/README.md b/README.md index e8b9086..ce0f7c2 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ Each probe is one GET request. A probe has these settings: | `verify_tls` | If `false`, the probe accepts any HTTPS certificate. Use it for a self-signed certificate on your own network | `true` | | `send_token` | If `true`, the probe sends your Organizr API key in a `token` header | `false` | | `auth` | The name of an `[auth.]` table. See [Secrets](#-secrets) | | +| `host` | The host name that the probe sends in the `Host` header, for example `snipe-it.{domain}`. It can use the names from `[hosts]`. Use it when an application answers only to its public name. A redirect to another host does not get it | The host of `url` | This check tests Sonarr through the reverse proxy and on the server: @@ -315,6 +316,7 @@ All users on the computer can write to `/tmp`. Thus, other users can create or d | A probe returns `301` or `302` | The URL redirects to a different URL. Find the final URL with the command below, and use that URL in the probe. Or set `follow_redirects = true` | | An Arr application with a login page fails | If you turned on the login page (authentication) in Sonarr, Radarr, Prowlarr, or another Arr application, add `login` to the URLs, for example `https://{domain}/sonarr/login` | | An external probe fails, but the internal probe passes | Make sure that the subdomain or the subfolder in the probe agrees with your reverse proxy. If you use Organizr, make sure that `organizr_token` is correct and that the probe has `send_token = true` | +| An internal probe returns `400`, but the external probe passes | The application accepts only its public host name, as Snipe-IT does since 8.8.4. Set `host` on the internal probe to that name, for example `host = "snipe-it.{domain}"` | | A probe shows `error: timed out` or `Connection refused` | The script did not get an answer. Make sure that the address and the port are correct, and that the host that runs the script can connect to them | | A probe shows `CERTIFICATE_VERIFY_FAILED` | The HTTPS certificate is not valid, for example because it is self-signed. If the address is on your own network, set `verify_tls = false` on the probe | | The script shows `the previous run is still going, so this run is skipped` | A run takes longer than the time between two runs. Lower `timeout`, increase `workers`, or run the script less often | diff --git a/healthchecks.example.toml b/healthchecks.example.toml index ba45536..e41e5a1 100644 --- a/healthchecks.example.toml +++ b/healthchecks.example.toml @@ -490,13 +490,15 @@ probes = [ { url = "http://{primary}:8580/sabnzbd/" }, ] +# Since 8.8.4, Snipe-IT answers 400 to any host other than the host of its +# APP_URL, so the internal probe sends that host. [[check]] name = "snipe-it" uuid = "" enabled = false probes = [ - { url = "https://snipe-it.{domain}/login", send_token = true }, - { url = "http://{primary}:8005/login" }, + { url = "https://snipe-it.{domain}/health", send_token = true }, + { url = "http://{primary}:8005/health", host = "snipe-it.{domain}" }, ] # If you turned on the login page in Sonarr (Sonarr v4 asks for one), add diff --git a/healthchecks_linux.py b/healthchecks_linux.py index 34bf91f..355bcff 100755 --- a/healthchecks_linux.py +++ b/healthchecks_linux.py @@ -82,7 +82,8 @@ GENERAL_KEYS = ("ping_url", "lock_dir", "timeout", "workers", "send_details", "organizr_token") AUTH_KEYS = ("user", "password", "password_env") CHECK_KEYS = ("name", "uuid", "enabled", "kind", "probes", "url", "component") -PROBE_KEYS = ("url", "expect", "follow_redirects", "verify_tls", "send_token", "auth") +PROBE_KEYS = ("url", "expect", "follow_redirects", "verify_tls", "send_token", "auth", "host") +HOST_RE = re.compile(r"[A-Za-z0-9.-]+(:[0-9]{1,5})?") KINDS = ("http", "statuspage") @@ -120,6 +121,7 @@ class Probe: verify_tls: bool = True send_token: bool = False auth: Credentials | None = None + host: str = "" @dataclass(frozen=True) @@ -225,7 +227,7 @@ def _check_url(url: str, where: str, what: str) -> str: return url -def _fill(template: str, hosts: Mapping[str, str], where: str) -> str: +def _fill(template: str, hosts: Mapping[str, str], where: str, what: str = "url") -> str: """Replace each {name} in template with its value from [hosts].""" def replace(match: re.Match[str]) -> str: @@ -233,7 +235,7 @@ def replace(match: re.Match[str]) -> str: if name not in hosts: raise ConfigError(f"{where}: unknown host '{{{name}}}'{_suggest(name, list(hosts))}; add it to [hosts]") if not hosts[name]: - raise ConfigError(f"{where}: url uses {{{name}}}, but [hosts] {name} is empty") + raise ConfigError(f"{where}: {what} uses {{{name}}}, but [hosts] {name} is empty") return hosts[name] return PLACEHOLDER_RE.sub(replace, template) @@ -331,9 +333,16 @@ def _probe( raise ConfigError(f"{where}: auth must be a string") if auth_name is not None and auth_name not in auths: raise ConfigError(f"{where}: no [auth.{auth_name}] table{_suggest(auth_name, list(auths))}") + host = _get_str(table, "host", where, "") + if "host" in table and not host: + raise ConfigError(f"{where}: host must be a host name, such as app.example.com, and it is empty") credentials = None if live: url = _check_url(_fill(url, hosts, where), where, "url") + if host: + host = _fill(host, hosts, where, "host") + if not HOST_RE.fullmatch(host): + raise ConfigError(f"{where}: host must be a host name, such as app.example.com, not {host}") if auth_name is not None: credentials = _credentials(auth_name, auths[auth_name], env, where) return Probe( @@ -343,6 +352,7 @@ def _probe( verify_tls=_get_bool(table, "verify_tls", where, True), send_token=_get_bool(table, "send_token", where, False), auth=credentials, + host=host, ) @@ -521,10 +531,10 @@ def _origin(url: str) -> tuple[str, str]: class _Redirects(urllib.request.HTTPRedirectHandler): - """Follow redirects only when asked, and send the secret headers again only to the same scheme, host and port. + """Follow redirects only when asked, and send the kept headers again only to the same scheme, host and port. - urllib copies normal headers to every redirect target, including other hosts, so the secrets are added as - unredirected headers and copied here by hand. + urllib copies normal headers to every redirect target, including other hosts, so the secrets and the Host + header of the probe are added as unredirected headers and copied here by hand. """ def __init__(self, follow: bool, secret_headers: Mapping[str, str]) -> None: @@ -577,13 +587,15 @@ def fetch(probe: Probe, token: str, timeout: float, read_limit: int = 0) -> tupl Any HTTP answer is returned, whatever its status; no answer at all raises ProbeFailed. """ - secrets = _secret_headers(probe, token) + kept = _secret_headers(probe, token) + if probe.host: + kept["Host"] = probe.host opener = urllib.request.build_opener( - _Redirects(probe.follow_redirects, secrets), + _Redirects(probe.follow_redirects, kept), urllib.request.HTTPSHandler(context=_tls_context(probe.verify_tls)), ) request = urllib.request.Request(probe.url, method="GET", headers={"User-Agent": USER_AGENT}) - for name, value in secrets.items(): + for name, value in kept.items(): request.add_unredirected_header(name, value) try: with opener.open(request, timeout=timeout) as response: diff --git a/tests/test_config.py b/tests/test_config.py index 2534af9..897f3bb 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -65,6 +65,21 @@ def test_general_settings_and_placeholders(tmp_path: Path) -> None: assert internal.follow_redirects and not internal.verify_tls +def test_probe_host_uses_the_hosts_table(tmp_path: Path) -> None: + text = '[hosts]\ndomain = "example.test"\nprimary = "10.0.0.5"\n\n' + check( + probes='{ url = "http://{primary}:8005/health", host = "snipe-it.{domain}" }, { url = "http://{primary}/" }' + ) + named, plain = load_config(_write(tmp_path, text), {}).checks[0].probes + assert named.host == "snipe-it.example.test" + assert plain.host == "" + + +@pytest.mark.parametrize("host", ["https://app.example.test", "app.example.test/x", "app example.test", ""]) +def test_probe_host_must_be_a_host_name(tmp_path: Path, host: str) -> None: + with pytest.raises(ConfigError, match="host must be a host name"): + load_config(_write(tmp_path, check(probes=f'{{ url = "http://example.test/", host = "{host}" }}')), {}) + + def test_lock_dir_expands_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) config = load_config(_write(tmp_path, '[general]\nlock_dir = "~/locks"\n' + check()), {}) diff --git a/tests/test_http.py b/tests/test_http.py index 8287592..23d1244 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -96,6 +96,33 @@ def test_secrets_do_not_follow_a_redirect_to_another_host(server: FakeServer, ot assert "authorization" not in landing.headers +def test_probe_sends_its_own_host_by_default(server: FakeServer) -> None: + server.answer("/", Answer(200)) + run_probe(Probe(f"{server.url}/"), "", 5) + assert server.requests[0].headers["host"] == server.host + + +def test_probe_sends_the_host_that_it_names(server: FakeServer) -> None: + # Snipe-IT 8.8.4 answers 400 to a Host header other than the host of its APP_URL. + server.answer("/health", Answer(200)) + assert run_probe(Probe(f"{server.url}/health", host="snipe-it.example.test"), "", 5).passed + assert server.requests[0].headers["host"] == "snipe-it.example.test" + + +def test_host_follows_a_redirect_to_the_same_host(server: FakeServer) -> None: + server.answer("/old", Answer(301, headers={"Location": "/new"})) + server.answer("/new", Answer(200)) + run_probe(Probe(f"{server.url}/old", follow_redirects=True, host="app.example.test"), "", 5) + assert server.requests_to("/new")[0].headers["host"] == "app.example.test" + + +def test_host_does_not_follow_a_redirect_to_another_host(server: FakeServer, other_server: FakeServer) -> None: + server.answer("/old", Answer(302, headers={"Location": f"{other_server.url}/landing"})) + other_server.answer("/landing", Answer(200)) + assert run_probe(Probe(f"{server.url}/old", follow_redirects=True, host="app.example.test"), "", 5).passed + assert other_server.requests[0].headers["host"] == other_server.host + + def test_basic_auth_header(server: FakeServer) -> None: server.answer("/", Answer(200)) run_probe(Probe(f"{server.url}/", auth=Credentials("admin", "pw:with:colons")), "", 5) From 778fc8653335855cc7c1b91656b9d9bb07501073 Mon Sep 17 00:00:00 2001 From: tronyx Date: Sun, 22 Nov 2026 13:33:43 -0500 Subject: [PATCH 2/2] Move the changelog entries under #9 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ healthchecks_linux.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5231602..a2a8efb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The project doesn't use version numbers. Each entry is one merge into `master`, ## Unreleased +Nothing yet. + +## 2026-11-22 · [#9](https://github.com/tronyx/HealthChecks-Linux/pull/9) + ### Added - A probe can have a `host` setting, for example `host = "snipe-it.{domain}"`. The probe sends it in the `Host` header instead of the host of its URL. A redirect to the same scheme, host and port keeps it, and a redirect to another host does not. Use it for an application that answers only to its public name. diff --git a/healthchecks_linux.py b/healthchecks_linux.py index 355bcff..6ad8d82 100755 --- a/healthchecks_linux.py +++ b/healthchecks_linux.py @@ -47,7 +47,7 @@ print(f"healthchecks-linux: Python 3.10 or newer is needed, but this is Python {_version}", file=sys.stderr) sys.exit(2) -__version__ = "2026-11-18" +__version__ = "2026-11-22" EXIT_OK = 0 EXIT_ERROR = 1