basic-ipfs 1.0.0__tar.gz → 1.0.1__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.
Files changed (23) hide show
  1. {basic_ipfs-1.0.0/basic_ipfs.egg-info → basic_ipfs-1.0.1}/PKG-INFO +4 -3
  2. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/README.md +3 -2
  3. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs/__init__.py +52 -10
  4. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs/cli.py +1 -1
  5. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs/kubo_checksums.py +2 -2
  6. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1/basic_ipfs.egg-info}/PKG-INFO +4 -3
  7. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/pyproject.toml +1 -1
  8. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/tests/test_basic.py +13 -1
  9. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/tests/test_download.py +121 -2
  10. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/tests/test_two_node.py +17 -4
  11. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/LICENSE +0 -0
  12. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/MANIFEST.in +0 -0
  13. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/SECURITY.md +0 -0
  14. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs/py.typed +0 -0
  15. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs.egg-info/SOURCES.txt +0 -0
  16. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs.egg-info/dependency_links.txt +0 -0
  17. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs.egg-info/entry_points.txt +0 -0
  18. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs.egg-info/requires.txt +0 -0
  19. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/basic_ipfs.egg-info/top_level.txt +0 -0
  20. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/setup.cfg +0 -0
  21. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/tests/test_cli.py +0 -0
  22. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/tests/test_concurrency.py +0 -0
  23. {basic_ipfs-1.0.0 → basic_ipfs-1.0.1}/tests/test_unit.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: basic-ipfs
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: An intentionally tiny IPFS library for Python. Auto-manages a real Kubo node.
5
5
  Author: Luke Profits
6
6
  License: MIT License
@@ -262,8 +262,9 @@ Auto-download is verified against an SHA-512 hash baked into the wheel
262
262
  (`basic_ipfs/kubo_checksums.py`) using a constant-time compare. Versions
263
263
  not in the table are refused — fetching a digest from the same origin as
264
264
  the archive adds no real protection, so the install is fail-closed. The
265
- download also pins its redirect target to `*.ipfs.tech` at every hop in
266
- the chain (not just the final URL).
265
+ download also pins its redirect target to `dist.ipfs.tech` (and its
266
+ subdomains) at every hop in the chain — not just the final URL — and
267
+ requires HTTPS on every hop.
267
268
 
268
269
  Two trust assumptions worth knowing before deploying on a shared host:
269
270
 
@@ -218,8 +218,9 @@ Auto-download is verified against an SHA-512 hash baked into the wheel
218
218
  (`basic_ipfs/kubo_checksums.py`) using a constant-time compare. Versions
219
219
  not in the table are refused — fetching a digest from the same origin as
220
220
  the archive adds no real protection, so the install is fail-closed. The
221
- download also pins its redirect target to `*.ipfs.tech` at every hop in
222
- the chain (not just the final URL).
221
+ download also pins its redirect target to `dist.ipfs.tech` (and its
222
+ subdomains) at every hop in the chain — not just the final URL — and
223
+ requires HTTPS on every hop.
223
224
 
224
225
  Two trust assumptions worth knowing before deploying on a shared host:
225
226
 
@@ -45,7 +45,7 @@ import zipfile
45
45
  from collections.abc import Iterable
46
46
  from pathlib import Path
47
47
  from typing import Any, TypedDict, Union, overload
48
- from urllib.parse import urlparse
48
+ from urllib.parse import urljoin, urlparse
49
49
 
50
50
  import requests
51
51
  from platformdirs import user_data_dir
@@ -345,8 +345,28 @@ def _download_session() -> requests.Session:
345
345
  return s
346
346
 
347
347
 
348
+ def _is_loopback_host(host: str) -> bool:
349
+ if host == "localhost":
350
+ return True
351
+ try:
352
+ return ipaddress.ip_address(host).is_loopback
353
+ except ValueError:
354
+ return False
355
+
356
+
348
357
  def _check_redirect_origin(final_url: str) -> None:
