Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
7a6b530
tls support
Fatumayattani Aug 15, 2025
d0345b6
update tls docs
Fatumayattani Aug 15, 2025
7dd3469
update tls support
Fatumayattani Aug 15, 2025
bfbc389
update changes
Fatumayattani Aug 15, 2025
a9b510e
tls support
Fatumayattani Aug 15, 2025
6ea3016
tls support doc #700
Fatumayattani Aug 15, 2025
77f4764
Add tls-support.rst to docs toctree
Fatumayattani Aug 17, 2025
cc68f63
Merge branch 'main' into main
seetadev Aug 18, 2025
73d44ed
Replace asyncio with trio, fix newline, and run pre-commit checks on …
Fatumayattani Aug 19, 2025
4f9eabe
Add initial Eclipse attack simulation module with tests
Fatumayattani Sep 23, 2025
d801a91
Merge branch 'main' into feature/attack-simulation
seetadev Sep 23, 2025
c1df722
Merge branch 'libp2p:main' into feature/attack-simulation
yashksaini-coder Sep 24, 2025
5691e6e
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Sep 24, 2025
6312c95
Refactor code for consistency and clarity; update README and configur…
yashksaini-coder Sep 24, 2025
999d293
Add real integration tests and metrics collection for Eclipse attack …
yashksaini-coder Sep 24, 2025
b46cdef
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Sep 25, 2025
44044f8
Linting errors resolved
yashksaini-coder Sep 25, 2025
efffe92
Merge remote-tracking branch 'refs/remotes/origin/feature/attack-simu…
yashksaini-coder Sep 25, 2025
ba04a4f
Refactor real metrics collector and test structure
yashksaini-coder Sep 25, 2025
32483ac
Remove non-existent file
yashksaini-coder Sep 25, 2025
16708e3
Enhance type annotations and improve metrics collection in Eclipse at…
yashksaini-coder Sep 25, 2025
fd4d578
performed more test and test suite for multiple config scenarios and …
yashksaini-coder Sep 25, 2025
7004d94
add newsfragment for PR #950 (Eclipse attack simulation module)
Fatumayattani Sep 26, 2025
e19929e
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Sep 26, 2025
82531b1
Add connection exhaustion attack implementation and tests passed
yashksaini-coder Sep 27, 2025
d9bd1b7
Implement flooding attack module and corresponding tests
yashksaini-coder Sep 27, 2025
a3dfead
Add protocol attack implementation and tests
yashksaini-coder Sep 27, 2025
c417fce
Add Sybil attacks and tests
yashksaini-coder Sep 27, 2025
0cd848c
Add attack analysis framework and metrics collection with comprehensi…
yashksaini-coder Sep 27, 2025
7afdad7
Phase 2 completed:
yashksaini-coder Sep 27, 2025
950b155
Performed make fix & typecheck run to fix all linting and typechecke…
yashksaini-coder Sep 27, 2025
5ec1eec
Remove obsolete attack simulation result files to streamline testing …
yashksaini-coder Sep 30, 2025
e9d3215
Add results directory handling and update file paths in eclipse attac…
yashksaini-coder Sep 30, 2025
a1c8a31
Refactor real network builder to create multiple honest and malicious…
yashksaini-coder Sep 30, 2025
463f6ac
Fix host initialization in TLS examples by removing unnecessary await
yashksaini-coder Sep 30, 2025
a7b963e
Update test to use a random port for address changes in DHT reissue test
yashksaini-coder Sep 30, 2025
587aa6f
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 4, 2025
6b3a421
Merge branch 'main' into feature/attack-simulation
acul71 Oct 4, 2025
d9e51e9
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 5, 2025
f437fcf
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 5, 2025
4c36e6c
Address pacrob feedback: remove duplicate AttackMetrics and cleanup t…
Fatumayattani Oct 6, 2025
8d9e7c8
refactor: improve temporary file handling and reduce test peer count …
yashksaini-coder Oct 6, 2025
003f4e5
refactor: standardize list type annotations in AttackMetrics class an…
yashksaini-coder Oct 6, 2025
0f81749
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 6, 2025
8ef6ebf
update README
yashksaini-coder Oct 6, 2025
8b9c4b4
fix: correct whitespace in README and improve comment formatting in m…
yashksaini-coder Oct 7, 2025
f61574e
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 10, 2025
1a5d9a9
Merge branch 'libp2p:main' into feature/attack-simulation
yashksaini-coder Oct 14, 2025
7777dd2
test: add replay and routing_poisoning attack simulations
Fatumayattani Oct 21, 2025
8bb588c
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 21, 2025
0838067
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 23, 2025
4bfd5e7
Enhance attack simulation framework with extended threat model and me…
yashksaini-coder Oct 24, 2025
14b8be7
Fix minor bugs and add mitigation documentation for attack simulations
Fatumayattani Oct 27, 2025
3cae619
Merge branch 'main' into feature/attack-simulation
yashksaini-coder Oct 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,6 @@ tests/interop/js_libp2p/js_node/src/package-lock.json

