certinspect 2.4.2__tar.gz → 2.4.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {certinspect-2.4.2/src/certinspect.egg-info → certinspect-2.4.4}/PKG-INFO +12 -4
- {certinspect-2.4.2 → certinspect-2.4.4}/README.md +11 -3
- {certinspect-2.4.2 → certinspect-2.4.4}/pyproject.toml +1 -1
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/__init__.py +1 -1
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/cli.py +13 -2
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/fetch.py +66 -20
- certinspect-2.4.4/src/certinspect/httpfetch.py +241 -0
- {certinspect-2.4.2 → certinspect-2.4.4/src/certinspect.egg-info}/PKG-INFO +12 -4
- {certinspect-2.4.2 → certinspect-2.4.4}/tests/test_cli.py +35 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/tests/test_fetch.py +107 -0
- certinspect-2.4.2/src/certinspect/httpfetch.py +0 -111
- {certinspect-2.4.2 → certinspect-2.4.4}/LICENSE +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/setup.cfg +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/args.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/completion.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/config.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/discover.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/exit_codes.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/formatter.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/models.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/parser.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/render.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/revocation.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect/state.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect.egg-info/SOURCES.txt +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect.egg-info/dependency_links.txt +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect.egg-info/entry_points.txt +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect.egg-info/requires.txt +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/src/certinspect.egg-info/top_level.txt +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/tests/test_discover.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/tests/test_formatter.py +0 -0
- {certinspect-2.4.2 → certinspect-2.4.4}/tests/test_parser.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: certinspect
|
|
3
|
-
Version: 2.4.
|
|
3
|
+
Version: 2.4.4
|
|
4
4
|
Summary: Command-line TLS certificate inspector
|
|
5
5
|
Author-email: Michele Angrisano <michele.angrisano@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -339,7 +339,10 @@ SAN:
|
|
|
339
339
|
|
|
340
340
|
By default certinspect opens a fully verified TLS handshake (chain +
|
|
341
341
|
hostname against the Python/OpenSSL trust store) and, when the certificate
|
|
342
|
-
advertises an OCSP responder, queries it for the revocation status.
|
|
342
|
+
advertises an OCSP responder, queries it for the revocation status. The
|
|
343
|
+
verified handshake is a second connection: if the server presents a different
|
|
344
|
+
certificate on it (a load balancer mid-rotation, say), the chain is reported
|
|
345
|
+
as not trusted instead of vouching for a certificate that was not inspected. If OCSP is
|
|
343
346
|
unavailable, certinspect falls back to the certificate's CRL distribution
|
|
344
347
|
points (downloaded over HTTP and verified against the issuer). Both checks are
|
|
345
348
|
soft-fail: when neither OCSP nor the CRL gives an answer the status is
|
|
@@ -349,7 +352,9 @@ is treated as `UNAVAILABLE` too, so a replayed answer cannot mask a later
|
|
|
349
352
|
revocation. The OCSP/CRL/CA-Issuer URLs come from the certificate, so
|
|
350
353
|
certinspect refuses to follow one that resolves to a loopback, link-local
|
|
351
354
|
(cloud metadata) or otherwise non-routable address, and caps the download
|
|
352
|
-
size — private/internal PKI addresses stay reachable.
|
|
355
|
+
size — private/internal PKI addresses stay reachable. It connects to the very
|
|
356
|
+
address it checked, so a DNS answer that changes in between (DNS rebinding)
|
|
357
|
+
cannot send the request elsewhere; through a proxy, the proxy resolves the name.
|
|
353
358
|
|
|
354
359
|
When the responder is down (or the certificate has no OCSP URL at all) the CRL
|
|
355
360
|
fallback can still catch a revoked certificate; the `via CRL` note shows which
|
|
@@ -756,7 +761,10 @@ The report is identical to a plain inspection; only how/where certinspect
|
|
|
756
761
|
connects changes. Split the single `--timeout` into `--connect-timeout` and
|
|
757
762
|
`--read-timeout` to fail fast on dead hosts while still allowing a slow
|
|
758
763
|
handshake (requests-style), and add `--retries` to ride out transient network
|
|
759
|
-
blips instead of reporting a false failure.
|
|
764
|
+
blips instead of reporting a false failure. The read timeout bounds each whole
|
|
765
|
+
server reply (TLS handshake, each STARTTLS reply, the proxy `CONNECT` answer),
|
|
766
|
+
not a single read, so a server sending one byte at a time cannot stall a run;
|
|
767
|
+
OCSP/CRL/CA-Issuer downloads have an overall 60-second limit.
|
|
760
768
|
|
|
761
769
|
```console
|
|
762
770
|
$ certinspect example.com --connect-timeout 3 --read-timeout 10 --retries 2
|
|
@@ -313,7 +313,10 @@ SAN:
|
|
|
313
313
|
|
|
314
314
|
By default certinspect opens a fully verified TLS handshake (chain +
|
|
315
315
|
hostname against the Python/OpenSSL trust store) and, when the certificate
|
|
316
|
-
advertises an OCSP responder, queries it for the revocation status.
|
|
316
|
+
advertises an OCSP responder, queries it for the revocation status. The
|
|
317
|
+
verified handshake is a second connection: if the server presents a different
|
|
318
|
+
certificate on it (a load balancer mid-rotation, say), the chain is reported
|
|
319
|
+
as not trusted instead of vouching for a certificate that was not inspected. If OCSP is
|
|
317
320
|
unavailable, certinspect falls back to the certificate's CRL distribution
|
|
318
321
|
points (downloaded over HTTP and verified against the issuer). Both checks are
|
|
319
322
|
soft-fail: when neither OCSP nor the CRL gives an answer the status is
|
|
@@ -323,7 +326,9 @@ is treated as `UNAVAILABLE` too, so a replayed answer cannot mask a later
|
|
|
323
326
|
revocation. The OCSP/CRL/CA-Issuer URLs come from the certificate, so
|
|
324
327
|
certinspect refuses to follow one that resolves to a loopback, link-local
|
|
325
328
|
(cloud metadata) or otherwise non-routable address, and caps the download
|
|
326
|
-
size — private/internal PKI addresses stay reachable.
|
|
329
|
+
size — private/internal PKI addresses stay reachable. It connects to the very
|
|
330
|
+
address it checked, so a DNS answer that changes in between (DNS rebinding)
|
|
331
|
+
cannot send the request elsewhere; through a proxy, the proxy resolves the name.
|
|
327
332
|
|
|
328
333
|
When the responder is down (or the certificate has no OCSP URL at all) the CRL
|
|
329
334
|
fallback can still catch a revoked certificate; the `via CRL` note shows which
|
|
@@ -730,7 +735,10 @@ The report is identical to a plain inspection; only how/where certinspect
|
|
|
730
735
|
connects changes. Split the single `--timeout` into `--connect-timeout` and
|
|
731
736
|
`--read-timeout` to fail fast on dead hosts while still allowing a slow
|
|
732
737
|
handshake (requests-style), and add `--retries` to ride out transient network
|
|
733
|
-
blips instead of reporting a false failure.
|
|
738
|
+
blips instead of reporting a false failure. The read timeout bounds each whole
|
|
739
|
+
server reply (TLS handshake, each STARTTLS reply, the proxy `CONNECT` answer),
|
|
740
|
+
not a single read, so a server sending one byte at a time cannot stall a run;
|
|
741
|
+
OCSP/CRL/CA-Issuer downloads have an overall 60-second limit.
|
|
734
742
|
|
|
735
743
|
```console
|
|
736
744
|
$ certinspect example.com --connect-timeout 3 --read-timeout 10 --retries 2
|
|
@@ -254,7 +254,9 @@ def _inspect(
|
|
|
254
254
|
if opts.file and (opts.verify or opts.chain):
|
|
255
255
|
file_bundle = load_certificates(der)
|
|
256
256
|
|
|
257
|
-
override, chain_certs = _check_chain(
|
|
257
|
+
override, chain_certs = _check_chain(
|
|
258
|
+
info, target, port, cert, conn, opts, file_bundle
|
|
259
|
+
)
|
|
258
260
|
codes.append(override)
|
|
259
261
|
if opts.verify and target:
|
|
260
262
|
codes.append(_check_revocation(info, cert, chain_certs, opts))
|
|
@@ -288,6 +290,7 @@ def _check_chain(
|
|
|
288
290
|
info: CertificateInfo,
|
|
289
291
|
target: str | None,
|
|
290
292
|
port: int,
|
|
293
|
+
cert,
|
|
291
294
|
conn: dict | None,
|
|
292
295
|
opts: InspectOptions,
|
|
293
296
|
file_bundle: list | None,
|
|
@@ -298,7 +301,9 @@ def _check_chain(
|
|
|
298
301
|
Sets ``chain_trusted``/``chain_error`` and, when it fails,
|
|
299
302
|
``chain_diagnosis``. Returns UNTRUSTED_OR_REVOKED when the chain is not
|
|
300
303
|
trusted (else None) and the verified chain, leaf first ([] when
|
|
301
|
-
unavailable).
|
|
304
|
+
unavailable). A host whose verification handshake presented a different
|
|
305
|
+
leaf than ``cert`` (a load balancer mid-rotation, say) is not trusted: the
|
|
306
|
+
verdict would belong to another certificate.
|
|
302
307
|
"""
|
|
303
308
|
if not opts.verify:
|
|
304
309
|
return None, []
|
|
@@ -315,6 +320,12 @@ def _check_chain(
|
|
|
315
320
|
opts.retries,
|
|
316
321
|
)
|
|
317
322
|
presented = conn.get("chain") if conn else None
|
|
323
|
+
if trusted and verified and verified[0] != cert:
|
|
324
|
+
trusted, verified = False, []
|
|
325
|
+
reason = (
|
|
326
|
+
"the server presented a different certificate on the "
|
|
327
|
+
"verification handshake"
|
|
328
|
+
)
|
|
318
329
|
else:
|
|
319
330
|
return None, []
|
|
320
331
|
|
|
@@ -27,11 +27,15 @@ from cryptography.x509.oid import NameOID
|
|
|
27
27
|
STARTTLS_PORTS = {"smtp": 587, "imap": 143, "pop3": 110, "ftp": 21}
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
def _read_connect_response(sock: socket.socket) -> bytes:
|
|
31
|
-
"""Read an HTTP CONNECT response up to the end of its header block.
|
|
30
|
+
def _read_connect_response(sock: socket.socket, timeout: float) -> bytes:
|
|
31
|
+
"""Read an HTTP CONNECT response up to the end of its header block.
|
|
32
|
+
|
|
33
|
+
The whole response must arrive within ``timeout`` seconds.
|
|
34
|
+
"""
|
|
35
|
+
reader = _Deadline(sock, timeout)
|
|
32
36
|
buf = bytearray()
|
|
33
37
|
while b"\r\n\r\n" not in buf:
|
|
34
|
-
chunk =
|
|
38
|
+
chunk = reader.recv(256)
|
|
35
39
|
if not chunk:
|
|
36
40
|
break
|
|
37
41
|
buf += chunk
|
|
@@ -95,7 +99,7 @@ def _open_socket(
|
|
|
95
99
|
request += f"Proxy-Authorization: Basic {token}\r\n"
|
|
96
100
|
request += "\r\n"
|
|
97
101
|
sock.sendall(request.encode("ascii"))
|
|
98
|
-
status_line = _read_connect_response(sock).split(b"\r\n", 1)[0]
|
|
102
|
+
status_line = _read_connect_response(sock, connect_timeout).split(b"\r\n", 1)[0]
|
|
99
103
|
fields = status_line.split(None, 2)
|
|
100
104
|
if len(fields) < 2 or fields[1] != b"200":
|
|
101
105
|
raise ValueError(
|
|
@@ -141,6 +145,28 @@ _MAX_STARTTLS_LINE_BYTES = 8192
|
|
|
141
145
|
_MAX_STARTTLS_REPLY_LINES = 100
|
|
142
146
|
|
|
143
147
|
|
|
148
|
+
class _Deadline:
|
|
149
|
+
"""A read-only view of a socket that bounds the total time of its reads.
|
|
150
|
+
|
|
151
|
+
A socket timeout applies to each ``recv`` alone, so a server sending one
|
|
152
|
+
byte just before it expires could keep a read loop going indefinitely.
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
def __init__(self, sock: socket.socket, timeout: float):
|
|
156
|
+
self._sock = sock
|
|
157
|
+
self._timeout = timeout
|
|
158
|
+
self._end = time.monotonic() + timeout
|
|
159
|
+
|
|
160
|
+
def recv(self, n: int) -> bytes:
|
|
161
|
+
remaining = self._end - time.monotonic()
|
|
162
|
+
if remaining <= 0:
|
|
163
|
+
raise TimeoutError(
|
|
164
|
+
f"server reply took longer than {self._timeout:g}s to arrive"
|
|
165
|
+
)
|
|
166
|
+
self._sock.settimeout(remaining)
|
|
167
|
+
return self._sock.recv(n)
|
|
168
|
+
|
|
169
|
+
|
|
144
170
|
def _readline(sock: socket.socket) -> bytes:
|
|
145
171
|
"""Read one line (up to and including ``\\n``) from a plaintext socket.
|
|
146
172
|
|
|
@@ -179,33 +205,49 @@ def _expect(line: bytes, prefix: bytes) -> None:
|
|
|
179
205
|
raise ValueError(f"unexpected STARTTLS reply: {line!r}")
|
|
180
206
|
|
|
181
207
|
|
|
182
|
-
def _negotiate_starttls(
|
|
208
|
+
def _negotiate_starttls(
|
|
209
|
+
sock: socket.socket, protocol: str, timeout: float | None = None
|
|
210
|
+
) -> None:
|
|
183
211
|
"""Run the plaintext STARTTLS handshake for ``protocol`` on ``sock``.
|
|
184
212
|
|
|
185
213
|
Supports the line-based protocols smtp, imap, pop3 and ftp. On return the
|
|
186
214
|
socket is ready to be wrapped in TLS. Raises ValueError if the server does
|
|
187
|
-
not agree to upgrade.
|
|
215
|
+
not agree to upgrade. With ``timeout``, each server reply must arrive in
|
|
216
|
+
full within that many seconds (TimeoutError otherwise), and the socket is
|
|
217
|
+
left with that timeout for the TLS handshake.
|
|
188
218
|
"""
|
|
219
|
+
try:
|
|
220
|
+
_run_starttls(sock, protocol, timeout)
|
|
221
|
+
finally:
|
|
222
|
+
if timeout is not None:
|
|
223
|
+
sock.settimeout(timeout)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _run_starttls(sock: socket.socket, protocol: str, timeout: float | None) -> None:
|
|
227
|
+
def reply():
|
|
228
|
+
return sock if timeout is None else _Deadline(sock, timeout)
|
|
229
|
+
|
|
189
230
|
proto = protocol.lower()
|
|
190
231
|
if proto == "smtp":
|
|
191
|
-
_expect(_read_reply(
|
|
232
|
+
_expect(_read_reply(reply()), b"220")
|
|
192
233
|
sock.sendall(b"EHLO certinspect\r\n")
|
|
193
|
-
_expect(_read_reply(
|
|
234
|
+
_expect(_read_reply(reply()), b"250")
|
|
194
235
|
sock.sendall(b"STARTTLS\r\n")
|
|
195
|
-
_expect(_read_reply(
|
|
236
|
+
_expect(_read_reply(reply()), b"220")
|
|
196
237
|
elif proto == "ftp":
|
|
197
|
-
_expect(_read_reply(
|
|
238
|
+
_expect(_read_reply(reply()), b"220")
|
|
198
239
|
sock.sendall(b"AUTH TLS\r\n")
|
|
199
|
-
_expect(_read_reply(
|
|
240
|
+
_expect(_read_reply(reply()), b"234")
|
|
200
241
|
elif proto == "pop3":
|
|
201
|
-
_expect(_readline(
|
|
242
|
+
_expect(_readline(reply()), b"+OK")
|
|
202
243
|
sock.sendall(b"STLS\r\n")
|
|
203
|
-
_expect(_readline(
|
|
244
|
+
_expect(_readline(reply()), b"+OK")
|
|
204
245
|
elif proto == "imap":
|
|
205
|
-
_expect(_readline(
|
|
246
|
+
_expect(_readline(reply()), b"* OK")
|
|
206
247
|
sock.sendall(b"a001 STARTTLS\r\n")
|
|
248
|
+
response = reply()
|
|
207
249
|
for _ in range(_MAX_STARTTLS_REPLY_LINES):
|
|
208
|
-
line = _readline(
|
|
250
|
+
line = _readline(response)
|
|
209
251
|
if not line:
|
|
210
252
|
raise ValueError("connection closed during STARTTLS")
|
|
211
253
|
if line.startswith(b"a001 "):
|
|
@@ -256,7 +298,7 @@ def get_server_cert(
|
|
|
256
298
|
host, port, connect_timeout, read_timeout, resolved_proxy
|
|
257
299
|
) as sock:
|
|
258
300
|
if starttls:
|
|
259
|
-
_negotiate_starttls(sock, starttls)
|
|
301
|
+
_negotiate_starttls(sock, starttls, read_timeout)
|
|
260
302
|
with context.wrap_socket(sock, server_hostname=servername or host) as ssock:
|
|
261
303
|
der = ssock.getpeercert(binary_form=True)
|
|
262
304
|
cipher = ssock.cipher()
|
|
@@ -308,8 +350,9 @@ def verify_chain(
|
|
|
308
350
|
|
|
309
351
|
Open a fully verified TLS handshake (system trust store, hostname check)
|
|
310
352
|
as a browser would. Return ``(trusted, reason, chain)`` where ``chain`` is
|
|
311
|
-
the verified certificate chain (leaf first)
|
|
312
|
-
|
|
353
|
+
the verified certificate chain (leaf first) on success — just the leaf on
|
|
354
|
+
interpreters without ``get_verified_chain`` (before 3.13) — and an empty
|
|
355
|
+
list on failure.
|
|
313
356
|
``reason`` is None on success or the verification message on failure.
|
|
314
357
|
Network-level failures are left to propagate. When ``starttls`` is set the
|
|
315
358
|
plaintext protocol is upgraded to TLS before the handshake.
|
|
@@ -337,9 +380,12 @@ def verify_chain(
|
|
|
337
380
|
host, port, connect_timeout, read_timeout, resolved_proxy
|
|
338
381
|
) as sock:
|
|
339
382
|
if starttls:
|
|
340
|
-
_negotiate_starttls(sock, starttls)
|
|
383
|
+
_negotiate_starttls(sock, starttls, read_timeout)
|
|
341
384
|
with context.wrap_socket(sock, server_hostname=servername or host) as ssock:
|
|
342
|
-
|
|
385
|
+
chain = _chain(ssock, "get_verified_chain") or [
|
|
386
|
+
x509.load_der_x509_certificate(ssock.getpeercert(binary_form=True))
|
|
387
|
+
]
|
|
388
|
+
return True, None, chain
|
|
343
389
|
except ssl.SSLCertVerificationError as err:
|
|
344
390
|
return False, err.verify_message or str(err), []
|
|
345
391
|
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"""SSRF-guarded HTTP GET/POST for certificate- and CT-log-supplied URLs.
|
|
2
|
+
|
|
3
|
+
OCSP, CRL, CA-Issuer and Certificate Transparency URLs all come from untrusted
|
|
4
|
+
input, so this small client screens the target host against internal or
|
|
5
|
+
non-routable addresses and caps the response size. Shared by the revocation
|
|
6
|
+
checks and the CT-log discovery.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import http.client
|
|
10
|
+
import ipaddress
|
|
11
|
+
import socket
|
|
12
|
+
import threading
|
|
13
|
+
import urllib.request
|
|
14
|
+
from urllib.parse import urlsplit
|
|
15
|
+
|
|
16
|
+
# Cap the size of any certificate-supplied HTTP response (OCSP/CRL/CA-Issuer)
|
|
17
|
+
# so a malicious certificate cannot point us at an unbounded download and
|
|
18
|
+
# exhaust memory. Real-world CRLs stay comfortably below this.
|
|
19
|
+
_MAX_HTTP_RESPONSE_BYTES = 16 * 1024 * 1024
|
|
20
|
+
|
|
21
|
+
# CRL/OCSP URLs legitimately redirect once or twice (http->https, a CDN).
|
|
22
|
+
_MAX_REDIRECTS = 5
|
|
23
|
+
|
|
24
|
+
# The socket timeout covers one read at a time; this bounds a whole fetch
|
|
25
|
+
# (redirects included) against a server that sends one byte at a time.
|
|
26
|
+
_FETCH_DEADLINE_SECONDS = 60.0
|
|
27
|
+
|
|
28
|
+
# Sockets opened by the fetch running in this thread, closed at its deadline.
|
|
29
|
+
_active = threading.local()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _is_blocked_fetch_address(
|
|
33
|
+
ip: ipaddress.IPv4Address | ipaddress.IPv6Address,
|
|
34
|
+
) -> bool:
|
|
35
|
+
"""Return True for addresses a certificate-supplied URL must not reach.
|
|
36
|
+
|
|
37
|
+
Loopback, link-local (which covers the cloud metadata endpoint
|
|
38
|
+
``169.254.169.254``), unspecified, multicast and reserved ranges are
|
|
39
|
+
refused. Private RFC1918 ranges are deliberately allowed so revocation
|
|
40
|
+
still works behind an internal PKI.
|
|
41
|
+
"""
|
|
42
|
+
return (
|
|
43
|
+
ip.is_loopback
|
|
44
|
+
or ip.is_link_local
|
|
45
|
+
or ip.is_unspecified
|
|
46
|
+
or ip.is_multicast
|
|
47
|
+
or ip.is_reserved
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _vetted_addresses(host: str, port: int | None) -> list[str]:
|
|
52
|
+
"""Resolve ``host`` and return its IP addresses, all of them allowed.
|
|
53
|
+
|
|
54
|
+
Raises ValueError when the name does not resolve or when any address is
|
|
55
|
+
internal or non-routable (see ``_is_blocked_fetch_address``).
|
|
56
|
+
"""
|
|
57
|
+
try:
|
|
58
|
+
infos = socket.getaddrinfo(host, port, proto=socket.IPPROTO_TCP)
|
|
59
|
+
except OSError as err:
|
|
60
|
+
raise ValueError(f"could not resolve {host}: {err}") from err
|
|
61
|
+
addresses: list[str] = []
|
|
62
|
+
for info in infos:
|
|
63
|
+
ip = ipaddress.ip_address(info[4][0])
|
|
64
|
+
if _is_blocked_fetch_address(ip):
|
|
65
|
+
raise ValueError(
|
|
66
|
+
f"refusing to fetch from {host}: {ip} is a non-routable or "
|
|
67
|
+
"internal address"
|
|
68
|
+
)
|
|
69
|
+
if str(ip) not in addresses:
|
|
70
|
+
addresses.append(str(ip))
|
|
71
|
+
return addresses
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _guard_fetch_host(url: str) -> None:
|
|
75
|
+
"""Refuse to fetch a certificate-supplied URL pointing at an internal host.
|
|
76
|
+
|
|
77
|
+
OCSP, CRL and CA-Issuer URLs come from the inspected certificate, i.e. from
|
|
78
|
+
untrusted input; following them blindly would turn certinspect into an SSRF
|
|
79
|
+
primitive able to reach the cloud metadata service or a port on localhost.
|
|
80
|
+
This is the early check on the URL; the connection itself goes to an
|
|
81
|
+
address vetted again at connect time (see ``_connect_vetted``), so a DNS
|
|
82
|
+
answer that changes in between cannot redirect it. Raises ValueError when
|
|
83
|
+
the target is not allowed, which the callers already treat as a soft-fail.
|
|
84
|
+
"""
|
|
85
|
+
parts = urlsplit(url)
|
|
86
|
+
if not parts.hostname:
|
|
87
|
+
raise ValueError(f"URL has no host: {url}")
|
|
88
|
+
_vetted_addresses(parts.hostname, parts.port)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _check_url(url: str) -> None:
|
|
92
|
+
"""Raise ValueError unless ``url`` is an http(s) URL on an allowed host."""
|
|
93
|
+
if not url.lower().startswith(("http://", "https://")):
|
|
94
|
+
raise ValueError(f"unsupported URL scheme: {url}")
|
|
95
|
+
_guard_fetch_host(url)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class _GuardedRedirectHandler(urllib.request.HTTPRedirectHandler):
|
|
99
|
+
"""Follow redirects only to URLs that pass the same checks as the first.
|
|
100
|
+
|
|
101
|
+
Without this, a public host named in a certificate could answer with a
|
|
102
|
+
redirect to loopback or the cloud metadata endpoint and urllib would follow
|
|
103
|
+
it unchecked.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
max_redirections = _MAX_REDIRECTS
|
|
107
|
+
|
|
108
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
109
|
+
_check_url(newurl)
|
|
110
|
+
return super().redirect_request(req, fp, code, msg, headers, newurl)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _track(sock: socket.socket) -> socket.socket:
|
|
114
|
+
"""Register ``sock`` with the fetch running in this thread, if any."""
|
|
115
|
+
sockets = getattr(_active, "sockets", None)
|
|
116
|
+
if sockets is not None:
|
|
117
|
+
sockets.append(sock)
|
|
118
|
+
return sock
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _connect_direct(address, timeout, source_address=None) -> socket.socket:
|
|
122
|
+
"""Connect like http.client does, to a proxy chosen by the environment."""
|
|
123
|
+
return _track(socket.create_connection(address, timeout, source_address))
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _connect_vetted(address, timeout, source_address=None) -> socket.socket:
|
|
127
|
+
"""Connect to a vetted IP of ``address``'s host, never re-resolving it.
|
|
128
|
+
|
|
129
|
+
Resolving once and connecting to that exact address closes the DNS
|
|
130
|
+
rebinding window: an attacker's name server cannot answer the check with a
|
|
131
|
+
public IP and the connection with loopback. TLS still verifies the
|
|
132
|
+
certificate against the original host name.
|
|
133
|
+
"""
|
|
134
|
+
host, port = address
|
|
135
|
+
error: OSError | None = None
|
|
136
|
+
for ip in _vetted_addresses(host, port):
|
|
137
|
+
try:
|
|
138
|
+
return _track(socket.create_connection((ip, port), timeout, source_address))
|
|
139
|
+
except OSError as err:
|
|
140
|
+
error = err
|
|
141
|
+
raise error or OSError(f"could not connect to {host}")
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _connection_class(base: type, connect) -> type:
|
|
145
|
+
"""Return ``base`` with its socket opened by ``connect``."""
|
|
146
|
+
|
|
147
|
+
class _Connection(base):
|
|
148
|
+
def __init__(self, *args, **kwargs):
|
|
149
|
+
super().__init__(*args, **kwargs)
|
|
150
|
+
# http.client's connection hook; HTTPSConnection wraps this socket.
|
|
151
|
+
self._create_connection = connect
|
|
152
|
+
|
|
153
|
+
return _Connection
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
_PINNED_HTTP = _connection_class(http.client.HTTPConnection, _connect_vetted)
|
|
157
|
+
_PINNED_HTTPS = _connection_class(http.client.HTTPSConnection, _connect_vetted)
|
|
158
|
+
_PROXIED_HTTP = _connection_class(http.client.HTTPConnection, _connect_direct)
|
|
159
|
+
_PROXIED_HTTPS = _connection_class(http.client.HTTPSConnection, _connect_direct)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _via_proxy(req: urllib.request.Request) -> bool:
|
|
163
|
+
"""True when urllib routes ``req`` through a proxy from the environment.
|
|
164
|
+
|
|
165
|
+
The proxy then resolves the target itself, so pinning cannot apply; only
|
|
166
|
+
the URL check guards those requests.
|
|
167
|
+
"""
|
|
168
|
+
return req.has_proxy() or bool(getattr(req, "_tunnel_host", None))
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class _PinnedHTTPHandler(urllib.request.HTTPHandler):
|
|
172
|
+
def http_open(self, req):
|
|
173
|
+
return self.do_open(_PROXIED_HTTP if _via_proxy(req) else _PINNED_HTTP, req)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class _PinnedHTTPSHandler(urllib.request.HTTPSHandler):
|
|
177
|
+
def https_open(self, req):
|
|
178
|
+
return self.do_open(
|
|
179
|
+
_PROXIED_HTTPS if _via_proxy(req) else _PINNED_HTTPS,
|
|
180
|
+
req,
|
|
181
|
+
context=self._context,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
_OPENER = urllib.request.build_opener(
|
|
186
|
+
_GuardedRedirectHandler, _PinnedHTTPHandler, _PinnedHTTPSHandler
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def fetch(url: str, *, data: bytes | None = None, timeout: float) -> bytes:
|
|
191
|
+
"""Perform a minimal HTTP(S) GET/POST and return the response body.
|
|
192
|
+
|
|
193
|
+
Only ``http`` and ``https`` URLs are accepted; the URLs come from the
|
|
194
|
+
certificate's own AIA/CRL extensions, i.e. from untrusted input, so the
|
|
195
|
+
target host is screened against internal/non-routable addresses — on the
|
|
196
|
+
first request and on every redirect — and the response size is capped. A
|
|
197
|
+
POST is used when ``data`` is given. The whole fetch must finish within
|
|
198
|
+
``_FETCH_DEADLINE_SECONDS`` (TimeoutError otherwise); a malformed HTTP
|
|
199
|
+
response raises ValueError.
|
|
200
|
+
"""
|
|
201
|
+
_check_url(url)
|
|
202
|
+
headers = {"Content-Type": "application/ocsp-request"} if data else {}
|
|
203
|
+
request = urllib.request.Request(url, data=data, headers=headers)
|
|
204
|
+
sockets: list[socket.socket] = []
|
|
205
|
+
expired = threading.Event()
|
|
206
|
+
|
|
207
|
+
def expire() -> None:
|
|
208
|
+
expired.set()
|
|
209
|
+
for sock in list(sockets):
|
|
210
|
+
try:
|
|
211
|
+
sock.shutdown(socket.SHUT_RDWR)
|
|
212
|
+
except OSError:
|
|
213
|
+
pass
|
|
214
|
+
|
|
215
|
+
timer = threading.Timer(_FETCH_DEADLINE_SECONDS, expire)
|
|
216
|
+
timer.daemon = True
|
|
217
|
+
_active.sockets = sockets
|
|
218
|
+
timer.start()
|
|
219
|
+
try:
|
|
220
|
+
with _OPENER.open(request, timeout=timeout) as response:
|
|
221
|
+
body = response.read(_MAX_HTTP_RESPONSE_BYTES + 1)
|
|
222
|
+
except (OSError, http.client.HTTPException) as err:
|
|
223
|
+
if expired.is_set():
|
|
224
|
+
raise _deadline_error(url) from err
|
|
225
|
+
if isinstance(err, http.client.HTTPException):
|
|
226
|
+
raise ValueError(f"malformed HTTP response from {url}: {err!r}") from err
|
|
227
|
+
raise
|
|
228
|
+
finally:
|
|
229
|
+
timer.cancel()
|
|
230
|
+
_active.sockets = None
|
|
231
|
+
if expired.is_set():
|
|
232
|
+
raise _deadline_error(url)
|
|
233
|
+
if len(body) > _MAX_HTTP_RESPONSE_BYTES:
|
|
234
|
+
raise ValueError(
|
|
235
|
+
f"response from {url} exceeds the {_MAX_HTTP_RESPONSE_BYTES}-byte limit"
|
|
236
|
+
)
|
|
237
|
+
return body
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _deadline_error(url: str) -> TimeoutError:
|
|
241
|
+
return TimeoutError(f"fetching {url} took longer than {_FETCH_DEADLINE_SECONDS:g}s")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: certinspect
|
|
3
|
-
Version: 2.4.
|
|
3
|
+
Version: 2.4.4
|
|
4
4
|
Summary: Command-line TLS certificate inspector
|
|
5
5
|
Author-email: Michele Angrisano <michele.angrisano@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -339,7 +339,10 @@ SAN:
|
|
|
339
339
|
|
|
340
340
|
By default certinspect opens a fully verified TLS handshake (chain +
|
|
341
341
|
hostname against the Python/OpenSSL trust store) and, when the certificate
|
|
342
|
-
advertises an OCSP responder, queries it for the revocation status.
|
|
342
|
+
advertises an OCSP responder, queries it for the revocation status. The
|
|
343
|
+
verified handshake is a second connection: if the server presents a different
|
|
344
|
+
certificate on it (a load balancer mid-rotation, say), the chain is reported
|
|
345
|
+
as not trusted instead of vouching for a certificate that was not inspected. If OCSP is
|
|
343
346
|
unavailable, certinspect falls back to the certificate's CRL distribution
|
|
344
347
|
points (downloaded over HTTP and verified against the issuer). Both checks are
|
|
345
348
|
soft-fail: when neither OCSP nor the CRL gives an answer the status is
|
|
@@ -349,7 +352,9 @@ is treated as `UNAVAILABLE` too, so a replayed answer cannot mask a later
|
|
|
349
352
|
revocation. The OCSP/CRL/CA-Issuer URLs come from the certificate, so
|
|
350
353
|
certinspect refuses to follow one that resolves to a loopback, link-local
|
|
351
354
|
(cloud metadata) or otherwise non-routable address, and caps the download
|
|
352
|
-
size — private/internal PKI addresses stay reachable.
|
|
355
|
+
size — private/internal PKI addresses stay reachable. It connects to the very
|
|
356
|
+
address it checked, so a DNS answer that changes in between (DNS rebinding)
|
|
357
|
+
cannot send the request elsewhere; through a proxy, the proxy resolves the name.
|
|
353
358
|
|
|
354
359
|
When the responder is down (or the certificate has no OCSP URL at all) the CRL
|
|
355
360
|
fallback can still catch a revoked certificate; the `via CRL` note shows which
|
|
@@ -756,7 +761,10 @@ The report is identical to a plain inspection; only how/where certinspect
|
|
|
756
761
|
connects changes. Split the single `--timeout` into `--connect-timeout` and
|
|
757
762
|
`--read-timeout` to fail fast on dead hosts while still allowing a slow
|
|
758
763
|
handshake (requests-style), and add `--retries` to ride out transient network
|
|
759
|
-
blips instead of reporting a false failure.
|
|
764
|
+
blips instead of reporting a false failure. The read timeout bounds each whole
|
|
765
|
+
server reply (TLS handshake, each STARTTLS reply, the proxy `CONNECT` answer),
|
|
766
|
+
not a single read, so a server sending one byte at a time cannot stall a run;
|
|
767
|
+
OCSP/CRL/CA-Issuer downloads have an overall 60-second limit.
|
|
760
768
|
|
|
761
769
|
```console
|
|
762
770
|
$ certinspect example.com --connect-timeout 3 --read-timeout 10 --retries 2
|
|
@@ -1723,6 +1723,41 @@ def test_main_verify_revocation_unavailable_is_soft_fail(
|
|
|
1723
1723
|
assert code == 0
|
|
1724
1724
|
|
|
1725
1725
|
|
|
1726
|
+
def _verified_leaf(der):
|
|
1727
|
+
"""Return a verify_chain replacement trusting a chain whose leaf is ``der``."""
|
|
1728
|
+
from cryptography import x509
|
|
1729
|
+
|
|
1730
|
+
leaf = x509.load_der_x509_certificate(der)
|
|
1731
|
+
return lambda *a, **k: (True, None, [leaf])
|
|
1732
|
+
|
|
1733
|
+
|
|
1734
|
+
def test_main_verify_rejects_a_different_leaf_on_the_verification_handshake(
|
|
1735
|
+
monkeypatch, capsys, make_cert
|
|
1736
|
+
):
|
|
1737
|
+
"""A load balancer serving another certificate to the verification
|
|
1738
|
+
handshake must not make the inspected certificate look trusted."""
|
|
1739
|
+
inspected = make_cert(san=["example.com"])
|
|
1740
|
+
monkeypatch.setattr("certinspect.cli.get_server_cert", _const_fetch(inspected))
|
|
1741
|
+
monkeypatch.setattr(
|
|
1742
|
+
"certinspect.cli.verify_chain", _verified_leaf(make_cert(san=["example.com"]))
|
|
1743
|
+
)
|
|
1744
|
+
code = _run_main(monkeypatch, ["example.com", "--json"])
|
|
1745
|
+
result = json.loads(capsys.readouterr().out)["results"][0]
|
|
1746
|
+
assert code == 6
|
|
1747
|
+
assert result["chain"]["trusted"] is False
|
|
1748
|
+
assert "different certificate" in result["chain"]["error"]
|
|
1749
|
+
|
|
1750
|
+
|
|
1751
|
+
def test_main_verify_trusts_the_same_leaf(monkeypatch, capsys, make_cert):
|
|
1752
|
+
inspected = make_cert(san=["example.com"])
|
|
1753
|
+
monkeypatch.setattr("certinspect.cli.get_server_cert", _const_fetch(inspected))
|
|
1754
|
+
monkeypatch.setattr("certinspect.cli.verify_chain", _verified_leaf(inspected))
|
|
1755
|
+
code = _run_main(monkeypatch, ["example.com", "--json"])
|
|
1756
|
+
result = json.loads(capsys.readouterr().out)["results"][0]
|
|
1757
|
+
assert code == 0
|
|
1758
|
+
assert result["chain"]["trusted"] is True
|
|
1759
|
+
|
|
1760
|
+
|
|
1726
1761
|
def test_main_file_verify_reports_untrusted_self_signed(
|
|
1727
1762
|
monkeypatch, capsys, tmp_path, make_cert
|
|
1728
1763
|
):
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Tests for the network helpers that do not require a live server."""
|
|
2
2
|
|
|
3
3
|
import threading
|
|
4
|
+
import time
|
|
4
5
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
5
6
|
|
|
6
7
|
import pytest
|
|
@@ -301,6 +302,42 @@ def test_negotiate_starttls_rejects_endless_replies(protocol, prefix, pattern):
|
|
|
301
302
|
_negotiate_starttls(_EndlessSocket(pattern, prefix=prefix), protocol)
|
|
302
303
|
|
|
303
304
|
|
|
305
|
+
class _DrippingSocket(_EndlessSocket):
|
|
306
|
+
"""A hostile server sending one byte per ``delay``, below any read timeout."""
|
|
307
|
+
|
|
308
|
+
def __init__(self, pattern: bytes, prefix: bytes = b"", delay: float = 0.02):
|
|
309
|
+
super().__init__(pattern, prefix=prefix)
|
|
310
|
+
self._delay = delay
|
|
311
|
+
self._give_up = time.monotonic() + 5.0
|
|
312
|
+
|
|
313
|
+
def recv(self, n: int) -> bytes:
|
|
314
|
+
if time.monotonic() > self._give_up:
|
|
315
|
+
raise AssertionError("client kept reading a dripping stream")
|
|
316
|
+
time.sleep(self._delay)
|
|
317
|
+
return super().recv(n)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def test_negotiate_starttls_bounds_a_dripped_reply():
|
|
321
|
+
from certinspect.fetch import _negotiate_starttls
|
|
322
|
+
|
|
323
|
+
start = time.monotonic()
|
|
324
|
+
with pytest.raises(TimeoutError, match="took longer than 0.2s"):
|
|
325
|
+
_negotiate_starttls(_DrippingSocket(b"x", prefix=b"220 "), "smtp", 0.2)
|
|
326
|
+
assert time.monotonic() - start < 1.0
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def test_open_socket_bounds_a_dripped_proxy_reply(monkeypatch):
|
|
330
|
+
from certinspect import fetch
|
|
331
|
+
|
|
332
|
+
dripping = _DrippingSocket(b"X-Padding: x\r\n", prefix=b"HTTP/1.1 200 OK\r\n")
|
|
333
|
+
monkeypatch.setattr(
|
|
334
|
+
fetch.socket, "create_connection", lambda addr, timeout=None: dripping
|
|
335
|
+
)
|
|
336
|
+
with pytest.raises(TimeoutError):
|
|
337
|
+
fetch._open_socket("example.com", 443, 0.2, 5.0, "http://proxy:8080")
|
|
338
|
+
assert dripping.closed is True
|
|
339
|
+
|
|
340
|
+
|
|
304
341
|
def test_verify_chain_uses_custom_ca(monkeypatch):
|
|
305
342
|
"""--cafile/--capath must build the SSL context from the given bundle."""
|
|
306
343
|
import ssl
|
|
@@ -827,6 +864,7 @@ def _guard_allowing(monkeypatch, *allowed_bases):
|
|
|
827
864
|
from certinspect import httpfetch
|
|
828
865
|
|
|
829
866
|
real_guard = httpfetch._guard_fetch_host
|
|
867
|
+
real_vetted = httpfetch._vetted_addresses
|
|
830
868
|
checked = []
|
|
831
869
|
|
|
832
870
|
def guard(url):
|
|
@@ -834,7 +872,13 @@ def _guard_allowing(monkeypatch, *allowed_bases):
|
|
|
834
872
|
if not url.startswith(allowed_bases):
|
|
835
873
|
real_guard(url)
|
|
836
874
|
|
|
875
|
+
def vetted(host, port):
|
|
876
|
+
if f"http://{host}:{port}" in allowed_bases:
|
|
877
|
+
return [host]
|
|
878
|
+
return real_vetted(host, port)
|
|
879
|
+
|
|
837
880
|
monkeypatch.setattr(httpfetch, "_guard_fetch_host", guard)
|
|
881
|
+
monkeypatch.setattr(httpfetch, "_vetted_addresses", vetted)
|
|
838
882
|
return checked
|
|
839
883
|
|
|
840
884
|
|
|
@@ -891,6 +935,69 @@ def test_http_caps_redirect_chain(monkeypatch, serve):
|
|
|
891
935
|
assert len(hits) == httpfetch._MAX_REDIRECTS + 1
|
|
892
936
|
|
|
893
937
|
|
|
938
|
+
def test_http_connects_to_the_vetted_address_despite_dns_rebinding(monkeypatch, serve):
|
|
939
|
+
"""A name that resolves public for the check and loopback afterwards
|
|
940
|
+
(DNS rebinding) must not reach the internal server."""
|
|
941
|
+
import socket as _socket
|
|
942
|
+
|
|
943
|
+
from certinspect import httpfetch
|
|
944
|
+
|
|
945
|
+
hits = []
|
|
946
|
+
internal = serve(_body(b"SECRET", hits))
|
|
947
|
+
port = int(internal.rsplit(":", 1)[1])
|
|
948
|
+
real_getaddrinfo = _socket.getaddrinfo
|
|
949
|
+
answers = iter(["93.184.215.14", "127.0.0.1"])
|
|
950
|
+
|
|
951
|
+
def rebinding_dns(host, *args, **kwargs):
|
|
952
|
+
if host != "rebind.example":
|
|
953
|
+
return real_getaddrinfo(host, *args, **kwargs)
|
|
954
|
+
return _addrinfo(next(answers, "127.0.0.1"))
|
|
955
|
+
|
|
956
|
+
monkeypatch.setattr(httpfetch.socket, "getaddrinfo", rebinding_dns)
|
|
957
|
+
with pytest.raises(ValueError, match="non-routable or internal"):
|
|
958
|
+
httpfetch.fetch(f"http://rebind.example:{port}/", timeout=3.0)
|
|
959
|
+
assert hits == []
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
def test_http_fetch_has_a_total_deadline(monkeypatch, serve):
|
|
963
|
+
"""A server dripping its body below the per-read timeout is cut off."""
|
|
964
|
+
from certinspect import httpfetch
|
|
965
|
+
|
|
966
|
+
def respond(handler):
|
|
967
|
+
handler.send_response(200)
|
|
968
|
+
handler.send_header("Content-Length", "1000")
|
|
969
|
+
handler.end_headers()
|
|
970
|
+
try:
|
|
971
|
+
for _ in range(200):
|
|
972
|
+
handler.wfile.write(b"x")
|
|
973
|
+
handler.wfile.flush()
|
|
974
|
+
time.sleep(0.02)
|
|
975
|
+
except OSError:
|
|
976
|
+
pass
|
|
977
|
+
|
|
978
|
+
public = serve(respond)
|
|
979
|
+
_guard_allowing(monkeypatch, public)
|
|
980
|
+
monkeypatch.setattr(httpfetch, "_FETCH_DEADLINE_SECONDS", 0.3)
|
|
981
|
+
|
|
982
|
+
start = time.monotonic()
|
|
983
|
+
with pytest.raises(TimeoutError, match="took longer than 0.3s"):
|
|
984
|
+
httpfetch.fetch(f"{public}/crl", timeout=3.0)
|
|
985
|
+
assert time.monotonic() - start < 2.0
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
def test_http_malformed_response_is_a_value_error(monkeypatch, serve):
|
|
989
|
+
"""A broken status line is a soft failure, not an unexpected exception."""
|
|
990
|
+
from certinspect import httpfetch
|
|
991
|
+
|
|
992
|
+
def respond(handler):
|
|
993
|
+
handler.wfile.write(b"NOT-HTTP garbage")
|
|
994
|
+
|
|
995
|
+
public = serve(respond)
|
|
996
|
+
_guard_allowing(monkeypatch, public)
|
|
997
|
+
with pytest.raises(ValueError, match="malformed HTTP response"):
|
|
998
|
+
httpfetch.fetch(f"{public}/crl", timeout=3.0)
|
|
999
|
+
|
|
1000
|
+
|
|
894
1001
|
# --- OCSP response freshness ------------------------------------------------
|
|
895
1002
|
|
|
896
1003
|
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
"""SSRF-guarded HTTP GET/POST for certificate- and CT-log-supplied URLs.
|
|
2
|
-
|
|
3
|
-
OCSP, CRL, CA-Issuer and Certificate Transparency URLs all come from untrusted
|
|
4
|
-
input, so this small client screens the target host against internal or
|
|
5
|
-
non-routable addresses and caps the response size. Shared by the revocation
|
|
6
|
-
checks and the CT-log discovery.
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
import ipaddress
|
|
10
|
-
import socket
|
|
11
|
-
import urllib.request
|
|
12
|
-
from urllib.parse import urlsplit
|
|
13
|
-
|
|
14
|
-
# Cap the size of any certificate-supplied HTTP response (OCSP/CRL/CA-Issuer)
|
|
15
|
-
# so a malicious certificate cannot point us at an unbounded download and
|
|
16
|
-
# exhaust memory. Real-world CRLs stay comfortably below this.
|
|
17
|
-
_MAX_HTTP_RESPONSE_BYTES = 16 * 1024 * 1024
|
|
18
|
-
|
|
19
|
-
# CRL/OCSP URLs legitimately redirect once or twice (http->https, a CDN).
|
|
20
|
-
_MAX_REDIRECTS = 5
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def _is_blocked_fetch_address(
|
|
24
|
-
ip: ipaddress.IPv4Address | ipaddress.IPv6Address,
|
|
25
|
-
) -> bool:
|
|
26
|
-
"""Return True for addresses a certificate-supplied URL must not reach.
|
|
27
|
-
|
|
28
|
-
Loopback, link-local (which covers the cloud metadata endpoint
|
|
29
|
-
``169.254.169.254``), unspecified, multicast and reserved ranges are
|
|
30
|
-
refused. Private RFC1918 ranges are deliberately allowed so revocation
|
|
31
|
-
still works behind an internal PKI.
|
|
32
|
-
"""
|
|
33
|
-
return (
|
|
34
|
-
ip.is_loopback
|
|
35
|
-
or ip.is_link_local
|
|
36
|
-
or ip.is_unspecified
|
|
37
|
-
or ip.is_multicast
|
|
38
|
-
or ip.is_reserved
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def _guard_fetch_host(url: str) -> None:
|
|
43
|
-
"""Refuse to fetch a certificate-supplied URL pointing at an internal host.
|
|
44
|
-
|
|
45
|
-
OCSP, CRL and CA-Issuer URLs come from the inspected certificate, i.e. from
|
|
46
|
-
untrusted input; following them blindly would turn certinspect into an SSRF
|
|
47
|
-
primitive able to reach the cloud metadata service or a port on localhost.
|
|
48
|
-
The host is resolved and every returned address checked. The guard is
|
|
49
|
-
best-effort (the HTTP client resolves DNS again, so a rebinding attacker
|
|
50
|
-
could still race it) but closes the obvious vectors. Raises ValueError when
|
|
51
|
-
the target is not allowed, which the callers already treat as a soft-fail.
|
|
52
|
-
"""
|
|
53
|
-
host = urlsplit(url).hostname
|
|
54
|
-
if not host:
|
|
55
|
-
raise ValueError(f"URL has no host: {url}")
|
|
56
|
-
try:
|
|
57
|
-
infos = socket.getaddrinfo(host, None, proto=socket.IPPROTO_TCP)
|
|
58
|
-
except OSError as err:
|
|
59
|
-
raise ValueError(f"could not resolve {host}: {err}") from err
|
|
60
|
-
for info in infos:
|
|
61
|
-
ip = ipaddress.ip_address(info[4][0])
|
|
62
|
-
if _is_blocked_fetch_address(ip):
|
|
63
|
-
raise ValueError(
|
|
64
|
-
f"refusing to fetch {url}: {ip} is a non-routable or internal address"
|
|
65
|
-
)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def _check_url(url: str) -> None:
|
|
69
|
-
"""Raise ValueError unless ``url`` is an http(s) URL on an allowed host."""
|
|
70
|
-
if not url.lower().startswith(("http://", "https://")):
|
|
71
|
-
raise ValueError(f"unsupported URL scheme: {url}")
|
|
72
|
-
_guard_fetch_host(url)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
class _GuardedRedirectHandler(urllib.request.HTTPRedirectHandler):
|
|
76
|
-
"""Follow redirects only to URLs that pass the same checks as the first.
|
|
77
|
-
|
|
78
|
-
Without this, a public host named in a certificate could answer with a
|
|
79
|
-
redirect to loopback or the cloud metadata endpoint and urllib would follow
|
|
80
|
-
it unchecked.
|
|
81
|
-
"""
|
|
82
|
-
|
|
83
|
-
max_redirections = _MAX_REDIRECTS
|
|
84
|
-
|
|
85
|
-
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
86
|
-
_check_url(newurl)
|
|
87
|
-
return super().redirect_request(req, fp, code, msg, headers, newurl)
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
_OPENER = urllib.request.build_opener(_GuardedRedirectHandler)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
def fetch(url: str, *, data: bytes | None = None, timeout: float) -> bytes:
|
|
94
|
-
"""Perform a minimal HTTP(S) GET/POST and return the response body.
|
|
95
|
-
|
|
96
|
-
Only ``http`` and ``https`` URLs are accepted; the URLs come from the
|
|
97
|
-
certificate's own AIA/CRL extensions, i.e. from untrusted input, so the
|
|
98
|
-
target host is screened against internal/non-routable addresses — on the
|
|
99
|
-
first request and on every redirect — and the response size is capped. A
|
|
100
|
-
POST is used when ``data`` is given.
|
|
101
|
-
"""
|
|
102
|
-
_check_url(url)
|
|
103
|
-
headers = {"Content-Type": "application/ocsp-request"} if data else {}
|
|
104
|
-
request = urllib.request.Request(url, data=data, headers=headers)
|
|
105
|
-
with _OPENER.open(request, timeout=timeout) as response:
|
|
106
|
-
body = response.read(_MAX_HTTP_RESPONSE_BYTES + 1)
|
|
107
|
-
if len(body) > _MAX_HTTP_RESPONSE_BYTES:
|
|
108
|
-
raise ValueError(
|
|
109
|
-
f"response from {url} exceeds the {_MAX_HTTP_RESPONSE_BYTES}-byte limit"
|
|
110
|
-
)
|
|
111
|
-
return body
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|