nemo_deploy.multimodal.image_url_validator#
Module Contents#
Functions#
Return True if ip must not be reachable via an image URL fetch. |
|
Resolve hostname to every A/AAAA address and reject if any is blocked. |
|
Raise ValueError if url is not a safe http/https URL. |
|
Issue a GET request connected directly to ip, without following redirects. |
|
Safely fetch image bytes from url, guarding against SSRF. |
|
Safely fetch an image and return it as a base64 data URI. |
Data#
API#
- nemo_deploy.multimodal.image_url_validator._BLOCKED_NETWORKS = None#
- nemo_deploy.multimodal.image_url_validator._is_blocked_ip(
- ip: ipaddress.IPv4Address | ipaddress.IPv6Address,
Return True if ip must not be reachable via an image URL fetch.
- nemo_deploy.multimodal.image_url_validator._resolve_and_validate(hostname: str) list[tuple[int, str]]#
Resolve hostname to every A/AAAA address and reject if any is blocked.
Returns a list of (address_family, ip_string) tuples for all resolved addresses. Validating every returned address (rather than just the first) guards against DNS answers that mix a public address with a private/link-local one (DNS rebinding / multi-answer SSRF).
- nemo_deploy.multimodal.image_url_validator.validate_image_url(url: str) None#
Raise ValueError if url is not a safe http/https URL.
Rejects file://, non-http(s) schemes, and URLs that resolve to private/link-local/loopback ranges (SSRF guard). This is a point-in-time check; use fetch_image_bytes_safely()/fetch_image_data_uri_safely() to actually retrieve the image, since those pin validation to the connection itself and guard redirects.
- nemo_deploy.multimodal.image_url_validator._pinned_request(
- url: str,
- hostname: str,
- ip: str,
- family: int,
- timeout: float,
Issue a GET request connected directly to ip, without following redirects.
Connecting to the pre-validated IP directly (instead of letting the HTTP client re-resolve hostname) closes the TOCTOU window between validation and connection that DNS rebinding exploits. The Host header and TLS SNI still use the original hostname so virtual hosting and certificate validation behave normally.
- nemo_deploy.multimodal.image_url_validator.fetch_image_bytes_safely(
- url: str,
- timeout: float = 5,
- max_redirects: int = 5,
Safely fetch image bytes from url, guarding against SSRF.
Unlike validate_image_url() followed by a separate request, this resolves and validates every hostname (including redirect targets) immediately before connecting, connects directly to the validated IP address, and never auto-follows redirects — each hop is re-validated from scratch.
- nemo_deploy.multimodal.image_url_validator.fetch_image_data_uri_safely(
- url: str,
- timeout: float = 5,
- max_redirects: int = 5,
Safely fetch an image and return it as a base64 data URI.
Useful for handing an image to code (e.g. third-party libraries) that would otherwise perform its own, unguarded network fetch given a raw URL — the network I/O happens here, under the SSRF guard, instead.