Skip to content

🛡️ Sentinel: [LOW] subtitle service answer capped while it streams - #378

Open
Randomname653 wants to merge 1 commit into
mainfrom
sentinel/subtitle-provider-cap
Open

Randomname653 wants to merge 1 commit into
mainfrom
sentinel/subtitle-provider-cap

Conversation

@Randomname653

Copy link
Copy Markdown
Owner

What & why

subtitle_provider.fetch_best refused an oversized answer only by its Content-Length. A chunked answer has no Content-Length, so the body was read whole (await resp.aread()) and only then compared to the 4 MB _MAX_BYTES. SECURITY.md ("Bodies from the outside") says subtitle downloads are bounded while streaming, never buffered first and measured after. The OpenSubtitles leg already works that way through subtitle_signals._bounded_text. The operator's own provider leg did not. The service is the operator's own, so this hardens a documented invariant rather than closing an exploit.

Problem (evidence)

I pointed a mock service (httpx.MockTransport) at fetch_best and had it send a 20 MB chunked text/plain answer. On current main it pulled 20,000,000 bytes into memory and then returned "". A 20 MB chunked 5xx body was also read in full.

Change

In place of resp.aread(), the body is read with aiter_bytes() into a buffer and reading stops once the buffer is over _MAX_BYTES. The decoded text uses the response charset with errors="replace", the same way Response.text decodes. The JSON error body is parsed from the same bytes. Everything that decides the result runs in the same order as before: the HTML sniff, the permanent flag, 404/204, 401/403, raise_for_status. Only after those does an oversized body return "". So an oversized 5xx is still transient (None) and an oversized HTML 404 is still treated as a misconfiguration (None). 17 lines added, 7 removed.

Measured effect / proof

  • 20 MB chunked 200: pulled 20,000,000 bytes before the change, at most cap + one chunk after. The result is "" both times.
  • 20 MB chunked 500: same bound, and the result stays None.

Tests

New tests/test_subtitle_provider_cap.py (stdlib harness, mock transport, counts the bytes pulled):

  • before: 9 passed, 2 failed (reading stops at the cap (pulled 20,000,000 bytes), an oversized 5xx is bounded too)
  • after: 11 passed, 0 failed

The other checks pin the behaviour that must not change: a normal chunked SRT is returned whole and its BOM is stripped, a latin-1 charset is honoured, a JSON permanent: true settles the title, permanent: false stays transient, a plain 404 is definitive, and the Content-Length pre-check still works.

python -m compileall -q src is clean. python tests/run_all.py: 358/359. The one failure is test_enrichment_status_perf.py, which also fails on untouched main in my sandbox because SQLite 3.45.1 picks another query plan; it passes in CI.

Risk & edge cases

  • The cap now counts decoded bytes rather than decoded characters. That matches the Content-Length pre-check, which already counts bytes. A file with many multi-byte characters near 4 MB could now be refused where it was accepted before, but real subtitle files are tens to hundreds of KB.
  • aiter_bytes yields content after decompression, so a gzip-compressed answer is also bounded by its decompressed size.
  • An empty body (204/404) decodes to "", the same as before.

Duplicate check

I searched the Sentinel, Bolt and Palette journals, PRs mentioning "subtitle" (open and closed: #166, #265, #210 and #150 are unrelated) and git log --oneline -300 (no change to subtitle_provider.py since 4c1140e). I also checked the open PRs (#376 and #377 don't touch this file) and confirmed origin/main was unchanged right before pushing.

Checks

  • python tests/run_all.py is green (except the known sandbox-only perf suite above)
  • Behaviour changes come with a test that would have caught the old behaviour
  • No personal data, tokens or hardcoded local paths in the diff
  • Based on current main

Generated by Claude Code

fetch_best refused an oversized answer only by its Content-Length; a
chunked body was read whole (resp.aread()) and only then compared to the
4 MB cap. Count while reading and stop at the cap, as SECURITY.md
('Bodies from the outside') promises for subtitle downloads. Status,
HTML and JSON-permanent handling unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014GBDVC3PydBM5F98w9EkKq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants