Harden input handling and filter non-global DNS addresses

X-API-Key compared as bytes (401 instead of 500 on non-ASCII), strict
parsing of the diff "since" parameter (400 instead of 500), FQDN
resolution keeps only global addresses (allow_non_global_ips to opt out).
Adds the code review report and the plan/summary for this change.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-21 09:42:51 +03:00
1 parent 02f7b49e12
commit 46d56654d8
8 files changed
+168 -9

No files matched your search

+19
View File
@@ -78,3 +78,22 @@ def test_post_schedule_auth(files, monkeypatch):
assert client.post("/schedule", json=body, headers={"X-API-Key": "secret"}).status_code == 200
assert load_json(cc.CONFIG_FILE, {})["schedule"]["asn"] == "*/5 * * * *"
assert client.get("/addresses").status_code == 200
def test_fqdn_keeps_only_global_addresses(files, monkeypatch):
answers = [(2, 1, 6, "", (address, 0)) for address in
("93.184.216.34", "127.0.0.1", "10.0.0.5", "0.0.0.0", "fe80::1%eth0", "2606:2800:220:1::1")]
monkeypatch.setattr(cc.socket, "getaddrinfo", lambda *args, **kwargs: answers)
(files / "config.json").write_text('{"fqdns": ["x.example"]}')
assert sorted(cc.FQDNCollector().resolve_fqdn("x.example")) == ["2606:2800:220:1::1", "93.184.216.34"]
# Внутренние имена: фильтр отключается, зона IPv6 отбрасывается
(files / "config.json").write_text('{"fqdns": ["x.example"], "allow_non_global_ips": true}')
everything = cc.FQDNCollector().resolve_fqdn("x.example")
assert {"127.0.0.1", "10.0.0.5", "fe80::1"} <= set(everything) and len(everything) == 6
# Глобальных адресов нет - результат пуст, как при ошибке DNS (сбор ничего не удаляет)
monkeypatch.setattr(cc.socket, "getaddrinfo", lambda *args, **kwargs: [answers[1]])
(files / "config.json").write_text('{"fqdns": ["x.example"]}')
assert cc.FQDNCollector().resolve_fqdn("x.example") == []