Skip one unreadable publisher document instead of the whole catalogue
This commit is contained in:
1 parent
aca8e5da1a
commit
58e4ec35ac
4 files changed
+89
-14
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass, field
|
||||
@@ -16,6 +17,8 @@ from ..transport import HttpError
|
||||
from .feeds_common import (
|
||||
FeedStage, FeedState, IndexFeedHttp, RefreshResult, conditional, response_validators, worker,
|
||||
)
|
||||
_LOG = logging.getLogger(__name__)
|
||||
|
||||
from .parsers import InvalidData
|
||||
|
||||
_SOURCE = "android"
|
||||
@@ -605,8 +608,17 @@ async def refresh_android(state: FeedState, *, http: IndexFeedHttp, staging: Fee
|
||||
if status == 304 and same_entry and request:
|
||||
continue
|
||||
if status != 200:
|
||||
raise InvalidData("Android bulletin was not downloaded")
|
||||
doc = await worker(_read_bulletin, path, entry)
|
||||
if old is None:
|
||||
raise InvalidData("Android bulletin was not downloaded")
|
||||
months.pop(stamp, None)
|
||||
continue
|
||||
try:
|
||||
doc = await worker(_read_bulletin, path, entry)
|
||||
except InvalidData:
|
||||
# One unreadable month must not block the rest of the archive.
|
||||
_LOG.warning("%s skipped unreadable bulletin %s", _SOURCE, stamp)
|
||||
months.pop(stamp, None)
|
||||
continue
|
||||
if old is not None:
|
||||
old_entry = _Month(old["entry"][0], stamp, _day(old["entry"][1]), tuple(old["entry"][2]))
|
||||
previous = await worker(_read_bulletin, staging.artifact_path(old["artifact"]), old_entry)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Mapping, Sequence
|
||||
from datetime import date
|
||||
@@ -14,6 +15,8 @@ from ..models import Doc, Issue, ProviderPage, Status, utc_now
|
||||
from ..transport import HttpError
|
||||
from .feeds_common import (FeedStage, FeedState, IndexFeedHttp, RefreshResult, conditional,
|
||||
instant, response_validators, stage_batches, worker)
|
||||
_LOG = logging.getLogger(__name__)
|
||||
|
||||
from .parsers import InvalidData
|
||||
|
||||
_SOURCE = "web"
|
||||
@@ -261,11 +264,20 @@ def _read_catalog(path: Path) -> list[Doc]:
|
||||
if len(content) > _LIMIT:
|
||||
raise InvalidData("Apache advisory catalog exceeds size limit")
|
||||
docs: dict[str, Doc] = {}
|
||||
skipped = 0
|
||||
for row in _array(json.loads(content), _MAX_RECORDS):
|
||||
if (doc := _advisory(row)) is not None:
|
||||
try:
|
||||
doc = _advisory(row)
|
||||
except InvalidData:
|
||||
# A record we cannot read is one gap, not a failed weekly import.
|
||||
skipped += 1
|
||||
continue
|
||||
if doc is not None:
|
||||
if doc.native_id in docs:
|
||||
raise InvalidData("duplicate Apache advisory ID")
|
||||
docs[doc.native_id] = doc
|
||||
if skipped:
|
||||
_LOG.warning("%s skipped %d unreadable records", _SOURCE, skipped)
|
||||
if not docs:
|
||||
raise InvalidData("Apache catalog contains no published advisories")
|
||||
return list(docs.values())
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
@@ -13,8 +14,11 @@ from urllib.parse import urljoin, urlsplit
|
||||
from .feeds_common import (BATCH_SIZE, JSON_LIMIT, MANIFEST_LIMIT, RECORD_LIMIT, FeedStage,
|
||||
FeedState, IndexFeedHttp, RefreshResult, conditional, hash_file,
|
||||
instant, read_json, response_validators, worker)
|
||||
from ..transport import HttpError
|
||||
from .parsers import InvalidData, parse
|
||||
|
||||
_LOG = logging.getLogger(__name__)
|
||||
|
||||
SIEMENS_METADATA = "https://cert-portal.siemens.com/productcert/csaf/provider-metadata.json"
|
||||
MSRC_METADATA = "https://api.msrc.microsoft.com/cvrf/v3.0/csaf/provider-metadata.json"
|
||||
MSRC_DIRECTORY = "https://msrc.microsoft.com/csaf/advisories/"
|
||||
@@ -167,25 +171,43 @@ async def _download_docs(
|
||||
path = staging.directory / "advisory.json"
|
||||
hash_path = staging.directory / "advisory.sha512"
|
||||
batch = []
|
||||
kept = 0
|
||||
for entry in entries:
|
||||
status, _ = await http.download(source, entry.url, path, limit=JSON_LIMIT)
|
||||
if status != 200:
|
||||
raise InvalidData("CSAF document was not downloaded")
|
||||
if entry.hash_url is not None:
|
||||
status, _ = await http.download(source, entry.hash_url, hash_path, limit=1024)
|
||||
if status != 200 or await worker(_expected_digest, hash_path) != await hash_file(path, "sha512"):
|
||||
raise InvalidData("CSAF document SHA-512 mismatch")
|
||||
await worker(hash_path.unlink)
|
||||
doc = await worker(_parse_advisory, source, path, entry)
|
||||
doc.artifact = await staging.artifact(path)
|
||||
batch.append(doc)
|
||||
try:
|
||||
docs = await _one_document(entry, source, http=http, staging=staging, path=path, hash_path=hash_path)
|
||||
except (InvalidData, HttpError) as exc:
|
||||
# One broken or withdrawn document must not cost the whole catalogue.
|
||||
if isinstance(exc, HttpError) and exc.status != "not_found":
|
||||
raise
|
||||
_LOG.warning("%s skipped %s %s", source, entry.id, getattr(exc, "status", type(exc).__name__))
|
||||
continue
|
||||
kept += 1
|
||||
batch.append(docs)
|
||||
if len(batch) == BATCH_SIZE:
|
||||
await staging.add("base", batch)
|
||||
batch = []
|
||||
if entries and not kept:
|
||||
raise InvalidData("no CSAF document in this catalogue could be imported")
|
||||
if batch:
|
||||
await staging.add("base", batch)
|
||||
|
||||
|
||||
async def _one_document(
|
||||
entry: _Advisory, source: str, *, http: IndexFeedHttp, staging: FeedStage, path: Path, hash_path: Path,
|
||||
):
|
||||
status, _ = await http.download(source, entry.url, path, limit=JSON_LIMIT)
|
||||
if status != 200:
|
||||
raise HttpError("not_found" if status == 404 else "invalid_data")
|
||||
if entry.hash_url is not None:
|
||||
status, _ = await http.download(source, entry.hash_url, hash_path, limit=1024)
|
||||
if status != 200 or await worker(_expected_digest, hash_path) != await hash_file(path, "sha512"):
|
||||
raise InvalidData("CSAF document SHA-512 mismatch")
|
||||
await worker(hash_path.unlink)
|
||||
doc = await worker(_parse_advisory, source, path, entry)
|
||||
doc.artifact = await staging.artifact(path)
|
||||
return doc
|
||||
|
||||
|
||||
def _parse_advisory(source: str, path: Path, entry: _Advisory):
|
||||
doc = parse(source, read_json(path), entry.url)
|
||||
if doc.native_id.casefold() != entry.id.casefold():
|
||||
|
||||
@@ -120,3 +120,32 @@ def test_adv_id_must_match_microsoft_tracking_id() -> None:
|
||||
def test_other_csaf_sources_remain_strict_about_undefined_products() -> None:
|
||||
with pytest.raises(InvalidData, match="unresolved CSAF status product"):
|
||||
parse("siemens", _cve_document(), "https://cert-portal.siemens.com/productcert/csaf/ssa-123456.json")
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_one_broken_document_does_not_cost_the_catalogue(tmp_path, monkeypatch, caplog) -> None:
|
||||
metadata = {"metadata_version": "2.0", "list_on_CSAF_aggregators": True,
|
||||
"distributions": [{"directory_url": _ROOT.rstrip("/")}]}
|
||||
manifest = (b'"2026/msrc_cve-2026-70175.json","2026-11-23T08:00:00Z"\r\n'
|
||||
b'"2026/msrc_adv260001.json","2026-10-13T07:00:00Z"\r\n')
|
||||
bodies = {_METADATA: metadata, _CVE_URL: _cve_document(), _ADV_URL: {"document": {"tracking": {}}}}
|
||||
original_client = httpx.AsyncClient
|
||||
|
||||
def make_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
|
||||
def respond(request: httpx.Request) -> httpx.Response:
|
||||
url = str(request.url)
|
||||
if url == _MANIFEST:
|
||||
return httpx.Response(200, content=manifest)
|
||||
return httpx.Response(200, json=bodies[url])
|
||||
|
||||
return original_client(transport=httpx.MockTransport(respond), trust_env=False)
|
||||
|
||||
monkeypatch.setattr(httpx, "AsyncClient", make_client)
|
||||
caplog.set_level("WARNING")
|
||||
settings = SimpleNamespace(proxies=("http://proxy.test:8080",), max_retries=0, live_route="proxy", live_timeout=9.5)
|
||||
async with Store(tmp_path) as store, HttpClients(settings, store) as clients:
|
||||
assert await Sync(store, clients.index).refresh_once("msrc")
|
||||
[cve] = await store.by_id(_CVE)
|
||||
assert cve.native_id == "msrc_" + _CVE
|
||||
assert not await store.by_id(_ADV)
|
||||
assert "msrc skipped msrc_adv260001" in caplog.text
|
||||
Reference in new issue
Block a user