citeget.netguard¶
Outbound-HTTP guard: refuse to fetch anything that is not on the public internet.
citeget fetches URLs that come from its users: the url of a reference, a
page to save as Markdown, a link found inside a fetched page. When citeget runs
as a hosted web app, that is a server-side request forgery (SSRF) surface: a
user could ask the server to fetch http://127.0.0.1:<admin port>/ or the
cloud metadata service and then download the response as a job result.
Every HTTP request citeget makes goes through get() (or a
guarded_session()), which enforces a NetPolicy:
only
httpandhttpsURLs, on the first request and on every redirect;the host is resolved inside the connection step, every resolved address must be a public one, and the socket is opened to that vetted address. There is no second DNS lookup, so a rebinding DNS server cannot swap the address between the check and the connect, and every redirect hop that opens a connection is vetted the same way;
loopback, private (RFC 1918,
fc00::/7), link-local (169.254.0.0/16,fe80::/10, which covers the usual metadata address), shared/CGNAT, unspecified, reserved and multicast addresses are refused, including when they are wrapped in IPv6 (::ffff:127.0.0.1, 6to4, NAT64, Teredo);proxies and
.netrcfrom the environment are ignored (trust_env=False) and explicit proxies are refused, soHTTP_PROXYcannot route a request around the check.
Because the check is made on the address the socket actually connects to,
alternative spellings of a host (2130706433, 0x7f.1, 017700000001,
a DNS name that resolves to 10.0.0.5) are all caught: they resolve to the
same refused address. Purely numeric non-canonical hosts are also refused up front
(check_url()), because resolvers disagree on what they mean.
The guard is on by default. A desktop user who wants citeget to fetch from
their own LAN or localhost can opt out with the environment variable
CITEGET_ALLOW_PRIVATE_NETWORK=1 or by passing
policy=NetPolicy(allow_private=True).
>>> is_public_address("8.8.8.8")
True
>>> [is_public_address(a) for a in ("127.0.0.1", "169.254.169.254", "10.1.2.3")]
[False, False, False]
>>> [is_public_address(a) for a in ("::1", "::ffff:127.0.0.1", "fd00:ec2::254")]
[False, False, False]
>>> check_url("file:///etc/passwd")
Traceback (most recent call last):
...
citeget.netguard.UnsafeURLError: Refused file:///etc/passwd: scheme 'file' is not allowed (allowed: http, https)
Module Attributes
Environment variable that switches the guard off ( |
|
no ceiling). |
|
room for most books, small enough that a few at once fit in a small server's RAM (the browser path holds a body ~5x over). |
|
parsing HTML or XML costs tens of times its size in memory, so text gets a far smaller ceiling. |
|
Longest a body may take to arrive. |
|
the total deadline is only checked between reads, so a read must not hang. |
|
a port nothing listens on. |
|
Chromium flags that leave the browser no network path of its own. |
|
nothing citeget reads needs them, and every request the route serves holds up the page (the handler runs on Playwright's one thread), so ad frames, long polls and beacons are not worth a fetch. |
|
Timeouts (connect, read), in seconds, for a page itself and for what it loads. |
|
Spooled browser downloads older than this are removed at the next launch: what a killed job left behind. |
Functions
|
Move the file a guarded context streamed for url to dest. |
|
A |
|
policy with the |
|
Vet a URL before |
|
Check the parts of url that can be judged without DNS; return it. |
The policy in force when none is passed, read from the environment now. |
|
|
A non-negative integer from environment variable var, else default. |
|
|
|
Route all of a Playwright |
|
A |
|
True if address (an IP literal, possibly with a |
|
Launch Chromium with no network of its own and return |
|
Delete spooled downloads older than max_age seconds. |
|
|
|
Resolve host and return its addresses, refusing if any is not public. |
|
Cap every response body fetched in this block at limit bytes. |
The directory browser downloads are spooled to (per uid, created 0700). |
Classes
Files the route handler streamed to disk for download navigations. |
|
|
A |
A |
|
|
What outbound requests are allowed to reach. |
Exceptions
A response body is larger than the policy's |
|
A URL, or an address it resolved to, is outside what the policy allows. |
- citeget.netguard.ALLOW_PRIVATE_ENV_VAR = 'CITEGET_ALLOW_PRIVATE_NETWORK'¶
Environment variable that switches the guard off (
1/true/yes).
- citeget.netguard.BLOCKED_RESOURCE_TYPES = frozenset({'csp_report', 'eventsource', 'font', 'image', 'manifest', 'media', 'other', 'ping', 'sub_frame', 'texttrack'})¶
nothing citeget reads needs them, and every request the route serves holds up the page (the handler runs on Playwright’s one thread), so ad frames, long polls and beacons are not worth a fetch.
- Type:
Resource types refused outright
- class citeget.netguard.BrowserDownloads[source]¶
Bases:
objectFiles the route handler streamed to disk for download navigations.
Fulfilling a whole book through Playwright copies it several times over (base64, a JSON pipe, the driver, CDP) and holds the
downloadevent until the last byte has crossed, so a large book times out. Instead the handler streams a download to a temp file itself and hands Chromium an empty attachment; the caller then takes the real file from here.
- citeget.netguard.CHROMIUM_GUARD_ARGS = ('--proxy-server=http://127.0.0.1:9', '--proxy-bypass-list=<-loopback>', '--force-webrtc-ip-handling-policy=disable_non_proxied_udp', '--dns-prefetch-disable')¶
Chromium flags that leave the browser no network path of its own.
- class citeget.netguard.Ceiling(limit=None, source='', fired=False)[source]¶
Bases:
objectA
response_ceiling()in force;firedonce it refused a body.
- citeget.netguard.DEAD_PROXY = 'http://127.0.0.1:9'¶
a port nothing listens on.
- Type:
Where Chromium’s own traffic is sent
- citeget.netguard.DEFAULT_MAX_RESPONSE_BYTES = 104857600¶
room for most books, small enough that a few at once fit in a small server’s RAM (the browser path holds a body ~5x over).
- Type:
Largest response body read
- citeget.netguard.DEFAULT_MAX_RESPONSE_SECONDS = 600¶
Longest a body may take to arrive. Socket timeouts are per read, so without this a server could trickle a body for hours and hold a job slot throughout.
- citeget.netguard.DEFAULT_MAX_TEXT_BYTES = 10485760¶
parsing HTML or XML costs tens of times its size in memory, so text gets a far smaller ceiling.
- Type:
Largest body decoded as text (
.text/.json())
- citeget.netguard.DEFAULT_TIMEOUT = (10, 60)¶
the total deadline is only checked between reads, so a read must not hang.
- Type:
Default
requeststimeout (connect, read) when the caller gives none
- citeget.netguard.DOCUMENT_TIMEOUT = (10, 60)¶
Timeouts (connect, read), in seconds, for a page itself and for what it loads.
- class citeget.netguard.GuardedResponse[source]¶
Bases:
ResponseA
requests.Responsethat honours aNetPolicy’s ceilings.iter_content(and so.content, which reads through it) is capped by size and by a deadline, both counted per response however many times the body is iterated;.text/.json()refuse a body over the text ceiling. Parsing.contentdirectly bypasses the text ceiling (not the size one): citeget’s parsers all go through.text/.json().- iter_content(chunk_size=1, decode_unicode=False)[source]¶
Iterates over the response data. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place.
chunk_size must be of type int or None. A value of None will function differently depending on the value of
stream. stream=True will read data as it arrives in whatever size the chunks are received. If stream=False, data is returned as a single chunk.If decode_unicode is True, content will be decoded using encoding information from the response. If no encoding information is available, bytes will be returned. This can be bypassed by manually setting
encodingon the response.
- json(**kwargs)[source]¶
Decodes the JSON response body (if any) as a Python object.
This may return a dictionary, list, etc. depending on what is in the response.
- Parameters:
**kwargs – Optional arguments that
json.loadstakes.- Raises:
requests.exceptions.JSONDecodeError – If the response body does not contain valid json.
- property text¶
Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
charset_normalizerorchardet.The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set
r.encodingappropriately before accessing this property.
- citeget.netguard.MAX_RESPONSE_BYTES_ENV_VAR = 'CITEGET_MAX_RESPONSE_BYTES'¶
no ceiling).
- Type:
Environment variables for the response ceilings (
0
- class citeget.netguard.NetPolicy(allow_private=False, allowed_schemes=('http', 'https'), max_redirects=10, max_response_bytes=104857600, max_text_bytes=10485760, max_response_seconds=600, max_response_bytes_source='CITEGET_MAX_RESPONSE_BYTES', on_too_large=None)[source]¶
Bases:
objectWhat outbound requests are allowed to reach.
- Parameters:
allow_private (
bool) – Allow loopback / private / link-local destinations. Off by default; this is what a hosted deployment relies on.allowed_schemes (
tuple) – URL schemes allowed on the first request and on every redirect hop.max_redirects (
int) – Redirect hops followed before giving up.max_response_bytes (
Optional[int]) – Largest (decoded) response body read; a larger one raisesResponseTooLargeError.Noneor0: no ceiling.max_text_bytes (
Optional[int]) – Largest body that.text/.json()will decode.max_response_seconds (
Optional[float]) – Longest a body may take to arrive, in total.max_response_bytes_source (
str) – What sets max_response_bytes, named in the refusal so the reader knows which knob to turn.on_too_large (
Optional[Callable[[],None]]) – Called when a body is refused for max_response_bytes, before the error is raised: how aresponse_ceiling()learns it fired although the caller swallowed the error.
- exception citeget.netguard.ResponseTooLargeError[source]¶
Bases:
UnsafeURLErrorA response body is larger than the policy’s
max_response_bytes.A refusal like any other: callers that report refusals report this too, and nobody retries it.
- citeget.netguard.STALE_SPOOL_SECONDS = 21600¶
Spooled browser downloads older than this are removed at the next launch: what a killed job left behind.
- exception citeget.netguard.UnsafeURLError[source]¶
Bases:
ValueErrorA URL, or an address it resolved to, is outside what the policy allows.
A
ValueErrorrather than arequestsexception on purpose: urllib3 retries and re-wrapsOSErrorsubclasses, which would bury the reason.
- citeget.netguard.adopt_browser_download(context, url, dest)[source]¶
Move the file a guarded context streamed for url to dest.
Works across filesystems; the stash entry is dropped only once the move has succeeded. False if there is no such file (or context is unguarded).
- Return type:
- citeget.netguard.browser_route_handler(*, policy=None, document_timeout=(10, 60), subresource_timeout=(5, 10), blocked_resource_types=frozenset({'csp_report', 'eventsource', 'font', 'image', 'manifest', 'media', 'other', 'ping', 'sub_frame', 'texttrack'}), downloads=None, fetch=None)[source]¶
A
context.route("**/*", ...)handler that fetches through the guard.- Parameters:
policy (
Optional[NetPolicy]) – TheNetPolicy(default:default_policy()now).document_timeout –
requeststimeout for navigations.subresource_timeout –
requeststimeout for everything a page loads.blocked_resource_types – Playwright resource types aborted unfetched.
downloads (
Optional[BrowserDownloads]) – Where download navigations are streamed to (seeBrowserDownloads); a new one if not given. Exposed on the handler ashandler.downloads.fetch – The fetcher,
(method, url, **requests_kwargs) -> Response; defaults torequest()with policy over one guarded session for the whole handler, so a page’s subresources reuse connections (the browser owns cookies; the session keeps none between requests). A seam for tests. Playwright calls the handler from one thread.
The handler has
.downloadsand.close(), which closes the session.
- citeget.netguard.capped(policy)[source]¶
policy with the
response_ceiling()in force applied, if tighter.- Return type:
Vet a URL before
page.goto: refusesfile:,data:, internal IPs.>>> check_navigation("data:text/html,<p>hi</p>") Traceback (most recent call last): ... citeget.netguard.UnsafeURLError: Refused data:text/html,<p>hi</p>: scheme 'data' is not allowed (allowed: http, https)- Return type:
- citeget.netguard.check_url(url, *, policy=None)[source]¶
Check the parts of url that can be judged without DNS; return it.
The scheme must be allowed and there must be a host. A host written as an IP literal is judged here too. Hostnames are judged when the connection is made (see
resolve_public()), since that is the only check a DNS rebind cannot race.>>> check_url("https://example.org/paper.pdf") 'https://example.org/paper.pdf' >>> check_url("http://[::1]:8080/") Traceback (most recent call last): ... citeget.netguard.UnsafeURLError: Refused http://[::1]:8080/: ::1 is not a public address- Return type:
- citeget.netguard.default_policy()[source]¶
The policy in force when none is passed, read from the environment now.
>>> default_policy().allow_private in (True, False) True- Return type:
- citeget.netguard.env_limit(var, default)[source]¶
A non-negative integer from environment variable var, else default.
- Return type:
- citeget.netguard.get(url, *, policy=None, **kwargs)[source]¶
requests.getwith the destination guard (seerequest()).
- citeget.netguard.guard_browser_context(context, *, policy=None, **handler_kwargs)[source]¶
Route all of a Playwright
BrowserContext’s traffic through the guard.The context should come from a browser launched with
CHROMIUM_GUARD_ARGSand be created withservice_workers="block"(seelaunch_guarded_chromium()); this installs the request route, the WebSocket block and the page init script. Returns context.
- citeget.netguard.guarded_session(*, policy=None)[source]¶
A
requests.Sessionthat enforces policy on every connection and hop.Use it as a context manager, like
requests.Session. Withpolicy.allow_privateit still restricts schemes, but connects wherever DNS points and honours environment proxies like plainrequests.
- citeget.netguard.is_public_address(address)[source]¶
True if address (an IP literal, possibly with a
%zone) is public.>>> is_public_address("fe80::1%eth0") False >>> is_public_address("2002:7f00:1::") # 6to4 wrapping 127.0.0.1 False >>> is_public_address("64:ff9b::a9fe:a9fe") # NAT64 wrapping 169.254.169.254 False >>> is_public_address("::ffff:0:7f00:1") # SIIT wrapping 127.0.0.1 False >>> is_public_address("fec0::1") # deprecated site-local False- Return type:
- citeget.netguard.launch_guarded_chromium(playwright, *, headless=True, policy=None, **context_kwargs)[source]¶
Launch Chromium with no network of its own and return
(browser, context).Every request the context makes is fetched through the guard.
- citeget.netguard.remove_stale_spools(*, max_age=21600)[source]¶
Delete spooled downloads older than max_age seconds.
- Return type:
- citeget.netguard.request(method, url, *, policy=None, session=None, **kwargs)[source]¶
requests.requestwith the destination guard. Same arguments and return.sessionis aguarded_session()(under the same policy) to reuse, so successive requests share connections; the caller closes it. The session’s cookie jar is emptied before and after each call: a redirect chain still carries cookies from hop to hop, and nothing is kept between calls. Not for concurrent use.The body is read before returning (or, with
stream=True, as the caller iterates), up topolicy.max_response_bytes, and the whole exchange, redirects included, must finish withinpolicy.max_response_secondsof the call.timeoutdefaults toDEFAULT_TIMEOUT.Raises
UnsafeURLErrorif the URL, a redirect target, or an address a host resolves to is refused, orproxies=is given;ResponseTooLargeError(a subclass) if the body is over the size or time ceiling, or if.text/.json()is asked of a body over the text ceiling.
- citeget.netguard.resolve_public(host, port, *, policy=None)[source]¶
Resolve host and return its addresses, refusing if any is not public.
Every address must pass: a name with one public and one private record is refused outright rather than connected to “the good one”.
Returns
getaddrinfotuples(family, type, proto, canonname, sockaddr).- Return type:
- citeget.netguard.response_ceiling(limit, *, source="a caller's ceiling")[source]¶
Cap every response body fetched in this block at limit bytes.
For a caller that cannot reach each fetch’s
policy=– a job spending a byte budget across strategies it does not see into. Applied wherever a policy is resolved (request(),guarded_session(),browser_route_handler()), so it only ever tightens: the policy’s own ceiling still holds when it is the smaller one. Nested blocks take the smaller limit.Noneor0adds no ceiling.Yields the
Ceilingin force. Callers in between usually swallow the refusal (a strategy that fails tries the next), soceiling.firedis how the block’s owner learns its ceiling, not some other failure, stopped a fetch.>>> with response_ceiling(400, source="the job budget") as ceiling: ... capped(NetPolicy()).max_response_bytes, ceiling.fired (400, False) >>> capped(NetPolicy()).max_response_bytes == DEFAULT_MAX_RESPONSE_BYTES True