[Security Hardening] Bound Protect media downloads by size
Maintainers usually reply within 1 day
Nobody has claimed this yet.
- #115 by @developingchet — closed without merging
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
Research direction
Start with WorkerService._fetch_media() and session setup in src/keystone/worker.py, then inspect the similar fetch in src/keystone/search.py; also review the video path in _extract_frame(). The work is already addressed by linked pull request #115, so coordinate before editing. Done means both fetch paths enforce a configurable byte limit for declared and streamed sizes while preserving timeouts, retries, and connection cleanup.
Written by the indexing model from the issue text.
Description
Summary
WorkerService._fetch_media() buffers a successful media response with await resp.read() and applies no size limit. A Protect controller or media endpoint that returns an unexpectedly large image or video export can make the worker allocate the whole body in memory, and the video path then writes it to a temporary file and hands it to FFmpeg. The request has a 90-second total timeout but no byte bound.
Affected code
_fetch_media(), specificallyreturn await resp.read(), resp.headers- Session setup with a 90-second total timeout:
worker.py - Temporary media file and FFmpeg decode:
_extract_frame()
A related pattern exists in the Find Anything image fetch, which reads the body before checking its size: search.py.
Evidence
Source inspection at dab11d2a37fa860b0bb6894fcd8a7f6451c54d0d. _fetch_media() validates the response status and returns resp.read() for every 200. _resolve_controller_url() limits the host to the configured controller, and the session pins the controller's TLS certificate, so the request cannot be pointed at an arbitrary host. aiohttp does not impose a body-size limit by default.
Reproduced with local HTTP servers and the method's own logic: a small response succeeds, a response whose declared Content-Length exceeds the limit is rejected, and a chunked response without Content-Length is rejected once the running byte count passes the limit. No real Protect controller or camera was used.
Impact
A compromised or misbehaving controller, or a media response larger than the host can hold, can exhaust the worker process's memory and the state directory's temporary space on the Keystone host. The media endpoint is the trusted Protect controller, so this is not an unauthenticated denial of service; the realistic cases are a compromised controller, a controller bug, or an operator-configured export larger than expected. The same process also serves capture and webhook work, so memory pressure affects the whole service.
Suggested remediation
- Add a configurable
controller.media_max_bytes(KEYSTONE_CONTROLLER_MEDIA_MAX_BYTES) with a realistic default (for example 256 MiB). - Reject a declared
Content-Lengthabove the limit before reading. - Otherwise read the body in chunks and abort once the running total exceeds the limit, so a chunked or understated response is still bounded.
- Keep the response context manager so the connection is released on success and on abort, and raise a clear error that does not include the URL credentials.
- Keep the existing non-200
MediaFetchErrorand retry behavior.
Acceptance criteria
- A valid image or video response is returned unchanged.
- A response with a declared size above the limit fails without buffering the body.
- A chunked response above the limit fails.
- A response that understates its size cannot exceed the limit.
- A stalled response still respects the existing timeout.
- A failed or aborted request releases the connection.
- The limit is configurable and
0or a negative value is rejected at startup. - Existing retry behavior for the video export path is preserved.
Compatibility considerations
The default is far above normal camera snapshots and short exports. The controller host restriction and TLS pinning are unchanged. Local processing, adoption, Find Anything and AI Port paths are unaffected.
- Dominant language
- Python
- Stars
- 1k
- Forks
- 114
- Avg merge
- 1h 8m
- Merged PRs (30d)
- 147
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from seeARMS/keystone
-
needs-triage
Maintainers usually reply within 1 day
-
Maintainers usually reply within 1 day
-
accepted
seeARMS/keystone#246 · 1 comment ·
Maintainers usually reply within 1 day
-
Maintainers usually reply within 1 day
-
seeARMS/keystone#241 · 1 comment ·
Maintainers usually reply within 1 day
All issues in seeARMS/keystone
Similar issues
-
Test: to_hijri() accepts datetime.datetime and uses only the date partPossibly taken A pull request linked to this issue is open or already merged. Opengood first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 · 3 comments ·
Maintainers usually reply within 1 day
-
polish
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
521xueweihan/HelloGitHub#4729 ·
-
[Bug]: Score parser reads parts of scientific notation as valid scoresPossibly taken @AshutoshSingh840 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
UKGovernmentBEIS/control-arena#992 ·
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
mvalentsev/awesome-free-ai-coding#70 ·
Maintainers usually reply within 1 day