pixano_inference.utils.media_security
Security policy for media ingestion (SSRF guard, size/time limits, path containment).
Client requests reference images and video by URL, local path, or base64. Dereferencing
those references is a classic SSRF and arbitrary-file-read surface. Every fetch and every
local-path resolution goes through :class:MediaPolicy here, which:
- accepts only
http/httpsURLs (neverfile:///s3://); - resolves the URL host and rejects private/loopback/link-local/reserved IPs (with an optional host allowlist for trusted internal services);
- re-validates every redirect hop;
- enforces connect/read timeouts and a streamed maximum-byte cap;
- denies local-path media unless the path resolves under a configured
media_rootsentry.
The active policy is a per-process global built lazily from :class:ServerSettings, so a
Ray worker reconstructs the same policy from the environment its driver passed on.
MediaPolicy(allow_url=True, url_host_allowlist=frozenset(), allow_private_ips=False, media_roots=(), connect_timeout_s=5.0, read_timeout_s=30.0, max_redirects=3, max_image_bytes=50 * 1024 * 1024, max_video_bytes=512 * 1024 * 1024)
dataclass
Resolved media-ingestion security policy for the current process.
from_env()
classmethod
Build a policy from the environment (via :class:ServerSettings).
from_settings(settings)
classmethod
Build a policy from :class:ServerSettings.
Source code in pixano_inference/utils/media_security.py
MediaSecurityError
Bases: ValueError
Raised when a media reference violates the security policy.
fetch_url_bytes(url, *, max_bytes, policy=None)
Fetch a URL as bytes under the media policy (SSRF-guarded, size/time-capped).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The http/https URL to fetch. |
required |
max_bytes
|
int
|
Maximum number of bytes to read before aborting. |
required |
policy
|
MediaPolicy | None
|
Policy to apply; defaults to the active process policy. |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
The response body bytes. |
Raises:
| Type | Description |
|---|---|
MediaSecurityError
|
On any policy violation (bad scheme/host, too many redirects, or the response exceeding max_bytes). |
Source code in pixano_inference/utils/media_security.py
get_media_policy()
Return the active media policy, lazily building a secure default from the env.
Source code in pixano_inference/utils/media_security.py
is_http_url(value)
Whether value is an http/https URL (the only fetchable schemes).
resolve_local_path(raw_path, policy=None)
Resolve a client-supplied local path, enforcing containment under media_roots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str | Path
|
The path from the request. |
required |
policy
|
MediaPolicy | None
|
Policy to apply; defaults to the active process policy. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The resolved, real path. |
Raises:
| Type | Description |
|---|---|
MediaSecurityError
|
If local-path media is disabled (no roots configured) or the path escapes every configured root. |