# Sphinx documentation build
_build/

# Attack simulation test results
tests/security/attack_simulation/results/
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The Python implementation of the libp2p networking stack

Examples <examples>
API <libp2p>
tls-support
GossipSub 1.2 <gossipsub-1.2>

.. toctree::
Expand Down
161 changes: 161 additions & 0 deletions docs/tls-support.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
Py-libp2p – TLS Support Documentation
======================================================

.. contents::
:depth: 2
:local:

Overview of TLS in Libp2p
-------------------------

**Purpose of TLS in P2P networking**

- Encrypts data between peers.
- Authenticates peer identity using certificates.
- Prevents man-in-the-middle attacks.

**Integration in libp2p security modules**

- TLS is one of the supported secure channel protocols (alongside Noise).
- Negotiated during connection setup.

**Current status**

- **py-libp2p**: Experimental, usable for local and interop tests.
- **go-libp2p / js-libp2p**: Stable and production-ready.

Installation Requirements
-------------------------

**Python requirements**

- Python 3.8+

**Install with TLS support**

.. code-block:: bash

pip install "libp2p[tls]"

**Additional dependencies**

Ubuntu / Debian:

.. code-block:: bash

sudo apt install build-essential python3-dev libffi-dev libssl-dev

macOS:

.. code-block:: bash

brew install openssl

Enabling TLS in py-libp2p
-------------------------

**Working example – Listener and Dialer**

Listener node:

.. code-block:: python

import trio
from libp2p import new_host
from libp2p.security.tls.transport import TLSTransport

async def main():
host = new_host(security_transports=[TLSTransport()])
await host.listen("/ip4/0.0.0.0/tcp/8000")
print("TLS-enabled listener at:", host.get_addrs())

await trio.sleep_forever()

if __name__ == "__main__":
trio.run(main())

Dialer node:

.. code-block:: python

import trio
from libp2p import new_host
from libp2p.security.tls.transport import TLSTransport
from libp2p.peer.peerinfo import info_from_p2p_addr

async def main():
host = new_host(security_transports=[TLSTransport()])

addr = "/ip4/127.0.0.1/tcp/8000/p2p/QmPeerIDHere"
peer_info = info_from_p2p_addr(addr)

await host.connect(peer_info)
print("Connected securely to", peer_info.peer_id)

if __name__ == "__main__":
trio.run(main())

**Defaults if no configuration is provided**

- Generates a self-signed certificate automatically.

Certificate Management
----------------------

**Generate a development certificate**

.. code-block:: bash

openssl req -x509 -newkey rsa:2048 \
-keyout key.pem -out cert.pem \
-days 365 -nodes -subj "/CN=py-libp2p"

- Store keys outside version control.
- Rotate certificates every 90 days in production.

Testing TLS Connections
-----------------------

**Local test steps**

1. Run the listener example.
2. Start the dialer with the listener's multiaddress.
3. Confirm the secure connection in logs.

**Interop testing**

- Ensure both nodes advertise `/tls/1.0.0`.
- Peer IDs must match certificate public keys.

Security Considerations
-----------------------

- Never disable certificate verification in production.
- Use TLS 1.3 or later.
- Pin certificates for critical peers.

Troubleshooting
---------------

.. list-table::
:header-rows: 1
:widths: 30 30 40