349
- host = (urlparse(final_url).hostname or "").lower()
358
+ parsed = urlparse(final_url)
359
+ host = (parsed.hostname or "").lower()
360
+ # Require HTTPS on every hop: a redirect to http:// would downgrade to
361
+ # plaintext — the SHA-512 check is still fail-closed against bad bytes,
362
+ # but the request headers would leak on the wire. Loopback is exempt:
363
+ # it never crosses a wire a MitM can sit on, and it keeps local mirrors
364
+ # (and the test suite) usable without a test-only knob.
365
+ if parsed.scheme != "https" and not _is_loopback_host(host):
366
+ raise IPFSBinaryNotFound(
367
+ f"Refusing non-HTTPS download URL {final_url!r}. "
368
+ f"Only https:// (or loopback) is allowed."
369
+ )
350
370
  for allowed in _ALLOWED_DOWNLOAD_HOSTS:
351
371
  if host == allowed or host.endswith("." + allowed):
352
372
  return
@@ -369,11 +389,12 @@ class _PinnedRedirectAdapter(requests.adapters.HTTPAdapter):
369
389
  """
370
390
 
371
391
  def send(self, request, **kwargs): # type: ignore[override]
372
- # Disable urllib3-level redirect following so each hop bubbles up
373
- # here for inspection. ``requests.Session.resolve_redirects`` will
374
- # then iterate over them, and we override its behaviour by
375
- # rebuilding the chain ourselves.
376
- kwargs["allow_redirects"] = False
392
+ # No redirect-disabling flag is needed here: HTTPAdapter.send never
393
+ # follows redirects itself (requests calls urllib3's urlopen with
394
+ # redirect=False; following lives in Session.resolve_redirects), so
395
+ # each hop naturally returns to this override. Because we consume
396
+ # the whole chain and return the final response, the session-level
397
+ # resolver then sees a non-redirect and never re-follows anything.
377
398
  resp = super().send(request, **kwargs)
378
399
  # Walk redirects up to a sane cap (matches requests' default of 30).
379
400
  seen = 0
@@ -383,18 +404,32 @@ class _PinnedRedirectAdapter(requests.adapters.HTTPAdapter):
383
404
  if not next_url:
384
405
  break
385
406
  # Resolve relative redirects against the previous URL.
386
- from urllib.parse import urljoin
387
407
  next_url = urljoin(resp.url, next_url)
388
- _check_redirect_origin(next_url)
389
- # Drop the body and follow.
408
+ # Drop the body before the origin check so a rejected hop does
409
+ # not leak the open connection.
390
410
  try:
391
411
  resp.close()
392
412
  except Exception:
393
413
  pass
414
+ _check_redirect_origin(next_url)
394
415
  new_req = request.copy()
395
416
  new_req.url = next_url
396
417
  # 303 / 302 / 301 on POST → GET, but we only do GET here.
397
418
  resp = super().send(new_req, **kwargs)
419
+ if resp.is_redirect:
420
+ # Chain exhausted the hop cap, or a redirect arrived without a
421
+ # Location header. Returning it would sail past
422
+ # raise_for_status() (30x < 400) and surface later as a
423
+ # confusing SHA mismatch — fail here with the real reason.
424
+ try:
425
+ resp.close()
426
+ except Exception:
427
+ pass
428
+ raise IPFSBinaryNotFound(
429
+ f"Refusing download: redirect chain from {request.url} did not "
430
+ f"resolve after {seen} hops (or a redirect lacked a Location "
431
+ f"header)."
432
+ )
398
433
  return resp
399
434
 
400
435
 
@@ -1182,6 +1217,13 @@ class IPFSManager:
1182
1217
  self._process.wait(timeout=5)
1183
1218
  except subprocess.TimeoutExpired:
1184
1219
  self._process.kill()
1220
+ # Reap the SIGKILLed child — without this the returncode
1221
+ # stays unset, the process lingers as a zombie until GC,
1222
+ # and Popen.__del__ emits a ResourceWarning.
1223
+ try:
1224
+ self._process.wait(timeout=10)
1225
+ except subprocess.TimeoutExpired:
1226
+ pass
1185
1227
 
1186
1228
  self._close_session_and_log()
1187
1229
  self._owns_daemon = False
@@ -292,7 +292,7 @@ def main(argv: list[str] | None = None) -> int:
292
292
  except IPFSError as exc:
293
293
  print(f"error: {exc}", file=sys.stderr)
294
294
  return 1
295
- except (FileNotFoundError, NotADirectoryError, IsADirectoryError, TypeError) as exc:
295
+ except (FileNotFoundError, NotADirectoryError, IsADirectoryError, TypeError, ValueError) as exc:
296
296
  print(f"error: {exc}", file=sys.stderr)
297
297
  return 2
298
298
 
@@ -4,8 +4,8 @@ Built-in SHA-512 checksums for Kubo binaries.
4
4
  Why baked in: if dist.ipfs.tech is compromised, an attacker could swap both
5
5
  the archive and its `.sha512` companion file. Verifying against a hash that
6
6
  shipped inside the wheel — installed via signed PyPI metadata — closes that
7
- gap. For unknown versions, we fall back to fetching the .sha512 alongside
8
- the archive (with an INFO-level log noting the weaker check).
7
+ gap. Versions not in this table are refused outright (fail-closed): fetching
8
+ a digest from the same origin as the archive would add no protection.
9
9
 
10
10
  To populate a new version: ``python scripts/refresh_checksums.py <version>``
11
11
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: basic-ipfs
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: An intentionally tiny IPFS library for Python. Auto-manages a real Kubo node.
5
5
  Author: Luke Profits
6
6
  License: MIT License
@@ -262,8 +262,9 @@ Auto-download is verified against an SHA-512 hash baked into the wheel
262
262
  (`basic_ipfs/kubo_checksums.py`) using a constant-time compare. Versions
263
263
  not in the table are refused — fetching a digest from the same origin as
264
264
  the archive adds no real protection, so the install is fail-closed. The
265
- download also pins its redirect target to `*.ipfs.tech` at every hop in
266
- the chain (not just the final URL).
265
+ download also pins its redirect target to `dist.ipfs.tech` (and its
266
+ subdomains) at every hop in the chain — not just the final URL — and
267
+ requires HTTPS on every hop.
267
268
 
268
269
  Two trust assumptions worth knowing before deploying on a shared host:
269
270
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "basic-ipfs"
7
- version = "1.0.0"
7
+ version = "1.0.1"
8
8
  description = "An intentionally tiny IPFS library for Python. Auto-manages a real Kubo node."
9
9
  readme = "README.md"
10
10
  license = { file = "LICENSE" }
@@ -289,7 +289,19 @@ def test_peers_returns_list():
289
289
 
290
290
  def test_my_node_multiaddress():
291
291
  ipv4, ipv6 = basic_ipfs.my_node_multiaddress()
292
- assert ipv4 is not None or ipv6 is not None
292
+ # On a NAT'd host with only private addresses (e.g. a CI runner), the
293
+ # `server` init profile stops Kubo announcing private ranges, so
294
+ # (None, None) is the documented, correct answer. Assert consistency
295
+ # with the raw announced-address list instead of assuming the host has
296
+ # something shareable.
297
+ usable = [
298
+ a for a in basic_ipfs._get_manager().my_addrs()
299
+ if basic_ipfs._addr_score(a) >= 0
300
+ ]
301
+ if usable:
302
+ assert ipv4 is not None or ipv6 is not None
303
+ else:
304
+ assert ipv4 is None and ipv6 is None
293
305
  if ipv4:
294
306
  assert ipv4.startswith("/ip4/")
295
307
  assert "/p2p/" in ipv4
@@ -1,18 +1,27 @@
1
1
  """
