mirror of
https://github.com/TauricResearch/TradingAgents.git
synced 2026-09-19 19:25:24 +03:00
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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user