* - Problem
- Cause
- Solution
* - Certificate not trusted
- Self-signed without trust store entry
- Add cert to local trust store or disable verification **only** in testing.
* - Protocol negotiation failed
- One peer does not support `/tls/1.0.0`
- Enable TLS on both peers or use Noise.
* - SSL handshake failure
- TLS version mismatch or clock skew
- Enforce TLS 1.3, sync system clock.
* - `ImportError: No module named libp2p.security.tls`
- TLS extras not installed
- Run `pip install "libp2p[tls]"`.
* - Connection refused
- Port blocked or listener not running
- Check firewall rules and listener status.
4 changes: 2 additions & 2 deletions libp2p/kad_dht/kad_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(
"""
super().__init__()

self.host = host
self.host: IHost = host
self.local_peer_id = host.get_id()

# Validate that mode is a DHTMode enum
Expand All @@ -128,7 +128,7 @@ def __init__(
self.enable_random_walk = enable_random_walk

# Initialize the routing table
self.routing_table = RoutingTable(self.local_peer_id, self.host)
self.routing_table = RoutingTable(self.local_peer_id, host)

self.protocol_prefix = protocol_prefix
self.enable_providers = enable_providers
Expand Down
1 change: 1 addition & 0 deletions newsfragments/950.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added an Eclipse attack simulation module with dual-layer architecture (simulation + real integration) and metrics collection framework.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ log_date_format = "%m-%d %H:%M:%S"
log_format = "%(levelname)8s %(asctime)s %(filename)20s %(message)s"
markers = ["slow: mark test as slow"]
xfail_strict = true
trio_mode = true

[tool.towncrier]
# Read https://github.com/libp2p/py-libp2p/blob/main/newsfragments/README.md for instructions
Expand Down
10 changes: 7 additions & 3 deletions tests/core/identity/identify_push/test_identify_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ async def test_all_peers_receive_identify_push_with_semaphore_under_high_peer_lo

async with host_pair_factory(security_protocol=security_protocol) as (host_a, _):
# Create dummy peers
# Breaking with more than 500 peers
# Trio have a async tasks limit of 1000
for _ in range(499):
# Reduced from 499 to 50 to avoid resource exhaustion
# and improve test reliability
for _ in range(50):
key_pair = create_new_key_pair()
dummy_host = new_host(key_pair=key_pair)
dummy_host.set_stream_handler(
Expand Down Expand Up @@ -596,8 +596,12 @@ async def test_all_peers_receive_identify_push_with_semaphore_under_high_peer_lo
dummy_peerstore = host.get_peerstore()
assert peer_id_a in dummy_peerstore.peer_ids()

# Cleanup: Cancel nursery and close all connections
nursery.cancel_scope.cancel()

# Give time for proper cleanup
await trio.sleep(0.1)


@pytest.mark.trio
async def test_identify_push_default_varint_format(security_protocol):
Expand Down
4 changes: 3 additions & 1 deletion tests/core/kad_dht/test_kad_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import hashlib
import logging
import os
import random
from typing import TypeVar
from unittest.mock import patch
import uuid
Expand Down Expand Up @@ -460,7 +461,8 @@ async def test_reissue_when_listen_addrs_change(dht_pair: tuple[KadDHT, KadDHT])
seq0 = env0.record().seq

# Simulate B's listen addrs changing (different port)
new_addr = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/123")
# Pick a port unlikely to be used, or increment existing port
new_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{random.randint(20000, 40000)}")

# Patch just for the duration we force B to respond:
with patch.object(dht_b.host, "get_addrs", return_value=[new_addr]):
Expand Down
7 changes: 3 additions & 4 deletions tests/core/security/tls/test_transport_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ async def test_sensitive_data_handling(nursery: trio.Nursery) -> None:
f for f in final_files - initial_files if f.name.startswith("tmp")
}

assert not remaining_files, (
f"Temporary files remained after cleanup: "
f"{[f.name for f in remaining_files]}"
)
msg = "Temporary files remained after cleanup: "
file_names = [f.name for f in remaining_files]
assert not remaining_files, msg + str(file_names)

# Verify no sensitive data in any new files
for f in final_files - initial_files:
Expand Down
Empty file added tests/security/__init__.py
Empty file.
Loading
Loading