|
4 | 4 | import hashlib |
5 | 5 | import json |
6 | 6 | import re |
| 7 | +import unicodedata |
7 | 8 | from collections.abc import Mapping |
8 | 9 | from dataclasses import dataclass |
9 | 10 | from datetime import date, datetime, timedelta, timezone |
|
12 | 13 | SCHEMA_VERSION = "1" |
13 | 14 | CONTRACT_VERSION = "political_event_weekly.v1" |
14 | 15 | CADENCE = "weekly" |
| 16 | +MAX_SAFE_JSON_INTEGER = 2**53 - 1 |
15 | 17 | _DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$") |
16 | 18 | _TIMESTAMP_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z$") |
17 | 19 | _SHA256_RE = re.compile(r"^[0-9a-f]{64}$") |
@@ -44,6 +46,7 @@ class WeeklyFeedStatus: |
44 | 46 | failed_feed_count: int |
45 | 47 | stale_feed_count: int |
46 | 48 | missing_feed_count: int |
| 49 | + complete: bool |
47 | 50 |
|
48 | 51 |
|
49 | 52 | @dataclass(frozen=True, slots=True) |
@@ -88,23 +91,28 @@ def _artifact(value: object) -> WeeklySourceArtifact: |
88 | 91 | if not isinstance(value, Mapping) or set(value) != _ARTIFACT_KEYS: |
89 | 92 | raise _invalid("source_artifact_invalid") |
90 | 93 | path = value["path"] |
91 | | - if type(path) is not str or not path or PurePosixPath(path).is_absolute() or ".." in PurePosixPath(path).parts or "\\" in path: |
| 94 | + canonical = PurePosixPath(path) if type(path) is str else None |
| 95 | + if ( |
| 96 | + type(path) is not str or not path or not path.isascii() or unicodedata.normalize("NFC", path) != path |
| 97 | + or canonical is None or canonical.is_absolute() or path != str(canonical) or path.endswith("/") |
| 98 | + or "//" in path or any(part in {"", ".", ".."} for part in canonical.parts) or "\\" in path |
| 99 | + ): |
92 | 100 | raise _invalid("source_artifact_invalid") |
93 | 101 | digest = value["sha256"] |
94 | 102 | row_count = value["row_count"] |
95 | | - if type(digest) is not str or not _SHA256_RE.fullmatch(digest) or type(row_count) is not int or row_count < 0: |
| 103 | + if type(digest) is not str or not _SHA256_RE.fullmatch(digest) or type(row_count) is not int or not 0 <= row_count <= MAX_SAFE_JSON_INTEGER: |
96 | 104 | raise _invalid("source_artifact_invalid") |
97 | 105 | return WeeklySourceArtifact(path, digest, row_count) |
98 | 106 |
|
99 | 107 |
|
100 | 108 | def _feed_status(value: object) -> WeeklyFeedStatus: |
101 | | - if not isinstance(value, Mapping) or set(value) != _FEED_KEYS or value.get("complete") is not True: |
| 109 | + if not isinstance(value, Mapping) or set(value) != _FEED_KEYS or type(value.get("complete")) is not bool: |
102 | 110 | raise _invalid("feed_status_invalid") |
103 | 111 | values = [value[key] for key in _FEED_KEYS if key != "complete"] |
104 | | - if any(type(item) is not int or item < 0 for item in values): |
| 112 | + if any(type(item) is not int or not 0 <= item <= MAX_SAFE_JSON_INTEGER for item in values): |
105 | 113 | raise _invalid("feed_status_invalid") |
106 | | - status = WeeklyFeedStatus(*(value[key] for key in ("feed_count", "successful_feed_count", "failed_feed_count", "stale_feed_count", "missing_feed_count"))) |
107 | | - if status.feed_count <= 0 or status.successful_feed_count != status.feed_count or any((status.failed_feed_count, status.stale_feed_count, status.missing_feed_count)): |
| 114 | + status = WeeklyFeedStatus(*(value[key] for key in ("feed_count", "successful_feed_count", "failed_feed_count", "stale_feed_count", "missing_feed_count")), value["complete"]) |
| 115 | + if status.feed_count <= 0 or status.successful_feed_count != status.feed_count or any((status.failed_feed_count, status.stale_feed_count, status.missing_feed_count)) or not status.complete: |
108 | 116 | raise _invalid("feed_status_incomplete") |
109 | 117 | return status |
110 | 118 |
|
@@ -148,7 +156,7 @@ def serialize_weekly_contract(contract: WeeklySourceContract) -> bytes: |
148 | 156 | "generated_at": contract.generated_at.isoformat(timespec="microseconds").replace("+00:00", "Z"), |
149 | 157 | "run_mode": contract.run_mode, "producer_ref": contract.producer_ref, "source_provenance": contract.source_provenance, |
150 | 158 | "source_artifacts": [{"path": item.path, "sha256": item.sha256, "row_count": item.row_count} for item in contract.source_artifacts], |
151 | | - "feed_status": {"feed_count": contract.feed_status.feed_count, "successful_feed_count": contract.feed_status.successful_feed_count, "failed_feed_count": 0, "stale_feed_count": 0, "missing_feed_count": 0, "complete": True}, |
| 159 | + "feed_status": {"feed_count": contract.feed_status.feed_count, "successful_feed_count": contract.feed_status.successful_feed_count, "failed_feed_count": contract.feed_status.failed_feed_count, "stale_feed_count": contract.feed_status.stale_feed_count, "missing_feed_count": contract.feed_status.missing_feed_count, "complete": contract.feed_status.complete}, |
152 | 160 | } |
153 | 161 | except (AttributeError, TypeError, ValueError, OverflowError): |
154 | 162 | raise _invalid("contract_invalid") from None |
|
0 commit comments