2
2
  Tests for the auto-download path.
3
3
 
4
- `requests` is mocked so we never hit the network. We exercise:
4
+ Most tests mock `requests` so we never hit the public network. We exercise:
5
5
  - SHA-512 verification (success, mismatch, missing)
6
6
  - Atomic install (no half-written binary on failure)
7
7
  - Disk-space preflight
8
- - Baked-in vs network-fetched checksum preference
8
+ - Refusal of Kubo versions with no baked-in checksum (fail-closed)
9
+
10
+ The "live" tests at the bottom send REAL requests through the real
11
+ `_download_session()` against a local loopback HTTP server — the mocked
12
+ tests can't catch a session/adapter that is broken at the wire level
13
+ (that exact escape shipped in 1.0.0).
9
14
  """
10
15
 
11
16
  from __future__ import annotations
12
17
 
13
18
  import hashlib
19
+ import http.server
14
20
  import io
21
+ import socketserver
15
22
  import tarfile
23
+ import threading
24
+ from contextlib import contextmanager
16
25
  from unittest import mock
17
26
 
18
27
  import pytest
@@ -406,3 +415,113 @@ def test_download_rejects_offsite_redirect(temp_install, monkeypatch):
406
415
  basic_ipfs._auto_download_kubo(temp_install)
407
416
  assert "attacker.example" in str(excinfo.value)
408
417
  assert not temp_install.exists()
418
+
419
+
420
+ # ---------------------------------------------------------------------------
421
+ # Live tests — real _download_session() against a local loopback HTTP server.
422
+ #
423
+ # These exist because every mocked test above bypasses the session/adapter
424
+ # entirely: 1.0.0 shipped with _PinnedRedirectAdapter passing an
425
+ # `allow_redirects` kwarg that HTTPAdapter.send() rejects, so EVERY real
426
+ # download crashed with TypeError while the whole suite stayed green.
427
+ # ---------------------------------------------------------------------------
428
+
429
+ _DIRECT_BODY = b"direct-payload"
430
+ _FINAL_BODY = _make_tarball()
431
+
432
+
433
+ class _RedirectHandler(http.server.BaseHTTPRequestHandler):
434
+ def do_GET(self):
435
+ routes = {
436
+ "/direct": ("body", _DIRECT_BODY),
437
+ "/chain": ("redirect", "/hop"),
438
+ "/hop": ("redirect", "/final"),
439
+ "/final": ("body", _FINAL_BODY),
440
+ "/offsite": ("redirect", "http://attacker.example/x"),
441
+ }
442
+ kind, value = routes.get(self.path, ("missing", None))
443
+ if kind == "redirect":
444
+ self.send_response(302)
445
+ self.send_header("Location", value)
446
+ self.end_headers()
447
+ elif kind == "body":
448
+ self.send_response(200)
449
+ self.send_header("Content-Length", str(len(value)))
450
+ self.end_headers()
451
+ self.wfile.write(value)
452
+ else:
453
+ self.send_response(404)
454
+ self.end_headers()
455
+
456
+ def log_message(self, *args): # keep pytest output clean
457
+ pass
458
+
459
+
460
+ @contextmanager
461
+ def _live_server():
462
+ srv = socketserver.TCPServer(("127.0.0.1", 0), _RedirectHandler)
463
+ port = srv.server_address[1]
464
+ thread = threading.Thread(target=srv.serve_forever, daemon=True)
465
+ thread.start()
466
+ try:
467
+ yield f"http://127.0.0.1:{port}"
468
+ finally:
469
+ srv.shutdown()
470
+ srv.server_close()
471
+ thread.join(timeout=5)
472
+
473
+
474
+ @pytest.fixture
475
+ def allow_loopback_host(monkeypatch):
476
+ monkeypatch.setattr(basic_ipfs, "_ALLOWED_DOWNLOAD_HOSTS", ("127.0.0.1",))
477
+
478
+
479
+ def test_download_session_live_direct_get(allow_loopback_host):
480
+ """Regression for 1.0.0: a plain GET through the real session must not
481
+ blow up inside the adapter (TypeError on the allow_redirects kwarg)."""
482
+ with _live_server() as base:
483
+ with basic_ipfs._download_session() as session:
484
+ with session.get(f"{base}/direct", stream=True, timeout=10) as r:
485
+ assert r.status_code == 200
486
+ assert b"".join(r.iter_content(chunk_size=4)) == _DIRECT_BODY
487
+
488
+
489
+ def test_download_session_live_follows_pinned_redirects(allow_loopback_host):
490
+ with _live_server() as base:
491
+ with basic_ipfs._download_session() as session:
492
+ with session.get(f"{base}/chain", stream=True, timeout=10) as r:
493
+ assert r.status_code == 200
494
+ assert b"".join(r.iter_content(chunk_size=1 << 16)) == _FINAL_BODY
495
+
496
+
497
+ def test_download_session_live_rejects_offsite_redirect(allow_loopback_host):
498
+ with _live_server() as base:
499
+ with basic_ipfs._download_session() as session:
500
+ with pytest.raises(IPFSBinaryNotFound) as excinfo:
501
+ session.get(f"{base}/offsite", stream=True, timeout=10)
502
+ assert "attacker.example" in str(excinfo.value)
503
+
504
+
505
+ def test_download_live_full_flow(allow_loopback_host, tmp_path):
506
+ """_download() end-to-end over the wire: real session, redirect chain,
507
+ per-hop origin check, streaming to disk, digest computation."""
508
+ dest = tmp_path / "archive.partial"
509
+ with _live_server() as base:
510
+ digest = basic_ipfs._download(f"{base}/chain", dest, timeout=10)
511
+ assert digest == hashlib.sha512(_FINAL_BODY).hexdigest()
512
+ assert dest.read_bytes() == _FINAL_BODY
513
+
514
+
515
+ def test_check_redirect_origin_allows_https():
516
+ basic_ipfs._check_redirect_origin("https://dist.ipfs.tech/kubo/x.tar.gz")
517
+
518
+
519
+ def test_check_redirect_origin_rejects_plain_http():
520
+ with pytest.raises(IPFSBinaryNotFound) as excinfo:
521
+ basic_ipfs._check_redirect_origin("http://dist.ipfs.tech/kubo/x.tar.gz")
522
+ assert "https" in str(excinfo.value).lower()
523
+
524
+
525
+ def test_check_redirect_origin_allows_loopback_http(monkeypatch):
526
+ monkeypatch.setattr(basic_ipfs, "_ALLOWED_DOWNLOAD_HOSTS", ("127.0.0.1",))
527
+ basic_ipfs._check_redirect_origin("http://127.0.0.1:8000/kubo.tar.gz")
@@ -50,9 +50,10 @@ def _spawn_node_b(
50
50
  "/ip6/::/tcp/{swarm_port}",
51
51
  ]
52
52
  basic_ipfs.start()
53
- # Print readiness sentinel and our address for the parent to consume.
53
+ # Print readiness sentinel, our address, and peer ID for the parent.
54
54
  ipv4, ipv6 = basic_ipfs.my_node_multiaddress()
55
- print("READY", json.dumps({{"ipv4": ipv4, "ipv6": ipv6}}), flush=True)
55
+ peer_id = basic_ipfs.status()["peer_id"]
56
+ print("READY", json.dumps({{"ipv4": ipv4, "ipv6": ipv6, "peer_id": peer_id}}), flush=True)
56
57
  # Stay alive — parent will signal completion by closing stdin.
57
58
  try:
58
59
  sys.stdin.read()
@@ -91,9 +92,14 @@ def test_two_nodes_share_content(tmp_path):
91
92
 
92
93
  import json
93
94
  info = json.loads(ready_line.split(" ", 1)[1])
94
- # Connect A → B
95
+ # Connect A → B. On a NAT'd host with only private addresses (CI
96
+ # runners), the `server` profile means B announces nothing shareable
97
+ # — but both nodes sit on the same machine, so dial B's swarm
98
+ # listener over loopback directly.
95
99
  target = info["ipv4"] or info["ipv6"]
96
- assert target, "node B has no shareable multiaddr"
100
+ if not target:
101
+ assert info["peer_id"], "node B reported no peer ID"
102
+ target = f"/ip4/127.0.0.1/tcp/{swarm_port_b}/p2p/{info['peer_id']}"
97
103
  basic_ipfs.connect_to_node(target)
98
104
 
99
105
  # A adds. Use bytes so the add is fast and the CID is deterministic.
@@ -124,3 +130,10 @@ def test_two_nodes_share_content(tmp_path):
124
130
  proc.wait(timeout=10)
125
131
  except (subprocess.TimeoutExpired, BrokenPipeError):
126
132
  proc.kill()
133
+ proc.wait(timeout=10)
134
+ finally:
135
+ # Close the remaining pipes — leaked TextIOWrappers trip
136
+ # pytest's unraisable-warning collector (filterwarnings=error).
137
+ for stream in (proc.stdout, proc.stderr):
138
+ if stream is not None:
139
+ stream.close()
File without changes
File without changes
File without changes
File without changes
File without changes