Skip to content

fix: deliver webhook events, authenticate notification channels, settle before modify_rate - #1931

Merged
K1NGD4VID merged 5 commits into
LabsCrypt:mainfrom
king-daveed:fix/webhook-fanout-notification-auth
Nov 24, 2026
Merged

K1NGD4VID merged 5 commits into
LabsCrypt:mainfrom
king-daveed:fix/webhook-fanout-notification-auth

Conversation

@king-daveed

Copy link
Copy Markdown
Contributor

Three separate defects: webhooks that were configured but never fired, a
notification channel flow that trusted the client, and a rate change that
discarded vested tokens.

Closes #1854
Closes #1855
Closes #1862

#1854 — webhooks now fire

dispatchWebhookEvent was exported and never called. A user could create a
subscription and get the test ping, and then never hear from the product again:
the indexer broadcast SSE only, and the runway worker's comment said "SSE and
webhooks" while calling SSE alone.

Both fan-outs now happen in one method, publishStreamEvent, which every
stream.* broadcast goes through — so a new event type cannot be added to one
and forgotten in the other. Subscribers are the stream's recipient and sender,
resolved from the row rather than from the event body.

Failure handling: a dead endpoint must not turn a processed chain event into a
retry, so the webhook leg logs and returns; the delivery attempt it records is
what surfaces the failure. The low-balance alert dispatches the same way.

#1855 — notification channel routes are bound to the session

POST /link-code took req.body.address, generated the code with
Math.random() (~31 bits from a non-cryptographic PRNG whose stream is
reproducible from a few outputs) and returned that code in the response — so the
second factor was in the first response. Anyone could mint a code for any
wallet, read it back, and attach their own Telegram chat to a stranger's stream
notifications. linkTelegram, setDiscordWebhook, /subscribe and
/unsubscribe trusted the body the same way.

  • Every route now runs requireAuth.
  • The address is the session's publicKey. A body that names a different wallet
    gets 403 rather than being silently overridden, so a confused client learns.
  • Codes come from crypto.randomBytes(5) → 10 uppercase hex characters, and are
    no longer echoed; the response carries the address and expiry only.

The router is still not mounted in app.ts; that is a separate decision about
whether the feature ships. The point of this change is that it is no longer
exploitable the moment someone does mount it.

#1862 — modify_rate settles before it moves the anchor

modify_rate set rate_per_second and last_update_time with no settlement,
contradicting the rationale top_up_stream documents in the same file:
last_update_time is the accrual anchor, and advancing it throws away
everything already vested but unwithdrawn. The recipient had to wait for the new
rate to re-accrue tokens that were already theirs; a sender could call
modify_rate(1) to defer vested wages arbitrarily.

The vested interval is paid out at the old rate before the anchor moves.

It is a real withdrawal, not a bookkeeping credit: calculate_claimable is
elapsed * rate clamped to deposited - withdrawn, so crediting
withdrawn_amount without paying would silently delete the tokens rather than
preserve them — the opposite of the issue. The payout reuses apply_withdrawal
(state committed before the transfer) and emits TokensWithdrawn, which is what
the backend indexer books withdrawals from, so the off-chain withdrawnAmount
cannot drift away from the chain.

Tests

  • cargo test -p stream_contract — 278 passed, 1 ignored
  • New: settled-before-change with continuity across the change (100 accrued at
    rate 1, then 50s at rate 2 adds exactly 100 more), nothing taken when nothing
    has vested, and a paused stream settling only up to its pause point.
  • vitest on notification.service.test.ts, soroban-event-worker.test.ts,
    webhook-delivery.test.ts — 39 passed
  • New assertions in the link-code test: code matches ^[0-9A-F]{10}$ and two
    codes for the same wallet differ.
  • tsc --noEmit on the backend — clean

Not included

#802 (withdrawnAmount incremented before the dedup guard) is already fixed on
main, and already has the replay regression test at
soroban-event-worker.test.ts:457. I did not touch it or claim it.

king-daveed and others added 4 commits November 24, 2026 14:31
dispatchWebhookEvent was exported and never called. Users could create a
subscription and the test ping arrived, while no production stream event ever
did: the worker broadcast SSE only, and the runway worker's comment promised
"SSE and webhooks" while calling SSE alone (LabsCrypt#1854).

Both fan-outs now live in one method, so an event type cannot be added to one
and forgotten in the other. Subscribers are the stream's recipient and sender;
a failed delivery is logged and recorded as a delivery attempt, never rethrown,
because a dead endpoint must not turn a processed chain event into a retry. The
low-balance alert dispatches the same way.
The notification router trusted the client. POST /link-code took
req.body.address, generated the verification code with Math.random() and
returned that code in the response, so anyone could mint a code for any wallet,
read it from their own HTTP response, and attach their own Telegram chat to a
stranger's stream notifications. linkTelegram, setDiscordWebhook and the
web-push endpoints trusted the body the same way (LabsCrypt#1855).

Every route now requires a session, and the address is the session's; a body
that names another wallet is refused with 403 rather than ignored. Codes come
from crypto.randomBytes (10 hex characters instead of ~31 predictable bits) and
are no longer echoed back - the caller gets the address and expiry. Tests assert
the code shape and that two codes differ.
…chor

modify_rate set rate and last_update_time with no settlement, contradicting the
rationale top_up_stream documents: last_update_time is the accrual anchor, and
advancing it discards everything already vested but unwithdrawn. A recipient
had to re-accrue tokens they had already earned, and a sender could call this
with rate 1 to defer vested wages arbitrarily (LabsCrypt#1862).

The vested interval is now paid out at the old rate before the anchor moves.
It is a real withdrawal rather than a bookkeeping credit: claimable is
elapsed*rate clamped to deposited-withdrawn, so crediting withdrawn_amount
without paying would delete the tokens. The payout reuses apply_withdrawal
(state committed before transfer) and emits TokensWithdrawn, the event the
backend indexer books withdrawals from, so the off-chain figure cannot drift.

Three tests: paid-before-change with continuity across the change, nothing
taken when nothing vested, and a paused stream settling only up to its pause.
Every handler on this router authenticates a wallet and then writes a
per-wallet record, which CodeQL flags as authorization without rate
limiting. Apply the shared express-rate-limit factory to the whole
router so a flood is rejected before any work happens.
@king-daveed
king-daveed force-pushed the fix/webhook-fanout-notification-auth branch from 13bbaeb to 4a1253c Compare November 24, 2026 14:45
@K1NGD4VID
K1NGD4VID merged commit 7aad7f2 into LabsCrypt:main Nov 24, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants