MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_onionmessage_reply_path_no_hops

Function test_onionmessage_reply_path_no_hops

tests/test_connection.py:4631–4670  ·  view source on GitHub ↗

A reply_path with num_hops=0 must not take the node down. The blinded reply_path's num_hops is a single attacker-supplied byte with no lower bound; a zero-hop path serialises an empty "hops" array to the offers plugin, which used to reject it via plugin_err and (as an important buil

(node_factory)

Source from the content-addressed store, hash-verified

4629
4630
4631def test_onionmessage_reply_path_no_hops(node_factory):
4632 """A reply_path with num_hops=0 must not take the node down.
4633
4634 The blinded reply_path's num_hops is a single attacker-supplied byte
4635 with no lower bound; a zero-hop path serialises an empty "hops" array
4636 to the offers plugin, which used to reject it via plugin_err and (as
4637 an important builtin) stop lightningd.
4638 """
4639 l1, l2 = node_factory.line_graph(2)
4640
4641 def ecdh(privkey_bytes, pubkey_bytes):
4642 priv = coincurve.PrivateKey(privkey_bytes)
4643 return priv.ecdh(coincurve.PublicKey(pubkey_bytes).public_key)
4644
4645 # Build an onion message to l2 with a reply_path carrying no hops.
4646 l2_pub = bytes.fromhex(l2.info['id'])
4647 blinding = coincurve.PrivateKey()
4648 path_key = blinding.public_key.format(True)
4649
4650 # Route-blinding tweak so the onion decrypts as l2's real key.
4651 ss = ecdh(blinding.secret, l2_pub)
4652 tweak = hmac.new(b'blinded_node_id', ss, sha256).digest()
4653 blinded_pub = coincurve.PublicKey(l2_pub).multiply(tweak).format(True)
4654
4655 # blinded_path: first_node_id (point), first_path_key (point), num_hops=0
4656 reply_path = l2_pub + path_key + b'\x00'
4657 tlv = TlvPayload()
4658 tlv.add_field(2, reply_path)
4659
4660 onion = l1.rpc.createonion(hops=[{'pubkey': blinded_pub.hex(),
4661 'payload': tlv.to_bytes().hex()}],
4662 assocdata="")
4663
4664 l2.rpc.injectonionmessage(message=onion['onion'], path_key=path_key.hex())
4665
4666 # With the fix, lightningd drops the hopless reply path when it decodes
4667 # the message (logged synchronously, before the offers hook runs), so the
4668 # node stays up. Without the fix this log never appears: the offers plugin
4669 # calls plugin_err and lightningd_exit takes the node down instead.
4670 l2.daemon.wait_for_log('Ignoring reply path with no hops', timeout=30)
4671
4672
4673def inject_onionmsg_tlv(sender, dest, tlv):

Callers

nothing calls this directly

Calls 8

add_fieldMethod · 0.95
to_bytesMethod · 0.95
TlvPayloadClass · 0.90
line_graphMethod · 0.80
digestMethod · 0.80
wait_for_logMethod · 0.80
ecdhFunction · 0.70
newMethod · 0.45

Tested by

no test coverage detected