harden(dataflows): bound the Reddit feed read before parsing

- ElementTree does not resolve external entities, so the reported XXE flag
  doesn't apply; the real residual is an unbounded read of untrusted network XML
- cap both the RSS and JSON reads at 5 MiB; overflow degrades to empty / RSS
  fallback through the existing failure paths #1206 #1276
This commit is contained in:
Yijia-Xiao
2026-09-01 05:14:23 +00:00
parent a4acd8a174
commit 5a26ae17a1
2 changed files with 30 additions and 4 deletions

View File

@@ -36,8 +36,9 @@ def _resp(read_fn):
def __exit__(self_inner, *a):
return False
def read(self_inner):
return read_fn()
def read(self_inner, size=-1):
data = read_fn()
return data if size is None or size < 0 else data[:size]
return _Resp()
@@ -183,6 +184,14 @@ class TestChunkedTransferErrorsHandled:
reddit._fetch_subreddit_json("NVDA", "stocks", 5, 5.0)
rss.assert_called_once()
def test_oversized_rss_feed_is_refused_not_parsed(self):
# A hostile/misbehaving endpoint streaming an unbounded body must not be
# read into memory before parsing; overflow degrades to an empty feed.
big = _resp(lambda: b"x" * 100)
with patch.object(reddit, "_MAX_FEED_BYTES", 10), \
patch.object(reddit, "urlopen", return_value=big):
assert reddit._fetch_subreddit_rss("NVDA", "stocks", 5, 5.0) == []
@pytest.mark.unit
class TestFormatterHandlesRssPosts: