Skip to content

HODL Invoice: payment_hash Uniqueness Race (TOCTOU) #52

Description

@txalkan

The current implementation in POST /lninvoice checks for hash uniqueness with a plain map lookup:

  1. Check if payment_hash exists in inbound_payments.
  2. Create invoice.
  3. Persist PaymentInfo.

Steps 1–3 are not atomic, resulting in a TOCTOU (Time Of Check To Time Of Use) race condition. Two concurrent requests with the same payment_hash can both pass step 1 before either reaches step 3.

Proposed fix

Extend UnlockedAppState with an in-memory such as:

inflight_hodl_hashes: Arc<Mutex<HashSet<PaymentHash>>>

Before the uniqueness check, insert the hash into the set using a RAII guard that removes it on Drop. Concurrent requests with the same hash fail at guard insertion before any LDK or persistence calls are made.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions