Repository navigation
🛡️ Sentinel: [LOW] subtitle service answer capped while it streams - #378
Open
Randomname653 wants to merge 1 commit into
Open
Randomname653 wants to merge 1 commit into
Randomname653 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
subtitle_provider.fetch_bestrefused an oversized answer only by itsContent-Length. A chunked answer has noContent-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 throughsubtitle_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) atfetch_bestand had it send a 20 MB chunkedtext/plainanswer. 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 withaiter_bytes()into a buffer and reading stops once the buffer is over_MAX_BYTES. The decoded text uses the response charset witherrors="replace", the same wayResponse.textdecodes. 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, thepermanentflag, 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
""both times.None.Tests
New
tests/test_subtitle_provider_cap.py(stdlib harness, mock transport, counts the bytes pulled):9 passed, 2 failed(reading stops at the cap (pulled 20,000,000 bytes),an oversized 5xx is bounded too)11 passed, 0 failedThe 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: truesettles the title,permanent: falsestays transient, a plain 404 is definitive, and theContent-Lengthpre-check still works.python -m compileall -q srcis clean.python tests/run_all.py: 358/359. The one failure istest_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
Content-Lengthpre-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_bytesyields content after decompression, so a gzip-compressed answer is also bounded by its decompressed size.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 tosubtitle_provider.pysince 4c1140e). I also checked the open PRs (#376 and #377 don't touch this file) and confirmedorigin/mainwas unchanged right before pushing.Checks
python tests/run_all.pyis green (except the known sandbox-only perf suite above)mainGenerated by Claude Code