Take Fortinet documents from the feed, its pages are WAF-blocked
This commit is contained in:
1 parent
cf301042fa
commit
716a7c5955
2 files changed
+61
-6
No files matched your search
@@ -105,7 +105,7 @@ def _xml(content: bytes, limit: int) -> ET.Element:
|
||||
raise InvalidData("invalid Fortinet XML") from error
|
||||
|
||||
|
||||
def _rss(content: bytes) -> dict[str, str]:
|
||||
def _rss(content: bytes) -> dict[str, list[str]]:
|
||||
root = _xml(content, _RSS_LIMIT)
|
||||
channels = root.findall("channel")
|
||||
if root.tag != "rss" or root.get("version") != "2.0" or len(channels) != 1:
|
||||
@@ -126,11 +126,30 @@ def _rss(content: bytes) -> dict[str, str]:
|
||||
entry.tail = None
|
||||
revision = hashlib.sha256(ET.tostring(entry, encoding="utf-8")).hexdigest()
|
||||
native = link[2]
|
||||
if revisions.setdefault(native, revision) != revision:
|
||||
document = _entry_document(entry, native)
|
||||
value = [revision, *document] if document else [revision]
|
||||
if revisions.setdefault(native, value) != value:
|
||||
raise InvalidData("conflicting Fortinet RSS advisory entry")
|
||||
return revisions
|
||||
|
||||
|
||||
_CSAF_NS = "{https://fortiguard.fortinet.com/rss/ns/csaf}link"
|
||||
_CVRF_NS = "{https://fortiguard.fortinet.com/rss/ns/cvrf}link"
|
||||
|
||||
|
||||
def _entry_document(entry: object, native: str) -> tuple[str, str] | None:
|
||||
"""The feed carries the structured document itself; the page is only a fallback."""
|
||||
for kind, tag in (("csaf", _CSAF_NS), ("cvrf", _CVRF_NS)):
|
||||
raw = entry.findtext(tag)
|
||||
if raw is None:
|
||||
continue
|
||||
candidate = _link(raw.strip(), f"https://{_HOST}")
|
||||
if not candidate or candidate[1] != kind or candidate[2] != native:
|
||||
raise InvalidData(f"invalid Fortinet RSS {kind} link")
|
||||
return kind, candidate[0]
|
||||
return None
|
||||
|
||||
|
||||
class _DownloadLinks(HTMLParser):
|
||||
def __init__(self, native: str) -> None:
|
||||
super().__init__()
|
||||
@@ -429,11 +448,15 @@ async def refresh_fortinet(state: FeedState, *, http: IndexFeedHttp, staging: Fe
|
||||
revisions = await worker(lambda: _rss(_read_bounded(path, _RSS_LIMIT)))
|
||||
docs: list[Doc] = []
|
||||
changed = False
|
||||
for native, revision in revisions.items():
|
||||
if previous.get(native) == revision:
|
||||
for native, value in revisions.items():
|
||||
if previous.get(native) == value:
|
||||
continue
|
||||
page = await fetch(_url_for(native), _DETAIL_LIMIT)
|
||||
kind, url = await worker(_download_link, page, native)
|
||||
if len(value) == 3:
|
||||
kind, url = value[1], value[2]
|
||||
else:
|
||||
# Older feed without namespaced links, or a page-only publisher.
|
||||
page = await fetch(_url_for(native), _DETAIL_LIMIT)
|
||||
kind, url = await worker(_download_link, page, native)
|
||||
content = await fetch(url, _DETAIL_LIMIT)
|
||||
docs.append(await worker(_csaf if kind == "csaf" else _cvrf, content, native))
|
||||
changed = True
|
||||
|
||||
@@ -273,3 +273,35 @@ async def test_xml_declarations_and_empty_feeds_never_publish(tmp_path: Path) ->
|
||||
assert not await sync.refresh_once("fortinet")
|
||||
assert wire.requests == [(RSS, {}), (RSS, {})]
|
||||
assert not (await store.state("fortinet"))["checkpoint"]
|
||||
|
||||
|
||||
def linked_rss(*items: tuple[str, str]) -> bytes:
|
||||
entries = "".join(
|
||||
f"<item><title>{IGNORED}</title><description>{revision}</description>"
|
||||
f"<link>https://www.fortiguard.com/psirt/{native}</link>"
|
||||
f'<csaf:link>{artifact_url(native)}</csaf:link>'
|
||||
"<pubDate>Tue, 09 Jun 2026 00:00:00 GMT</pubDate></item>"
|
||||
for native, revision in items
|
||||
)
|
||||
return ('<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" '
|
||||
'xmlns:csaf="https://fortiguard.fortinet.com/rss/ns/csaf">'
|
||||
f'<channel>{entries}</channel></rss>').encode()
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_feed_supplied_document_skips_the_blocked_advisory_page(tmp_path: Path) -> None:
|
||||
# The publisher's HTML pages are denied to some egress IPs; the feed names the
|
||||
# structured document, so the page must not be requested at all.
|
||||
wire = IndexWire({RSS: linked_rss((NATIVE, "first")), artifact_url(): csaf()})
|
||||
async with Store(tmp_path) as store:
|
||||
assert await Sync(store, wire).refresh_once("fortinet")
|
||||
assert await store.by_id(NATIVE)
|
||||
assert [url for url, _headers in wire.requests] == [RSS, artifact_url()]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_a_mismatched_feed_link_is_rejected(tmp_path: Path) -> None:
|
||||
body = linked_rss((NATIVE, "first")).replace(artifact_url(NATIVE).encode(), artifact_url(OTHER).encode())
|
||||
wire = IndexWire({RSS: body})
|
||||
async with Store(tmp_path) as store:
|
||||
assert not await Sync(store, wire).refresh_once("fortinet")
|
||||
Reference in new issue
Block a user