[Security Hardening] Bound Protect media downloads by size

Open
#108 0 comments 0 reactions 0 assignees View on GitHub

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
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
backend, security

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

needs-triage security

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

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-Length above 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 MediaFetchError and 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 0 or 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from seeARMS/keystone

All issues in seeARMS/keystone

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.